| 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 |
|
| 15 |
return hex; |
| 16 |
}; |
| 17 |
|
| 18 |
|
| 19 |
return declare("dojox.widget.ColorPicker", FormWidget, { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
showRgb: true, |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
showHsv: true, |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
showHex: true, |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
webSafe: true, |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
animatePoint: true, |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
slideDuration: 250, |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
liveUpdate: false, |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
PICKER_HUE_H: 150, |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
PICKER_SAT_VAL_H: 150, |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
PICKER_SAT_VAL_W: 150, |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
PICKER_HUE_SELECTOR_H: 8, |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
PICKER_SAT_SELECTOR_H: 10, |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
PICKER_SAT_SELECTOR_W: 10, |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 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 |
|
| 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 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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(col, force){ |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
col = color.fromString(col); |
| 226 |
this._updatePickerLocations(col); |
| 227 |
this._updateColorInputs(col); |
| 228 |
this._updateValue(col, force); |
| 229 |
}, |
| 230 |
|
| 231 |
_setTimer: function(mover){ |
| 232 |
if(mover.node != this.cursorNode && mover.node != this.hueCursorNode){ return; } |
| 233 |
|
| 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(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(h){ |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
html.style(this.colorUnderlay, "backgroundColor", color.fromHsv(h,100,100).toHex()); |
| 254 |
|
| 255 |
}, |
| 256 |
|
| 257 |
_updateHueCursorNode: function(count, node, e){ |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
if(count !== -1){ |
| 268 |
var y = html.style(this.hueCursorNode, "top"); |
| 269 |
var selCenter = this.PICKER_HUE_SELECTOR_H/2; |
| 270 |
|
| 271 |
|
| 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 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 351 |
|
| 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 |
|
| 368 |
if(h!=this._hue){ |
| 369 |
this._setHue(h); |
| 370 |
} |
| 371 |
}, |
| 372 |
|
| 373 |
_colorInputChange: function(e){ |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
var col, hasit = false; |
| 378 |
switch(e.target){ |
| 379 |
|
| 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(col, fireChange){ |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
var hex = col.toHex(); |
| 413 |
|
| 414 |
this.value = this.valueNode.value = hex; |
| 415 |
|
| 416 |
|
| 417 |
if(fireChange && (!this._timer || this.liveUpdate)){ |
| 418 |
this.onChange(hex); |
| 419 |
} |
| 420 |
}, |
| 421 |
|
| 422 |
_updatePickerLocations: function(col){ |
| 423 |
|
| 424 |
|
| 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 |
|
| 461 |
if(hsv.h != this._hue){ |
| 462 |
this._setHue(hsv.h); |
| 463 |
} |
| 464 |
|
| 465 |
}, |
| 466 |
|
| 467 |
_updateColorInputs: function( col){ |
| 468 |
|
| 469 |
|
| 470 |
|
| 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)); |
| 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(evt){ |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 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(evt){ |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
var satSelCenterH = this.PICKER_SAT_SELECTOR_H/2; |
| 526 |
var satSelCenterW = this.PICKER_SAT_SELECTOR_W/2; |
| 527 |
|
| 528 |
|
| 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(e){ |
| 555 |
|
| 556 |
|
| 557 |
}, |
| 558 |
|
| 559 |
focus: function(){ |
| 560 |
|
| 561 |
|
| 562 |
if(!this.focused){ |
| 563 |
FocusManager.focus(this.focusNode); |
| 564 |
} |
| 565 |
}, |
| 566 |
|
| 567 |
_stopDrag: function(e){ |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
Event.stop(e); |
| 573 |
}, |
| 574 |
|
| 575 |
destroy: function(){ |
| 576 |
|
| 577 |
|
| 578 |
this.inherited(arguments); |
| 579 |
ArrayUtil.forEach(this._subs, function(sub){ |
| 580 |
Hub.unsubscribe(sub); |
| 581 |
}); |
| 582 |
delete this._subs; |
| 583 |
} |
| 584 |
}); |
| 585 |
}); |