OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /** | |
6 * @constructor | |
7 * @extends {WebInspector.Object} | |
8 */ | |
9 WebInspector.SwatchPopoverHelper = function() | |
10 { | |
11 this._popover = new WebInspector.Popover(); | |
12 this._popover.setCanShrink(false); | |
13 this._popover.setNoMargins(true); | |
14 this._popover.element.addEventListener("mousedown", consumeEvent, false); | |
15 | |
16 this._hideProxy = this.hide.bind(this, true); | |
17 this._boundOnKeyDown = this._onKeyDown.bind(this); | |
18 this._boundFocusOut = this._onFocusOut.bind(this); | |
19 } | |
20 | |
21 WebInspector.SwatchPopoverHelper.prototype = { | |
22 /** | |
23 * @param {!Event} event | |
24 */ | |
25 _onFocusOut: function(event) | |
26 { | |
27 if (!event.relatedTarget || event.relatedTarget.isSelfOrDescendant(this.
_view.contentElement)) | |
28 return; | |
29 this._hideProxy(); | |
30 }, | |
31 | |
32 /** | |
33 * @return {boolean} | |
34 */ | |
35 isShowing: function() | |
36 { | |
37 return this._popover.isShowing(); | |
38 }, | |
39 | |
40 /** | |
41 * @param {!WebInspector.Widget} view | |
42 * @param {!Element} anchorElement | |
43 * @param {function(boolean)=} hiddenCallback | |
44 */ | |
45 show: function(view, anchorElement, hiddenCallback) | |
46 { | |
47 if (this._popover.isShowing()) { | |
48 if (this._anchorElement === anchorElement) | |
49 return; | |
50 | |
51 // Reopen the picker for another anchor element. | |
52 this.hide(true); | |
53 } | |
54 | |
55 delete this._isHidden; | |
56 this._anchorElement = anchorElement; | |
57 this._view = view; | |
58 this._hiddenCallback = hiddenCallback; | |
59 this.reposition(); | |
60 | |
61 var document = this._popover.element.ownerDocument; | |
62 document.addEventListener("mousedown", this._hideProxy, false); | |
63 document.defaultView.addEventListener("resize", this._hideProxy, false); | |
64 this._view.contentElement.addEventListener("keydown", this._boundOnKeyDo
wn, false); | |
65 }, | |
66 | |
67 reposition: function() | |
68 { | |
69 if (!this._previousFocusElement) | |
70 this._previousFocusElement = WebInspector.currentFocusElement(); | |
71 // Unbind "blur" listener to avoid reenterability: |popover.showView| wi
ll hide the popover and trigger it synchronously. | |
72 this._view.contentElement.removeEventListener("focusout", this._boundFoc
usOut, false); | |
73 this._popover.showView(this._view, this._anchorElement); | |
74 this._view.contentElement.addEventListener("focusout", this._boundFocusO
ut, false); | |
75 WebInspector.setCurrentFocusElement(this._view.contentElement); | |
76 }, | |
77 | |
78 /** | |
79 * @param {boolean=} commitEdit | |
80 */ | |
81 hide: function(commitEdit) | |
82 { | |
83 if (this._isHidden) | |
84 return; | |
85 var document = this._popover.element.ownerDocument; | |
86 this._isHidden = true; | |
87 this._popover.hide(); | |
88 | |
89 document.removeEventListener("mousedown", this._hideProxy, false); | |
90 document.defaultView.removeEventListener("resize", this._hideProxy, fals
e); | |
91 | |
92 if (this._hiddenCallback) | |
93 this._hiddenCallback.call(null, !!commitEdit); | |
94 | |
95 WebInspector.setCurrentFocusElement(this._previousFocusElement); | |
96 delete this._previousFocusElement; | |
97 delete this._anchorElement; | |
98 if (this._view) { | |
99 this._view.detach(); | |
100 this._view.contentElement.removeEventListener("keydown", this._bound
OnKeyDown, false); | |
101 this._view.contentElement.removeEventListener("focusout", this._boun
dFocusOut, false); | |
102 delete this._view; | |
103 } | |
104 }, | |
105 | |
106 /** | |
107 * @param {!Event} event | |
108 */ | |
109 _onKeyDown: function(event) | |
110 { | |
111 if (event.key === "Enter") { | |
112 this.hide(true); | |
113 event.consume(true); | |
114 return; | |
115 } | |
116 if (event.key === "Escape") { | |
117 this.hide(false); | |
118 event.consume(true); | |
119 } | |
120 }, | |
121 | |
122 __proto__: WebInspector.Object.prototype | |
123 } | |
124 | |
125 /** | |
126 * @constructor | |
127 * @param {!WebInspector.StylePropertyTreeElement} treeElement | |
128 * @param {!WebInspector.SwatchPopoverHelper} swatchPopoverHelper | |
129 * @param {string} text | |
130 */ | |
131 WebInspector.BezierPopoverIcon = function(treeElement, swatchPopoverHelper, text
) | |
132 { | |
133 this._treeElement = treeElement; | |
134 this._swatchPopoverHelper = swatchPopoverHelper; | |
135 this._createDOM(text); | |
136 | |
137 this._boundBezierChanged = this._bezierChanged.bind(this); | |
138 this._boundOnScroll = this._onScroll.bind(this); | |
139 } | |
140 | |
141 WebInspector.BezierPopoverIcon.prototype = { | |
142 /** | |
143 * @return {!Element} | |
144 */ | |
145 element: function() | |
146 { | |
147 return this._element; | |
148 }, | |
149 | |
150 /** | |
151 * @param {string} text | |
152 */ | |
153 _createDOM: function(text) | |
154 { | |
155 this._element = createElement("nobr"); | |
156 this._element.title = WebInspector.UIString("Open cubic bezier editor"); | |
157 | |
158 this._iconElement = this._element.createChild("div", "popover-icon bezie
r-icon"); | |
159 var svg = this._iconElement.createSVGChild("svg"); | |
160 svg.setAttribute("height", 10); | |
161 svg.setAttribute("width", 10); | |
162 this._iconElement.addEventListener("click", this._iconClick.bind(this),
false); | |
163 var g = svg.createSVGChild("g"); | |
164 var path = g.createSVGChild("path"); | |
165 path.setAttribute("d", "M2,8 C2,3 8,7 8,2"); | |
166 | |
167 this._bezierValueElement = this._element.createChild("span"); | |
168 this._bezierValueElement.textContent = text; | |
169 }, | |
170 | |
171 /** | |
172 * @param {!Event} event | |
173 */ | |
174 _iconClick: function(event) | |
175 { | |
176 event.consume(true); | |
177 if (this._swatchPopoverHelper.isShowing()) { | |
178 this._swatchPopoverHelper.hide(true); | |
179 return; | |
180 } | |
181 | |
182 this._bezierEditor = new WebInspector.BezierEditor(); | |
183 var geometry = WebInspector.Geometry.CubicBezier.parse(this._bezierValue
Element.textContent); | |
184 this._bezierEditor.setBezier(geometry); | |
185 this._bezierEditor.addEventListener(WebInspector.BezierEditor.Events.Bez
ierChanged, this._boundBezierChanged); | |
186 this._swatchPopoverHelper.show(this._bezierEditor, this._iconElement, th
is._onPopoverHidden.bind(this)); | |
187 this._scrollerElement = this._iconElement.enclosingNodeOrSelfWithClass("
style-panes-wrapper"); | |
188 if (this._scrollerElement) | |
189 this._scrollerElement.addEventListener("scroll", this._boundOnScroll
, false); | |
190 | |
191 this._originalPropertyText = this._treeElement.property.propertyText; | |
192 this._treeElement.parentPane().setEditingStyle(true); | |
193 var uiLocation = WebInspector.cssWorkspaceBinding.propertyUILocation(thi
s._treeElement.property, false /* forName */); | |
194 if (uiLocation) | |
195 WebInspector.Revealer.reveal(uiLocation, true /* omitFocus */); | |
196 }, | |
197 | |
198 /** | |
199 * @param {!WebInspector.Event} event | |
200 */ | |
201 _bezierChanged: function(event) | |
202 { | |
203 this._bezierValueElement.textContent = /** @type {string} */ (event.data
); | |
204 this._treeElement.applyStyleText(this._treeElement.renderedPropertyText(
), false); | |
205 }, | |
206 | |
207 /** | |
208 * @param {!Event} event | |
209 */ | |
210 _onScroll: function(event) | |
211 { | |
212 this._swatchPopoverHelper.reposition(); | |
213 }, | |
214 | |
215 /** | |
216 * @param {boolean} commitEdit | |
217 */ | |
218 _onPopoverHidden: function(commitEdit) | |
219 { | |
220 if (this._scrollerElement) | |
221 this._scrollerElement.removeEventListener("scroll", this._boundOnScr
oll, false); | |
222 | |
223 this._bezierEditor.removeEventListener(WebInspector.BezierEditor.Events.
BezierChanged, this._boundBezierChanged); | |
224 delete this._bezierEditor; | |
225 | |
226 var propertyText = commitEdit ? this._treeElement.renderedPropertyText()
: this._originalPropertyText; | |
227 this._treeElement.applyStyleText(propertyText, true); | |
228 this._treeElement.parentPane().setEditingStyle(false); | |
229 delete this._originalPropertyText; | |
230 } | |
231 } | |
232 | |
233 /** | |
234 * @constructor | |
235 * @param {!WebInspector.StylePropertyTreeElement} treeElement | |
236 * @param {!WebInspector.SwatchPopoverHelper} swatchPopoverHelper | |
237 * @param {string} colorText | |
238 */ | |
239 WebInspector.ColorSwatchPopoverIcon = function(treeElement, swatchPopoverHelper,
colorText) | |
240 { | |
241 this._treeElement = treeElement; | |
242 this._treeElement[WebInspector.ColorSwatchPopoverIcon._treeElementSymbol] =
this; | |
243 this._swatchPopoverHelper = swatchPopoverHelper; | |
244 | |
245 this._swatch = WebInspector.ColorSwatch.create(); | |
246 this._swatch.setColorText(colorText); | |
247 this._swatch.setFormat(WebInspector.Color.detectColorFormat(this._swatch.col
or())); | |
248 var shiftClickMessage = WebInspector.UIString("Shift + Click to change color
format."); | |
249 this._swatch.iconElement().title = WebInspector.UIString("Open color picker.
%s", shiftClickMessage); | |
250 this._swatch.iconElement().addEventListener("click", this._iconClick.bind(th
is)); | |
251 this._contrastColor = null; | |
252 | |
253 this._boundSpectrumChanged = this._spectrumChanged.bind(this); | |
254 this._boundOnScroll = this._onScroll.bind(this); | |
255 } | |
256 | |
257 WebInspector.ColorSwatchPopoverIcon._treeElementSymbol = Symbol("WebInspector.Co
lorSwatchPopoverIcon._treeElementSymbol"); | |
258 | |
259 /** | |
260 * @param {!WebInspector.StylePropertyTreeElement} treeElement | |
261 * @return {?WebInspector.ColorSwatchPopoverIcon} | |
262 */ | |
263 WebInspector.ColorSwatchPopoverIcon.forTreeElement = function(treeElement) | |
264 { | |
265 return treeElement[WebInspector.ColorSwatchPopoverIcon._treeElementSymbol] |
| null; | |
266 } | |
267 | |
268 WebInspector.ColorSwatchPopoverIcon.prototype = { | |
269 /** | |
270 * @return {!Element} | |
271 */ | |
272 element: function() | |
273 { | |
274 return this._swatch; | |
275 }, | |
276 | |
277 /** | |
278 * @param {!WebInspector.Color} color | |
279 */ | |
280 setContrastColor: function(color) | |
281 { | |
282 this._contrastColor = color; | |
283 if (this._spectrum) | |
284 this._spectrum.setContrastColor(this._contrastColor); | |
285 }, | |
286 | |
287 /** | |
288 * @param {!Event} event | |
289 */ | |
290 _iconClick: function(event) | |
291 { | |
292 event.consume(true); | |
293 this.showPopover(); | |
294 }, | |
295 | |
296 showPopover: function() | |
297 { | |
298 if (this._swatchPopoverHelper.isShowing()) { | |
299 this._swatchPopoverHelper.hide(true); | |
300 return; | |
301 } | |
302 | |
303 var color = this._swatch.color(); | |
304 var format = this._swatch.format(); | |
305 if (format === WebInspector.Color.Format.Original) | |
306 format = color.format(); | |
307 this._spectrum = new WebInspector.Spectrum(); | |
308 this._spectrum.setColor(color, format); | |
309 if (this._contrastColor) | |
310 this._spectrum.setContrastColor(this._contrastColor); | |
311 | |
312 this._spectrum.addEventListener(WebInspector.Spectrum.Events.SizeChanged
, this._spectrumResized, this); | |
313 this._spectrum.addEventListener(WebInspector.Spectrum.Events.ColorChange
d, this._boundSpectrumChanged); | |
314 this._swatchPopoverHelper.show(this._spectrum, this._swatch.iconElement(
), this._onPopoverHidden.bind(this)); | |
315 this._scrollerElement = this._swatch.enclosingNodeOrSelfWithClass("style
-panes-wrapper"); | |
316 if (this._scrollerElement) | |
317 this._scrollerElement.addEventListener("scroll", this._boundOnScroll
, false); | |
318 | |
319 this._originalPropertyText = this._treeElement.property.propertyText; | |
320 this._treeElement.parentPane().setEditingStyle(true); | |
321 var uiLocation = WebInspector.cssWorkspaceBinding.propertyUILocation(thi
s._treeElement.property, false /* forName */); | |
322 if (uiLocation) | |
323 WebInspector.Revealer.reveal(uiLocation, true /* omitFocus */); | |
324 }, | |
325 | |
326 /** | |
327 * @param {!WebInspector.Event} event | |
328 */ | |
329 _spectrumResized: function(event) | |
330 { | |
331 this._swatchPopoverHelper.reposition(); | |
332 }, | |
333 | |
334 /** | |
335 * @param {!WebInspector.Event} event | |
336 */ | |
337 _spectrumChanged: function(event) | |
338 { | |
339 var colorString = /** @type {string} */ (event.data); | |
340 this._swatch.setColorText(colorString); | |
341 this._treeElement.applyStyleText(this._treeElement.renderedPropertyText(
), false); | |
342 }, | |
343 | |
344 /** | |
345 * @param {!Event} event | |
346 */ | |
347 _onScroll: function(event) | |
348 { | |
349 this._swatchPopoverHelper.reposition(); | |
350 }, | |
351 | |
352 /** | |
353 * @param {boolean} commitEdit | |
354 */ | |
355 _onPopoverHidden: function(commitEdit) | |
356 { | |
357 if (this._scrollerElement) | |
358 this._scrollerElement.removeEventListener("scroll", this._boundOnScr
oll, false); | |
359 | |
360 this._spectrum.removeEventListener(WebInspector.Spectrum.Events.ColorCha
nged, this._boundSpectrumChanged); | |
361 delete this._spectrum; | |
362 | |
363 var propertyText = commitEdit ? this._treeElement.renderedPropertyText()
: this._originalPropertyText; | |
364 this._treeElement.applyStyleText(propertyText, true); | |
365 this._treeElement.parentPane().setEditingStyle(false); | |
366 delete this._originalPropertyText; | |
367 } | |
368 } | |
OLD | NEW |