]> git.gir.st - VimFx.git/blob - extension/lib/scrollable-elements.coffee
Merge branch 'master' into develop
[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 @frames = new WeakSet()
31 @largest = null
32
33 has: (element) -> @elements.has(element)
34
35 add: (element) ->
36 @elements.add(element)
37 utils.onRemoved(@window, element, @delete.bind(this, element))
38 @addFrame(element)
39
40 if not @largest or utils.area(element) > utils.area(@largest)
41 @largest = element
42
43 addFrame: (element) ->
44 window = element.ownerDocument.defaultView
45 frame = window.frameElement
46 return if not frame or @frames.has(frame)
47
48 utils.onRemoved(@window, frame, =>
49 @frames.delete(frame)
50 @reject(utils.windowContainsDeep.bind(null, window))
51 )
52
53 delete: (element) =>
54 @elements.delete(element)
55 @updateLargest() if @largest == element
56
57 reject: (fn) ->
58 @elements.forEach((element) => @elements.delete(element) if fn(element))
59 @updateLargest()
60
61 updateLargest: ->
62 @largest = null
63
64 # Find a new largest scrollable element (if there are any left).
65 largestArea = -1
66 @elements.forEach((element) =>
67 area = utils.area(element)
68 if area > largestArea
69 @largest = element
70 largestArea = area
71 )
72
73 module.exports = ScrollableElements
Imprint / Impressum