1 { createElement } = require 'utils'
2 { SerializableBloomFilter
3 , DummyBloomFilter } = require 'mode-hints/bloomfilter'
5 { getPref } = require 'prefs'
7 HTMLDocument = Ci.nsIDOMHTMLDocument
8 HTMLAnchorElement = Ci.nsIDOMHTMLAnchorElement
10 realBloomFilter = new SerializableBloomFilter('hints_bloom_data', 256 * 32, 16)
11 dummyBloomFilter = new DummyBloomFilter()
13 # Wraps the markable element and provides methods to manipulate the markers.
15 # Creates the marker DOM node.
16 constructor: (@element, @elementShape) ->
17 document = @element.ownerDocument
18 @markerElement = createElement(document, 'div', {class: 'VimFxHintMarker'})
20 Object.defineProperty this, 'bloomFilter',
21 get: -> if getPref('hints_bloom_on') then realBloomFilter else dummyBloomFilter
23 show: -> @setVisibility(true)
24 hide: -> @setVisibility(false)
25 setVisibility: (visible) ->
26 @markerElement.classList.toggle('VimFxHiddenHintMarker', !visible)
28 if @hintChars.startsWith(@enteredHintChars) then @show() else @hide()
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) ->
34 markerElement: { offsetHeight: height, offsetWidth: width }
35 elementShape: { nonCoveredPoint: { x: left, y: top, offset, rect } }
38 # The `nonCoveredPoint` is 1px from the edges of the element (see
39 # hints.coffee). It looks nicer if the marker is placed exactly at the
44 # Center the marker vertically on the non-covered point.
47 # Make sure that the marker stays within its element (vertically).
48 top = Math.min(top, rect.bottom - height)
49 top = Math.max(top, rect.top)
51 # Make the position relative to the top frame.
55 # Make sure that the marker stays within the viewport.
56 left = Math.min(left, viewport.right - width)
57 top = Math.min(top, viewport.bottom - height)
58 left = Math.max(left, viewport.left)
59 top = Math.max(top, viewport.top)
61 # Make the position relative to the document, rather than to the viewport.
62 left += viewport.scrollX
63 top += viewport.scrollY
65 # The positioning is absolute.
66 @markerElement.style.left = "#{ left }px"
67 @markerElement.style.top = "#{ top }px"
71 left, right: left + width,
72 top, bottom: top + height,
76 setHint: (@hintChars) ->
77 # Hint chars that have been matched so far.
78 @enteredHintChars = ''
80 document = @element.ownerDocument
82 while @markerElement.hasChildNodes()
83 @markerElement.firstChild.remove()
85 fragment = document.createDocumentFragment()
86 for char in @hintChars
87 charContainer = createElement(document, 'span')
88 charContainer.textContent = char.toUpperCase()
89 fragment.appendChild(charContainer)
91 @markerElement.appendChild(fragment)
93 matchHintChar: (char) ->
94 @toggleLastHintChar(true)
95 @enteredHintChars += char.toLowerCase()
99 @enteredHintChars = @enteredHintChars[...-1]
100 @toggleLastHintChar(false)
103 toggleLastHintChar: (visible) ->
104 @markerElement.children[@enteredHintChars.length]
105 ?.classList.toggle('VimFxCharMatch', visible)
108 return @hintChars == @enteredHintChars
114 # Returns string features of the element that can be used in the bloom filter
115 # in order to add relevance to the hint marker.
116 extractBloomFeatures: ->
119 # Class name of an element (walks up the node tree to find first element
120 # with at least one class).
123 while el.classList?.length == 0 and el not instanceof HTMLDocument
124 suffix += " #{ el.tagName }"
127 for className in el.classList
128 features["#{ el.tagName }.#{ className }#{ suffix }"] = 10
131 features["#{ el.tagName }.#{ @element.id }"] = 5
133 if @element instanceof HTMLAnchorElement
134 features["a"] = 20 # Reward links no matter what.
135 features["#{ el.tagName }.#{ @element.href }"] = 60
136 features["#{ el.tagName }.#{ @element.title }"] = 40
140 # Returns rating of all present bloom features (plus 1).
143 for feature, weight of @extractBloomFeatures()
144 rating += if @bloomFilter.test(feature) then weight else 0
149 for feature, weight of @extractBloomFeatures()
150 @bloomFilter.add(feature)
153 exports.Marker = Marker