2 # Copyright Anton Khodakivskiy 2012, 2013.
3 # Copyright Simon Lydell 2013, 2014, 2015.
5 # This file is part of VimFx.
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.
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.
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/>.
21 # This file contains an abstraction for hint markers. It creates the UI for a
22 # marker and provides methods to manipulate the markers.
24 utils = require('./utils')
27 # `@wrapper` is a stand-in for the element that the marker represents. See
28 # `injectHints` in hints.coffee for more information.
29 constructor: (@wrapper, @document) ->
30 @elementShape = @wrapper.shape
31 @markerElement = utils.createBox(@document, 'marker')
32 @markerElement.setAttribute('data-type', @wrapper.type)
33 @weight = @elementShape.area
39 show: -> @setVisibility(true)
40 hide: -> @setVisibility(false)
41 setVisibility: (visible) ->
42 @markerElement.classList.toggle('marker--hidden', not visible)
44 # To be called when the marker has been both assigned a hint and inserted
45 # into the DOM, and thus gotten a height and width.
46 setPosition: (viewport, zoom) ->
48 markerElement: {clientHeight: height, clientWidth: width}
49 elementShape: {nonCoveredPoint: {x: left, y: top, offset, rect}}
52 # Center the marker vertically on the non-covered point.
53 top -= Math.ceil(height / 2)
55 # Make sure that the marker stays within its element (vertically).
56 top = Math.min(top, rect.bottom - height)
57 top = Math.max(top, rect.top)
59 # Make the position relative to the top frame.
63 # Make sure that the marker stays within the viewport.
64 left = Math.min(left, viewport.right - width)
65 top = Math.min(top, viewport.bottom - height)
66 left = Math.max(left, viewport.left)
67 top = Math.max(top, viewport.top)
69 # Take the current zoom into account.
70 left = Math.round(left * zoom)
71 top = Math.round(top * zoom)
73 # The positioning is absolute.
74 @markerElement.style.left = "#{left}px"
75 @markerElement.style.top = "#{top}px"
79 left, right: left + width,
80 top, bottom: top + height,
86 @markerElement.textContent = ''
87 fragment = @document.createDocumentFragment()
88 utils.createBox(@document, 'marker-char', fragment, char) for char in @hint
89 @markerElement.appendChild(fragment)
91 matchHintChar: (char) ->
92 if char == @hint[@hintIndex]
93 @toggleLastHintChar(true)
101 @toggleLastHintChar(false)
103 toggleLastHintChar: (visible) ->
104 @markerElement.children[@hintIndex]
105 .classList.toggle('marker-char--matched', visible)
107 isMatched: -> (@hintIndex == @hint.length)
109 markMatched: (matched) ->
110 @markerElement.classList.toggle('marker--matched', matched)
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
115 rotateOverlappingMarkers = (originalMarkers, forward) ->
116 # Shallow working copy. This is necessary since `markers` will be mutated and
118 markers = originalMarkers[..]
121 stacks = (getStackFor(markers.pop(), markers) while markers.length > 0)
124 # Stacks of length 1 don't participate in any overlapping, and can therefore
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)
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.
135 indexStack.unshift(indexStack.pop())
137 indexStack.push(indexStack.shift())
139 for marker, index in stack
140 marker.markerElement.style.zIndex = indexStack[index]
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) ->
150 {top, bottom, left, right} = marker.position
153 while index < markers.length
154 nextMarker = markers[index]
156 next = nextMarker.position
157 overlapsVertically = (next.bottom >= top and next.top <= bottom)
158 overlapsHorizontally = (next.right >= left and next.left <= right)
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))
165 # Continue the search.
172 rotateOverlappingMarkers