192ColorPicker.prototype.selectColorAtIndex = function(index) {
193 index = Math.max(Math.min(this._container.childNodes.length - 1, index), 0);
194 this._container.childNodes[index].focus();
195};
196
197ColorPicker.prototype._handleMouseMove = function(event) {
198 if (event.target.classList.contains("color-swatch"))
199 event.target.focus();
200};
201
202ColorPicker.prototype._handleMouseDown = function(event) {
203 // Prevent blur.
204 if (event.target.classList.contains("color-swatch"))
205 event.preventDefault();
206};
207
208ColorPicker.prototype._handleKeyDown = function(event) {
209 var key = event.keyIdentifier;
210 if (key === "U+001B") // ESC
211 handleCancel();
212 else if (key == "Left" || key == "Up" || key == "Right" || key == "Down") {
213 var selectedElement = document.activeElement;
214 var index = 0;
215 if (selectedElement.classList.contains("other-color")) {
216 if (key != "Right" && key != "Up")
217 return;
218 index = this._container.childNodes.length - 1;
219 } else if (selectedElement.classList.contains("color-swatch")) {
220 index = parseInt(selectedElement.dataset.index, 10);
221 switch (key) {
222 case "Left":
223 index--;
224 break;
225 case "Right":
226 index++;
227 break;
228 case "Up":
229 index -= SwatchesPerRow;
230 break;
231 case "Down":
232 index += SwatchesPerRow;
233 break;
234 }
235 if (index > this._container.childNodes.length - 1) {
236 this._otherButton.focus();
237 return;
238 }
239 }
240 this.selectColorAtIndex(index);
241 }
242 event.preventDefault();
243};
244