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