]> git.gir.st - VimFx.git/blob - extension/packages/mode-hints/marker.coffee
Refactor commit 6919c1d6c slightly
[VimFx.git] / extension / packages / mode-hints / marker.coffee
1 { createElement } = require 'utils'
2 { SerializableBloomFilter
3 , DummyBloomFilter } = require 'mode-hints/bloomfilter'
4
5 { getPref } = require 'prefs'
6
7 HTMLDocument = Ci.nsIDOMHTMLDocument
8 HTMLAnchorElement = Ci.nsIDOMHTMLAnchorElement
9
10 realBloomFilter = new SerializableBloomFilter('hints_bloom_data', 256 * 32, 16)
11 dummyBloomFilter = new DummyBloomFilter()
12
13 # Wraps the markable element and provides methods to manipulate the markers.
14 class Marker
15 # Creates the marker DOM node.
16 constructor: (@element, @elementShape) ->
17 document = @element.ownerDocument
18 @markerElement = createElement(document, 'div', {class: 'VimFxHintMarker'})
19
20 Object.defineProperty this, 'bloomFilter',
21 get: -> if getPref('hints_bloom_on') then realBloomFilter else dummyBloomFilter
22
23 show: -> @setVisibility(true)
24 hide: -> @setVisibility(false)
25 setVisibility: (visible) ->
26 @markerElement.classList.toggle('VimFxHiddenHintMarker', !visible)
27 updateVisibility: ->
28 if @hintChars.startsWith(@enteredHintChars) then @show() else @hide()
29
30 # To be called when the marker has been both assigned a hint and inserted
31 # into the DOM, and thus gotten a height and width.
32 setPosition: (viewport) ->
33 {
34 markerElement: { offsetHeight: height, offsetWidth: width }
35 elementShape: { nonCoveredPoint: { x: left, y: top, offset, rect } }
36 } = this
37
38 # Center the marker vertically on the non-covered point.
39 top -= height / 2
40
41 # Make sure that the marker stays within its element (vertically).
42 top = Math.min(top, rect.bottom - height)
43 top = Math.max(top, rect.top)
44
45 # Make the position relative to the top frame.
46 left += offset.left
47 top += offset.top
48
49 # Make sure that the marker stays within the viewport.
50 left = Math.min(left, viewport.right - width)
51 top = Math.min(top, viewport.bottom - height)
52 left = Math.max(left, viewport.left)
53 top = Math.max(top, viewport.top)
54
55 # Make the position relative to the document, rather than to the viewport.
56 left += viewport.scrollX
57 top += viewport.scrollY
58
59 # The positioning is absolute.
60 @markerElement.style.left = "#{ left }px"
61 @markerElement.style.top = "#{ top }px"
62
63 # For quick access.
64 @position = {
65 left, right: left + width,
66 top, bottom: top + height,
67 height, width
68 }
69
70 setHint: (@hintChars) ->
71 # Hint chars that have been matched so far.
72 @enteredHintChars = ''
73
74 document = @element.ownerDocument
75
76 while @markerElement.hasChildNodes()
77 @markerElement.firstChild.remove()
78
79 fragment = document.createDocumentFragment()
80 for char in @hintChars
81 charContainer = createElement(document, 'span')
82 charContainer.textContent = char.toUpperCase()
83 fragment.appendChild(charContainer)
84
85 @markerElement.appendChild(fragment)
86
87 matchHintChar: (char) ->
88 @toggleLastHintChar(true)
89 @enteredHintChars += char.toLowerCase()
90 @updateVisibility()
91
92 deleteHintChar: ->
93 @enteredHintChars = @enteredHintChars[...-1]
94 @toggleLastHintChar(false)
95 @updateVisibility()
96
97 toggleLastHintChar: (visible) ->
98 @markerElement.children[@enteredHintChars.length]
99 ?.classList.toggle('VimFxCharMatch', visible)
100
101 isMatched: ->
102 return @hintChars == @enteredHintChars
103
104 reset: ->
105 @setHint(@hintChars)
106 @show()
107
108 # Returns string features of the element that can be used in the bloom filter
109 # in order to add relevance to the hint marker.
110 extractBloomFeatures: ->
111 features = {}
112
113 # Class name of an element (walks up the node tree to find first element
114 # with at least one class).
115 suffix = ''
116 el = @element
117 while el.classList?.length == 0 and el not instanceof HTMLDocument
118 suffix += " #{ el.tagName }"
119 el = el.parentNode
120 if el?.classList?
121 for className in el.classList
122 features["#{ el.tagName }.#{ className }#{ suffix }"] = 10
123
124 if @element.id
125 features["#{ el.tagName }.#{ @element.id }"] = 5
126
127 if @element instanceof HTMLAnchorElement
128 features["a"] = 20 # Reward links no matter what.
129 features["#{ el.tagName }.#{ @element.href }"] = 60
130 features["#{ el.tagName }.#{ @element.title }"] = 40
131
132 return features
133
134 # Returns rating of all present bloom features (plus 1).
135 calcBloomRating: ->
136 rating = 1
137 for feature, weight of @extractBloomFeatures()
138 rating += if @bloomFilter.test(feature) then weight else 0
139
140 return rating
141
142 reward: ->
143 for feature, weight of @extractBloomFeatures()
144 @bloomFilter.add(feature)
145 @bloomFilter.save()
146
147 exports.Marker = Marker
Imprint / Impressum