]> git.gir.st - VimFx.git/blob - extension/lib/marker.coffee
Merge pull request #451 from lydell/hints
[VimFx.git] / extension / lib / marker.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013.
3 # Copyright Simon Lydell 2013, 2014.
4 #
5 # This file is part of VimFx.
6 #
7 # VimFx is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # VimFx is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
19 ###
20
21 # Wraps the markable element and provides methods to manipulate the markers.
22 class Marker
23 # Creates the marker DOM node.
24 constructor: (@element, @elementShape, { @semantic, @type }) ->
25 document = @element.ownerDocument
26 @markerElement = document.createElement('div')
27 @markerElement.classList.add('VimFxHintMarker')
28 @weight = @elementShape.area
29 @numChildren = 0
30
31 reset: ->
32 @setHint(@hint)
33 @show()
34
35 show: -> @setVisibility(true)
36 hide: -> @setVisibility(false)
37 setVisibility: (visible) ->
38 @markerElement.classList.toggle('VimFxHiddenHintMarker', not visible)
39
40 # To be called when the marker has been both assigned a hint and inserted
41 # into the DOM, and thus gotten a height and width.
42 setPosition: (viewport) ->
43 {
44 markerElement: { clientHeight: height, clientWidth: width }
45 elementShape: { nonCoveredPoint: { x: left, y: top, offset, rect } }
46 } = this
47
48 # Center the marker vertically on the non-covered point.
49 top -= height / 2
50
51 # Make sure that the marker stays within its element (vertically).
52 top = Math.min(top, rect.bottom - height)
53 top = Math.max(top, rect.top)
54
55 # Make the position relative to the top frame.
56 left += offset.left
57 top += offset.top
58
59 # Make sure that the marker stays within the viewport.
60 left = Math.min(left, viewport.right - width)
61 top = Math.min(top, viewport.bottom - height)
62 left = Math.max(left, viewport.left)
63 top = Math.max(top, viewport.top)
64
65 # The positioning is absolute.
66 @markerElement.style.left = "#{ left }px"
67 @markerElement.style.top = "#{ top }px"
68
69 # For quick access.
70 @position = {
71 left, right: left + width,
72 top, bottom: top + height,
73 height, width
74 }
75
76 setHint: (@hint) ->
77 @hintIndex = 0
78
79 document = @element.ownerDocument
80
81 while @markerElement.hasChildNodes()
82 @markerElement.firstChild.remove()
83
84 fragment = document.createDocumentFragment()
85 for char in @hint
86 charContainer = document.createElement('span')
87 charContainer.textContent = char
88 fragment.appendChild(charContainer)
89
90 @markerElement.appendChild(fragment)
91
92 matchHintChar: (char) ->
93 if char == @hint[@hintIndex]
94 @toggleLastHintChar(true)
95 @hintIndex++
96 return true
97 return false
98
99 deleteHintChar: ->
100 if @hintIndex > 0
101 @hintIndex--
102 @toggleLastHintChar(false)
103
104 toggleLastHintChar: (visible) ->
105 @markerElement.children[@hintIndex]
106 .classList.toggle('VimFxCharMatch', visible)
107
108 isMatched: -> (@hintIndex == @hint.length)
109
110 markMatched: (matched) ->
111 @markerElement.classList.toggle('VimFxMatchedHintMarker', matched)
112
113 # Finds all stacks of markers that overlap each other (by using `getStackFor`)
114 # (#1), and rotates their `z-index`:es (#2), thus alternating which markers are
115 # visible.
116 rotateOverlappingMarkers = (originalMarkers, forward) ->
117 # Shallow working copy. This is necessary since `markers` will be mutated and
118 # eventually empty.
119 markers = originalMarkers[..]
120
121 # (#1)
122 stacks = (getStackFor(markers.pop(), markers) while markers.length > 0)
123
124 # (#2)
125 # Stacks of length 1 don't participate in any overlapping, and can therefore
126 # be skipped.
127 for stack in stacks when stack.length > 1
128 # This sort is not required, but makes the rotation more predictable.
129 stack.sort((a, b) -> a.markerElement.style.zIndex -
130 b.markerElement.style.zIndex)
131
132 # Array of z-indices.
133 indexStack = (marker.markerElement.style.zIndex for marker in stack)
134 # Shift the array of indices one item forward or back.
135 if forward
136 indexStack.unshift(indexStack.pop())
137 else
138 indexStack.push(indexStack.shift())
139
140 for marker, index in stack
141 marker.markerElement.style.zIndex = indexStack[index]
142
143 return
144
145 # Get an array containing `marker` and all markers that overlap `marker`, if
146 # any, which is called a "stack". All markers in the returned stack are spliced
147 # out from `markers`, thus mutating it.
148 getStackFor = (marker, markers) ->
149 stack = [marker]
150
151 { top, bottom, left, right } = marker.position
152
153 index = 0
154 while index < markers.length
155 nextMarker = markers[index]
156
157 next = nextMarker.position
158 overlapsVertically = (next.bottom >= top and next.top <= bottom)
159 overlapsHorizontally = (next.right >= left and next.left <= right)
160
161 if overlapsVertically and overlapsHorizontally
162 # Also get all markers overlapping this one.
163 markers.splice(index, 1)
164 stack = stack.concat(getStackFor(nextMarker, markers))
165 else
166 # Continue the search.
167 index++
168
169 return stack
170
171 exports.Marker = Marker
172 exports.rotateOverlappingMarkers = rotateOverlappingMarkers
Imprint / Impressum