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

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

Issue 1308563002: DevTools: allow editing custom color palette, move it out of experimental. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: rebaselined Created 5 years, 4 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 | Source/devtools/front_end/elements/spectrum.css » ('j') | 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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 this._hexValue.addEventListener("keydown", this._inputChanged.bind(this), fa lse); 95 this._hexValue.addEventListener("keydown", this._inputChanged.bind(this), fa lse);
96 this._hexValue.addEventListener("mousewheel", this._inputChanged.bind(this), false); 96 this._hexValue.addEventListener("mousewheel", this._inputChanged.bind(this), false);
97 97
98 var label = this._hexContainer.createChild("div", "spectrum-text-label"); 98 var label = this._hexContainer.createChild("div", "spectrum-text-label");
99 label.textContent = "HEX"; 99 label.textContent = "HEX";
100 100
101 WebInspector.installDragHandle(this._hueElement, dragStart.bind(this, positi onHue.bind(this)), positionHue.bind(this), null, "default"); 101 WebInspector.installDragHandle(this._hueElement, dragStart.bind(this, positi onHue.bind(this)), positionHue.bind(this), null, "default");
102 WebInspector.installDragHandle(this._alphaElement, dragStart.bind(this, posi tionAlpha.bind(this)), positionAlpha.bind(this), null, "default"); 102 WebInspector.installDragHandle(this._alphaElement, dragStart.bind(this, posi tionAlpha.bind(this)), positionAlpha.bind(this), null, "default");
103 WebInspector.installDragHandle(this._colorElement, dragStart.bind(this, posi tionColor.bind(this)), positionColor.bind(this), null, "default"); 103 WebInspector.installDragHandle(this._colorElement, dragStart.bind(this, posi tionColor.bind(this)), positionColor.bind(this), null, "default");
104 104
105 if (Runtime.experiments.isEnabled("colorPalettes")) { 105 this.element.classList.add("palettes-enabled");
106 this.element.classList.add("palettes-enabled"); 106 /** @type {!Map.<string, !WebInspector.Spectrum.Palette>} */
107 /** @type {!Map.<string, !WebInspector.Spectrum.Palette>} */ 107 this._palettes = new Map();
108 this._palettes = new Map(); 108 this._palettePanel = this.contentElement.createChild("div", "palette-panel") ;
109 this._palettePanel = this.contentElement.createChild("div", "palette-pan el"); 109 this._palettePanelShowing = false;
110 this._palettePanelShowing = false; 110 this._paletteContainer = this.contentElement.createChild("div", "spectrum-pa lette");
111 this._paletteContainer = this.contentElement.createChild("div", "spectru m-palette"); 111 this._paletteContainer.addEventListener("contextmenu", this._showPaletteColo rContextMenu.bind(this, -1));
112 var paletteSwitcher = this.contentElement.createChild("div", "spectrum-p alette-switcher spectrum-switcher"); 112 WebInspector.installDragHandle(this._paletteContainer, this._paletteDragStar t.bind(this), this._paletteDrag.bind(this), this._paletteDragEnd.bind(this), "de fault");
113 appendSwitcherIcon(paletteSwitcher); 113 var paletteSwitcher = this.contentElement.createChild("div", "spectrum-palet te-switcher spectrum-switcher");
114 paletteSwitcher.addEventListener("click", this._togglePalettePanel.bind( this, true)); 114 appendSwitcherIcon(paletteSwitcher);
115 var overlay = this.contentElement.createChild("div", "spectrum-overlay f ill"); 115 paletteSwitcher.addEventListener("click", this._togglePalettePanel.bind(this , true));
116 overlay.addEventListener("click", this._togglePalettePanel.bind(this, fa lse)); 116 var overlay = this.contentElement.createChild("div", "spectrum-overlay fill" );
117 overlay.addEventListener("click", this._togglePalettePanel.bind(this, false) );
117 118
118 this._addColorToolbar = new WebInspector.Toolbar(); 119 this._addColorToolbar = new WebInspector.Toolbar();
119 var addColorButton = new WebInspector.ToolbarButton(WebInspector.UIStrin g("Add to palette"), "add-toolbar-item"); 120 this._addColorToolbar.element.classList.add("add-color-toolbar");
120 addColorButton.addEventListener("click", this._addColorToCustomPalette.b ind(this)); 121 var addColorButton = new WebInspector.ToolbarButton(WebInspector.UIString("A dd to palette"), "add-toolbar-item");
121 this._addColorToolbar.appendToolbarItem(addColorButton); 122 addColorButton.addEventListener("click", this._addColorToCustomPalette.bind( this));
123 this._addColorToolbar.appendToolbarItem(addColorButton);
122 124
123 new WebInspector.Spectrum.PaletteGenerator(this._generatedPaletteLoaded. bind(this)); 125 new WebInspector.Spectrum.PaletteGenerator(this._generatedPaletteLoaded.bind (this));
124 }
125 126
126 /** 127 /**
127 * @param {function(!Event)} callback 128 * @param {function(!Event)} callback
128 * @param {!Event} event 129 * @param {!Event} event
129 * @return {boolean} 130 * @return {boolean}
130 * @this {WebInspector.Spectrum} 131 * @this {WebInspector.Spectrum}
131 */ 132 */
132 function dragStart(callback, event) 133 function dragStart(callback, event)
133 { 134 {
134 this._hueAlphaLeft = this._hueElement.totalOffsetLeft(); 135 this._hueAlphaLeft = this._hueElement.totalOffsetLeft();
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 Input: "Input", 181 Input: "Input",
181 Model: "Model", 182 Model: "Model",
182 Other: "Other" 183 Other: "Other"
183 } 184 }
184 185
185 WebInspector.Spectrum.Events = { 186 WebInspector.Spectrum.Events = {
186 ColorChanged: "ColorChanged", 187 ColorChanged: "ColorChanged",
187 SizeChanged: "SizeChanged" 188 SizeChanged: "SizeChanged"
188 }; 189 };
189 190
191 WebInspector.Spectrum._colorChipSize = 24;
192 WebInspector.Spectrum._itemsPerPaletteRow = 8;
193
190 WebInspector.Spectrum.prototype = { 194 WebInspector.Spectrum.prototype = {
191 _updatePalettePanel: function() 195 _updatePalettePanel: function()
192 { 196 {
193 this._palettePanel.removeChildren(); 197 this._palettePanel.removeChildren();
194 var title = this._palettePanel.createChild("div", "palette-title"); 198 var title = this._palettePanel.createChild("div", "palette-title");
195 title.textContent = WebInspector.UIString("Color Palettes"); 199 title.textContent = WebInspector.UIString("Color Palettes");
196 var toolbar = new WebInspector.Toolbar(this._palettePanel); 200 var toolbar = new WebInspector.Toolbar(this._palettePanel);
197 var closeButton = new WebInspector.ToolbarButton("Return to color picker ", "delete-toolbar-item"); 201 var closeButton = new WebInspector.ToolbarButton("Return to color picker ", "delete-toolbar-item");
198 closeButton.addEventListener("click", this._togglePalettePanel.bind(this , false)); 202 closeButton.addEventListener("click", this._togglePalettePanel.bind(this , false));
199 toolbar.appendToolbarItem(closeButton); 203 toolbar.appendToolbarItem(closeButton);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 * @param {!WebInspector.Spectrum.Palette} palette 237 * @param {!WebInspector.Spectrum.Palette} palette
234 * @param {boolean} animate 238 * @param {boolean} animate
235 * @param {!Event=} event 239 * @param {!Event=} event
236 */ 240 */
237 _showPalette: function(palette, animate, event) 241 _showPalette: function(palette, animate, event)
238 { 242 {
239 this._paletteContainer.removeChildren(); 243 this._paletteContainer.removeChildren();
240 for (var i = 0; i < palette.colors.length; i++) { 244 for (var i = 0; i < palette.colors.length; i++) {
241 var animationDelay = animate ? i * 100 / palette.colors.length : 0; 245 var animationDelay = animate ? i * 100 / palette.colors.length : 0;
242 var colorElement = this._createPaletteColor(palette.colors[i], anima tionDelay); 246 var colorElement = this._createPaletteColor(palette.colors[i], anima tionDelay);
243 colorElement.addEventListener("click", this._paletteColorSelected.bi nd(this, palette.colors[i])); 247 colorElement.addEventListener("mousedown", this._paletteColorSelecte d.bind(this, palette.colors[i]));
244 if (palette.mutable) 248 if (palette.mutable) {
249 colorElement.__mutable = true;
250 colorElement.__color = palette.colors[i];
245 colorElement.addEventListener("contextmenu", this._showPaletteCo lorContextMenu.bind(this, i)); 251 colorElement.addEventListener("contextmenu", this._showPaletteCo lorContextMenu.bind(this, i));
252 }
246 this._paletteContainer.appendChild(colorElement); 253 this._paletteContainer.appendChild(colorElement);
247 } 254 }
255 this._paletteContainerMutable = palette.mutable;
248 256
249 var numItems = palette.colors.length; 257 var numItems = palette.colors.length;
250 if (palette.mutable) 258 if (palette.mutable)
251 numItems++; 259 numItems++;
252 var rowsNeeded = Math.max(1, Math.ceil(numItems / 8)); 260 var rowsNeeded = Math.max(1, Math.ceil(numItems / WebInspector.Spectrum. _itemsPerPaletteRow));
253 for (var i = 0; palette.mutable && i < rowsNeeded * 8 - numItems; i++) 261 for (var i = 0; palette.mutable && i < rowsNeeded * WebInspector.Spectru m._itemsPerPaletteRow - numItems; i++)
254 this._paletteContainer.createChild("div", "spectrum-palette-color em pty-color"); 262 this._paletteContainer.createChild("div", "spectrum-palette-color em pty-color");
255 if (palette.mutable) 263 if (palette.mutable)
256 this._paletteContainer.appendChild(this._addColorToolbar.element); 264 this.contentElement.appendChild(this._addColorToolbar.element);
265 else
266 this._addColorToolbar.element.remove();
257 267
258 this._togglePalettePanel(false); 268 this._togglePalettePanel(false);
259 var paletteColorHeight = 12; 269 var paletteColorHeight = 12;
260 var paletteMargin = 12; 270 var paletteMargin = 12;
261 this.element.style.height = (this._paletteContainer.offsetTop + paletteM argin + (paletteColorHeight + paletteMargin) * rowsNeeded) + "px"; 271 this.element.style.height = (this._paletteContainer.offsetTop + paletteM argin + (paletteColorHeight + paletteMargin) * rowsNeeded) + "px";
262 this.dispatchEventToListeners(WebInspector.Spectrum.Events.SizeChanged); 272 this.dispatchEventToListeners(WebInspector.Spectrum.Events.SizeChanged);
263 }, 273 },
264 274
265 /** 275 /**
276 * @param {!Event} e
277 * @return {number}
278 */
279 _slotIndexForEvent: function(e)
280 {
281 var localX = e.pageX - this._paletteContainer.totalOffsetLeft();
282 var localY = e.pageY - this._paletteContainer.totalOffsetTop();
283 var col = Math.min(localX / WebInspector.Spectrum._colorChipSize | 0, We bInspector.Spectrum._itemsPerPaletteRow - 1);
284 var row = (localY / WebInspector.Spectrum._colorChipSize) | 0;
285 return Math.min(row * WebInspector.Spectrum._itemsPerPaletteRow + col, t his._paletteContainer.children.length - 1);
286 },
287
288 /**
289 * @param {!Event} e
290 * @return {boolean}
291 */
292 _paletteDragStart: function(e)
293 {
294 var element = e.deepElementFromPoint();
295 if (!element.__mutable)
296 return false;
297
298 var index = this._slotIndexForEvent(e);
299 this._dragElement = element;
300 this._dragHotSpotX = e.pageX - (index % WebInspector.Spectrum._itemsPerP aletteRow) * WebInspector.Spectrum._colorChipSize;
301 this._dragHotSpotY = e.pageY - (index / WebInspector.Spectrum._itemsPerP aletteRow | 0) * WebInspector.Spectrum._colorChipSize;
302 return true;
303 },
304
305 /**
306 * @param {!Event} e
307 */
308 _paletteDrag: function(e)
309 {
310 if (e.pageX < this._paletteContainer.totalOffsetLeft() || e.pageY < this ._paletteContainer.totalOffsetTop())
311 return;
312 var newIndex = this._slotIndexForEvent(e);
313 var offsetX = e.pageX - (newIndex % WebInspector.Spectrum._itemsPerPalet teRow) * WebInspector.Spectrum._colorChipSize;
314 var offsetY = e.pageY - (newIndex / WebInspector.Spectrum._itemsPerPalet teRow | 0) * WebInspector.Spectrum._colorChipSize;
315
316 this._dragElement.style.position = "relative";
317 this._dragElement.style.left = (offsetX - this._dragHotSpotX) + "px";
318 this._dragElement.style.top = (offsetY - this._dragHotSpotY) + "px";
319 var children = Array.prototype.slice.call(this._paletteContainer.childre n);
320 var index = children.indexOf(this._dragElement);
321 if (index !== newIndex)
322 this._paletteContainer.insertBefore(this._dragElement, children[newI ndex > index ? newIndex + 1 : newIndex]);
323 },
324
325 /**
326 * @param {!Event} e
327 */
328 _paletteDragEnd: function(e)
329 {
330 this._dragElement.style.removeProperty("position");
331 this._dragElement.style.removeProperty("left");
332 this._dragElement.style.removeProperty("top");
333 var children = this._paletteContainer.children;
334 var colors = [];
335 for (var i = 0; i < children.length; ++i) {
336 if (children[i].__color)
337 colors.push(children[i].__color);
338 }
339 var palette = this._customPaletteSetting.get();
340 palette.colors = colors;
341 this._customPaletteSetting.set(palette);
342 this._showPalette(this._customPaletteSetting.get(), false);
343 },
344
345 /**
266 * @param {!WebInspector.Spectrum.Palette} generatedPalette 346 * @param {!WebInspector.Spectrum.Palette} generatedPalette
267 */ 347 */
268 _generatedPaletteLoaded: function(generatedPalette) 348 _generatedPaletteLoaded: function(generatedPalette)
269 { 349 {
270 if (generatedPalette.colors.length) 350 if (generatedPalette.colors.length)
271 this._palettes.set(generatedPalette.title, generatedPalette); 351 this._palettes.set(generatedPalette.title, generatedPalette);
272 this._palettes.set(WebInspector.Spectrum.MaterialPalette.title, WebInspe ctor.Spectrum.MaterialPalette); 352 this._palettes.set(WebInspector.Spectrum.MaterialPalette.title, WebInspe ctor.Spectrum.MaterialPalette);
273 /** @type {!WebInspector.Spectrum.Palette} */ 353 /** @type {!WebInspector.Spectrum.Palette} */
274 var defaultCustomPalette = { title: "Custom", colors: [], mutable: true }; 354 var defaultCustomPalette = { title: "Custom", colors: [], mutable: true };
275 this._customPaletteSetting = WebInspector.settings.createSetting("custom ColorPalette", defaultCustomPalette); 355 this._customPaletteSetting = WebInspector.settings.createSetting("custom ColorPalette", defaultCustomPalette);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 this._customPaletteSetting.set(palette); 407 this._customPaletteSetting.set(palette);
328 this._showPalette(this._customPaletteSetting.get(), false); 408 this._showPalette(this._customPaletteSetting.get(), false);
329 }, 409 },
330 410
331 /** 411 /**
332 * @param {number} colorIndex 412 * @param {number} colorIndex
333 * @param {!Event} event 413 * @param {!Event} event
334 */ 414 */
335 _showPaletteColorContextMenu: function(colorIndex, event) 415 _showPaletteColorContextMenu: function(colorIndex, event)
336 { 416 {
417 if (!this._paletteContainerMutable)
418 return;
337 var contextMenu = new WebInspector.ContextMenu(event); 419 var contextMenu = new WebInspector.ContextMenu(event);
338 contextMenu.appendItem(WebInspector.UIString("Remove color"), this._dele tePaletteColor.bind(this, colorIndex)); 420 if (colorIndex !== -1) {
339 contextMenu.appendItem(WebInspector.UIString("Clear palette"), this._del etePaletteColor.bind(this, undefined)); 421 contextMenu.appendItem(WebInspector.UIString("Remove color"), this._ deletePaletteColors.bind(this, colorIndex, false));
422 contextMenu.appendItem(WebInspector.UIString("Remove all to the righ t"), this._deletePaletteColors.bind(this, colorIndex, true));
423 }
424 contextMenu.appendItem(WebInspector.UIString("Clear palette"), this._del etePaletteColors.bind(this, -1, true));
340 contextMenu.show(); 425 contextMenu.show();
341 }, 426 },
342 427
343 /** 428 /**
344 * @param {number=} colorIndex 429 * @param {number} colorIndex
430 * @param {boolean} toRight
345 */ 431 */
346 _deletePaletteColor: function(colorIndex) 432 _deletePaletteColors: function(colorIndex, toRight)
347 { 433 {
348 var palette = this._customPaletteSetting.get(); 434 var palette = this._customPaletteSetting.get();
349 if (colorIndex !== undefined) 435 if (toRight)
436 palette.colors.splice(colorIndex + 1, palette.colors.length - colorI ndex - 1);
437 else
350 palette.colors.splice(colorIndex, 1); 438 palette.colors.splice(colorIndex, 1);
351 else
352 palette.colors = [];
353 this._customPaletteSetting.set(palette); 439 this._customPaletteSetting.set(palette);
354 this._showPalette(this._customPaletteSetting.get(), false); 440 this._showPalette(this._customPaletteSetting.get(), false);
355 }, 441 },
356 442
357 /** 443 /**
358 * @param {!WebInspector.Color} color 444 * @param {!WebInspector.Color} color
359 * @param {string} colorFormat 445 * @param {string} colorFormat
360 */ 446 */
361 setColor: function(color, colorFormat) 447 setColor: function(color, colorFormat)
362 { 448 {
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 "#8BC34A", 894 "#8BC34A",
809 "#CDDC39", 895 "#CDDC39",
810 "#FFEB3B", 896 "#FFEB3B",
811 "#FFC107", 897 "#FFC107",
812 "#FF9800", 898 "#FF9800",
813 "#FF5722", 899 "#FF5722",
814 "#795548", 900 "#795548",
815 "#9E9E9E", 901 "#9E9E9E",
816 "#607D8B" 902 "#607D8B"
817 ]}; 903 ]};
OLDNEW
« no previous file with comments | « no previous file | Source/devtools/front_end/elements/spectrum.css » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698