1 CONTAINER_ID = 'VimFxHintMarkerContainer'
3 { interfaces: Ci } = Components
4 HTMLDocument = Ci.nsIDOMHTMLDocument
5 XULDocument = Ci.nsIDOMXULDocument
6 { Marker } = require 'marker'
7 utils = require 'utils'
8 { addHuffmanCodeWordsTo } = require 'huffman'
10 createHintsContainer = (document) ->
11 container = document.createElement 'div'
12 container.id = CONTAINER_ID
13 container.className = 'VimFxReset'
16 # Creates and injects hint markers into the DOM
17 injectHints = (document) ->
20 # First remove previous hints container
23 # For now we aren't able to handle hint markers in XUL Documents :(
24 if document instanceof HTMLDocument# or document instanceof XULDocument
25 if document.documentElement
26 # Find and create markers
27 markers = Marker.createMarkers document
29 container = createHintsContainer document
31 # For performance use Document Fragment
32 fragment = document.createDocumentFragment()
34 fragment.appendChild marker.markerElement
36 container.appendChild fragment
37 document.documentElement.appendChild container
39 for frame in document.defaultView.frames
40 markers = markers.concat inner(frame.document)
44 markers = inner(document)
46 # Add hints to the markers
47 hintChars = utils.getHintChars()
48 weightsAndMarkers = markers.map (marker) -> [marker.weight, marker]
49 addHuffmanCodeWordsTo weightsAndMarkers, {alphabet: hintChars}
50 for [weight, marker, hint] in weightsAndMarkers
55 # Remove previously injected hints from the DOM
56 removeHints = (document) ->
57 if container = document.getElementById CONTAINER_ID
58 document.documentElement.removeChild container
60 for frame in document.defaultView.frames
61 removeHints frame.document
64 exports.injectHints = injectHints
65 exports.removeHints = removeHints