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

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: Remove empty line 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
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._draggerElement, colorDragStart.bind(thi s), colorDrag.bind(this), null, "default");
lushnikov 2015/05/07 16:02:26 can we rename draggerElement into colorElement whi
samli 2015/05/08 01:43:40 Done.
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(0);
lushnikov 2015/05/07 16:02:26 .slice()
samli 2015/05/08 01:43:40 Done.
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 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
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._draggerElement.offsetWidth;
431 this.dragHeight = this._draggerElement.offsetHeight; 349 this.dragHeight = this._draggerElement.offsetHeight;
432 this._dragHelperElementHeight = this._dragHelperElement.offsetHeight / 2 ; 350 this._dragHelperElementHeight = this._dragHelperElement.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