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

Side by Side Diff: Source/devtools/front_end/elements/Spectrum.js

Issue 1116933007: Devtools: Refactor color picker to use WI.installDragHandle() (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Brian Grinstead All rights reserved. 2 * Copyright (C) 2011 Brian Grinstead All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 7 *
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 icon.setAttribute("width", 16); 42 icon.setAttribute("width", 16);
43 var path = icon.createSVGChild("path"); 43 var path = icon.createSVGChild("path");
44 path.setAttribute("d", "M5,6 L11,6 L8,2 Z M5,10 L11,10 L8,14 Z"); 44 path.setAttribute("d", "M5,6 L11,6 L8,2 Z M5,10 L11,10 L8,14 Z");
45 return icon; 45 return icon;
46 } 46 }
47 47
48 WebInspector.VBox.call(this, true); 48 WebInspector.VBox.call(this, true);
49 this.registerRequiredCSS("elements/spectrum.css"); 49 this.registerRequiredCSS("elements/spectrum.css");
50 this.contentElement.tabIndex = 0; 50 this.contentElement.tabIndex = 0;
51 51
52 this._draggerElement = this.contentElement.createChild("div", "spectrum-colo r"); 52 this._colorElement = this.contentElement.createChild("div", "spectrum-color" );
53 this._dragHelperElement = this._draggerElement.createChild("div", "spectrum- sat fill").createChild("div", "spectrum-val fill").createChild("div", "spectrum- dragger"); 53 this._colorDragElement = this._colorElement.createChild("div", "spectrum-sat fill").createChild("div", "spectrum-val fill").createChild("div", "spectrum-dra gger");
54 54
55 var swatchElement = this.contentElement.createChild("span", "swatch"); 55 var swatchElement = this.contentElement.createChild("span", "swatch");
56 this._swatchInnerElement = swatchElement.createChild("span", "swatch-inner") ; 56 this._swatchInnerElement = swatchElement.createChild("span", "swatch-inner") ;
57 57
58 this._hueElement = this.contentElement.createChild("div", "spectrum-hue"); 58 this._hueElement = this.contentElement.createChild("div", "spectrum-hue");
59 this._hueSlider = this._hueElement.createChild("div", "spectrum-slider"); 59 this._hueSlider = this._hueElement.createChild("div", "spectrum-slider");
60 this._alphaElement = this.contentElement.createChild("div", "spectrum-alpha" ); 60 this._alphaElement = this.contentElement.createChild("div", "spectrum-alpha" );
61 this._alphaElementBackground = this._alphaElement.createChild("div", "spectr um-alpha-background"); 61 this._alphaElementBackground = this._alphaElement.createChild("div", "spectr um-alpha-background");
62 this._alphaSlider = this._alphaElement.createChild("div", "spectrum-slider") ; 62 this._alphaSlider = this._alphaElement.createChild("div", "spectrum-slider") ;
63 63
(...skipping 16 matching lines...) Expand all
80 80
81 // HEX display. 81 // HEX display.
82 this._hexContainer = this.contentElement.createChild("div", "spectrum-text s pectrum-text-hex source-code"); 82 this._hexContainer = this.contentElement.createChild("div", "spectrum-text s pectrum-text-hex source-code");
83 this._hexValue = this._hexContainer.createChild("input", "spectrum-text-valu e"); 83 this._hexValue = this._hexContainer.createChild("input", "spectrum-text-valu e");
84 this._hexValue.maxLength = 7; 84 this._hexValue.maxLength = 7;
85 this._hexValue.addEventListener("input", this._inputChanged.bind(this)); 85 this._hexValue.addEventListener("input", this._inputChanged.bind(this));
86 86
87 var label = this._hexContainer.createChild("div", "spectrum-text-label"); 87 var label = this._hexContainer.createChild("div", "spectrum-text-label");
88 label.textContent = "HEX"; 88 label.textContent = "HEX";
89 89
90 WebInspector.Spectrum.draggable(this._hueElement, hueDrag.bind(this)); 90 WebInspector.installDragHandle(this._hueElement, dragStart.bind(this), hueDr ag.bind(this), null, "default");
91 WebInspector.Spectrum.draggable(this._alphaElement, alphaDrag.bind(this)); 91 WebInspector.installDragHandle(this._alphaElement, dragStart.bind(this), alp haDrag.bind(this), null, "default");
92 WebInspector.Spectrum.draggable(this._draggerElement, colorDrag.bind(this), colorDragStart.bind(this)); 92 WebInspector.installDragHandle(this._colorElement, colorDragStart.bind(this) , colorDrag.bind(this), null, "default");
93 93
94 /** 94 /**
95 * @param {!Element} element 95 * @param {!Event} event
96 * @param {number} dragX 96 * @return {boolean}
97 * @param {number} dragY
98 * @this {WebInspector.Spectrum} 97 * @this {WebInspector.Spectrum}
99 */ 98 */
100 function hueDrag(element, dragX, dragY) 99 function dragStart(event)
101 { 100 {
102 this._hsv[0] = (this._hueAlphaWidth - dragX) / this._hueAlphaWidth; 101 this._mouseDownPosition = new WebInspector.Geometry.Point(event.x, event .y);
102 this._originalColor = this._hsv.slice();
103 return true;
104 }
105
106 /**
107 * @param {!Event} event
108 * @return {boolean}
109 * @this {WebInspector.Spectrum}
110 */
111 function colorDragStart(event)
112 {
113 this._hsv[1] = event.offsetX / this.dragWidth;
114 this._hsv[2] = (this.dragHeight - event.offsetY) / this.dragHeight;
115 this._onchange();
116 return dragStart.call(this, event);
117 }
118
119 /**
120 * @param {!Event} event
121 * @this {WebInspector.Spectrum}
122 */
123 function hueDrag(event)
124 {
125 var deltaX = event.x - this._mouseDownPosition.x;
126 this._hsv[0] = Number.constrain(this._originalColor[0] - deltaX / this._ hueAlphaWidth, 0, 1);
103 this._onchange(); 127 this._onchange();
104 } 128 }
105 129
106 /** 130 /**
107 * @param {!Element} element 131 * @param {!Event} event
108 * @param {number} dragX
109 * @param {number} dragY
110 * @this {WebInspector.Spectrum} 132 * @this {WebInspector.Spectrum}
111 */ 133 */
112 function alphaDrag(element, dragX, dragY) 134 function alphaDrag(event)
113 { 135 {
114 this._hsv[3] = Math.round((dragX / this._hueAlphaWidth) * 100) / 100; 136 var deltaX = event.x - this._mouseDownPosition.x;
137 var newAlpha = this._originalColor[3] + Math.round((deltaX / this._hueAl phaWidth) * 100) / 100;
138 this._hsv[3] = Number.constrain(newAlpha, 0, 1);
115 if (this._color().hasAlpha() && (this._currentFormat === WebInspector.Co lor.Format.ShortHEX || this._currentFormat === WebInspector.Color.Format.HEX || this._currentFormat === WebInspector.Color.Format.Nickname)) 139 if (this._color().hasAlpha() && (this._currentFormat === WebInspector.Co lor.Format.ShortHEX || this._currentFormat === WebInspector.Color.Format.HEX || this._currentFormat === WebInspector.Color.Format.Nickname))
116 this.setColorFormat(WebInspector.Color.Format.RGB); 140 this.setColorFormat(WebInspector.Color.Format.RGB);
117 this._onchange(); 141 this._onchange();
118 } 142 }
119 143
120 var initialHelperOffset;
121
122 /** 144 /**
145 * @param {!Event} event
123 * @this {WebInspector.Spectrum} 146 * @this {WebInspector.Spectrum}
124 */ 147 */
125 function colorDragStart() 148 function colorDrag(event)
126 { 149 {
127 initialHelperOffset = { x: this._dragHelperElement.offsetLeft, y: this._ dragHelperElement.offsetTop }; 150 var deltaX = event.x - this._mouseDownPosition.x;
128 } 151 var deltaY = event.y - this._mouseDownPosition.y;
129 152 this._hsv[1] = Number.constrain(this._originalColor[1] + deltaX / this.d ragWidth, 0, 1);
130 /** 153 this._hsv[2] = Number.constrain(this._originalColor[2] - deltaY / this.d ragHeight, 0, 1);
131 * @param {!Element} element
132 * @param {number} dragX
133 * @param {number} dragY
134 * @param {!MouseEvent} event
135 * @this {WebInspector.Spectrum}
136 */
137 function colorDrag(element, dragX, dragY, event)
138 {
139 if (event.shiftKey) {
140 if (Math.abs(dragX - initialHelperOffset.x) >= Math.abs(dragY - init ialHelperOffset.y))
141 dragY = initialHelperOffset.y;
142 else
143 dragX = initialHelperOffset.x;
144 }
145
146 this._hsv[1] = dragX / this.dragWidth;
147 this._hsv[2] = (this.dragHeight - dragY) / this.dragHeight;
148
149 this._onchange(); 154 this._onchange();
150 } 155 }
151 }; 156 };
152 157
153 WebInspector.Spectrum.Events = { 158 WebInspector.Spectrum.Events = {
154 ColorChanged: "ColorChanged" 159 ColorChanged: "ColorChanged"
155 }; 160 };
156 161
157 /**
158 * @param {!Element} element
159 * @param {function(!Element, number, number, !MouseEvent)=} onmove
160 * @param {function(!Element, !MouseEvent)=} onstart
161 * @param {function(!Element, !MouseEvent)=} onstop
162 */
163 WebInspector.Spectrum.draggable = function(element, onmove, onstart, onstop) {
164
165 var dragging;
166 var offset;
167 var scrollOffset;
168 var maxHeight;
169 var maxWidth;
170
171 /**
172 * @param {!Event} e
173 */
174 function consume(e)
175 {
176 e.consume(true);
177 }
178
179 /**
180 * @param {!Event} e
181 */
182 function move(e)
183 {
184 if (dragging) {
185 var dragX = Math.max(0, Math.min(e.pageX - offset.left + scrollOffse t.left, maxWidth));
186 var dragY = Math.max(0, Math.min(e.pageY - offset.top + scrollOffset .top, maxHeight));
187
188 if (onmove)
189 onmove(element, dragX, dragY, /** @type {!MouseEvent} */ (e));
190 }
191 }
192
193 /**
194 * @param {!Event} e
195 */
196 function start(e)
197 {
198 var mouseEvent = /** @type {!MouseEvent} */ (e);
199 var rightClick = mouseEvent.which ? (mouseEvent.which === 3) : (mouseEve nt.button === 2);
200
201 if (!rightClick && !dragging) {
202
203 if (onstart)
204 onstart(element, mouseEvent);
205
206 dragging = true;
207 maxHeight = element.clientHeight;
208 maxWidth = element.clientWidth;
209
210 scrollOffset = element.scrollOffset();
211 offset = element.totalOffset();
212
213 element.ownerDocument.addEventListener("selectstart", consume, false );
214 element.ownerDocument.addEventListener("dragstart", consume, false);
215 element.ownerDocument.addEventListener("mousemove", move, false);
216 element.ownerDocument.addEventListener("mouseup", stop, false);
217
218 move(mouseEvent);
219 consume(mouseEvent);
220 }
221 }
222
223 /**
224 * @param {!Event} e
225 */
226 function stop(e)
227 {
228 if (dragging) {
229 element.ownerDocument.removeEventListener("selectstart", consume, fa lse);
230 element.ownerDocument.removeEventListener("dragstart", consume, fals e);
231 element.ownerDocument.removeEventListener("mousemove", move, false);
232 element.ownerDocument.removeEventListener("mouseup", stop, false);
233
234 if (onstop)
235 onstop(element, /** @type {!MouseEvent} */ (e));
236 }
237
238 dragging = false;
239 }
240
241 element.addEventListener("mousedown", start, false);
242 };
243
244 WebInspector.Spectrum.prototype = { 162 WebInspector.Spectrum.prototype = {
245 /** 163 /**
246 * @param {!WebInspector.Color} color 164 * @param {!WebInspector.Color} color
247 */ 165 */
248 setColor: function(color) 166 setColor: function(color)
249 { 167 {
250 this._hsv = color.hsva(); 168 this._hsv = color.hsva();
251 this._update(); 169 this._update();
252 }, 170 },
253 171
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 { 234 {
317 var h = this._hsv[0]; 235 var h = this._hsv[0];
318 var s = this._hsv[1]; 236 var s = this._hsv[1];
319 var v = this._hsv[2]; 237 var v = this._hsv[2];
320 var alpha = this._hsv[3]; 238 var alpha = this._hsv[3];
321 239
322 // Where to show the little circle that displays your current selected c olor. 240 // Where to show the little circle that displays your current selected c olor.
323 var dragX = s * this.dragWidth; 241 var dragX = s * this.dragWidth;
324 var dragY = this.dragHeight - (v * this.dragHeight); 242 var dragY = this.dragHeight - (v * this.dragHeight);
325 243
326 dragX = Math.max(-this._dragHelperElementHeight, 244 dragX = Math.max(-this._colorDragElementHeight,
327 Math.min(this.dragWidth - this._dragHelperElementHeight, dragX - this._dragHelperElementHeight)); 245 Math.min(this.dragWidth - this._colorDragElementHeight, dragX - this._colorDragElementHeight));
328 dragY = Math.max(-this._dragHelperElementHeight, 246 dragY = Math.max(-this._colorDragElementHeight,
329 Math.min(this.dragHeight - this._dragHelperElementHeight , dragY - this._dragHelperElementHeight)); 247 Math.min(this.dragHeight - this._colorDragElementHeight, dragY - this._colorDragElementHeight));
330 248
331 this._dragHelperElement.positionAt(dragX, dragY); 249 this._colorDragElement.positionAt(dragX, dragY);
332 250
333 // Where to show the bar that displays your current selected hue. 251 // Where to show the bar that displays your current selected hue.
334 var hueSlideX = (1 - h) * this._hueAlphaWidth - this.slideHelperWidth; 252 var hueSlideX = (1 - h) * this._hueAlphaWidth - this.slideHelperWidth;
335 this._hueSlider.style.left = hueSlideX + "px"; 253 this._hueSlider.style.left = hueSlideX + "px";
336 var alphaSlideX = alpha * this._hueAlphaWidth - this.slideHelperWidth; 254 var alphaSlideX = alpha * this._hueAlphaWidth - this.slideHelperWidth;
337 this._alphaSlider.style.left = alphaSlideX + "px"; 255 this._alphaSlider.style.left = alphaSlideX + "px";
338 }, 256 },
339 257
340 _updateInput: function() 258 _updateInput: function()
341 { 259 {
(...skipping 17 matching lines...) Expand all
359 if (!isRgb && (i === 1 || i === 2)) 277 if (!isRgb && (i === 1 || i === 2))
360 this._textValues[i].value += "%"; 278 this._textValues[i].value += "%";
361 } 279 }
362 this._textValues[3].value = Math.round(colorValues[3] * 100) / 100; 280 this._textValues[3].value = Math.round(colorValues[3] * 100) / 100;
363 } 281 }
364 }, 282 },
365 283
366 _updateUI: function() 284 _updateUI: function()
367 { 285 {
368 var h = WebInspector.Color.fromHSVA([this._hsv[0], 1, 1, 1]); 286 var h = WebInspector.Color.fromHSVA([this._hsv[0], 1, 1, 1]);
369 this._draggerElement.style.backgroundColor = /** @type {string} */ (h.as String(WebInspector.Color.Format.RGB)); 287 this._colorElement.style.backgroundColor = /** @type {string} */ (h.asSt ring(WebInspector.Color.Format.RGB));
370 this._swatchInnerElement.style.backgroundColor = /** @type {string} */ ( this._color().asString(WebInspector.Color.Format.RGBA)); 288 this._swatchInnerElement.style.backgroundColor = /** @type {string} */ ( this._color().asString(WebInspector.Color.Format.RGBA));
371 // Show border if the swatch is white. 289 // Show border if the swatch is white.
372 this._swatchInnerElement.classList.toggle("swatch-inner-white", this._co lor().hsla()[2] > 0.9); 290 this._swatchInnerElement.classList.toggle("swatch-inner-white", this._co lor().hsla()[2] > 0.9);
373 this._dragHelperElement.style.backgroundColor = /** @type {string} */ (t his._color().asString(WebInspector.Color.Format.RGBA)); 291 this._colorDragElement.style.backgroundColor = /** @type {string} */ (th is._color().asString(WebInspector.Color.Format.RGBA));
374 var noAlpha = WebInspector.Color.fromHSVA(this._hsv.slice(0,3).concat(1) ); 292 var noAlpha = WebInspector.Color.fromHSVA(this._hsv.slice(0,3).concat(1) );
375 this._alphaElementBackground.style.backgroundImage = String.sprintf("lin ear-gradient(to right, rgba(0,0,0,0), %s)", noAlpha.asString(WebInspector.Color. Format.RGB)); 293 this._alphaElementBackground.style.backgroundImage = String.sprintf("lin ear-gradient(to right, rgba(0,0,0,0), %s)", noAlpha.asString(WebInspector.Color. Format.RGB));
376 }, 294 },
377 295
378 _formatViewSwitch: function() 296 _formatViewSwitch: function()
379 { 297 {
380 var cf = WebInspector.Color.Format; 298 var cf = WebInspector.Color.Format;
381 if (this._currentFormat === cf.RGB) 299 if (this._currentFormat === cf.RGB)
382 this._currentFormat = cf.HSL; 300 this._currentFormat = cf.HSL;
383 else if (this._currentFormat === cf.HSL && !this._color().hasAlpha()) 301 else if (this._currentFormat === cf.HSL && !this._color().hasAlpha())
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 338
421 this._dispatchChangeEvent(); 339 this._dispatchChangeEvent();
422 this._updateHelperLocations(); 340 this._updateHelperLocations();
423 this._updateUI(); 341 this._updateUI();
424 }, 342 },
425 343
426 wasShown: function() 344 wasShown: function()
427 { 345 {
428 this._hueAlphaWidth = this._hueElement.offsetWidth; 346 this._hueAlphaWidth = this._hueElement.offsetWidth;
429 this.slideHelperWidth = this._hueSlider.offsetWidth / 2; 347 this.slideHelperWidth = this._hueSlider.offsetWidth / 2;
430 this.dragWidth = this._draggerElement.offsetWidth; 348 this.dragWidth = this._colorElement.offsetWidth;
431 this.dragHeight = this._draggerElement.offsetHeight; 349 this.dragHeight = this._colorElement.offsetHeight;
432 this._dragHelperElementHeight = this._dragHelperElement.offsetHeight / 2 ; 350 this._colorDragElementHeight = this._colorDragElement.offsetHeight / 2;
433 this._update(); 351 this._update();
434 }, 352 },
435 353
436 __proto__: WebInspector.VBox.prototype 354 __proto__: WebInspector.VBox.prototype
437 } 355 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698