1 if(!dojo._hasResource["dijit.ColorPalette"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dijit.ColorPalette"] = true;
3 dojo.provide("dijit.ColorPalette");
5 dojo.require("dijit._Widget");
6 dojo.require("dijit._Templated");
7 dojo.require("dojo.colors");
8 dojo.require("dojo.i18n");
9 dojo.requireLocalization("dojo", "colors", null, "zh,pt,da,tr,ru,de,sv,ja,he,fi,nb,el,ar,pt-pt,cs,fr,es,nl,ko,zh-tw,pl,it,hu,ROOT");
11 dojo.declare("dijit.ColorPalette",
12 [dijit._Widget, dijit._Templated],
14 // summary: A keyboard accessible color-picking widget
16 // Grid showing various colors, so the user can pick a certain color
17 // Can be used standalone, or as a popup.
20 // | <div dojoType="dijit.ColorPalette"></div>
23 // | var picker = new dijit.ColorPalette({ },srcNode);
24 // | picker.startup();
26 // defaultTimeout: Number
27 // number of milliseconds before a held key or button becomes typematic
30 // timeoutChangeRate: Number
31 // fraction of time used to change the typematic timer between events
32 // 1.0 means that each typematic event fires at defaultTimeout intervals
33 // < 1.0 means that each typematic event fires at an increasing faster rate
34 timeoutChangeRate: 0.90,
37 // Size of grid, either "7x10" or "3x4".
41 // The value of the selected color.
44 //_currentFocus: Integer
45 // Index of the currently focused color.
49 // This is the number of colors horizontally across.
53 /// This is the number of colors vertically down.
57 // This represents the value of the colors.
58 // The first level is a hashmap of the different arrays available
59 // The next two dimensions represent the columns and rows of colors.
62 "7x10": [["white", "seashell", "cornsilk", "lemonchiffon","lightyellow", "palegreen", "paleturquoise", "lightcyan", "lavender", "plum"],
63 ["lightgray", "pink", "bisque", "moccasin", "khaki", "lightgreen", "lightseagreen", "lightskyblue", "cornflowerblue", "violet"],
64 ["silver", "lightcoral", "sandybrown", "orange", "palegoldenrod", "chartreuse", "mediumturquoise", "skyblue", "mediumslateblue","orchid"],
65 ["gray", "red", "orangered", "darkorange", "yellow", "limegreen", "darkseagreen", "royalblue", "slateblue", "mediumorchid"],
66 ["dimgray", "crimson", "chocolate", "coral", "gold", "forestgreen", "seagreen", "blue", "blueviolet", "darkorchid"],
67 ["darkslategray","firebrick","saddlebrown", "sienna", "olive", "green", "darkcyan", "mediumblue","darkslateblue", "darkmagenta" ],
68 ["black", "darkred", "maroon", "brown", "darkolivegreen", "darkgreen", "midnightblue", "navy", "indigo", "purple"]],
70 "3x4": [["white", "lime", "green", "blue"],
71 ["silver", "yellow", "fuchsia", "navy"],
72 ["gray", "red", "purple", "black"]]
77 // This is stores the path to the palette images
79 "7x10": dojo.moduleUrl("dijit", "templates/colors7x10.png"),
80 "3x4": dojo.moduleUrl("dijit", "templates/colors3x4.png")
83 // _paletteCoords: Map
84 // This is a map that is used to calculate the coordinates of the
85 // images that make up the palette.
87 "leftOffset": 3, "topOffset": 3,
88 "cWidth": 20, "cHeight": 20
92 // templatePath: String
93 // Path to the template of this widget.
94 templateString:"<div class=\"dijitInline dijitColorPalette\">\n\t<div class=\"dijitColorPaletteInner\" dojoAttachPoint=\"divNode\" waiRole=\"grid\" tabIndex=\"${tabIndex}\">\n\t\t<img class=\"dijitColorPaletteUnder\" dojoAttachPoint=\"imageNode\" waiRole=\"presentation\">\n\t</div>\t\n</div>\n",
96 // _paletteDims: Object
97 // Size of the supported palettes for alignment purposes.
99 "7x10": {"width": "206px", "height": "145px"},
100 "3x4": {"width": "86px", "height": "64px"}
107 postCreate: function(){
108 // A name has to be given to the colorMap, this needs to be unique per Palette.
109 dojo.mixin(this.divNode.style, this._paletteDims[this.palette]);
110 this.imageNode.setAttribute("src", this._imagePaths[this.palette]);
111 var choices = this._palettes[this.palette];
112 this.domNode.style.position = "relative";
113 this._cellNodes = [];
114 this.colorNames = dojo.i18n.getLocalization("dojo", "colors", this.lang);
115 var url = dojo.moduleUrl("dojo", "resources/blank.gif"),
116 colorObject = new dojo.Color(),
117 coords = this._paletteCoords;
118 for(var row=0; row < choices.length; row++){
119 for(var col=0; col < choices[row].length; col++) {
120 var imgNode = dojo.doc.createElement("img");
122 dojo.addClass(imgNode, "dijitPaletteImg");
123 var color = choices[row][col],
124 colorValue = colorObject.setColor(dojo.Color.named[color]);
125 imgNode.alt = this.colorNames[color];
126 imgNode.color = colorValue.toHex();
127 var imgStyle = imgNode.style;
128 imgStyle.color = imgStyle.backgroundColor = imgNode.color;
129 var cellNode = dojo.doc.createElement("span");
130 cellNode.appendChild(imgNode);
131 dojo.forEach(["Dijitclick", "MouseEnter", "Focus", "Blur"], function(handler) {
132 this.connect(cellNode, "on" + handler.toLowerCase(), "_onCell" + handler);
134 this.divNode.appendChild(cellNode);
135 var cellStyle = cellNode.style;
136 cellStyle.top = coords.topOffset + (row * coords.cHeight) + "px";
137 cellStyle.left = coords.leftOffset + (col * coords.cWidth) + "px";
138 dojo.attr(cellNode, "tabindex", "-1");
139 cellNode.title = this.colorNames[color];
140 dojo.addClass(cellNode, "dijitPaletteCell");
141 dijit.setWaiRole(cellNode, "gridcell");
142 cellNode.index = this._cellNodes.length;
143 this._cellNodes.push(cellNode);
146 this._xDim = choices[0].length;
147 this._yDim = choices.length;
148 this.connect(this.divNode, "onfocus", "_onDivNodeFocus");
150 // Now set all events
151 // The palette itself is navigated to with the tab key on the keyboard
152 // Keyboard navigation within the Palette is with the arrow keys
153 // Spacebar selects the color.
154 // For the up key the index is changed by negative the x dimension.
156 var keyIncrementMap = {
157 UP_ARROW: -this._xDim,
158 // The down key the index is increase by the x dimension.
159 DOWN_ARROW: this._xDim,
160 // Right and left move the index by 1.
164 for(var key in keyIncrementMap){
165 this._connects.push(dijit.typematic.addKeyListener(this.domNode,
166 {keyCode:dojo.keys[key], ctrlKey:false, altKey:false, shiftKey:false},
169 var increment = keyIncrementMap[key];
170 return function(count){ this._navigateByKey(increment, count); };
172 this.timeoutChangeRate, this.defaultTimeout));
178 // Focus this ColorPalette. Puts focus on the first swatch.
182 onChange: function(color){
184 // Callback when a color is selected.
186 // Hex value corresponding to color.
187 // console.debug("Color selected is: "+color);
190 _focusFirst: function(){
191 this._currentFocus = 0;
192 var cellNode = this._cellNodes[this._currentFocus];
193 window.setTimeout(function(){dijit.focus(cellNode)}, 0);
196 _onDivNodeFocus: function(evt){
197 // focus bubbles on Firefox 2, so just make sure that focus has really
198 // gone to the container
199 if(evt.target === this.divNode){
204 _onFocus: function(){
205 // while focus is on the palette, set its tabindex to -1 so that on a
206 // shift-tab from a cell, the container is not in the tab order
207 dojo.attr(this.divNode, "tabindex", "-1");
211 this._removeCellHighlight(this._currentFocus);
212 // when focus leaves the palette, restore its tabindex, since it was
213 // modified by _onFocus().
214 dojo.attr(this.divNode, "tabindex", this.tabIndex);
217 _onCellDijitclick: function(/*Event*/ evt){
219 // Handler for click, enter key & space key. Selects the color.
222 var target = evt.currentTarget;
223 if (this._currentFocus != target.index){
224 this._currentFocus = target.index;
225 window.setTimeout(function(){dijit.focus(target)}, 0);
227 this._selectColor(target);
231 _onCellMouseEnter: function(/*Event*/ evt){
233 // Handler for onMouseOver. Put focus on the color under the mouse.
236 var target = evt.currentTarget;
237 window.setTimeout(function(){dijit.focus(target)}, 0);
240 _onCellFocus: function(/*Event*/ evt){
242 // Handler for onFocus. Removes highlight of
243 // the color that just lost focus, and highlights
247 this._removeCellHighlight(this._currentFocus);
248 this._currentFocus = evt.currentTarget.index;
249 dojo.addClass(evt.currentTarget, "dijitPaletteCellHighlight");
252 _onCellBlur: function(/*Event*/ evt){
254 // needed for Firefox 2 on Mac OS X
255 this._removeCellHighlight(this._currentFocus);
258 _removeCellHighlight: function(index){
259 dojo.removeClass(this._cellNodes[index], "dijitPaletteCellHighlight");
262 _selectColor: function(selectNode){
264 // This selects a color. It triggers the onChange event
266 // The area node that covers the color being selected.
267 var img = selectNode.getElementsByTagName("img")[0];
268 this.onChange(this.value = img.color);
271 _navigateByKey: function(increment, typeCount){
273 // This is the callback for typematic.
274 // It changes the focus and the highlighed color.
276 // How much the key is navigated.
278 // How many times typematic has fired.
280 // typecount == -1 means the key is released.
281 if(typeCount == -1){ return; }
283 var newFocusIndex = this._currentFocus + increment;
284 if(newFocusIndex < this._cellNodes.length && newFocusIndex > -1)
286 var focusNode = this._cellNodes[newFocusIndex];