Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1578)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/elements/StylesPopoverHelper.js

Issue 2157533003: DevTools: Source color picker experiment. Make color swatches clickable (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * @constructor 6 * @constructor
7 * @extends {WebInspector.Object}
8 */
9 WebInspector.StylesPopoverHelper = function()
lushnikov 2016/07/16 00:30:26 Let's extract the StylesPopoverHelper in a few ste
flandy 2016/07/19 17:52:19 Done.
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._repositionBound = this.reposition.bind(this);
19 this._boundFocusOut = this._onFocusOut.bind(this);
20 }
21
22 WebInspector.StylesPopoverHelper.prototype = {
23 /**
24 * @param {!Event} event
25 */
26 _onFocusOut: function(event)
27 {
28 if (!event.relatedTarget || event.relatedTarget.isSelfOrDescendant(this. _view.contentElement))
29 return;
30 this._hideProxy();
31 },
32
33 /**
34 * @return {boolean}
35 */
36 isShowing: function()
37 {
38 return this._popover.isShowing();
39 },
40
41 /**
42 * @param {!WebInspector.Widget} view
43 * @param {!Element} anchorElement
44 * @param {function(boolean)=} hiddenCallback
45 */
46 show: function(view, anchorElement, hiddenCallback)
47 {
48 if (this._popover.isShowing()) {
49 if (this._anchorElement === anchorElement)
50 return;
51
52 // Reopen the picker for another anchor element.
53 this.hide(true);
54 }
55
56 delete this._isHidden;
57 this._anchorElement = anchorElement;
58 this._view = view;
59 this._hiddenCallback = hiddenCallback;
60 this.reposition();
61
62 var document = this._popover.element.ownerDocument;
63 document.addEventListener("mousedown", this._hideProxy, false);
64 document.defaultView.addEventListener("resize", this._hideProxy, false);
65 this._view.contentElement.addEventListener("keydown", this._boundOnKeyDo wn, false);
66
67 this._scrollerElement = anchorElement.enclosingNodeOrSelfWithClass("styl e-panes-wrapper");
lushnikov 2016/07/16 00:30:26 I would try inlining scroll/repositioning function
flandy 2016/07/19 17:52:19 Done.
68 if (this._scrollerElement)
69 this._scrollerElement.addEventListener("scroll", this._repositionBou nd, false);
70 },
71
72 /**
73 * @param {!Event=} event
74 */
75 reposition: function(event)
76 {
77 if (!this._previousFocusElement)
78 this._previousFocusElement = WebInspector.currentFocusElement();
79 // Unbind "blur" listener to avoid reenterability: |popover.showView| wi ll hide the popover and trigger it synchronously.
80 this._view.contentElement.removeEventListener("focusout", this._boundFoc usOut, false);
81 this._popover.showView(this._view, this._anchorElement);
82 this._view.contentElement.addEventListener("focusout", this._boundFocusO ut, false);
83 WebInspector.setCurrentFocusElement(this._view.contentElement);
84 },
85
86 /**
87 * @param {boolean=} commitEdit
88 */
89 hide: function(commitEdit)
90 {
91 if (this._isHidden)
92 return;
93 var document = this._popover.element.ownerDocument;
94 this._isHidden = true;
95 this._popover.hide();
96
97 if (this._scrollerElement)
98 this._scrollerElement.removeEventListener("scroll", this._reposition Bound, false);
99
100 document.removeEventListener("mousedown", this._hideProxy, false);
101 document.defaultView.removeEventListener("resize", this._hideProxy, fals e);
102
103 if (this._hiddenCallback)
104 this._hiddenCallback.call(null, !!commitEdit);
105
106 WebInspector.setCurrentFocusElement(this._previousFocusElement);
107 delete this._previousFocusElement;
108 delete this._anchorElement;
109 if (this._view) {
110 this._view.detach();
111 this._view.contentElement.removeEventListener("keydown", this._bound OnKeyDown, false);
112 this._view.contentElement.removeEventListener("focusout", this._boun dFocusOut, false);
113 delete this._view;
114 }
115 },
116
117 /**
118 * @param {!Event} event
119 */
120 _onKeyDown: function(event)
121 {
122 if (event.key === "Enter") {
123 this.hide(true);
124 event.consume(true);
125 return;
126 }
127 if (event.key === "Escape") {
128 this.hide(false);
129 event.consume(true);
130 }
131 },
132
133 __proto__: WebInspector.Object.prototype
134 }
135
136 /**
137 * @constructor
138 * @param {!WebInspector.StylePropertyTreeElement} treeElement 7 * @param {!WebInspector.StylePropertyTreeElement} treeElement
139 * @param {!WebInspector.StylesPopoverHelper} stylesPopoverHelper 8 * @param {!WebInspector.IconPopoverHelper} iconPopoverHelper
140 * @param {string} text 9 * @param {string} text
141 */ 10 */
142 WebInspector.BezierPopoverIcon = function(treeElement, stylesPopoverHelper, text ) 11 WebInspector.BezierPopoverIcon = function(treeElement, iconPopoverHelper, text)
143 { 12 {
144 this._treeElement = treeElement; 13 this._treeElement = treeElement;
145 this._stylesPopoverHelper = stylesPopoverHelper; 14 this._iconPopoverHelper = iconPopoverHelper;
146 this._createDOM(text); 15 this._createDOM(text);
147 16
148 this._boundBezierChanged = this._bezierChanged.bind(this); 17 this._boundBezierChanged = this._bezierChanged.bind(this);
149 } 18 }
150 19
151 WebInspector.BezierPopoverIcon.prototype = { 20 WebInspector.BezierPopoverIcon.prototype = {
152 /** 21 /**
153 * @return {!Element} 22 * @return {!Element}
154 */ 23 */
155 element: function() 24 element: function()
(...skipping 21 matching lines...) Expand all
177 this._bezierValueElement = this._element.createChild("span"); 46 this._bezierValueElement = this._element.createChild("span");
178 this._bezierValueElement.textContent = text; 47 this._bezierValueElement.textContent = text;
179 }, 48 },
180 49
181 /** 50 /**
182 * @param {!Event} event 51 * @param {!Event} event
183 */ 52 */
184 _iconClick: function(event) 53 _iconClick: function(event)
185 { 54 {
186 event.consume(true); 55 event.consume(true);
187 if (this._stylesPopoverHelper.isShowing()) { 56 if (this._iconPopoverHelper.isShowing()) {
188 this._stylesPopoverHelper.hide(true); 57 this._iconPopoverHelper.hide(true);
189 return; 58 return;
190 } 59 }
191 60
192 this._bezierEditor = new WebInspector.BezierEditor(); 61 this._bezierEditor = new WebInspector.BezierEditor();
193 var geometry = WebInspector.Geometry.CubicBezier.parse(this._bezierValue Element.textContent); 62 var geometry = WebInspector.Geometry.CubicBezier.parse(this._bezierValue Element.textContent);
194 this._bezierEditor.setBezier(geometry); 63 this._bezierEditor.setBezier(geometry);
195 this._bezierEditor.addEventListener(WebInspector.BezierEditor.Events.Bez ierChanged, this._boundBezierChanged); 64 this._bezierEditor.addEventListener(WebInspector.BezierEditor.Events.Bez ierChanged, this._boundBezierChanged);
196 this._stylesPopoverHelper.show(this._bezierEditor, this._iconElement, th is._onPopoverHidden.bind(this)); 65 var scrollerElement = this._iconElement.enclosingNodeOrSelfWithClass("st yle-panes-wrapper");
66 this._iconPopoverHelper.show(this._bezierEditor, this._iconElement, scro llerElement, this._onPopoverHidden.bind(this));
197 67
198 this._originalPropertyText = this._treeElement.property.propertyText; 68 this._originalPropertyText = this._treeElement.property.propertyText;
199 this._treeElement.parentPane().setEditingStyle(true); 69 this._treeElement.parentPane().setEditingStyle(true);
200 var uiLocation = WebInspector.cssWorkspaceBinding.propertyUILocation(thi s._treeElement.property, false /* forName */); 70 var uiLocation = WebInspector.cssWorkspaceBinding.propertyUILocation(thi s._treeElement.property, false /* forName */);
201 if (uiLocation) 71 if (uiLocation)
202 WebInspector.Revealer.reveal(uiLocation, true /* omitFocus */); 72 WebInspector.Revealer.reveal(uiLocation, true /* omitFocus */);
203 }, 73 },
204 74
205 /** 75 /**
206 * @param {!WebInspector.Event} event 76 * @param {!WebInspector.Event} event
(...skipping 15 matching lines...) Expand all
222 var propertyText = commitEdit ? this._treeElement.renderedPropertyText() : this._originalPropertyText; 92 var propertyText = commitEdit ? this._treeElement.renderedPropertyText() : this._originalPropertyText;
223 this._treeElement.applyStyleText(propertyText, true); 93 this._treeElement.applyStyleText(propertyText, true);
224 this._treeElement.parentPane().setEditingStyle(false); 94 this._treeElement.parentPane().setEditingStyle(false);
225 delete this._originalPropertyText; 95 delete this._originalPropertyText;
226 } 96 }
227 } 97 }
228 98
229 /** 99 /**
230 * @constructor 100 * @constructor
231 * @param {!WebInspector.StylePropertyTreeElement} treeElement 101 * @param {!WebInspector.StylePropertyTreeElement} treeElement
232 * @param {!WebInspector.StylesPopoverHelper} stylesPopoverHelper 102 * @param {!WebInspector.IconPopoverHelper} iconPopoverHelper
233 * @param {string} colorText 103 * @param {string} colorText
234 */ 104 */
235 WebInspector.ColorSwatchPopoverIcon = function(treeElement, stylesPopoverHelper, colorText) 105 WebInspector.ColorSwatchPopoverIcon = function(treeElement, iconPopoverHelper, c olorText)
236 { 106 {
237 this._treeElement = treeElement; 107 this._treeElement = treeElement;
238 this._treeElement[WebInspector.ColorSwatchPopoverIcon._treeElementSymbol] = this; 108 this._treeElement[WebInspector.ColorSwatchPopoverIcon._treeElementSymbol] = this;
239 this._stylesPopoverHelper = stylesPopoverHelper; 109 this._iconPopoverHelper = iconPopoverHelper;
240 110
241 this._swatch = WebInspector.ColorSwatch.create(); 111 this._swatch = WebInspector.ColorSwatch.create();
242 this._swatch.setColorText(colorText); 112 this._swatch.setColorText(colorText);
243 this._swatch.setFormat(WebInspector.Color.detectColorFormat(this._swatch.col or())); 113 this._swatch.setFormat(WebInspector.Color.detectColorFormat(this._swatch.col or()));
244 var shiftClickMessage = WebInspector.UIString("Shift + Click to change color format."); 114 var shiftClickMessage = WebInspector.UIString("Shift + Click to change color format.");
245 this._swatch.iconElement().title = WebInspector.UIString("Open color picker. %s", shiftClickMessage); 115 this._swatch.iconElement().title = WebInspector.UIString("Open color picker. %s", shiftClickMessage);
246 this._swatch.iconElement().addEventListener("click", this._iconClick.bind(th is)); 116 this._swatch.iconElement().addEventListener("click", this._iconClick.bind(th is));
247 this._contrastColor = null; 117 this._contrastColor = null;
248 118
249 this._boundSpectrumChanged = this._spectrumChanged.bind(this); 119 this._boundSpectrumChanged = this._spectrumChanged.bind(this);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 * @param {!Event} event 153 * @param {!Event} event
284 */ 154 */
285 _iconClick: function(event) 155 _iconClick: function(event)
286 { 156 {
287 event.consume(true); 157 event.consume(true);
288 this.showPopover(); 158 this.showPopover();
289 }, 159 },
290 160
291 showPopover: function() 161 showPopover: function()
292 { 162 {
293 if (this._stylesPopoverHelper.isShowing()) { 163 if (this._iconPopoverHelper.isShowing()) {
294 this._stylesPopoverHelper.hide(true); 164 this._iconPopoverHelper.hide(true);
295 return; 165 return;
296 } 166 }
297 167
298 var color = this._swatch.color(); 168 var color = this._swatch.color();
299 var format = this._swatch.format(); 169 var format = this._swatch.format();
300 if (format === WebInspector.Color.Format.Original) 170 if (format === WebInspector.Color.Format.Original)
301 format = color.format(); 171 format = color.format();
302 this._spectrum = new WebInspector.Spectrum(); 172 this._spectrum = new WebInspector.Spectrum();
303 this._spectrum.setColor(color, format); 173 this._spectrum.setColor(color, format);
304 if (this._contrastColor) 174 if (this._contrastColor)
305 this._spectrum.setContrastColor(this._contrastColor); 175 this._spectrum.setContrastColor(this._contrastColor);
306 176
307 this._spectrum.addEventListener(WebInspector.Spectrum.Events.SizeChanged , this._spectrumResized, this); 177 this._spectrum.addEventListener(WebInspector.Spectrum.Events.SizeChanged , this._spectrumResized, this);
308 this._spectrum.addEventListener(WebInspector.Spectrum.Events.ColorChange d, this._boundSpectrumChanged); 178 this._spectrum.addEventListener(WebInspector.Spectrum.Events.ColorChange d, this._boundSpectrumChanged);
309 this._stylesPopoverHelper.show(this._spectrum, this._swatch.iconElement( ), this._onPopoverHidden.bind(this)); 179
180 var anchorElement = this._swatch.iconElement();
181 var scrollerElement = anchorElement.enclosingNodeOrSelfWithClass("style- panes-wrapper");
182 this._iconPopoverHelper.show(this._spectrum, anchorElement, scrollerElem ent, this._onPopoverHidden.bind(this));
310 183
311 this._originalPropertyText = this._treeElement.property.propertyText; 184 this._originalPropertyText = this._treeElement.property.propertyText;
312 this._treeElement.parentPane().setEditingStyle(true); 185 this._treeElement.parentPane().setEditingStyle(true);
313 var uiLocation = WebInspector.cssWorkspaceBinding.propertyUILocation(thi s._treeElement.property, false /* forName */); 186 var uiLocation = WebInspector.cssWorkspaceBinding.propertyUILocation(thi s._treeElement.property, false /* forName */);
314 if (uiLocation) 187 if (uiLocation)
315 WebInspector.Revealer.reveal(uiLocation, true /* omitFocus */); 188 WebInspector.Revealer.reveal(uiLocation, true /* omitFocus */);
316 }, 189 },
317 190
318 /** 191 /**
319 * @param {!WebInspector.Event} event 192 * @param {!WebInspector.Event} event
320 */ 193 */
321 _spectrumResized: function(event) 194 _spectrumResized: function(event)
322 { 195 {
323 this._stylesPopoverHelper.reposition(); 196 this._iconPopoverHelper.reposition();
324 }, 197 },
325 198
326 /** 199 /**
327 * @param {!WebInspector.Event} event 200 * @param {!WebInspector.Event} event
328 */ 201 */
329 _spectrumChanged: function(event) 202 _spectrumChanged: function(event)
330 { 203 {
331 var colorString = /** @type {string} */ (event.data); 204 var colorString = /** @type {string} */ (event.data);
332 this._swatch.setColorText(colorString); 205 this._swatch.setColorText(colorString);
333 this._treeElement.applyStyleText(this._treeElement.renderedPropertyText( ), false); 206 this._treeElement.applyStyleText(this._treeElement.renderedPropertyText( ), false);
334 }, 207 },
335 208
336 /** 209 /**
337 * @param {boolean} commitEdit 210 * @param {boolean} commitEdit
338 */ 211 */
339 _onPopoverHidden: function(commitEdit) 212 _onPopoverHidden: function(commitEdit)
340 { 213 {
341 this._spectrum.removeEventListener(WebInspector.Spectrum.Events.ColorCha nged, this._boundSpectrumChanged); 214 this._spectrum.removeEventListener(WebInspector.Spectrum.Events.ColorCha nged, this._boundSpectrumChanged);
342 delete this._spectrum; 215 delete this._spectrum;
343 216
344 var propertyText = commitEdit ? this._treeElement.renderedPropertyText() : this._originalPropertyText; 217 var propertyText = commitEdit ? this._treeElement.renderedPropertyText() : this._originalPropertyText;
345 this._treeElement.applyStyleText(propertyText, true); 218 this._treeElement.applyStyleText(propertyText, true);
346 this._treeElement.parentPane().setEditingStyle(false); 219 this._treeElement.parentPane().setEditingStyle(false);
347 delete this._originalPropertyText; 220 delete this._originalPropertyText;
348 } 221 }
349 } 222 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698