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

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: 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 var addColorButton = new WebInspector.ToolbarButton(WebInspector.UIString("A dd to palette"), "add-toolbar-item");
120 addColorButton.addEventListener("click", this._addColorToCustomPalette.b ind(this)); 121 addColorButton.addEventListener("click", this._addColorToCustomPalette.bind( this));
121 this._addColorToolbar.appendToolbarItem(addColorButton); 122 this._addColorToolbar.appendToolbarItem(addColorButton);
122 123
123 new WebInspector.Spectrum.PaletteGenerator(this._generatedPaletteLoaded. bind(this)); 124 new WebInspector.Spectrum.PaletteGenerator(this._generatedPaletteLoaded.bind (this));
124 }
125 125
126 /** 126 /**
127 * @param {function(!Event)} callback 127 * @param {function(!Event)} callback
128 * @param {!Event} event 128 * @param {!Event} event
129 * @return {boolean} 129 * @return {boolean}
130 * @this {WebInspector.Spectrum} 130 * @this {WebInspector.Spectrum}
131 */ 131 */
132 function dragStart(callback, event) 132 function dragStart(callback, event)
133 { 133 {
134 this._hueAlphaLeft = this._hueElement.totalOffsetLeft(); 134 this._hueAlphaLeft = this._hueElement.totalOffsetLeft();
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 * @param {!WebInspector.Spectrum.Palette} palette 233 * @param {!WebInspector.Spectrum.Palette} palette
234 * @param {boolean} animate 234 * @param {boolean} animate
235 * @param {!Event=} event 235 * @param {!Event=} event
236 */ 236 */
237 _showPalette: function(palette, animate, event) 237 _showPalette: function(palette, animate, event)
238 { 238 {
239 this._paletteContainer.removeChildren(); 239 this._paletteContainer.removeChildren();
240 for (var i = 0; i < palette.colors.length; i++) { 240 for (var i = 0; i < palette.colors.length; i++) {
241 var animationDelay = animate ? i * 100 / palette.colors.length : 0; 241 var animationDelay = animate ? i * 100 / palette.colors.length : 0;
242 var colorElement = this._createPaletteColor(palette.colors[i], anima tionDelay); 242 var colorElement = this._createPaletteColor(palette.colors[i], anima tionDelay);
243 colorElement.addEventListener("click", this._paletteColorSelected.bi nd(this, palette.colors[i])); 243 colorElement.addEventListener("mousedown", this._paletteColorSelecte d.bind(this, palette.colors[i]));
244 if (palette.mutable) 244 if (palette.mutable) {
245 colorElement.__mutable = true;
dgozman 2015/08/21 01:11:29 Could use |this._paletteContainerMutable| instead.
246 colorElement.__color = palette.colors[i];
245 colorElement.addEventListener("contextmenu", this._showPaletteCo lorContextMenu.bind(this, i)); 247 colorElement.addEventListener("contextmenu", this._showPaletteCo lorContextMenu.bind(this, i));
248 }
246 this._paletteContainer.appendChild(colorElement); 249 this._paletteContainer.appendChild(colorElement);
247 } 250 }
251 this._paletteContainerMutable = palette.mutable;
248 252
249 var numItems = palette.colors.length; 253 var numItems = palette.colors.length;
250 if (palette.mutable) 254 if (palette.mutable)
251 numItems++; 255 numItems++;
252 var rowsNeeded = Math.max(1, Math.ceil(numItems / 8)); 256 var rowsNeeded = Math.max(1, Math.ceil(numItems / 8));
253 for (var i = 0; palette.mutable && i < rowsNeeded * 8 - numItems; i++) 257 for (var i = 0; palette.mutable && i < rowsNeeded * 8 - numItems; i++)
254 this._paletteContainer.createChild("div", "spectrum-palette-color em pty-color"); 258 this._paletteContainer.createChild("div", "spectrum-palette-color em pty-color");
255 if (palette.mutable) 259 if (palette.mutable)
256 this._paletteContainer.appendChild(this._addColorToolbar.element); 260 this._paletteContainer.appendChild(this._addColorToolbar.element);
257 261
258 this._togglePalettePanel(false); 262 this._togglePalettePanel(false);
259 var paletteColorHeight = 12; 263 var paletteColorHeight = 12;
260 var paletteMargin = 12; 264 var paletteMargin = 12;
261 this.element.style.height = (this._paletteContainer.offsetTop + paletteM argin + (paletteColorHeight + paletteMargin) * rowsNeeded) + "px"; 265 this.element.style.height = (this._paletteContainer.offsetTop + paletteM argin + (paletteColorHeight + paletteMargin) * rowsNeeded) + "px";
262 this.dispatchEventToListeners(WebInspector.Spectrum.Events.SizeChanged); 266 this.dispatchEventToListeners(WebInspector.Spectrum.Events.SizeChanged);
263 }, 267 },
264 268
265 /** 269 /**
270 * @param {!Event} e
271 * @return {number}
272 */
273 _slotIndexForEvent: function(e)
274 {
275 var localX = e.pageX - this._paletteContainer.totalOffsetLeft();
276 var localY = e.pageY - this._paletteContainer.totalOffsetTop();
277 var col = localX / 24 % 8 | 0;
dgozman 2015/08/21 01:11:29 nit: add parenthesis for consistency.
dgozman 2015/08/21 01:11:29 Why %8, and not Math.min(, 7)?
278 var row = (localY / 24) | 0;
dgozman 2015/08/21 01:11:28 24 to named constant!
279 return Math.min(row * 8 + col, this._paletteContainer.children.length - 2);
dgozman 2015/08/21 01:11:29 Let's move add button out of container.
280 },
281
282 /**
283 * @param {!Event} e
284 * @return {boolean}
285 */
286 _paletteDragStart: function(e)
287 {
288 var element = e.deepElementFromPoint();
289 if (!element.__mutable)
290 return false;
291
292 var index = this._slotIndexForEvent(e);
293 this._dragElement = element;
294 this._dragHotSpotX = e.pageX - (index % 8) * 24;
295 this._dragHotSpotY = e.pageY - (index / 8 | 0) * 24;
296 return true;
297 },
298
299 /**
300 * @param {!Event} e
301 */
302 _paletteDrag: function(e)
303 {
304 if (e.pageX < this._paletteContainer.totalOffsetLeft() || e.pageY < this ._paletteContainer.totalOffsetTop())
305 return;
306 var newIndex = this._slotIndexForEvent(e);
307 var offsetX = e.pageX - (newIndex % 8) * 24;
308 var offsetY = e.pageY - (newIndex / 8 | 0) * 24;
309
310 this._dragElement.style.position = "relative";
311 this._dragElement.style.left = (offsetX - this._dragHotSpotX) + "px";
312 this._dragElement.style.top = (offsetY - this._dragHotSpotY) + "px";
313 var children = Array.prototype.slice.call(this._paletteContainer.childre n);
314 var index = children.indexOf(this._dragElement);
315 if (index !== newIndex)
316 this._paletteContainer.insertBefore(this._dragElement, children[new Index > index ? newIndex + 1 : newIndex]);
dgozman 2015/08/21 01:11:29 nit: weird indent. Presubmit?
317 },
318
319 /**
320 * @param {!Event} e
321 */
322 _paletteDragEnd: function(e)
323 {
324 this._dragElement.style.removeProperty("position");
325 this._dragElement.style.removeProperty("left");
326 this._dragElement.style.removeProperty("top");
327 var children = this._paletteContainer.children;
328 var colors = [];
329 for (var i = 0; i < children.length; ++i) {
330 if (children[i].__color)
331 colors.push(children[i].__color);
332 }
333 var palette = this._customPaletteSetting.get();
334 palette.colors = colors;
335 this._customPaletteSetting.set(palette);
336 this._showPalette(this._customPaletteSetting.get(), false);
337 },
338
339 /**
266 * @param {!WebInspector.Spectrum.Palette} generatedPalette 340 * @param {!WebInspector.Spectrum.Palette} generatedPalette
267 */ 341 */
268 _generatedPaletteLoaded: function(generatedPalette) 342 _generatedPaletteLoaded: function(generatedPalette)
269 { 343 {
270 if (generatedPalette.colors.length) 344 if (generatedPalette.colors.length)
271 this._palettes.set(generatedPalette.title, generatedPalette); 345 this._palettes.set(generatedPalette.title, generatedPalette);
272 this._palettes.set(WebInspector.Spectrum.MaterialPalette.title, WebInspe ctor.Spectrum.MaterialPalette); 346 this._palettes.set(WebInspector.Spectrum.MaterialPalette.title, WebInspe ctor.Spectrum.MaterialPalette);
273 /** @type {!WebInspector.Spectrum.Palette} */ 347 /** @type {!WebInspector.Spectrum.Palette} */
274 var defaultCustomPalette = { title: "Custom", colors: [], mutable: true }; 348 var defaultCustomPalette = { title: "Custom", colors: [], mutable: true };
275 this._customPaletteSetting = WebInspector.settings.createSetting("custom ColorPalette", defaultCustomPalette); 349 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); 401 this._customPaletteSetting.set(palette);
328 this._showPalette(this._customPaletteSetting.get(), false); 402 this._showPalette(this._customPaletteSetting.get(), false);
329 }, 403 },
330 404
331 /** 405 /**
332 * @param {number} colorIndex 406 * @param {number} colorIndex
333 * @param {!Event} event 407 * @param {!Event} event
334 */ 408 */
335 _showPaletteColorContextMenu: function(colorIndex, event) 409 _showPaletteColorContextMenu: function(colorIndex, event)
336 { 410 {
411 if (!this._paletteContainerMutable)
412 return;
337 var contextMenu = new WebInspector.ContextMenu(event); 413 var contextMenu = new WebInspector.ContextMenu(event);
338 contextMenu.appendItem(WebInspector.UIString("Remove color"), this._dele tePaletteColor.bind(this, colorIndex)); 414 if (colorIndex !== -1) {
339 contextMenu.appendItem(WebInspector.UIString("Clear palette"), this._del etePaletteColor.bind(this, undefined)); 415 contextMenu.appendItem(WebInspector.UIString("Remove color"), this._ deletePaletteColors.bind(this, colorIndex, false));
dgozman 2015/08/21 01:11:29 We use |UIString.capitalize| in context menus.
416 contextMenu.appendItem(WebInspector.UIString("Remove all to the righ t"), this._deletePaletteColors.bind(this, colorIndex, true));
417 }
418 contextMenu.appendItem(WebInspector.UIString("Clear palette"), this._del etePaletteColors.bind(this, -1, true));
340 contextMenu.show(); 419 contextMenu.show();
341 }, 420 },
342 421
343 /** 422 /**
344 * @param {number=} colorIndex 423 * @param {number} colorIndex
424 * @param {boolean} toRight
345 */ 425 */
346 _deletePaletteColor: function(colorIndex) 426 _deletePaletteColors: function(colorIndex, toRight)
347 { 427 {
348 var palette = this._customPaletteSetting.get(); 428 var palette = this._customPaletteSetting.get();
349 if (colorIndex !== undefined) 429 if (toRight)
430 palette.colors.splice(colorIndex + 1, palette.colors.length - colorI ndex - 1);
431 else
350 palette.colors.splice(colorIndex, 1); 432 palette.colors.splice(colorIndex, 1);
351 else
352 palette.colors = [];
353 this._customPaletteSetting.set(palette); 433 this._customPaletteSetting.set(palette);
354 this._showPalette(this._customPaletteSetting.get(), false); 434 this._showPalette(this._customPaletteSetting.get(), false);
355 }, 435 },
356 436
357 /** 437 /**
358 * @param {!WebInspector.Color} color 438 * @param {!WebInspector.Color} color
359 * @param {string} colorFormat 439 * @param {string} colorFormat
360 */ 440 */
361 setColor: function(color, colorFormat) 441 setColor: function(color, colorFormat)
362 { 442 {
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 "#8BC34A", 888 "#8BC34A",
809 "#CDDC39", 889 "#CDDC39",
810 "#FFEB3B", 890 "#FFEB3B",
811 "#FFC107", 891 "#FFC107",
812 "#FF9800", 892 "#FF9800",
813 "#FF5722", 893 "#FF5722",
814 "#795548", 894 "#795548",
815 "#9E9E9E", 895 "#9E9E9E",
816 "#607D8B" 896 "#607D8B"
817 ]}; 897 ]};
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