2 # Copyright Anton Khodakivskiy 2012, 2013.
3 # Copyright Simon Lydell 2013, 2014.
5 # This file is part of VimFx.
7 # VimFx is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # VimFx is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
21 { createElement } = require('../utils')
22 { SerializableBloomFilter
23 , DummyBloomFilter } = require('./bloomfilter')
24 { getPref } = require('../prefs')
26 HTMLDocument = Ci.nsIDOMHTMLDocument
27 HTMLAnchorElement = Ci.nsIDOMHTMLAnchorElement
29 realBloomFilter = new SerializableBloomFilter('hints_bloom_data', 256 * 32, 16)
30 dummyBloomFilter = new DummyBloomFilter()
32 # Wraps the markable element and provides methods to manipulate the markers.
34 # Creates the marker DOM node.
35 constructor: (@element, @elementShape) ->
36 document = @element.ownerDocument
37 @markerElement = createElement(document, 'div', {class: 'VimFxHintMarker'})
39 Object.defineProperty this, 'bloomFilter',
40 get: -> if getPref('hints_bloom_on') then realBloomFilter else dummyBloomFilter
42 show: -> @setVisibility(true)
43 hide: -> @setVisibility(false)
44 setVisibility: (visible) ->
45 @markerElement.classList.toggle('VimFxHiddenHintMarker', !visible)
47 if @hintChars.startsWith(@enteredHintChars) then @show() else @hide()
49 # To be called when the marker has been both assigned a hint and inserted
50 # into the DOM, and thus gotten a height and width.
51 setPosition: (viewport) ->
53 markerElement: { offsetHeight: height, offsetWidth: width }
54 elementShape: { nonCoveredPoint: { x: left, y: top, offset, rect } }
57 # Center the marker vertically on the non-covered point.
60 # Make sure that the marker stays within its element (vertically).
61 top = Math.min(top, rect.bottom - height)
62 top = Math.max(top, rect.top)
64 # Make the position relative to the top frame.
68 # Make sure that the marker stays within the viewport.
69 left = Math.min(left, viewport.right - width)
70 top = Math.min(top, viewport.bottom - height)
71 left = Math.max(left, viewport.left)
72 top = Math.max(top, viewport.top)
74 # Make the position relative to the document, rather than to the viewport.
75 left += viewport.scrollX
76 top += viewport.scrollY
78 # The positioning is absolute.
79 @markerElement.style.left = "#{ left }px"
80 @markerElement.style.top = "#{ top }px"
84 left, right: left + width,
85 top, bottom: top + height,
89 setHint: (@hintChars) ->
90 # Hint chars that have been matched so far.
91 @enteredHintChars = ''
93 document = @element.ownerDocument
95 while @markerElement.hasChildNodes()
96 @markerElement.firstChild.remove()
98 fragment = document.createDocumentFragment()
99 for char in @hintChars
100 charContainer = createElement(document, 'span')
101 charContainer.textContent = char.toUpperCase()
102 fragment.appendChild(charContainer)
104 @markerElement.appendChild(fragment)
106 matchHintChar: (char) ->
107 @toggleLastHintChar(true)
108 @enteredHintChars += char.toLowerCase()
112 @enteredHintChars = @enteredHintChars[...-1]
113 @toggleLastHintChar(false)
116 toggleLastHintChar: (visible) ->
117 @markerElement.children[@enteredHintChars.length]
118 ?.classList.toggle('VimFxCharMatch', visible)
121 return @hintChars == @enteredHintChars
127 # Returns string features of the element that can be used in the bloom filter
128 # in order to add relevance to the hint marker.
129 extractBloomFeatures: ->
132 # Class name of an element (walks up the node tree to find first element
133 # with at least one class).
136 while el.classList?.length == 0 and el not instanceof HTMLDocument
137 suffix += " #{ el.tagName }"
140 for className in el.classList
141 features["#{ el.tagName }.#{ className }#{ suffix }"] = 10
144 features["#{ el.tagName }.#{ @element.id }"] = 5
146 if @element instanceof HTMLAnchorElement
147 features["a"] = 20 # Reward links no matter what.
148 features["#{ el.tagName }.#{ @element.href }"] = 60
149 features["#{ el.tagName }.#{ @element.title }"] = 40
153 # Returns rating of all present bloom features (plus 1).
156 for feature, weight of @extractBloomFeatures()
157 rating += if @bloomFilter.test(feature) then weight else 0
162 for feature, weight of @extractBloomFeatures()
163 @bloomFilter.add(feature)
166 exports.Marker = Marker