]> git.gir.st - VimFx.git/blob - extension/lib/scrollable-elements.coffee
Consider the entire page as largest if scrollable
[VimFx.git] / extension / lib / scrollable-elements.coffee
1 ###
2 # Copyright Simon Lydell 2015.
3 #
4 # This file is part of VimFx.
5 #
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.
10 #
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.
15 #
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/>.
18 ###
19
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
23 # removed.
24
25 utils = require('./utils')
26
27 class ScrollableElements
28 constructor: (@window) ->
29 @elements = new Set()
30 @largest = null
31
32 has: (element) -> @elements.has(element)
33
34 add: (element) ->
35 @elements.add(element)
36 utils.onRemoved(@window, element, @delete.bind(this, element))
37 @largest = element if @isLargest(element)
38
39 delete: (element) =>
40 @elements.delete(element)
41 @updateLargest() if @largest == element
42
43 reject: (fn) ->
44 @elements.forEach((element) => @elements.delete(element) if fn(element))
45 @updateLargest()
46
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))
54
55 updateLargest: ->
56 # Reset `@largest` and find a new largest scrollable element (if there are
57 # any left).
58 @largest = null
59 @elements.forEach((element) => @largest = element if @isLargest(element))
60
61 module.exports = ScrollableElements
Imprint / Impressum