]> git.gir.st - VimFx.git/blob - extension/packages/marker.coffee
Fix: commit 586dd356 broke hint marker rotation
[VimFx.git] / extension / packages / marker.coffee
1 { SerializableBloomFilter
2 , DummyBloomFilter } = require 'bloomfilter'
3
4 { getPref } = require 'prefs'
5
6 HTMLDocument = Ci.nsIDOMHTMLDocument
7 HTMLAnchorElement = Ci.nsIDOMHTMLAnchorElement
8
9 Z_INDEX_START = 99999999 # The highest `z-index` used in style.css
10 _zIndex = Z_INDEX_START
11 # Each marker will get a `z-index` of `_zIndex++`. In theory, `z-index` can be infinitely large. In
12 # practice, Firefox uses a 32-bit signed integer to store it, so the maximum value is 2147483647
13 # (http://www.puidokas.com/max-z-index/). However, we do not need to worry about hitting the limit,
14 # since the user would have to browse through a bit more than 2 billion links in a single Firefox
15 # session before that happens.
16
17 realBloomFilter = new SerializableBloomFilter('hints_bloom_data', 256 * 32, 16)
18 dummyBloomFilter = new DummyBloomFilter()
19
20 # Wraps the markable element and provides methods to manipulate the markers
21 class Marker
22 # Creates the marker DOM node
23 constructor: (@element) ->
24 document = @element.ownerDocument
25 window = document.defaultView
26 @markerElement = document.createElement('div')
27 @markerElement.className = 'VimFxReset VimFxHintMarker'
28
29 Object.defineProperty this, 'bloomFilter',
30 get: -> if getPref('hints_bloom_on') then realBloomFilter else dummyBloomFilter
31
32 show: -> @markerElement.className = 'VimFxReset VimFxHintMarker'
33 hide: -> @markerElement.className = 'VimFxReset VimFxHiddenHintMarker'
34
35 setPosition: (top, left) ->
36 # The positioning is absulute
37 @markerElement.style.top = top + 'px'
38 @markerElement.style.left = left + 'px'
39
40 # For quick access
41 @position = {top, left}
42
43 # Each marker gets a unique `z-index`, so that it can be determined if a marker overlaps another.
44 @markerElement.style.setProperty('z-index', _zIndex++, 'important')
45
46 # To be called when the marker has been both assigned a hint and inserted into the DOM, and thus
47 # gotten a height and width.
48 completePosition: ->
49 {
50 position: { top, left }
51 markerElement: { offsetHeight: height, offsetWidth: width }
52 } = this
53 @position = {top, bottom: top + height, left, right: left + width, height, width}
54
55 setHint: (@hintChars) ->
56 # Hint chars that have been matched so far
57 @enteredHintChars = ''
58
59 document = @element.ownerDocument
60
61 while @markerElement.hasChildNodes()
62 @markerElement.removeChild(@markerElement.firstChild)
63
64 fragment = document.createDocumentFragment()
65 for char in @hintChars
66 span = document.createElement('span')
67 span.className = 'VimFxReset'
68 span.textContent = char.toUpperCase()
69 fragment.appendChild(span)
70
71 @markerElement.appendChild(fragment)
72
73 # Add another char to the `enteredHintString`,
74 # see if it still matches `hintString`, apply classes to
75 # the distinct hint characters and show/hide marker when
76 # the entered string partially (not) matches the hint string.
77 matchHintChar: (char) ->
78 if char == 'Backspace'
79 # Handle backspace key by removing a previously entered hint char and resetting its class
80 if @enteredHintChars.length > 0
81 @enteredHintChars = @enteredHintChars[0...-1]
82 @markerElement.children[@enteredHintChars.length]?.className = 'VimFxReset'
83 else
84 # Otherwise append hint char and change hint class
85 @markerElement.children[@enteredHintChars.length]?.className = 'VimFxReset VimFxCharMatch'
86 @enteredHintChars += char.toLowerCase()
87
88 # If entered hint chars no longer partially match the hint chars then hide the marker,
89 # otherwise show it back
90 if @hintChars.search(@enteredHintChars) == 0 then @show() else @hide()
91
92 # Checks if the marker will be matched if the next character entered is `char`
93 willMatch: (char) ->
94 char == 'Backspace' or @hintChars.search(@enteredHintChars + char.toLowerCase()) == 0
95
96 isMatched: ->
97 return @hintChars == @enteredHintChars
98
99 # Returns string features of the element that can be used in the bloom filter
100 # in order to add relevance to the hint marker
101 extractBloomFeatures: ->
102 features = {}
103
104 # Class name of an element (walks up the node tree to find first element with at least one class)
105 suffix = ''
106 el = @element
107 while el.classList?.length == 0 and el not instanceof HTMLDocument
108 suffix = "#{ suffix } #{ el.tagName }"
109 el = el.parentNode
110 if el and el.classList
111 for className in el.classList
112 features["#{ el.tagName }.#{ className }#{ suffix }"] = 10
113
114 # Element id
115 if @element.id
116 features["#{ el.tagName }.#{ @element.id }"] = 5
117
118 if @element instanceof HTMLAnchorElement
119 features["a"] = 20 # Reward links no matter what
120 features["#{ el.tagName }.#{ @element.href }"] = 60
121 features["#{ el.tagName }.#{ @element.title }"] = 40
122
123 return features
124
125 # Returns rating of all present bloom features (plus 1)
126 calcBloomRating: ->
127 rating = 1
128 for feature, weight of @extractBloomFeatures()
129 rating += if @bloomFilter.test(feature) then weight else 0
130
131 return rating
132
133 reward: ->
134 for feature, weight of @extractBloomFeatures()
135 @bloomFilter.add(feature)
136 @bloomFilter.save()
137
138 exports.Marker = Marker
Imprint / Impressum