1 utils = require 'utils'
2 { Marker } = require 'marker'
3 { addHuffmanCodeWordsTo } = require 'huffman'
5 { interfaces: Ci } = Components
7 HTMLDocument = Ci.nsIDOMHTMLDocument
8 XULDocument = Ci.nsIDOMXULDocument
10 CONTAINER_ID = 'VimFxHintMarkerContainer'
12 createHintsContainer = (document) ->
13 container = document.createElement('div')
14 container.id = CONTAINER_ID
15 container.className = 'VimFxReset'
18 # Creates and injects hint markers into the DOM
19 injectHints = (document) ->
20 markers = getMarkers(document)
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
31 getMarkers = (document) ->
32 # First remove previous hints container
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)
41 container = createHintsContainer(document)
43 # For performance use Document Fragment
44 fragment = document.createDocumentFragment()
46 fragment.appendChild(marker.markerElement)
48 container.appendChild(fragment)
49 document.documentElement.appendChild(container)
51 for frame in document.defaultView.frames
52 markers = markers.concat(getMarkers(frame.document))
56 # Remove previously injected hints from the DOM
57 removeHints = (document) ->
58 if container = document.getElementById(CONTAINER_ID)
59 document.documentElement.removeChild(container)
61 for frame in document.defaultView.frames
62 removeHints(frame.document)
65 exports.injectHints = injectHints
66 exports.removeHints = removeHints