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 # `MarkerContainer::injectHints` for more information.
29 constructor: (@wrapper, @document, {@isComplementary}) ->
30 @elementShape = @wrapper.shape
31 @markerElement = utils.createBox(@document, 'marker')
32 @markerElement.setAttribute('data-type', @wrapper.type)
33 @weight = @wrapper.combinedArea
38 @text = @wrapper.text?.toLowerCase() ? ''
43 @originalPosition = null
48 @setHint(@originalHint)
52 show: -> @setVisibility(true)
53 hide: -> @setVisibility(false)
55 setVisibility: (@visible) ->
56 @markerElement.classList.toggle('marker--hidden', not @visible)
58 # To be called when the marker has been both assigned a hint and inserted
59 # into the DOM, and thus gotten a width and height.
60 setPosition: (@viewport, @zoom) ->
64 nonCoveredPoint: {x: left, y: top, offset}
67 rect = @markerElement.getBoundingClientRect()
69 @width = rect.width / @zoom
70 @height = rect.height / @zoom
72 # Center the marker vertically on the non-covered point.
73 top -= Math.ceil(@height / 2)
76 # Move the marker just to the left of the text of its element.
77 left -= Math.max(0, @width - textOffset)
79 # Otherwise make sure that it doesn’t flow outside the right side of its
80 # element. This is to avoid the following situation (where `+` is a small
81 # button, `Link text` is a (larger) link and `DAG` and `E` are the hints
82 # placed on top of them.) This makes it clearer which hint does what.
83 # Example site: Hackernews.
85 # z-layer before after
86 # bottom +Link text +Link text
89 left -= Math.max(0, @width - elementWidth)
91 # Make the position relative to the top frame.
95 @originalPosition = {left, top}
96 @moveTo(left + @dx, top + @dy)
98 moveTo: (left, top) ->
99 # Make sure that the marker stays within the viewport.
100 left = Math.min(left, @viewport.right - @width)
101 top = Math.min(top, @viewport.bottom - @height)
102 left = Math.max(left, @viewport.left)
103 top = Math.max(top, @viewport.top)
105 # Take the current zoom into account.
106 left = Math.round(left * @zoom)
107 top = Math.round(top * @zoom)
109 # The positioning is absolute.
110 @markerElement.style.left = "#{left}px"
111 @markerElement.style.top = "#{top}px"
115 left, right: left + @width,
116 top, bottom: top + @height,
119 updatePosition: (@dx, @dy) ->
120 @moveTo(@originalPosition.left + @dx, @originalPosition.top + @dy)
123 @setPosition(@viewport, @zoom)
126 @originalHint ?= @hint
127 @markerElement.textContent = ''
128 fragment = @document.createDocumentFragment()
129 utils.createBox(@document, 'marker-char', fragment, char) for char in @hint
130 @markerElement.appendChild(fragment)
133 return @hint.startsWith(hint)
135 matchText: (strings) ->
136 return strings.every((string) => @text.includes(string))
138 markMatchedPart: (hint) ->
139 matchEnd = if @matchHint(hint) then hint.length else 0
140 for child, index in @markerElement.children
141 child.classList.toggle('marker-char--matched', index < matchEnd)
144 markMatched: (matched) ->
145 @markerElement.classList.toggle('marker--matched', matched)
147 markHighlighted: (highlighted) ->
148 @markerElement.classList.toggle('marker--highlighted', highlighted)
150 module.exports = Marker