2 # Copyright Anton Khodakivskiy 2012, 2013.
3 # Copyright Simon Lydell 2013, 2014, 2015, 2016.
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
41 @originalPosition = null
47 show: -> @setVisibility(true)
48 hide: -> @setVisibility(false)
50 setVisibility: (visible) ->
51 @markerElement.classList.toggle('marker--hidden', not visible)
53 # To be called when the marker has been both assigned a hint and inserted
54 # into the DOM, and thus gotten a width and height.
55 setPosition: (@viewport, @zoom) ->
57 markerElement: {clientWidth, clientHeight}
58 elementShape: {nonCoveredPoint: {x: left, y: top, offset}}
61 @width = clientWidth / @zoom
62 @height = clientHeight / @zoom
64 # Center the marker vertically on the non-covered point.
65 top -= Math.ceil(@height / 2)
67 # Make the position relative to the top frame.
71 @originalPosition = {left, top}
74 moveTo: (left, top) ->
75 # Make sure that the marker stays within the viewport.
76 left = Math.min(left, @viewport.right - @width)
77 top = Math.min(top, @viewport.bottom - @height)
78 left = Math.max(left, @viewport.left)
79 top = Math.max(top, @viewport.top)
81 # Take the current zoom into account.
82 left = Math.round(left * @zoom)
83 top = Math.round(top * @zoom)
85 # The positioning is absolute.
86 @markerElement.style.left = "#{left}px"
87 @markerElement.style.top = "#{top}px"
91 left, right: left + @width,
92 top, bottom: top + @height,
95 updatePosition: (dx, dy) ->
96 @moveTo(@originalPosition.left + dx, @originalPosition.top + dy)
100 @markerElement.textContent = ''
101 fragment = @document.createDocumentFragment()
102 utils.createBox(@document, 'marker-char', fragment, char) for char in @hint
103 @markerElement.appendChild(fragment)
105 matchHintChar: (char) ->
106 if char == @hint[@hintIndex]
107 @toggleLastHintChar(true)
115 @toggleLastHintChar(false)
117 toggleLastHintChar: (visible) ->
118 @markerElement.children[@hintIndex]
119 .classList.toggle('marker-char--matched', visible)
121 isMatched: -> (@hintIndex == @hint.length)
123 markMatched: (matched) ->
124 @markerElement.classList.toggle('marker--matched', matched)
126 # Finds all stacks of markers that overlap each other (by using `getStackFor`)
127 # (#1), and rotates their `z-index`:es (#2), thus alternating which markers are
129 rotateOverlappingMarkers = (originalMarkers, forward) ->
130 # Shallow working copy. This is necessary since `markers` will be mutated and
132 markers = originalMarkers[..]
135 stacks = (getStackFor(markers.pop(), markers) while markers.length > 0)
138 # Stacks of length 1 don't participate in any overlapping, and can therefore
140 for stack in stacks when stack.length > 1
141 # This sort is not required, but makes the rotation more predictable.
142 stack.sort((a, b) -> a.markerElement.style.zIndex -
143 b.markerElement.style.zIndex)
145 # Array of z-indices.
146 indexStack = (marker.markerElement.style.zIndex for marker in stack)
147 # Shift the array of indices one item forward or back.
149 indexStack.unshift(indexStack.pop())
151 indexStack.push(indexStack.shift())
153 for marker, index in stack
154 marker.markerElement.style.zIndex = indexStack[index]
158 # Get an array containing `marker` and all markers that overlap `marker`, if
159 # any, which is called a "stack". All markers in the returned stack are spliced
160 # out from `markers`, thus mutating it.
161 getStackFor = (marker, markers) ->
164 {top, bottom, left, right} = marker.position
167 while index < markers.length
168 nextMarker = markers[index]
170 next = nextMarker.position
171 overlapsVertically = (next.bottom >= top and next.top <= bottom)
172 overlapsHorizontally = (next.right >= left and next.left <= right)
174 if overlapsVertically and overlapsHorizontally
175 # Also get all markers overlapping this one.
176 markers.splice(index, 1)
177 stack = stack.concat(getStackFor(nextMarker, markers))
179 # Continue the search.
186 rotateOverlappingMarkers