Debian Codegraph @

dojox/widget/ColorPicker.js

raw symbols SCIP
1 define([
2 "dojo/_base/kernel","dojo/_base/declare","dojo/_base/lang","dojo/_base/array",
3 "dojo/_base/html","dojo/_base/connect","dojo/_base/sniff","dojo/_base/window",
4 "dojo/_base/event","dojo/dom","dojo/dom-class","dojo/keys","dojo/fx","dojo/dnd/move",
5 "dijit/registry","dijit/_base/focus","dijit/form/_FormWidget","dijit/typematic",
6 "dojox/color","dojo/i18n","dojo/i18n!./nls/ColorPicker","dojo/i18n!dojo/cldr/nls/number",
7 "dojo/text!./ColorPicker/ColorPicker.html"
8 ], function(kernel,declare,lang,ArrayUtil,html,Hub,has,win,Event,DOM,DOMClass,Keys,fx,move,
9 registry,FocusManager,FormWidget,Typematic,color,i18n,bundle1,bundle2,template){
10
11 kernel.experimental("dojox.widget.ColorPicker");
12
13 var webSafeFromHex = function(hex){
14 // stub, this is planned later:
15 return hex;
16 };
17
18 // TODO: shouldn't this extend _FormValueWidget?
19 return declare("dojox.widget.ColorPicker", FormWidget, {
20 // summary:
21 // a HSV color picker - similar to Photoshop picker
22 // description:
23 // Provides an interactive HSV ColorPicker similar to
24 // PhotoShop's color selction tool. This is an enhanced
25 // version of the default dijit.ColorPalette, though provides
26 // no accessibility.
27 // example:
28 // | var picker = new dojox.widget.ColorPicker({
29 // | // a couple of example toggles:
30 // | animatePoint:false,
31 // | showHsv: false,
32 // | webSafe: false,
33 // | showRgb: false
34 // | });
35 // example:
36 // | <!-- markup: -->
37 // | <div dojoType="dojox.widget.ColorPicker"></div>
38
39 // showRgb: Boolean
40 // show/update RGB input nodes
41 showRgb: true,
42
43 // showHsv: Boolean
44 // show/update HSV input nodes
45 showHsv: true,
46
47 // showHex: Boolean
48 // show/update Hex value field
49 showHex: true,
50
51 // webSafe: Boolean
52 // deprecated? or just use a toggle to show/hide that node, too?
53 webSafe: true,
54
55 // animatePoint: Boolean
56 // toggle to use slideTo (true) or just place the cursor (false) on click
57 animatePoint: true,
58
59 // slideDuration: Integer
60 // time in ms picker node will slide to next location (non-dragging) when animatePoint=true
61 slideDuration: 250,
62
63 // liveUpdate: Boolean
64 // Set to true to fire onChange in an indeterminate way
65 liveUpdate: false,
66
67 // PICKER_HUE_H: int
68 // Height of the hue picker, used to calculate positions
69 PICKER_HUE_H: 150,
70
71 // PICKER_SAT_VAL_H: int
72 // Height of the 2d picker, used to calculate positions
73 PICKER_SAT_VAL_H: 150,
74
75 // PICKER_SAT_VAL_W: int
76 // Width of the 2d picker, used to calculate positions
77 PICKER_SAT_VAL_W: 150,
78
79 // PICKER_HUE_SELECTOR_H: int
80 // Height of the hue selector DOM node, used to calc offsets so that selection
81 // is center of the image node.
82 PICKER_HUE_SELECTOR_H: 8,
83
84 // PICKER_SAT_SELECTOR_H: int
85 // Height of the saturation selector DOM node, used to calc offsets so that selection
86 // is center of the image node.
87 PICKER_SAT_SELECTOR_H: 10,
88
89 // PICKER_SAT_SELECTOR_W: int
90 // Width of the saturation selector DOM node, used to calc offsets so that selection
91 // is center of the image node.
92 PICKER_SAT_SELECTOR_W: 10,
93
94 // value: String
95 // Default color for this component. Only hex values are accepted as incoming/returned
96 // values. Adjust this value with `.attr`, eg: dijit.byId("myPicker").attr("value", "#ededed");
97 // to cause the points to adjust and the values to reflect the current color.
98 value: "#ffffff",
99
100 _underlay: require.toUrl("dojox/widget/ColorPicker/images/underlay.png"),
101
102 _hueUnderlay: require.toUrl("dojox/widget/ColorPicker/images/hue.png"),
103
104 _pickerPointer: require.toUrl("dojox/widget/ColorPicker/images/pickerPointer.png"),
105
106 _huePickerPointer: require.toUrl("dojox/widget/ColorPicker/images/hueHandle.png"),
107
108 _huePickerPointerAlly: require.toUrl("dojox/widget/ColorPicker/images/hueHandleA11y.png"),
109
110 templateString: template,
111
112 postMixInProperties: function(){
113 if(DOMClass.contains(win.body(), "dijit_a11y")){
114 // Use the pointer that will show up in high contrast.
115 this._huePickerPointer = this._huePickerPointerAlly;
116 }
117 this._uId = registry.getUniqueId(this.id);
118 lang.mixin(this, i18n.getLocalization("dojox.widget", "ColorPicker"));
119 lang.mixin(this, i18n.getLocalization("dojo.cldr", "number"));
120 this.inherited(arguments);
121 },
122
123 postCreate: function(){
124 // summary:
125 // As quickly as we can, set up ie6 alpha-filter support for our
126 // underlay. we don't do image handles (done in css), just the 'core'
127 // of this widget: the underlay.
128 this.inherited(arguments);
129 if(has("ie") < 7){
130 this.colorUnderlay.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this._underlay+"', sizingMethod='scale')";
131 this.colorUnderlay.src = this._blankGif.toString();
132 }
133 // hide toggle-able nodes:
134 if(!this.showRgb){ this.rgbNode.style.visibility = "hidden"; }
135 if(!this.showHsv){ this.hsvNode.style.visibility = "hidden"; }
136 if(!this.showHex){ this.hexNode.style.visibility = "hidden"; }
137 if(!this.webSafe){ this.safePreviewNode.style.visibility = "hidden"; }
138 },
139
140 startup: function(){
141 if(this._started){
142 return;
143 }
144 this._started = true;
145 this.set("value", this.value);
146 this._mover = new move.boxConstrainedMoveable(this.cursorNode, {
147 box: {
148 t: -(this.PICKER_SAT_SELECTOR_H/2),
149 l: -(this.PICKER_SAT_SELECTOR_W/2),
150 w:this.PICKER_SAT_VAL_W,
151 h:this.PICKER_SAT_VAL_H
152 }
153 });
154
155 this._hueMover = new move.boxConstrainedMoveable(this.hueCursorNode, {
156 box: {
157 t: -(this.PICKER_HUE_SELECTOR_H/2),
158 l:0,
159 w:0,
160 h:this.PICKER_HUE_H
161 }
162 });
163
164 this._subs = [];
165 // no dnd/move/move published ... use a timer:
166 this._subs.push(Hub.subscribe("/dnd/move/stop", lang.hitch(this, "_clearTimer")));
167 this._subs.push(Hub.subscribe("/dnd/move/start", lang.hitch(this, "_setTimer")));
168
169 // Bind to up, down, left and right arrows on the hue and saturation nodes.
170 this._keyListeners = [];
171 this._connects.push(Typematic.addKeyListener(this.hueCursorNode,{
172 charOrCode: Keys.UP_ARROW,
173 shiftKey: false,
174 metaKey: false,
175 ctrlKey: false,
176 altKey: false
177 }, this, lang.hitch(this, this._updateHueCursorNode), 25, 25));
178 this._connects.push(Typematic.addKeyListener(this.hueCursorNode,{
179 charOrCode: Keys.DOWN_ARROW,
180 shiftKey: false,
181 metaKey: false,
182 ctrlKey: false,
183 altKey: false
184 }, this, lang.hitch(this, this._updateHueCursorNode), 25, 25));
185 this._connects.push(Typematic.addKeyListener(this.cursorNode,{
186 charOrCode: Keys.UP_ARROW,
187 shiftKey: false,
188 metaKey: false,
189 ctrlKey: false,
190 altKey: false
191 }, this, lang.hitch(this, this._updateCursorNode), 25, 25));
192 this._connects.push(Typematic.addKeyListener(this.cursorNode,{
193 charOrCode: Keys.DOWN_ARROW,
194 shiftKey: false,
195 metaKey: false,
196 ctrlKey: false,
197 altKey: false
198 }, this, lang.hitch(this, this._updateCursorNode), 25, 25));
199 this._connects.push(Typematic.addKeyListener(this.cursorNode,{
200 charOrCode: Keys.LEFT_ARROW,
201 shiftKey: false,
202 metaKey: false,
203 ctrlKey: false,
204 altKey: false
205 }, this, lang.hitch(this, this._updateCursorNode), 25, 25));
206 this._connects.push(Typematic.addKeyListener(this.cursorNode,{
207 charOrCode: Keys.RIGHT_ARROW,
208 shiftKey: false,
209 metaKey: false,
210 ctrlKey: false,
211 altKey: false
212 }, this, lang.hitch(this, this._updateCursorNode), 25, 25));
213 },
214
215 _setValueAttr: function(value, fireOnChange){
216 if(!this._started){ return; }
217 this.setColor(value, fireOnChange);
218 },
219
220 setColor: function(/* String */col, force){
221 // summary:
222 // Set a color on a picker. Usually used to set
223 // initial color as an alternative to passing defaultColor option
224 // to the constructor.
225 col = color.fromString(col);
226 this._updatePickerLocations(col);
227 this._updateColorInputs(col);
228 this._updateValue(col, force);
229 },
230
231 _setTimer: function(/* dojo/dnd/Mover */mover){
232 if(mover.node != this.cursorNode && mover.node != this.hueCursorNode){ return; }
233 // FIXME: should I assume this? focus on mouse down so on mouse up
234 FocusManager.focus(mover.node);
235 DOM.setSelectable(this.domNode,false);
236 this._timer = setInterval(lang.hitch(this, "_updateColor"), 45);
237 },
238
239 _clearTimer: function(/* dojo/dnd/Mover */mover){
240 if(!this._timer){ return; }
241 clearInterval(this._timer);
242 this._timer = null;
243 this.onChange(this.value);
244 DOM.setSelectable(this.domNode,true);
245 },
246
247 _setHue: function(/* Float */h){
248 // summary:
249 // Sets a natural color background for the
250 // underlay image against closest hue value (full saturation)
251 // h:
252 // 0..360
253 html.style(this.colorUnderlay, "backgroundColor", color.fromHsv(h,100,100).toHex());
254
255 },
256
257 _updateHueCursorNode: function(count, node, e){
258 // summary:
259 // Function used by the typematic code to handle cursor position and update
260 // via keyboard.
261 // count: Number
262 // -1 means stop, anything else is just how many times it was called.
263 // node: DomNode
264 // The node generating the event.
265 // e: Event
266 // The event.
267 if(count !== -1){
268 var y = html.style(this.hueCursorNode, "top");
269 var selCenter = this.PICKER_HUE_SELECTOR_H/2;
270
271 // Account for our offset
272 y += selCenter;
273 var update = false;
274 if(e.charOrCode == Keys.UP_ARROW){
275 if(y > 0){
276 y -= 1;
277 update = true;
278 }
279 }else if(e.charOrCode == Keys.DOWN_ARROW){
280 if(y < this.PICKER_HUE_H){
281 y += 1;
282 update = true;
283 }
284 }
285 y -= selCenter;
286 if(update){
287 html.style(this.hueCursorNode, "top", y + "px");
288 }
289 }else{
290 this._updateColor(true);
291 }
292 },
293
294 _updateCursorNode: function(count, node, e){
295 // summary:
296 // Function used by the typematic code to handle cursor position and update
297 // via keyboard.
298 // count:
299 // -1 means stop, anything else is just how many times it was called.
300 // node:
301 // The node generating the event.
302 // e:
303 // The event.
304 var selCenterH = this.PICKER_SAT_SELECTOR_H/2;
305 var selCenterW = this.PICKER_SAT_SELECTOR_W/2;
306
307 if(count !== -1){
308 var y = html.style(this.cursorNode, "top");
309 var x = html.style(this.cursorNode, "left");
310
311 // Account for our offsets to center
312 y += selCenterH;
313 x += selCenterW;
314
315 var update = false;
316 if(e.charOrCode == Keys.UP_ARROW){
317 if(y > 0){
318 y -= 1;
319 update = true;
320 }
321 }else if(e.charOrCode == Keys.DOWN_ARROW){
322 if(y < this.PICKER_SAT_VAL_H){
323 y += 1;
324 update = true;
325 }
326 }else if(e.charOrCode == Keys.LEFT_ARROW){
327 if(x > 0){
328 x -= 1;
329 update = true;
330 }
331 }else if(e.charOrCode == Keys.RIGHT_ARROW){
332 if(x < this.PICKER_SAT_VAL_W){
333 x += 1;
334 update = true;
335 }
336 }
337 if(update){
338 // Account for our offsets to center
339 y -= selCenterH;
340 x -= selCenterW;
341 html.style(this.cursorNode, "top", y + "px");
342 html.style(this.cursorNode, "left", x + "px");
343 }
344 }else{
345 this._updateColor(true);
346 }
347 },
348
349 _updateColor: function(fireChange){
350 // summary:
351 // update the previewNode color, and input values [optional]
352
353 var hueSelCenter = this.PICKER_HUE_SELECTOR_H/2,
354 satSelCenterH = this.PICKER_SAT_SELECTOR_H/2,
355 satSelCenterW = this.PICKER_SAT_SELECTOR_W/2;
356
357 var _huetop = html.style(this.hueCursorNode,"top") + hueSelCenter,
358 _pickertop = html.style(this.cursorNode,"top") + satSelCenterH,
359 _pickerleft = html.style(this.cursorNode,"left") + satSelCenterW,
360 h = Math.round(360 - (_huetop / this.PICKER_HUE_H * 360)),
361 col = color.fromHsv(h, _pickerleft / this.PICKER_SAT_VAL_W * 100, 100 - (_pickertop / this.PICKER_SAT_VAL_H * 100))
362 ;
363
364 this._updateColorInputs(col);
365 this._updateValue(col, fireChange);
366
367 // update hue, not all the pickers
368 if(h!=this._hue){
369 this._setHue(h);
370 }
371 },
372
373 _colorInputChange: function(e){
374 // summary:
375 // updates picker position and inputs
376 // according to rgb, hex or hsv input changes
377 var col, hasit = false;
378 switch(e.target){
379 //transform to hsv to pixels
380
381 case this.hexCode:
382 col = color.fromString(e.target.value);
383 hasit = true;
384
385 break;
386 case this.Rval:
387 case this.Gval:
388 case this.Bval:
389 col = color.fromArray([this.Rval.value, this.Gval.value, this.Bval.value]);
390 hasit = true;
391 break;
392 case this.Hval:
393 case this.Sval:
394 case this.Vval:
395 col = color.fromHsv(this.Hval.value, this.Sval.value, this.Vval.value);
396 hasit = true;
397 break;
398 }
399
400 if(hasit){
401 this._updatePickerLocations(col);
402 this._updateColorInputs(col);
403 this._updateValue(col, true);
404 }
405
406 },
407
408 _updateValue: function(/* dojox/color/Color */col, /* Boolean */fireChange){
409 // summary:
410 // updates the value of the widget
411 // can cancel reverse onChange by specifying second param
412 var hex = col.toHex();
413
414 this.value = this.valueNode.value = hex;
415
416 // anytime we muck with the color, fire onChange?
417 if(fireChange && (!this._timer || this.liveUpdate)){
418 this.onChange(hex);
419 }
420 },
421
422 _updatePickerLocations: function(/* dojox/color/Color */col){
423 // summary:
424 // update handles on the pickers acording to color values
425
426 var hueSelCenter = this.PICKER_HUE_SELECTOR_H/2,
427 satSelCenterH = this.PICKER_SAT_SELECTOR_H/2,
428 satSelCenterW = this.PICKER_SAT_SELECTOR_W/2;
429
430 var hsv = col.toHsv(),
431 ypos = Math.round(this.PICKER_HUE_H - hsv.h / 360 * this.PICKER_HUE_H) - hueSelCenter,
432 newLeft = Math.round(hsv.s / 100 * this.PICKER_SAT_VAL_W) - satSelCenterW,
433 newTop = Math.round(this.PICKER_SAT_VAL_H - hsv.v / 100 * this.PICKER_SAT_VAL_H) - satSelCenterH
434 ;
435
436 if(this.animatePoint){
437 fx.slideTo({
438 node: this.hueCursorNode,
439 duration: this.slideDuration,
440 top: ypos,
441 left: 0
442 }).play();
443
444 fx.slideTo({
445 node: this.cursorNode,
446 duration: this.slideDuration,
447 top: newTop,
448 left: newLeft
449 }).play();
450
451 }
452 else {
453 html.style(this.hueCursorNode, "top", ypos + "px");
454 html.style(this.cursorNode, {
455 left: newLeft + "px",
456 top: newTop + "px"
457 });
458 }
459
460 // limit hue calculations to only when it changes
461 if(hsv.h != this._hue){
462 this._setHue(hsv.h);
463 }
464
465 },
466
467 _updateColorInputs: function(/* dojox/color/Color */ col){
468 // summary:
469 // updates color inputs that were changed through other inputs
470 // or by clicking on the picker
471
472 var hex = col.toHex();
473
474 if(this.showRgb){
475 this.Rval.value = col.r;
476 this.Gval.value = col.g;
477 this.Bval.value = col.b;
478 }
479
480 if(this.showHsv){
481 var hsv = col.toHsv();
482 this.Hval.value = Math.round((hsv.h)); // convert to 0..360
483 this.Sval.value = Math.round(hsv.s);
484 this.Vval.value = Math.round(hsv.v);
485 }
486
487 if(this.showHex){
488 this.hexCode.value = hex;
489 }
490
491 this.previewNode.style.backgroundColor = hex;
492
493 if(this.webSafe){
494 this.safePreviewNode.style.backgroundColor = webSafeFromHex(hex);
495 }
496 },
497
498 _setHuePoint: function(/* Event */evt){
499 // summary:
500 // set the hue picker handle on relative y coordinates
501
502 //#13268 Fix for IE and Edge, as they don't support evt.layerX/Y
503 var selCenter = this.PICKER_HUE_SELECTOR_H/2;
504 var ypos = evt.layerY || (evt.y - evt.target.getBoundingClientRect().top);
505 ypos -= selCenter;
506 if(this.animatePoint){
507 fx.slideTo({
508 node: this.hueCursorNode,
509 duration:this.slideDuration,
510 top: ypos,
511 left: 0,
512 onEnd: lang.hitch(this, function(){ this._updateColor(true); FocusManager.focus(this.hueCursorNode); })
513 }).play();
514 }else{
515 html.style(this.hueCursorNode, "top", ypos + "px");
516 this._updateColor(true);
517 }
518 },
519
520 _setPoint: function(/* Event */evt){
521 // summary:
522 // set our picker point based on relative x/y coordinates
523
524 // evt.preventDefault();
525 var satSelCenterH = this.PICKER_SAT_SELECTOR_H/2;
526 var satSelCenterW = this.PICKER_SAT_SELECTOR_W/2;
527
528 //#13268 Fix for IE and Edge, as they don't support evt.layerX/Y
529
530 var newTop = evt.layerY || (evt.y - evt.target.getBoundingClientRect().top);
531 newTop -= satSelCenterH;
532 var newLeft = evt.layerX || (evt.x - evt.target.getBoundingClientRect().left);
533 newLeft -= satSelCenterW;
534
535 if(evt){ FocusManager.focus(evt.target); }
536
537 if(this.animatePoint){
538 fx.slideTo({
539 node: this.cursorNode,
540 duration: this.slideDuration,
541 top: newTop,
542 left: newLeft,
543 onEnd: lang.hitch(this, function(){ this._updateColor(true); FocusManager.focus(this.cursorNode); })
544 }).play();
545 }else{
546 html.style(this.cursorNode, {
547 left: newLeft + "px",
548 top: newTop + "px"
549 });
550 this._updateColor(true);
551 }
552 },
553
554 _handleKey: function(/* Event */e){
555 // TODO: not implemented YET
556 // var keys = d.keys;
557 },
558
559 focus: function(){
560 // summary:
561 // Put focus on this widget, only if focus isn't set on it already.
562 if(!this.focused){
563 FocusManager.focus(this.focusNode);
564 }
565 },
566
567 _stopDrag: function(e){
568 // summary:
569 // Function to halt the mouse down default
570 // to disable dragging of images out of the color
571 // picker.
572 Event.stop(e);
573 },
574
575 destroy: function(){
576 // summary:
577 // Over-ride to clean up subscriptions, etc.
578 this.inherited(arguments);
579 ArrayUtil.forEach(this._subs, function(sub){
580 Hub.unsubscribe(sub);
581 });
582 delete this._subs;
583 }
584 });
585 });