]> git.gir.st - VimFx.git/blob - extension/lib/marker.coffee
Consistently refer to options as "options"
[VimFx.git] / extension / lib / marker.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013.
3 # Copyright Simon Lydell 2013, 2014, 2015, 2016.
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 # This file contains an abstraction for hint markers. It creates the UI for a
22 # marker and provides methods to manipulate the markers.
23
24 utils = require('./utils')
25
26 class Marker
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
34 @width = 0
35 @height = 0
36 @hint = ''
37 @originalHint = null
38 @text = @wrapper.text?.toLowerCase() ? ''
39 @visible = true
40 @zoom = 1
41 @viewport = null
42 @position = null
43 @originalPosition = null
44 @dx = 0
45 @dy = 0
46
47 reset: ->
48 @setHint(@originalHint)
49 @show()
50 @refreshPosition()
51
52 show: -> @setVisibility(true)
53 hide: -> @setVisibility(false)
54
55 setVisibility: (@visible) ->
56 @markerElement.classList.toggle('marker--hidden', not @visible)
57
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) ->
61 {
62 textOffset
63 width: elementWidth
64 nonCoveredPoint: {x: left, y: top, offset}
65 } = @elementShape
66
67 rect = @markerElement.getBoundingClientRect()
68
69 @width = rect.width / @zoom
70 @height = rect.height / @zoom
71
72 # Center the marker vertically on the non-covered point.
73 top -= Math.ceil(@height / 2)
74
75 if textOffset?
76 # Move the marker just to the left of the text of its element.
77 left -= Math.max(0, @width - textOffset)
78 else
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.
84 #
85 # z-layer before after
86 # bottom +Link text +Link text
87 # middle DAG DAG
88 # top E E
89 left -= Math.max(0, @width - elementWidth)
90
91 # Make the position relative to the top frame.
92 left += offset.left
93 top += offset.top
94
95 @originalPosition = {left, top}
96 @moveTo(left + @dx, top + @dy)
97
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)
104
105 # Take the current zoom into account.
106 left = Math.round(left * @zoom)
107 top = Math.round(top * @zoom)
108
109 # The positioning is absolute.
110 @markerElement.style.left = "#{left}px"
111 @markerElement.style.top = "#{top}px"
112
113 # For quick access.
114 @position = {
115 left, right: left + @width,
116 top, bottom: top + @height,
117 }
118
119 updatePosition: (@dx, @dy) ->
120 @moveTo(@originalPosition.left + @dx, @originalPosition.top + @dy)
121
122 refreshPosition: ->
123 @setPosition(@viewport, @zoom)
124
125 setHint: (@hint) ->
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)
131
132 matchHint: (hint) ->
133 return @hint.startsWith(hint)
134
135 matchText: (strings) ->
136 return strings.every((string) => @text.includes(string))
137
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)
142 return
143
144 markMatched: (matched) ->
145 @markerElement.classList.toggle('marker--matched', matched)
146
147 markHighlighted: (highlighted) ->
148 @markerElement.classList.toggle('marker--highlighted', highlighted)
149
150 module.exports = Marker
Imprint / Impressum