]> git.gir.st - VimFx.git/blob - extension/packages/hints.coffee
Fix frames handling
[VimFx.git] / extension / packages / hints.coffee
1 CONTAINER_ID = 'VimFxHintMarkerContainer'
2
3 { interfaces: Ci } = Components
4 HTMLDocument = Ci.nsIDOMHTMLDocument
5 XULDocument = Ci.nsIDOMXULDocument
6 { Marker } = require 'marker'
7 utils = require 'utils'
8 { addHuffmanCodeWordsTo } = require 'huffman'
9
10 createHintsContainer = (document) ->
11 container = document.createElement 'div'
12 container.id = CONTAINER_ID
13 container.className = 'VimFxReset'
14 return container
15
16 # Creates and injects hint markers into the DOM
17 injectHints = (document) ->
18
19 inner = (document) ->
20 # First remove previous hints container
21 removeHints document
22
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
28
29 container = createHintsContainer document
30
31 # For performance use Document Fragment
32 fragment = document.createDocumentFragment()
33 for marker in markers
34 fragment.appendChild marker.markerElement
35
36 container.appendChild fragment
37 document.documentElement.appendChild container
38
39 for frame in document.defaultView.frames
40 markers = markers.concat inner(frame.document)
41
42 return markers
43
44 markers = inner(document)
45
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
51 marker.setHint hint
52
53 return markers
54
55 # Remove previously injected hints from the DOM
56 removeHints = (document) ->
57 if container = document.getElementById CONTAINER_ID
58 document.documentElement.removeChild container
59
60 for frame in document.defaultView.frames
61 removeHints frame.document
62
63
64 exports.injectHints = injectHints
65 exports.removeHints = removeHints
Imprint / Impressum