]> git.gir.st - VimFx.git/blob - extension/packages/hints.coffee
Code cleanup
[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 markers = getMarkers(document)
21
22 # Add hints to the markers
23 hintChars = utils.getHintChars()
24 weightsAndMarkers = ([marker.weight, marker] for marker in markers)
25 addHuffmanCodeWordsTo(weightsAndMarkers, {alphabet: hintChars})
26 for [weight, marker, hint] in weightsAndMarkers
27 marker.setHint(hint)
28
29 return markers
30
31 getMarkers = (document) ->
32 # First remove previous hints container
33 removeHints(document)
34
35 # For now we aren't able to handle hint markers in XUL Documents :(
36 if document instanceof HTMLDocument# or document instanceof XULDocument
37 if document.documentElement
38 # Find and create markers
39 markers = Marker.createMarkers(document)
40
41 container = createHintsContainer(document)
42
43 # For performance use Document Fragment
44 fragment = document.createDocumentFragment()
45 for marker in markers
46 fragment.appendChild(marker.markerElement)
47
48 container.appendChild(fragment)
49 document.documentElement.appendChild(container)
50
51 for frame in document.defaultView.frames
52 markers = markers.concat(getMarkers(frame.document))
53
54 return markers or []
55
56 # Remove previously injected hints from the DOM
57 removeHints = (document) ->
58 if container = document.getElementById(CONTAINER_ID)
59 document.documentElement.removeChild(container)
60
61 for frame in document.defaultView.frames
62 removeHints(frame.document)
63
64
65 exports.injectHints = injectHints
66 exports.removeHints = removeHints
Imprint / Impressum