]> git.gir.st - VimFx.git/blob - extension/packages/hints.coffee
Merge branch 'bugfixes' into huffman
[VimFx.git] / extension / packages / hints.coffee
1 utils = require 'utils'
2 { Marker } = require 'marker'
3 { addHuffmanCodeWordsTo } = require 'huffman'
4
5 { interfaces: Ci } = Components
6
7 HTMLDocument = Ci.nsIDOMHTMLDocument
8 XULDocument = Ci.nsIDOMXULDocument
9
10 CONTAINER_ID = 'VimFxHintMarkerContainer'
11
12 createHintsContainer = (document) ->
13 container = document.createElement('div')
14 container.id = CONTAINER_ID
15 container.className = 'VimFxReset'
16 return container
17
18 # Creates and injects hint markers into the DOM
19 injectHints = (document) ->
20
21 inner = (document) ->
22 # First remove previous hints container
23 removeHints(document)
24
25 # For now we aren't able to handle hint markers in XUL Documents :(
26 if document instanceof HTMLDocument# or document instanceof XULDocument
27 if document.documentElement
28 # Find and create markers
29 markers = Marker.createMarkers(document)
30
31 container = createHintsContainer(document)
32
33 # For performance use Document Fragment
34 fragment = document.createDocumentFragment()
35 for marker in markers
36 fragment.appendChild(marker.markerElement)
37
38 container.appendChild(fragment)
39 document.documentElement.appendChild(container)
40
41 for frame in document.defaultView.frames
42 markers = markers.concat(inner(frame.document))
43
44 return markers or []
45
46 markers = inner(document)
47
48 # Add hints to the markers
49 hintChars = utils.getHintChars()
50 weightsAndMarkers = markers.map((marker) -> [marker.weight, marker])
51 addHuffmanCodeWordsTo(weightsAndMarkers, {alphabet: hintChars})
52 for [weight, marker, hint] in weightsAndMarkers
53 marker.setHint(hint)
54
55 return markers
56
57 # Remove previously injected hints from the DOM
58 removeHints = (document) ->
59 if container = document.getElementById(CONTAINER_ID)
60 document.documentElement.removeChild(container)
61
62 for frame in document.defaultView.frames
63 removeHints(frame.document)
64
65
66 exports.injectHints = injectHints
67 exports.removeHints = removeHints
Imprint / Impressum