]> git.gir.st - VimFx.git/blob - extension/lib/scrollable-elements.coffee
Fix state being lost on some pages
[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
38 if not @largest or utils.area(element) > utils.area(@largest)
39 @largest = element
40
41 delete: (element) =>
42 @elements.delete(element)
43 @updateLargest() if @largest == element
44
45 reject: (fn) ->
46 @elements.forEach((element) => @elements.delete(element) if fn(element))
47 @updateLargest()
48
49 updateLargest: ->
50 @largest = null
51
52 # Find a new largest scrollable element (if there are any left).
53 largestArea = -1
54 @elements.forEach((element) =>
55 area = utils.area(element)
56 if area > largestArea
57 @largest = element
58 largestArea = area
59 )
60
61 module.exports = ScrollableElements
Imprint / Impressum