2150 |
mathias |
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");
|
|
|
4 |
|
|
|
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, "ko,zh,ja,zh-tw,ru,it,hu,ROOT,fr,pt,pl,es,de,cs");
|
|
|
10 |
|
|
|
11 |
dojo.declare(
|
|
|
12 |
"dijit.ColorPalette",
|
|
|
13 |
[dijit._Widget, dijit._Templated],
|
|
|
14 |
{
|
|
|
15 |
// summary
|
|
|
16 |
// Grid showing various colors, so the user can pick a certain color
|
|
|
17 |
|
|
|
18 |
// defaultTimeout: Number
|
|
|
19 |
// number of milliseconds before a held key or button becomes typematic
|
|
|
20 |
defaultTimeout: 500,
|
|
|
21 |
|
|
|
22 |
// timeoutChangeRate: Number
|
|
|
23 |
// fraction of time used to change the typematic timer between events
|
|
|
24 |
// 1.0 means that each typematic event fires at defaultTimeout intervals
|
|
|
25 |
// < 1.0 means that each typematic event fires at an increasing faster rate
|
|
|
26 |
timeoutChangeRate: 0.90,
|
|
|
27 |
|
|
|
28 |
// palette: String
|
|
|
29 |
// Size of grid, either "7x10" or "3x4".
|
|
|
30 |
palette: "7x10",
|
|
|
31 |
|
|
|
32 |
//_value: String
|
|
|
33 |
// The value of the selected color.
|
|
|
34 |
value: null,
|
|
|
35 |
|
|
|
36 |
//_currentFocus: Integer
|
|
|
37 |
// Index of the currently focused color.
|
|
|
38 |
_currentFocus: 0,
|
|
|
39 |
|
|
|
40 |
// _xDim: Integer
|
|
|
41 |
// This is the number of colors horizontally across.
|
|
|
42 |
_xDim: null,
|
|
|
43 |
|
|
|
44 |
// _yDim: Integer
|
|
|
45 |
/// This is the number of colors vertically down.
|
|
|
46 |
_yDim: null,
|
|
|
47 |
|
|
|
48 |
// _palettes: Map
|
|
|
49 |
// This represents the value of the colors.
|
|
|
50 |
// The first level is a hashmap of the different arrays available
|
|
|
51 |
// The next two dimensions represent the columns and rows of colors.
|
|
|
52 |
_palettes: {
|
|
|
53 |
|
|
|
54 |
"7x10": [["white", "seashell", "cornsilk", "lemonchiffon","lightyellow", "palegreen", "paleturquoise", "lightcyan", "lavender", "plum"],
|
|
|
55 |
["lightgray", "pink", "bisque", "moccasin", "khaki", "lightgreen", "lightseagreen", "lightskyblue", "cornflowerblue", "violet"],
|
|
|
56 |
["silver", "lightcoral", "sandybrown", "orange", "palegoldenrod", "chartreuse", "mediumturquoise", "skyblue", "mediumslateblue","orchid"],
|
|
|
57 |
["gray", "red", "orangered", "darkorange", "yellow", "limegreen", "darkseagreen", "royalblue", "slateblue", "mediumorchid"],
|
|
|
58 |
["dimgray", "crimson", "chocolate", "coral", "gold", "forestgreen", "seagreen", "blue", "blueviolet", "darkorchid"],
|
|
|
59 |
["darkslategray","firebrick","saddlebrown", "sienna", "olive", "green", "darkcyan", "mediumblue","darkslateblue", "darkmagenta" ],
|
|
|
60 |
["black", "darkred", "maroon", "brown", "darkolivegreen", "darkgreen", "midnightblue", "navy", "indigo", "purple"]],
|
|
|
61 |
|
|
|
62 |
"3x4": [["white", "lime", "green", "blue"],
|
|
|
63 |
["silver", "yellow", "fuchsia", "navy"],
|
|
|
64 |
["gray", "red", "purple", "black"]]
|
|
|
65 |
|
|
|
66 |
},
|
|
|
67 |
|
|
|
68 |
// _imagePaths: Map
|
|
|
69 |
// This is stores the path to the palette images
|
|
|
70 |
_imagePaths: {
|
|
|
71 |
"7x10": dojo.moduleUrl("dijit", "templates/colors7x10.png"),
|
|
|
72 |
"3x4": dojo.moduleUrl("dijit", "templates/colors3x4.png")
|
|
|
73 |
},
|
|
|
74 |
|
|
|
75 |
// _paletteCoords: Map
|
|
|
76 |
// This is a map that is used to calculate the coordinates of the
|
|
|
77 |
// images that make up the palette.
|
|
|
78 |
_paletteCoords: {
|
|
|
79 |
"leftOffset": 4, "topOffset": 4,
|
|
|
80 |
"cWidth": 20, "cHeight": 20
|
|
|
81 |
|
|
|
82 |
},
|
|
|
83 |
|
|
|
84 |
// templatePath: String
|
|
|
85 |
// Path to the template of this widget.
|
|
|
86 |
templateString:"<div class=\"dijitInline dijitColorPalette\">\n\t<div class=\"dijitColorPaletteInner\" dojoAttachPoint=\"divNode\" waiRole=\"grid\" tabIndex=\"-1\">\n\t\t<img class=\"dijitColorPaletteUnder\" dojoAttachPoint=\"imageNode\" waiRole=\"presentation\">\n\t</div>\t\n</div>\n",
|
|
|
87 |
|
|
|
88 |
// _paletteDims: Object
|
|
|
89 |
// Size of the supported palettes for alignment purposes.
|
|
|
90 |
_paletteDims: {
|
|
|
91 |
"7x10": {"width": "206px", "height": "145px"},
|
|
|
92 |
"3x4": {"width": "86px", "height": "64px"}
|
|
|
93 |
},
|
|
|
94 |
|
|
|
95 |
|
|
|
96 |
postCreate: function(){
|
|
|
97 |
// A name has to be given to the colorMap, this needs to be unique per Palette.
|
|
|
98 |
dojo.mixin(this.divNode.style, this._paletteDims[this.palette]);
|
|
|
99 |
this.imageNode.setAttribute("src", this._imagePaths[this.palette]);
|
|
|
100 |
var choices = this._palettes[this.palette];
|
|
|
101 |
this.domNode.style.position = "relative";
|
|
|
102 |
this._highlightNodes = [];
|
|
|
103 |
this.colorNames = dojo.i18n.getLocalization("dojo", "colors", this.lang);
|
|
|
104 |
var url= dojo.moduleUrl("dijit", "templates/blank.gif");
|
|
|
105 |
var colorObject = new dojo.Color(),
|
|
|
106 |
coords = this._paletteCoords;
|
|
|
107 |
for(var row=0; row < choices.length; row++){
|
|
|
108 |
for(var col=0; col < choices[row].length; col++) {
|
|
|
109 |
var highlightNode = document.createElement("img");
|
|
|
110 |
highlightNode.src = url;
|
|
|
111 |
dojo.addClass(highlightNode, "dijitPaletteImg");
|
|
|
112 |
var color = choices[row][col],
|
|
|
113 |
colorValue = colorObject.setColor(dojo.Color.named[color]);
|
|
|
114 |
highlightNode.alt = this.colorNames[color];
|
|
|
115 |
highlightNode.color = colorValue.toHex();
|
|
|
116 |
var highlightStyle = highlightNode.style;
|
|
|
117 |
highlightStyle.color = highlightStyle.backgroundColor = highlightNode.color;
|
|
|
118 |
dojo.forEach(["Dijitclick", "MouseOut", "MouseOver", "Blur", "Focus"], function(handler) {
|
|
|
119 |
this.connect(highlightNode, "on" + handler.toLowerCase(), "_onColor" + handler);
|
|
|
120 |
}, this);
|
|
|
121 |
this.divNode.appendChild(highlightNode);
|
|
|
122 |
highlightStyle.top = coords.topOffset + (row * coords.cHeight) + "px";
|
|
|
123 |
highlightStyle.left = coords.leftOffset + (col * coords.cWidth) + "px";
|
|
|
124 |
highlightNode.setAttribute("tabIndex", "-1");
|
|
|
125 |
highlightNode.title = this.colorNames[color];
|
|
|
126 |
dijit.setWaiRole(highlightNode, "gridcell");
|
|
|
127 |
highlightNode.index = this._highlightNodes.length;
|
|
|
128 |
this._highlightNodes.push(highlightNode);
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
this._highlightNodes[this._currentFocus].tabIndex = 0;
|
|
|
132 |
this._xDim = choices[0].length;
|
|
|
133 |
this._yDim = choices.length;
|
|
|
134 |
|
|
|
135 |
// Now set all events
|
|
|
136 |
// The palette itself is navigated to with the tab key on the keyboard
|
|
|
137 |
// Keyboard navigation within the Palette is with the arrow keys
|
|
|
138 |
// Spacebar selects the color.
|
|
|
139 |
// For the up key the index is changed by negative the x dimension.
|
|
|
140 |
|
|
|
141 |
var keyIncrementMap = {
|
|
|
142 |
UP_ARROW: -this._xDim,
|
|
|
143 |
// The down key the index is increase by the x dimension.
|
|
|
144 |
DOWN_ARROW: this._xDim,
|
|
|
145 |
// Right and left move the index by 1.
|
|
|
146 |
RIGHT_ARROW: 1,
|
|
|
147 |
LEFT_ARROW: -1
|
|
|
148 |
};
|
|
|
149 |
for(var key in keyIncrementMap){
|
|
|
150 |
this._connects.push(dijit.typematic.addKeyListener(this.domNode,
|
|
|
151 |
{keyCode:dojo.keys[key], ctrlKey:false, altKey:false, shiftKey:false},
|
|
|
152 |
this,
|
|
|
153 |
function(){
|
|
|
154 |
var increment = keyIncrementMap[key];
|
|
|
155 |
return function(count){ this._navigateByKey(increment, count); };
|
|
|
156 |
}(),
|
|
|
157 |
this.timeoutChangeRate, this.defaultTimeout));
|
|
|
158 |
}
|
|
|
159 |
},
|
|
|
160 |
|
|
|
161 |
focus: function(){
|
|
|
162 |
// summary:
|
|
|
163 |
// Focus this ColorPalette.
|
|
|
164 |
dijit.focus(this._highlightNodes[this._currentFocus]);
|
|
|
165 |
},
|
|
|
166 |
|
|
|
167 |
onChange: function(color){
|
|
|
168 |
// summary:
|
|
|
169 |
// Callback when a color is selected.
|
|
|
170 |
// color: String
|
|
|
171 |
// Hex value corresponding to color.
|
|
|
172 |
// console.debug("Color selected is: "+color);
|
|
|
173 |
},
|
|
|
174 |
|
|
|
175 |
_onColorDijitclick: function(/*Event*/ evt){
|
|
|
176 |
// summary:
|
|
|
177 |
// Handler for click, enter key & space key. Selects the color.
|
|
|
178 |
// evt:
|
|
|
179 |
// The event.
|
|
|
180 |
var target = evt.currentTarget;
|
|
|
181 |
if (this._currentFocus != target.index){
|
|
|
182 |
this._currentFocus = target.index;
|
|
|
183 |
dijit.focus(target);
|
|
|
184 |
}
|
|
|
185 |
this._selectColor(target);
|
|
|
186 |
dojo.stopEvent(evt);
|
|
|
187 |
},
|
|
|
188 |
|
|
|
189 |
_onColorMouseOut: function(/*Event*/ evt){
|
|
|
190 |
// summary:
|
|
|
191 |
// Handler for onMouseOut. Removes highlight.
|
|
|
192 |
// evt:
|
|
|
193 |
// The mouse event.
|
|
|
194 |
dojo.removeClass(evt.currentTarget, "dijitPaletteImgHighlight");
|
|
|
195 |
},
|
|
|
196 |
|
|
|
197 |
_onColorMouseOver: function(/*Event*/ evt){
|
|
|
198 |
// summary:
|
|
|
199 |
// Handler for onMouseOver. Highlights the color.
|
|
|
200 |
// evt:
|
|
|
201 |
// The mouse event.
|
|
|
202 |
var target = evt.currentTarget;
|
|
|
203 |
target.tabIndex = 0;
|
|
|
204 |
target.focus();
|
|
|
205 |
},
|
|
|
206 |
|
|
|
207 |
_onColorBlur: function(/*Event*/ evt){
|
|
|
208 |
// summary:
|
|
|
209 |
// Handler for onBlur. Removes highlight and sets
|
|
|
210 |
// the first color as the palette's tab point.
|
|
|
211 |
// evt:
|
|
|
212 |
// The blur event.
|
|
|
213 |
dojo.removeClass(evt.currentTarget, "dijitPaletteImgHighlight");
|
|
|
214 |
evt.currentTarget.tabIndex = -1;
|
|
|
215 |
this._currentFocus = 0;
|
|
|
216 |
this._highlightNodes[0].tabIndex = 0;
|
|
|
217 |
},
|
|
|
218 |
|
|
|
219 |
_onColorFocus: function(/*Event*/ evt){
|
|
|
220 |
// summary:
|
|
|
221 |
// Handler for onFocus. Highlights the color.
|
|
|
222 |
// evt:
|
|
|
223 |
// The focus event.
|
|
|
224 |
if(this._currentFocus != evt.currentTarget.index){
|
|
|
225 |
this._highlightNodes[this._currentFocus].tabIndex = -1;
|
|
|
226 |
}
|
|
|
227 |
this._currentFocus = evt.currentTarget.index;
|
|
|
228 |
dojo.addClass(evt.currentTarget, "dijitPaletteImgHighlight");
|
|
|
229 |
|
|
|
230 |
},
|
|
|
231 |
|
|
|
232 |
_selectColor: function(selectNode){
|
|
|
233 |
// summary:
|
|
|
234 |
// This selects a color. It triggers the onChange event
|
|
|
235 |
// area:
|
|
|
236 |
// The area node that covers the color being selected.
|
|
|
237 |
this.onChange(this.value = selectNode.color);
|
|
|
238 |
},
|
|
|
239 |
|
|
|
240 |
_navigateByKey: function(increment, typeCount){
|
|
|
241 |
// summary:we
|
|
|
242 |
// This is the callback for typematic.
|
|
|
243 |
// It changes the focus and the highlighed color.
|
|
|
244 |
// increment:
|
|
|
245 |
// How much the key is navigated.
|
|
|
246 |
// typeCount:
|
|
|
247 |
// How many times typematic has fired.
|
|
|
248 |
|
|
|
249 |
// typecount == -1 means the key is released.
|
|
|
250 |
if(typeCount == -1){ return; }
|
|
|
251 |
|
|
|
252 |
var newFocusIndex = this._currentFocus + increment;
|
|
|
253 |
if(newFocusIndex < this._highlightNodes.length && newFocusIndex > -1)
|
|
|
254 |
{
|
|
|
255 |
var focusNode = this._highlightNodes[newFocusIndex];
|
|
|
256 |
focusNode.tabIndex = 0;
|
|
|
257 |
focusNode.focus();
|
|
|
258 |
}
|
|
|
259 |
}
|
|
|
260 |
});
|
|
|
261 |
|
|
|
262 |
}
|