2 # Copyright Simon Lydell 2015.
4 # This file is part of VimFx.
6 # VimFx is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # VimFx is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
20 # This file contains an abstraction for keeping track of scrollable elements,
21 # automatically keeping the largest scrollable element up-to-date. It stops
22 # tracking elements that are removed from the DOM, or whose containg frame is
25 utils = require('./utils')
27 class ScrollableElements
28 constructor: (@window) ->
32 has: (element) -> @elements.has(element)
35 @elements.add(element)
36 utils.onRemoved(@window, element, @delete.bind(this, element))
37 @largest = element if @isLargest(element)
40 @elements.delete(element)
41 @updateLargest() if @largest == element
44 @elements.forEach((element) => @elements.delete(element) if fn(element))
47 isLargest: (element) ->
48 # Always consider the toplevel document the largest scrollable element, if
49 # it is scrollable. (Its area may be smaller than other elements).
50 return not @largest or
51 element == @window.document.documentElement or
52 (@largest != @window.document.documentElement and
53 utils.area(element) > utils.area(@largest))
56 # Reset `@largest` and find a new largest scrollable element (if there are
59 @elements.forEach((element) => @largest = element if @isLargest(element))
61 module.exports = ScrollableElements