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.
24 utils = require('./utils')
26 class ScrollableElements
27 constructor: (@window, @MINIMUM_SCROLL) ->
31 # In quirks mode (when the page lacks a doctype), such as on Hackernews,
32 # `<body>` is considered the root element rather than `<html>`. The 'overflow'
33 # event is triggered for `<html>` though (_not_ `<body>`!). This method takes
34 # care of returning the appropriate element, so we don’t need to think about
37 document = element.ownerDocument
38 if element == document.documentElement and
39 document.compatMode == 'BackCompat' and document.body?
44 has: (element) -> @elements.has(@quirks(element))
47 element = @quirks(element)
48 @elements.add(element)
49 utils.onRemoved(@window, element, @delete.bind(this, element))
50 @largest = element if @isLargest(element)
53 element = @quirks(element)
54 @elements.delete(element)
55 @updateLargest() if @largest == element
58 @elements.forEach((element) => @elements.delete(element) if fn(element))
61 isScrollable: (element) ->
62 element = @quirks(element)
63 return element.scrollTopMax >= @MINIMUM_SCROLL or
64 element.scrollLeftMax >= @MINIMUM_SCROLL
66 # It makes the most sense to consider the uppermost scrollable element the
67 # largest. In other words, if a scrollable element contains another scrollable
68 # element (or a frame containing one), the parent should be considered largest
69 # even if the child has greater area.
70 isLargest: (element) ->
71 return true unless @largest
72 return true if utils.containsDeep(element, @largest)
73 return false if utils.containsDeep(@largest, element)
74 return utils.area(element) > utils.area(@largest)
77 # Reset `@largest` and find a new largest scrollable element (if there are
80 @elements.forEach((element) => @largest = element if @isLargest(element))
82 # Elements may overflow when zooming in or out. However, the `.scrollHeight`
83 # of the element is not correctly updated when the 'overflow' event occurs,
84 # making it possible for unscrollable elements to slip in. This method tells
85 # whether the largest element really is scrollable, updating it if needed.
86 hasOrUpdateLargestScrollable: ->
87 if @largest and @isScrollable(@largest)
90 @reject((element) => not @isScrollable(element))
93 module.exports = ScrollableElements