]> git.gir.st - VimFx.git/blob - extension/lib/mode-hints/marker.coffee
Make `require` more like Node.js
[VimFx.git] / extension / lib / mode-hints / marker.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013.
3 # Copyright Simon Lydell 2013, 2014.
4 #
5 # This file is part of VimFx.
6 #
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.
11 #
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.
16 #
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/>.
19 ###
20
21 { createElement } = require('../utils')
22 { SerializableBloomFilter
23 , DummyBloomFilter } = require('./bloomfilter')
24 { getPref } = require('../prefs')
25
26 HTMLDocument = Ci.nsIDOMHTMLDocument
27 HTMLAnchorElement = Ci.nsIDOMHTMLAnchorElement
28
29 realBloomFilter = new SerializableBloomFilter('hints_bloom_data', 256 * 32, 16)
30 dummyBloomFilter = new DummyBloomFilter()
31
32 # Wraps the markable element and provides methods to manipulate the markers.
33 class Marker
34 # Creates the marker DOM node.
35 constructor: (@element, @elementShape) ->
36 document = @element.ownerDocument
37 @markerElement = createElement(document, 'div', {class: 'VimFxHintMarker'})
38
39 Object.defineProperty this, 'bloomFilter',
40 get: -> if getPref('hints_bloom_on') then realBloomFilter else dummyBloomFilter
41
42 show: -> @setVisibility(true)
43 hide: -> @setVisibility(false)
44 setVisibility: (visible) ->
45 @markerElement.classList.toggle('VimFxHiddenHintMarker', !visible)
46 updateVisibility: ->
47 if @hintChars.startsWith(@enteredHintChars) then @show() else @hide()
48
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) ->
52 {
53 markerElement: { offsetHeight: height, offsetWidth: width }
54 elementShape: { nonCoveredPoint: { x: left, y: top, offset, rect } }
55 } = this
56
57 # Center the marker vertically on the non-covered point.
58 top -= height / 2
59
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)
63
64 # Make the position relative to the top frame.
65 left += offset.left
66 top += offset.top
67
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)
73
74 # Make the position relative to the document, rather than to the viewport.
75 left += viewport.scrollX
76 top += viewport.scrollY
77
78 # The positioning is absolute.
79 @markerElement.style.left = "#{ left }px"
80 @markerElement.style.top = "#{ top }px"
81
82 # For quick access.
83 @position = {
84 left, right: left + width,
85 top, bottom: top + height,
86 height, width
87 }
88
89 setHint: (@hintChars) ->
90 # Hint chars that have been matched so far.
91 @enteredHintChars = ''
92
93 document = @element.ownerDocument
94
95 while @markerElement.hasChildNodes()
96 @markerElement.firstChild.remove()
97
98 fragment = document.createDocumentFragment()
99 for char in @hintChars
100 charContainer = createElement(document, 'span')
101 charContainer.textContent = char.toUpperCase()
102 fragment.appendChild(charContainer)
103
104 @markerElement.appendChild(fragment)
105
106 matchHintChar: (char) ->
107 @toggleLastHintChar(true)
108 @enteredHintChars += char.toLowerCase()
109 @updateVisibility()
110
111 deleteHintChar: ->
112 @enteredHintChars = @enteredHintChars[...-1]
113 @toggleLastHintChar(false)
114 @updateVisibility()
115
116 toggleLastHintChar: (visible) ->
117 @markerElement.children[@enteredHintChars.length]
118 ?.classList.toggle('VimFxCharMatch', visible)
119
120 isMatched: ->
121 return @hintChars == @enteredHintChars
122
123 reset: ->
124 @setHint(@hintChars)
125 @show()
126
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: ->
130 features = {}
131
132 # Class name of an element (walks up the node tree to find first element
133 # with at least one class).
134 suffix = ''
135 el = @element
136 while el.classList?.length == 0 and el not instanceof HTMLDocument
137 suffix += " #{ el.tagName }"
138 el = el.parentNode
139 if el?.classList?
140 for className in el.classList
141 features["#{ el.tagName }.#{ className }#{ suffix }"] = 10
142
143 if @element.id
144 features["#{ el.tagName }.#{ @element.id }"] = 5
145
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
150
151 return features
152
153 # Returns rating of all present bloom features (plus 1).
154 calcBloomRating: ->
155 rating = 1
156 for feature, weight of @extractBloomFeatures()
157 rating += if @bloomFilter.test(feature) then weight else 0
158
159 return rating
160
161 reward: ->
162 for feature, weight of @extractBloomFeatures()
163 @bloomFilter.add(feature)
164 @bloomFilter.save()
165
166 exports.Marker = Marker
Imprint / Impressum