]> git.gir.st - VimFx.git/blob - extension/packages/marker.coffee
Group all hint injection code in hints.coffee
[VimFx.git] / extension / packages / marker.coffee
1 # Marker class wraps the markable element and provides
2 # methods to manipulate the markers
3 class Marker
4 # Creates the marker DOM node
5 constructor: (@element) ->
6 document = @element.ownerDocument
7 window = document.defaultView
8 @markerElement = document.createElement('div')
9 @markerElement.className = 'VimFxReset VimFxHintMarker'
10
11 # Shows the marker
12 show: -> @markerElement.className = 'VimFxReset VimFxHintMarker'
13
14 # Hides the marker
15 hide: -> @markerElement.className = 'VimFxReset VimFxHiddenHintMarker'
16
17 # Positions the marker on the page. The positioning is absulute
18 setPosition: (rect) ->
19 @markerElement.style.left = rect.left + 'px'
20 @markerElement.style.top = rect.top + 'px'
21
22 # Assigns hint string to the marker
23 setHint: (@hintChars) ->
24 # Hint chars that have been matched so far
25 @enteredHintChars = ''
26
27 document = @element.ownerDocument
28
29 while @markerElement.hasChildNodes()
30 @markerElement.removeChild(@markerElement.firstChild)
31
32 fragment = document.createDocumentFragment()
33 for char in @hintChars
34 span = document.createElement('span')
35 span.className = 'VimFxReset'
36 span.textContent = char.toUpperCase()
37 fragment.appendChild(span)
38
39 @markerElement.appendChild(fragment)
40
41 # Add another char to the `enteredHintString`,
42 # see if it still matches `hintString`, apply classes to
43 # the distinct hint characters and show/hide marker when
44 # the entered string partially (not) matches the hint string
45 matchHintChar: (char) ->
46 # Handle backspace key by removing a previously entered hint char
47 # and resetting its class
48 if char == 'Backspace'
49 if @enteredHintChars.length > 0
50 @enteredHintChars = @enteredHintChars[0...-1]
51 @markerElement.children[@enteredHintChars.length]?.className = 'VimFxReset'
52 # Otherwise append hint char and change hint class
53 else
54 @markerElement.children[@enteredHintChars.length]?.className = 'VimFxReset VimFxCharMatch'
55 @enteredHintChars += char.toLowerCase()
56
57 # If entered hint chars no longer partially match the hint chars
58 # then hide the marker. Othersie show it back
59 if @hintChars.search(@enteredHintChars) == 0 then @show() else @hide()
60
61 # Checks if the marker will be matched if the next character entered is `char`
62 willMatch: (char) ->
63 char == 'Backspace' or @hintChars.search(@enteredHintChars + char.toLowerCase()) == 0
64
65 # Checks if enterd hint chars completely match the hint chars
66 isMatched: ->
67 return @hintChars == @enteredHintChars
68
69
70 exports.Marker = Marker
Imprint / Impressum