]> git.gir.st - VimFx.git/blob - extension/lib/marker.coffee
Move `rotateOverlappingMarkers()` into marker.coffee
[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
30 reset: ->
31 @setHint(@hint)
32 @show()
33
34 show: -> @setVisibility(true)
35 hide: -> @setVisibility(false)
36 setVisibility: (visible) ->
37 @markerElement.classList.toggle('VimFxHiddenHintMarker', not visible)
38
39 # To be called when the marker has been both assigned a hint and inserted
40 # into the DOM, and thus gotten a height and width.
41 setPosition: (viewport) ->
42 {
43 markerElement: { offsetHeight: height, offsetWidth: width }
44 elementShape: { nonCoveredPoint: { x: left, y: top, offset, rect } }
45 } = this
46
47 # Center the marker vertically on the non-covered point.
48 top -= height / 2
49
50 # Make sure that the marker stays within its element (vertically).
51 top = Math.min(top, rect.bottom - height)
52 top = Math.max(top, rect.top)
53
54 # Make the position relative to the top frame.
55 left += offset.left
56 top += offset.top
57
58 # Make sure that the marker stays within the viewport.
59 left = Math.min(left, viewport.right - width)
60 top = Math.min(top, viewport.bottom - height)
61 left = Math.max(left, viewport.left)
62 top = Math.max(top, viewport.top)
63
64 # The positioning is absolute.
65 @markerElement.style.left = "#{ left }px"
66 @markerElement.style.top = "#{ top }px"
67
68 # For quick access.
69 @position = {
70 left, right: left + width,
71 top, bottom: top + height,
72 height, width
73 }
74
75 setHint: (@hint) ->
76 @hintIndex = 0
77
78 document = @element.ownerDocument
79
80 while @markerElement.hasChildNodes()
81 @markerElement.firstChild.remove()
82
83 fragment = document.createDocumentFragment()
84 for char in @hint
85 charContainer = document.createElement('span')
86 charContainer.textContent = char
87 fragment.appendChild(charContainer)
88
89 @markerElement.appendChild(fragment)
90
91 matchHintChar: (char) ->
92 if char == @hint[@hintIndex]
93 @toggleLastHintChar(true)
94 @hintIndex++
95 return true
96 return false
97
98 deleteHintChar: ->
99 if @hintIndex > 0
100 @hintIndex--
101 @toggleLastHintChar(false)
102
103 toggleLastHintChar: (visible) ->
104 @markerElement.children[@hintIndex]
105 .classList.toggle('VimFxCharMatch', visible)
106
107 isMatched: -> (@hintIndex == @hint.length)
108
109 markMatched: (matched) ->
110 @markerElement.classList.toggle('VimFxMatchedHintMarker', matched)
111
112 # Finds all stacks of markers that overlap each other (by using `getStackFor`)
113 # (#1), and rotates their `z-index`:es (#2), thus alternating which markers are
114 # visible.
115 rotateOverlappingMarkers = (originalMarkers, forward) ->
116 # Shallow working copy. This is necessary since `markers` will be mutated and
117 # eventually empty.
118 markers = originalMarkers[..]
119
120 # (#1)
121 stacks = (getStackFor(markers.pop(), markers) while markers.length > 0)
122
123 # (#2)
124 # Stacks of length 1 don't participate in any overlapping, and can therefore
125 # be skipped.
126 for stack in stacks when stack.length > 1
127 # This sort is not required, but makes the rotation more predictable.
128 stack.sort((a, b) -> a.markerElement.style.zIndex -
129 b.markerElement.style.zIndex)
130
131 # Array of z-indices.
132 indexStack = (marker.markerElement.style.zIndex for marker in stack)
133 # Shift the array of indices one item forward or back.
134 if forward
135 indexStack.unshift(indexStack.pop())
136 else
137 indexStack.push(indexStack.shift())
138
139 for marker, index in stack
140 marker.markerElement.style.zIndex = indexStack[index]
141
142 return
143
144 # Get an array containing `marker` and all markers that overlap `marker`, if
145 # any, which is called a "stack". All markers in the returned stack are spliced
146 # out from `markers`, thus mutating it.
147 getStackFor = (marker, markers) ->
148 stack = [marker]
149
150 { top, bottom, left, right } = marker.position
151
152 index = 0
153 while index < markers.length
154 nextMarker = markers[index]
155
156 next = nextMarker.position
157 overlapsVertically = (next.bottom >= top and next.top <= bottom)
158 overlapsHorizontally = (next.right >= left and next.left <= right)
159
160 if overlapsVertically and overlapsHorizontally
161 # Also get all markers overlapping this one.
162 markers.splice(index, 1)
163 stack = stack.concat(getStackFor(nextMarker, markers))
164 else
165 # Continue the search.
166 index++
167
168 return stack
169
170 exports.Marker = Marker
171 exports.rotateOverlappingMarkers = rotateOverlappingMarkers
Imprint / Impressum