| 1 |
// Copyright 2009 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Character Picker widget for picking any Unicode character. |
| 17 |
* |
| 18 |
* @see ../demos/charpicker.html |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.provide('goog.ui.CharPicker'); |
| 22 |
|
| 23 |
goog.require('goog.a11y.aria'); |
| 24 |
goog.require('goog.a11y.aria.State'); |
| 25 |
goog.require('goog.array'); |
| 26 |
goog.require('goog.asserts'); |
| 27 |
goog.require('goog.dom'); |
| 28 |
goog.require('goog.dom.TagName'); |
| 29 |
goog.require('goog.dom.classlist'); |
| 30 |
goog.require('goog.events'); |
| 31 |
goog.require('goog.events.Event'); |
| 32 |
goog.require('goog.events.EventHandler'); |
| 33 |
goog.require('goog.events.EventType'); |
| 34 |
goog.require('goog.events.InputHandler'); |
| 35 |
goog.require('goog.events.KeyCodes'); |
| 36 |
goog.require('goog.events.KeyHandler'); |
| 37 |
goog.require('goog.i18n.CharListDecompressor'); |
| 38 |
goog.require('goog.i18n.CharPickerData'); |
| 39 |
goog.require('goog.i18n.uChar'); |
| 40 |
goog.require('goog.i18n.uChar.NameFetcher'); |
| 41 |
goog.require('goog.structs.Set'); |
| 42 |
goog.require('goog.style'); |
| 43 |
goog.require('goog.ui.Button'); |
| 44 |
goog.require('goog.ui.Component'); |
| 45 |
goog.require('goog.ui.ContainerScroller'); |
| 46 |
goog.require('goog.ui.FlatButtonRenderer'); |
| 47 |
goog.require('goog.ui.HoverCard'); |
| 48 |
goog.require('goog.ui.LabelInput'); |
| 49 |
goog.require('goog.ui.Menu'); |
| 50 |
goog.require('goog.ui.MenuButton'); |
| 51 |
goog.require('goog.ui.MenuItem'); |
| 52 |
goog.require('goog.ui.Tooltip'); |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
/** |
| 57 |
* Character Picker Class. This widget can be used to pick any Unicode |
| 58 |
* character by traversing a category-subcategory structure or by inputing its |
| 59 |
* hex value. |
| 60 |
* |
| 61 |
* See charpicker.html demo for example usage. |
| 62 |
* @param {goog.i18n.CharPickerData} charPickerData Category names and charlist. |
| 63 |
* @param {!goog.i18n.uChar.NameFetcher} charNameFetcher Object which fetches |
| 64 |
* the names of the characters that are shown in the widget. These names |
| 65 |
* may be stored locally or come from an external source. |
| 66 |
* @param {Array<string>=} opt_recents List of characters to be displayed in |
| 67 |
* resently selected characters area. |
| 68 |
* @param {number=} opt_initCategory Sequence number of initial category. |
| 69 |
* @param {number=} opt_initSubcategory Sequence number of initial subcategory. |
| 70 |
* @param {number=} opt_rowCount Number of rows in the grid. |
| 71 |
* @param {number=} opt_columnCount Number of columns in the grid. |
| 72 |
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper. |
| 73 |
* @constructor |
| 74 |
* @extends {goog.ui.Component} |
| 75 |
* @final |
| 76 |
*/ |
| 77 |
goog.ui.CharPicker = function( |
| 78 |
charPickerData, charNameFetcher, opt_recents, opt_initCategory, |
| 79 |
opt_initSubcategory, opt_rowCount, opt_columnCount, opt_domHelper) { |
| 80 |
goog.ui.Component.call(this, opt_domHelper); |
| 81 |
|
| 82 |
/** |
| 83 |
* Object used to retrieve character names. |
| 84 |
* @type {!goog.i18n.uChar.NameFetcher} |
| 85 |
* @private |
| 86 |
*/ |
| 87 |
this.charNameFetcher_ = charNameFetcher; |
| 88 |
|
| 89 |
/** |
| 90 |
* Object containing character lists and category names. |
| 91 |
* @type {goog.i18n.CharPickerData} |
| 92 |
* @private |
| 93 |
*/ |
| 94 |
this.data_ = charPickerData; |
| 95 |
|
| 96 |
/** |
| 97 |
* The category number to be used on widget init. |
| 98 |
* @type {number} |
| 99 |
* @private |
| 100 |
*/ |
| 101 |
this.initCategory_ = opt_initCategory || 0; |
| 102 |
|
| 103 |
/** |
| 104 |
* The subcategory number to be used on widget init. |
| 105 |
* @type {number} |
| 106 |
* @private |
| 107 |
*/ |
| 108 |
this.initSubcategory_ = opt_initSubcategory || 0; |
| 109 |
|
| 110 |
/** |
| 111 |
* Number of columns in the grid. |
| 112 |
* @type {number} |
| 113 |
* @private |
| 114 |
*/ |
| 115 |
this.columnCount_ = opt_columnCount || 10; |
| 116 |
|
| 117 |
/** |
| 118 |
* Number of entries to be added to the grid. |
| 119 |
* @type {number} |
| 120 |
* @private |
| 121 |
*/ |
| 122 |
this.gridsize_ = (opt_rowCount || 10) * this.columnCount_; |
| 123 |
|
| 124 |
/** |
| 125 |
* Number of the recently selected characters displayed. |
| 126 |
* @type {number} |
| 127 |
* @private |
| 128 |
*/ |
| 129 |
this.recentwidth_ = this.columnCount_ + 1; |
| 130 |
|
| 131 |
/** |
| 132 |
* List of recently used characters. |
| 133 |
* @type {Array<string>} |
| 134 |
* @private |
| 135 |
*/ |
| 136 |
this.recents_ = opt_recents || []; |
| 137 |
|
| 138 |
/** |
| 139 |
* Handler for events. |
| 140 |
* @type {goog.events.EventHandler<!goog.ui.CharPicker>} |
| 141 |
* @private |
| 142 |
*/ |
| 143 |
this.eventHandler_ = new goog.events.EventHandler(this); |
| 144 |
|
| 145 |
/** |
| 146 |
* Decompressor used to get the list of characters from a base88 encoded |
| 147 |
* character list. |
| 148 |
* @type {Object} |
| 149 |
* @private |
| 150 |
*/ |
| 151 |
this.decompressor_ = new goog.i18n.CharListDecompressor(); |
| 152 |
}; |
| 153 |
goog.inherits(goog.ui.CharPicker, goog.ui.Component); |
| 154 |
|
| 155 |
|
| 156 |
/** |
| 157 |
* The last selected character. |
| 158 |
* @type {?string} |
| 159 |
* @private |
| 160 |
*/ |
| 161 |
goog.ui.CharPicker.prototype.selectedChar_ = null; |
| 162 |
|
| 163 |
|
| 164 |
/** |
| 165 |
* Set of formatting characters whose display need to be swapped with nbsp |
| 166 |
* to prevent layout issues. |
| 167 |
* @type {goog.structs.Set} |
| 168 |
* @private |
| 169 |
*/ |
| 170 |
goog.ui.CharPicker.prototype.layoutAlteringChars_ = null; |
| 171 |
|
| 172 |
|
| 173 |
/** |
| 174 |
* The top category menu. |
| 175 |
* @type {goog.ui.Menu} |
| 176 |
* @private |
| 177 |
*/ |
| 178 |
goog.ui.CharPicker.prototype.menu_ = null; |
| 179 |
|
| 180 |
|
| 181 |
/** |
| 182 |
* The top category menu button. |
| 183 |
* @type {goog.ui.MenuButton} |
| 184 |
* @private |
| 185 |
*/ |
| 186 |
goog.ui.CharPicker.prototype.menubutton_ = null; |
| 187 |
|
| 188 |
|
| 189 |
/** |
| 190 |
* The subcategory menu. |
| 191 |
* @type {goog.ui.Menu} |
| 192 |
* @private |
| 193 |
*/ |
| 194 |
goog.ui.CharPicker.prototype.submenu_ = null; |
| 195 |
|
| 196 |
|
| 197 |
/** |
| 198 |
* The subcategory menu button. |
| 199 |
* @type {goog.ui.MenuButton} |
| 200 |
* @private |
| 201 |
*/ |
| 202 |
goog.ui.CharPicker.prototype.submenubutton_ = null; |
| 203 |
|
| 204 |
|
| 205 |
/** @type {number} */ |
| 206 |
goog.ui.CharPicker.prototype.itempos; |
| 207 |
|
| 208 |
|
| 209 |
/** @type {!Array<string>} */ |
| 210 |
goog.ui.CharPicker.prototype.items; |
| 211 |
|
| 212 |
|
| 213 |
/** @private {!goog.events.KeyHandler} */ |
| 214 |
goog.ui.CharPicker.prototype.keyHandler_; |
| 215 |
|
| 216 |
|
| 217 |
/** |
| 218 |
* Category index used to index the data tables. |
| 219 |
* @type {number} |
| 220 |
*/ |
| 221 |
goog.ui.CharPicker.prototype.category; |
| 222 |
|
| 223 |
|
| 224 |
/** @private {Element} */ |
| 225 |
goog.ui.CharPicker.prototype.stick_ = null; |
| 226 |
|
| 227 |
|
| 228 |
/** |
| 229 |
* The element representing the number of rows visible in the grid. |
| 230 |
* This along with goog.ui.CharPicker.stick_ would help to create a scrollbar |
| 231 |
* of right size. |
| 232 |
* @type {HTMLElement} |
| 233 |
* @private |
| 234 |
*/ |
| 235 |
goog.ui.CharPicker.prototype.stickwrap_ = null; |
| 236 |
|
| 237 |
|
| 238 |
/** |
| 239 |
* The component containing all the buttons for each character in display. |
| 240 |
* @type {goog.ui.Component} |
| 241 |
* @private |
| 242 |
*/ |
| 243 |
goog.ui.CharPicker.prototype.grid_ = null; |
| 244 |
|
| 245 |
|
| 246 |
/** |
| 247 |
* The component used for extra information about the character set displayed. |
| 248 |
* @type {goog.ui.Component} |
| 249 |
* @private |
| 250 |
*/ |
| 251 |
goog.ui.CharPicker.prototype.notice_ = null; |
| 252 |
|
| 253 |
|
| 254 |
/** |
| 255 |
* Grid displaying recently selected characters. |
| 256 |
* @type {goog.ui.Component} |
| 257 |
* @private |
| 258 |
*/ |
| 259 |
goog.ui.CharPicker.prototype.recentgrid_ = null; |
| 260 |
|
| 261 |
|
| 262 |
/** |
| 263 |
* Input field for entering the hex value of the character. |
| 264 |
* @type {goog.ui.Component} |
| 265 |
* @private |
| 266 |
*/ |
| 267 |
goog.ui.CharPicker.prototype.input_ = null; |
| 268 |
|
| 269 |
|
| 270 |
/** |
| 271 |
* OK button for entering hex value of the character. |
| 272 |
* @private {goog.ui.Button} |
| 273 |
*/ |
| 274 |
goog.ui.CharPicker.prototype.okbutton_ = null; |
| 275 |
|
| 276 |
|
| 277 |
/** |
| 278 |
* Element displaying character name in preview. |
| 279 |
* @type {Element} |
| 280 |
* @private |
| 281 |
*/ |
| 282 |
goog.ui.CharPicker.prototype.charNameEl_ = null; |
| 283 |
|
| 284 |
|
| 285 |
/** |
| 286 |
* Element displaying character in preview. |
| 287 |
* @type {Element} |
| 288 |
* @private |
| 289 |
*/ |
| 290 |
goog.ui.CharPicker.prototype.zoomEl_ = null; |
| 291 |
|
| 292 |
|
| 293 |
/** |
| 294 |
* Element displaying character number (codepoint) in preview. |
| 295 |
* @type {Element} |
| 296 |
* @private |
| 297 |
*/ |
| 298 |
goog.ui.CharPicker.prototype.unicodeEl_ = null; |
| 299 |
|
| 300 |
|
| 301 |
/** |
| 302 |
* Hover card for displaying the preview of a character. |
| 303 |
* Preview would contain character in large size and its U+ notation. It would |
| 304 |
* also display the name, if available. |
| 305 |
* @type {goog.ui.HoverCard} |
| 306 |
* @private |
| 307 |
*/ |
| 308 |
goog.ui.CharPicker.prototype.hc_ = null; |
| 309 |
|
| 310 |
|
| 311 |
/** |
| 312 |
* Gets the last selected character. |
| 313 |
* @return {?string} The last selected character. |
| 314 |
*/ |
| 315 |
goog.ui.CharPicker.prototype.getSelectedChar = function() { |
| 316 |
return this.selectedChar_; |
| 317 |
}; |
| 318 |
|
| 319 |
|
| 320 |
/** |
| 321 |
* Gets the list of characters user selected recently. |
| 322 |
* @return {Array<string>} The recent character list. |
| 323 |
*/ |
| 324 |
goog.ui.CharPicker.prototype.getRecentChars = function() { |
| 325 |
return this.recents_; |
| 326 |
}; |
| 327 |
|
| 328 |
|
| 329 |
/** @override */ |
| 330 |
goog.ui.CharPicker.prototype.createDom = function() { |
| 331 |
goog.ui.CharPicker.superClass_.createDom.call(this); |
| 332 |
|
| 333 |
this.decorateInternal( |
| 334 |
this.getDomHelper().createElement(goog.dom.TagName.DIV)); |
| 335 |
}; |
| 336 |
|
| 337 |
|
| 338 |
/** @override */ |
| 339 |
goog.ui.CharPicker.prototype.disposeInternal = function() { |
| 340 |
goog.dispose(this.hc_); |
| 341 |
this.hc_ = null; |
| 342 |
goog.dispose(this.eventHandler_); |
| 343 |
this.eventHandler_ = null; |
| 344 |
goog.ui.CharPicker.superClass_.disposeInternal.call(this); |
| 345 |
}; |
| 346 |
|
| 347 |
|
| 348 |
/** @override */ |
| 349 |
goog.ui.CharPicker.prototype.decorateInternal = function(element) { |
| 350 |
goog.ui.CharPicker.superClass_.decorateInternal.call(this, element); |
| 351 |
|
| 352 |
// The chars below cause layout disruption or too narrow to hover: |
| 353 |
// \u0020, \u00AD, \u2000 - \u200f, \u2028 - \u202f, \u3000, \ufeff |
| 354 |
var chrs = this.decompressor_.toCharList(':2%C^O80V1H2s2G40Q%s0'); |
| 355 |
this.layoutAlteringChars_ = new goog.structs.Set(chrs); |
| 356 |
|
| 357 |
this.menu_ = new goog.ui.Menu(this.getDomHelper()); |
| 358 |
|
| 359 |
var categories = this.data_.categories; |
| 360 |
for (var i = 0; i < this.data_.categories.length; i++) { |
| 361 |
this.menu_.addChild(this.createMenuItem_(i, categories[i]), true); |
| 362 |
} |
| 363 |
|
| 364 |
this.menubutton_ = new goog.ui.MenuButton( |
| 365 |
'Category Menu', this.menu_, |
| 366 |
/* opt_renderer */ undefined, this.getDomHelper()); |
| 367 |
this.addChild(this.menubutton_, true); |
| 368 |
|
| 369 |
this.submenu_ = new goog.ui.Menu(this.getDomHelper()); |
| 370 |
|
| 371 |
this.submenubutton_ = new goog.ui.MenuButton( |
| 372 |
'Subcategory Menu', this.submenu_, /* opt_renderer */ undefined, |
| 373 |
this.getDomHelper()); |
| 374 |
this.addChild(this.submenubutton_, true); |
| 375 |
|
| 376 |
// The containing component for grid component and the scroller. |
| 377 |
var gridcontainer = new goog.ui.Component(this.getDomHelper()); |
| 378 |
this.addChild(gridcontainer, true); |
| 379 |
|
| 380 |
var stickwrap = new goog.ui.Component(this.getDomHelper()); |
| 381 |
gridcontainer.addChild(stickwrap, true); |
| 382 |
this.stickwrap_ = /** @type {!HTMLElement} */ (stickwrap.getElement()); |
| 383 |
|
| 384 |
var stick = new goog.ui.Component(this.getDomHelper()); |
| 385 |
stickwrap.addChild(stick, true); |
| 386 |
this.stick_ = stick.getElement(); |
| 387 |
|
| 388 |
this.grid_ = new goog.ui.Component(this.getDomHelper()); |
| 389 |
gridcontainer.addChild(this.grid_, true); |
| 390 |
|
| 391 |
this.notice_ = new goog.ui.Component(this.getDomHelper()); |
| 392 |
this.notice_.setElementInternal( |
| 393 |
this.getDomHelper().createDom(goog.dom.TagName.DIV)); |
| 394 |
this.addChild(this.notice_, true); |
| 395 |
|
| 396 |
// The component used for displaying 'Recent Selections' label. |
| 397 |
/** |
| 398 |
* @desc The text label above the list of recently selected characters. |
| 399 |
*/ |
| 400 |
var MSG_CHAR_PICKER_RECENT_SELECTIONS = goog.getMsg('Recent Selections:'); |
| 401 |
var recenttext = new goog.ui.Component(this.getDomHelper()); |
| 402 |
recenttext.setElementInternal( |
| 403 |
this.getDomHelper().createDom( |
| 404 |
goog.dom.TagName.SPAN, null, MSG_CHAR_PICKER_RECENT_SELECTIONS)); |
| 405 |
this.addChild(recenttext, true); |
| 406 |
|
| 407 |
this.recentgrid_ = new goog.ui.Component(this.getDomHelper()); |
| 408 |
this.addChild(this.recentgrid_, true); |
| 409 |
|
| 410 |
// The component used for displaying 'U+'. |
| 411 |
var uplus = new goog.ui.Component(this.getDomHelper()); |
| 412 |
uplus.setElementInternal( |
| 413 |
this.getDomHelper().createDom(goog.dom.TagName.SPAN, null, 'U+')); |
| 414 |
this.addChild(uplus, true); |
| 415 |
|
| 416 |
/** |
| 417 |
* @desc The text inside the input box to specify the hex code of a character. |
| 418 |
*/ |
| 419 |
var MSG_CHAR_PICKER_HEX_INPUT = goog.getMsg('Hex Input'); |
| 420 |
this.input_ = |
| 421 |
new goog.ui.LabelInput(MSG_CHAR_PICKER_HEX_INPUT, this.getDomHelper()); |
| 422 |
this.addChild(this.input_, true); |
| 423 |
|
| 424 |
this.okbutton_ = new goog.ui.Button( |
| 425 |
'OK', /* opt_renderer */ undefined, this.getDomHelper()); |
| 426 |
this.addChild(this.okbutton_, true); |
| 427 |
this.okbutton_.setEnabled(false); |
| 428 |
|
| 429 |
this.zoomEl_ = this.getDomHelper().createDom( |
| 430 |
goog.dom.TagName.DIV, |
| 431 |
{id: 'zoom', className: goog.getCssName('goog-char-picker-char-zoom')}); |
| 432 |
|
| 433 |
this.charNameEl_ = this.getDomHelper().createDom( |
| 434 |
goog.dom.TagName.DIV, |
| 435 |
{id: 'charName', className: goog.getCssName('goog-char-picker-name')}); |
| 436 |
|
| 437 |
this.unicodeEl_ = this.getDomHelper().createDom( |
| 438 |
goog.dom.TagName.DIV, |
| 439 |
{id: 'unicode', className: goog.getCssName('goog-char-picker-unicode')}); |
| 440 |
|
| 441 |
var card = this.getDomHelper().createDom( |
| 442 |
goog.dom.TagName.DIV, {'id': 'preview'}, this.zoomEl_, this.charNameEl_, |
| 443 |
this.unicodeEl_); |
| 444 |
goog.style.setElementShown(card, false); |
| 445 |
this.hc_ = new goog.ui.HoverCard( |
| 446 |
{'DIV': 'char'}, |
| 447 |
/* opt_checkDescendants */ undefined, this.getDomHelper()); |
| 448 |
this.hc_.setElement(card); |
| 449 |
var self = this; |
| 450 |
|
| 451 |
/** |
| 452 |
* Function called by hover card just before it is visible to collect data. |
| 453 |
*/ |
| 454 |
function onBeforeShow() { |
| 455 |
var trigger = self.hc_.getAnchorElement(); |
| 456 |
var ch = self.getChar_(trigger); |
| 457 |
if (ch) { |
| 458 |
goog.dom.setTextContent(self.zoomEl_, self.displayChar_(ch)); |
| 459 |
goog.dom.setTextContent(self.unicodeEl_, goog.i18n.uChar.toHexString(ch)); |
| 460 |
// Clear the character name since we don't want to show old data because |
| 461 |
// it is retrieved asynchronously and the DOM object is re-used |
| 462 |
goog.dom.setTextContent(self.charNameEl_, ''); |
| 463 |
self.charNameFetcher_.getName(ch, function(charName) { |
| 464 |
if (charName) { |
| 465 |
goog.dom.setTextContent(self.charNameEl_, charName); |
| 466 |
} |
| 467 |
}); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
goog.events.listen( |
| 472 |
this.hc_, goog.ui.HoverCard.EventType.BEFORE_SHOW, onBeforeShow); |
| 473 |
goog.asserts.assert(element); |
| 474 |
goog.dom.classlist.add(element, goog.getCssName('goog-char-picker')); |
| 475 |
goog.dom.classlist.add( |
| 476 |
goog.asserts.assert(this.stick_), goog.getCssName('goog-stick')); |
| 477 |
goog.dom.classlist.add( |
| 478 |
goog.asserts.assert(this.stickwrap_), goog.getCssName('goog-stickwrap')); |
| 479 |
goog.dom.classlist.add( |
| 480 |
goog.asserts.assert(gridcontainer.getElement()), |
| 481 |
goog.getCssName('goog-char-picker-grid-container')); |
| 482 |
goog.dom.classlist.add( |
| 483 |
goog.asserts.assert(this.grid_.getElement()), |
| 484 |
goog.getCssName('goog-char-picker-grid')); |
| 485 |
goog.dom.classlist.add( |
| 486 |
goog.asserts.assert(this.recentgrid_.getElement()), |
| 487 |
goog.getCssName('goog-char-picker-grid')); |
| 488 |
goog.dom.classlist.add( |
| 489 |
goog.asserts.assert(this.recentgrid_.getElement()), |
| 490 |
goog.getCssName('goog-char-picker-recents')); |
| 491 |
|
| 492 |
goog.dom.classlist.add( |
| 493 |
goog.asserts.assert(this.notice_.getElement()), |
| 494 |
goog.getCssName('goog-char-picker-notice')); |
| 495 |
goog.dom.classlist.add( |
| 496 |
goog.asserts.assert(uplus.getElement()), |
| 497 |
goog.getCssName('goog-char-picker-uplus')); |
| 498 |
goog.dom.classlist.add( |
| 499 |
goog.asserts.assert(this.input_.getElement()), |
| 500 |
goog.getCssName('goog-char-picker-input-box')); |
| 501 |
goog.dom.classlist.add( |
| 502 |
goog.asserts.assert(this.okbutton_.getElement()), |
| 503 |
goog.getCssName('goog-char-picker-okbutton')); |
| 504 |
goog.dom.classlist.add( |
| 505 |
goog.asserts.assert(card), goog.getCssName('goog-char-picker-hovercard')); |
| 506 |
|
| 507 |
this.hc_.className = goog.getCssName('goog-char-picker-hovercard'); |
| 508 |
|
| 509 |
this.grid_.buttoncount = this.gridsize_; |
| 510 |
this.recentgrid_.buttoncount = this.recentwidth_; |
| 511 |
this.populateGridWithButtons_(this.grid_); |
| 512 |
this.populateGridWithButtons_(this.recentgrid_); |
| 513 |
|
| 514 |
this.updateGrid_(this.recentgrid_, this.recents_); |
| 515 |
this.setSelectedCategory_(this.initCategory_, this.initSubcategory_); |
| 516 |
new goog.ui.ContainerScroller(this.menu_); |
| 517 |
new goog.ui.ContainerScroller(this.submenu_); |
| 518 |
|
| 519 |
goog.dom.classlist.add( |
| 520 |
goog.asserts.assert(this.menu_.getElement()), |
| 521 |
goog.getCssName('goog-char-picker-menu')); |
| 522 |
goog.dom.classlist.add( |
| 523 |
goog.asserts.assert(this.submenu_.getElement()), |
| 524 |
goog.getCssName('goog-char-picker-menu')); |
| 525 |
}; |
| 526 |
|
| 527 |
|
| 528 |
/** @override */ |
| 529 |
goog.ui.CharPicker.prototype.enterDocument = function() { |
| 530 |
goog.ui.CharPicker.superClass_.enterDocument.call(this); |
| 531 |
var inputkh = new goog.events.InputHandler(this.input_.getElement()); |
| 532 |
this.keyHandler_ = new goog.events.KeyHandler(this.input_.getElement()); |
| 533 |
|
| 534 |
// Stop the propagation of ACTION events at menu and submenu buttons. |
| 535 |
// If stopped at capture phase, the button will not be set to normal state. |
| 536 |
// If not stopped, the user widget will receive the event, which is |
| 537 |
// undesired. User widget should receive an event only on the character |
| 538 |
// click. |
| 539 |
this.eventHandler_ |
| 540 |
.listen( |
| 541 |
this.menubutton_, goog.ui.Component.EventType.ACTION, |
| 542 |
goog.events.Event.stopPropagation) |
| 543 |
.listen( |
| 544 |
this.submenubutton_, goog.ui.Component.EventType.ACTION, |
| 545 |
goog.events.Event.stopPropagation) |
| 546 |
.listen( |
| 547 |
this, goog.ui.Component.EventType.ACTION, this.handleSelectedItem_, |
| 548 |
true) |
| 549 |
.listen( |
| 550 |
inputkh, goog.events.InputHandler.EventType.INPUT, this.handleInput_) |
| 551 |
.listen( |
| 552 |
this.keyHandler_, goog.events.KeyHandler.EventType.KEY, |
| 553 |
this.handleEnter_) |
| 554 |
.listen( |
| 555 |
this.recentgrid_, goog.ui.Component.EventType.FOCUS, |
| 556 |
this.handleFocus_) |
| 557 |
.listen(this.grid_, goog.ui.Component.EventType.FOCUS, this.handleFocus_); |
| 558 |
|
| 559 |
goog.events.listen( |
| 560 |
this.okbutton_.getElement(), goog.events.EventType.MOUSEDOWN, |
| 561 |
this.handleOkClick_, true, this); |
| 562 |
|
| 563 |
goog.events.listen( |
| 564 |
this.stickwrap_, goog.events.EventType.SCROLL, this.handleScroll_, true, |
| 565 |
this); |
| 566 |
}; |
| 567 |
|
| 568 |
|
| 569 |
/** |
| 570 |
* Handles the button focus by updating the aria label with the character name |
| 571 |
* so it becomes possible to get spoken feedback while tabbing through the |
| 572 |
* visible symbols. |
| 573 |
* @param {goog.events.Event} e The focus event. |
| 574 |
* @private |
| 575 |
*/ |
| 576 |
goog.ui.CharPicker.prototype.handleFocus_ = function(e) { |
| 577 |
var button = e.target; |
| 578 |
var element = button.getElement(); |
| 579 |
var ch = this.getChar_(element); |
| 580 |
|
| 581 |
// Clear the aria label to avoid speaking the old value in case the button |
| 582 |
// element has no char attribute or the character name cannot be retrieved. |
| 583 |
goog.a11y.aria.setState(element, goog.a11y.aria.State.LABEL, ''); |
| 584 |
|
| 585 |
if (ch) { |
| 586 |
// This is working with screen readers because the call to getName is |
| 587 |
// synchronous once the values have been prefetched by the RemoteNameFetcher |
| 588 |
// and because it is always synchronous when using the LocalNameFetcher. |
| 589 |
// Also, the special character itself is not used as the label because some |
| 590 |
// screen readers, notably ChromeVox, are not able to speak them. |
| 591 |
// TODO(user): Consider changing the NameFetcher API to provide a |
| 592 |
// method that lets the caller retrieve multiple character names at once |
| 593 |
// so that this asynchronous gymnastic can be avoided. |
| 594 |
this.charNameFetcher_.getName(ch, function(charName) { |
| 595 |
if (charName) { |
| 596 |
goog.a11y.aria.setState(element, goog.a11y.aria.State.LABEL, charName); |
| 597 |
} |
| 598 |
}); |
| 599 |
} |
| 600 |
}; |
| 601 |
|
| 602 |
|
| 603 |
/** |
| 604 |
* On scroll, updates the grid with characters correct to the scroll position. |
| 605 |
* @param {goog.events.Event} e Scroll event to handle. |
| 606 |
* @private |
| 607 |
*/ |
| 608 |
goog.ui.CharPicker.prototype.handleScroll_ = function(e) { |
| 609 |
var height = e.target.scrollHeight; |
| 610 |
var top = e.target.scrollTop; |
| 611 |
var itempos = |
| 612 |
Math.ceil(top * this.items.length / (this.columnCount_ * height)) * |
| 613 |
this.columnCount_; |
| 614 |
if (this.itempos != itempos) { |
| 615 |
this.itempos = itempos; |
| 616 |
this.modifyGridWithItems_(this.grid_, this.items, itempos); |
| 617 |
} |
| 618 |
e.stopPropagation(); |
| 619 |
}; |
| 620 |
|
| 621 |
|
| 622 |
/** |
| 623 |
* On a menu click, sets correct character set in the grid; on a grid click |
| 624 |
* accept the character as the selected one and adds to recent selection, if not |
| 625 |
* already present. |
| 626 |
* @param {goog.events.Event} e Event for the click on menus or grid. |
| 627 |
* @private |
| 628 |
*/ |
| 629 |
goog.ui.CharPicker.prototype.handleSelectedItem_ = function(e) { |
| 630 |
var parent = /** @type {goog.ui.Component} */ (e.target).getParent(); |
| 631 |
if (parent == this.menu_) { |
| 632 |
this.menu_.setVisible(false); |
| 633 |
this.setSelectedCategory_(e.target.getValue()); |
| 634 |
} else if (parent == this.submenu_) { |
| 635 |
this.submenu_.setVisible(false); |
| 636 |
this.setSelectedSubcategory_(e.target.getValue()); |
| 637 |
} else if (parent == this.grid_) { |
| 638 |
var button = e.target.getElement(); |
| 639 |
this.selectedChar_ = this.getChar_(button); |
| 640 |
this.updateRecents_(this.selectedChar_); |
| 641 |
} else if (parent == this.recentgrid_) { |
| 642 |
this.selectedChar_ = this.getChar_(e.target.getElement()); |
| 643 |
} |
| 644 |
}; |
| 645 |
|
| 646 |
|
| 647 |
/** |
| 648 |
* When user types the characters displays the preview. Enables the OK button, |
| 649 |
* if the character is valid. |
| 650 |
* @param {goog.events.Event} e Event for typing in input field. |
| 651 |
* @private |
| 652 |
*/ |
| 653 |
goog.ui.CharPicker.prototype.handleInput_ = function(e) { |
| 654 |
var ch = this.getInputChar(); |
| 655 |
if (ch) { |
| 656 |
goog.dom.setTextContent(this.zoomEl_, ch); |
| 657 |
goog.dom.setTextContent(this.unicodeEl_, goog.i18n.uChar.toHexString(ch)); |
| 658 |
goog.dom.setTextContent(this.charNameEl_, ''); |
| 659 |
var coord = |
| 660 |
new goog.ui.Tooltip.ElementTooltipPosition(this.input_.getElement()); |
| 661 |
this.hc_.setPosition(coord); |
| 662 |
this.hc_.triggerForElement(this.input_.getElement()); |
| 663 |
this.okbutton_.setEnabled(true); |
| 664 |
} else { |
| 665 |
this.hc_.cancelTrigger(); |
| 666 |
this.hc_.setVisible(false); |
| 667 |
this.okbutton_.setEnabled(false); |
| 668 |
} |
| 669 |
}; |
| 670 |
|
| 671 |
|
| 672 |
/** |
| 673 |
* On OK click accepts the character and updates the recent char list. |
| 674 |
* @param {goog.events.Event=} opt_event Event for click on OK button. |
| 675 |
* @return {boolean} Indicates whether to propagate event. |
| 676 |
* @private |
| 677 |
*/ |
| 678 |
goog.ui.CharPicker.prototype.handleOkClick_ = function(opt_event) { |
| 679 |
var ch = this.getInputChar(); |
| 680 |
if (ch && ch.charCodeAt(0)) { |
| 681 |
this.selectedChar_ = ch; |
| 682 |
this.updateRecents_(ch); |
| 683 |
return true; |
| 684 |
} |
| 685 |
return false; |
| 686 |
}; |
| 687 |
|
| 688 |
|
| 689 |
/** |
| 690 |
* Behaves exactly like the OK button on Enter key. |
| 691 |
* @param {goog.events.KeyEvent} e Event for enter on the input field. |
| 692 |
* @return {boolean} Indicates whether to propagate event. |
| 693 |
* @private |
| 694 |
*/ |
| 695 |
goog.ui.CharPicker.prototype.handleEnter_ = function(e) { |
| 696 |
if (e.keyCode == goog.events.KeyCodes.ENTER) { |
| 697 |
return this.handleOkClick_() ? |
| 698 |
this.dispatchEvent(goog.ui.Component.EventType.ACTION) : |
| 699 |
false; |
| 700 |
} |
| 701 |
return false; |
| 702 |
}; |
| 703 |
|
| 704 |
|
| 705 |
/** |
| 706 |
* Gets the character from the event target. |
| 707 |
* @param {Element} e Event target containing the 'char' attribute. |
| 708 |
* @return {string} The character specified in the event. |
| 709 |
* @private |
| 710 |
*/ |
| 711 |
goog.ui.CharPicker.prototype.getChar_ = function(e) { |
| 712 |
return e.getAttribute('char'); |
| 713 |
}; |
| 714 |
|
| 715 |
|
| 716 |
/** |
| 717 |
* Creates a menu entry for either the category listing or subcategory listing. |
| 718 |
* @param {number} id Id to be used for the entry. |
| 719 |
* @param {string} caption Text displayed for the menu item. |
| 720 |
* @return {!goog.ui.MenuItem} Menu item to be added to the menu listing. |
| 721 |
* @private |
| 722 |
*/ |
| 723 |
goog.ui.CharPicker.prototype.createMenuItem_ = function(id, caption) { |
| 724 |
var item = new goog.ui.MenuItem(caption, /* model */ id, this.getDomHelper()); |
| 725 |
item.setVisible(true); |
| 726 |
return item; |
| 727 |
}; |
| 728 |
|
| 729 |
|
| 730 |
/** |
| 731 |
* Sets the category and updates the submenu items and grid accordingly. |
| 732 |
* @param {number} category Category index used to index the data tables. |
| 733 |
* @param {number=} opt_subcategory Subcategory index used with category index. |
| 734 |
* @private |
| 735 |
*/ |
| 736 |
goog.ui.CharPicker.prototype.setSelectedCategory_ = function( |
| 737 |
category, opt_subcategory) { |
| 738 |
this.category = category; |
| 739 |
this.menubutton_.setCaption(this.data_.categories[category]); |
| 740 |
while (this.submenu_.hasChildren()) { |
| 741 |
this.submenu_.removeChildAt(0, true).dispose(); |
| 742 |
} |
| 743 |
|
| 744 |
var subcategories = this.data_.subcategories[category]; |
| 745 |
for (var i = 0; i < subcategories.length; i++) { |
| 746 |
var item = this.createMenuItem_(i, subcategories[i]); |
| 747 |
this.submenu_.addChild(item, true); |
| 748 |
} |
| 749 |
this.setSelectedSubcategory_(opt_subcategory || 0); |
| 750 |
}; |
| 751 |
|
| 752 |
|
| 753 |
/** |
| 754 |
* Sets the subcategory and updates the grid accordingly. |
| 755 |
* @param {number} subcategory Sub-category index used to index the data tables. |
| 756 |
* @private |
| 757 |
*/ |
| 758 |
goog.ui.CharPicker.prototype.setSelectedSubcategory_ = function(subcategory) { |
| 759 |
var subcategories = this.data_.subcategories; |
| 760 |
var name = subcategories[this.category][subcategory]; |
| 761 |
this.submenubutton_.setCaption(name); |
| 762 |
this.setSelectedGrid_(this.category, subcategory); |
| 763 |
}; |
| 764 |
|
| 765 |
|
| 766 |
/** |
| 767 |
* Updates the grid according to a given category and subcategory. |
| 768 |
* @param {number} category Index to the category table. |
| 769 |
* @param {number} subcategory Index to the subcategory table. |
| 770 |
* @private |
| 771 |
*/ |
| 772 |
goog.ui.CharPicker.prototype.setSelectedGrid_ = function( |
| 773 |
category, subcategory) { |
| 774 |
var charLists = this.data_.charList; |
| 775 |
var charListStr = charLists[category][subcategory]; |
| 776 |
var content = this.decompressor_.toCharList(charListStr); |
| 777 |
this.charNameFetcher_.prefetch(charListStr); |
| 778 |
this.updateGrid_(this.grid_, content); |
| 779 |
}; |
| 780 |
|
| 781 |
|
| 782 |
/** |
| 783 |
* Updates the grid with new character list. |
| 784 |
* @param {goog.ui.Component} grid The grid which is updated with a new set of |
| 785 |
* characters. |
| 786 |
* @param {Array<string>} items Characters to be added to the grid. |
| 787 |
* @private |
| 788 |
*/ |
| 789 |
goog.ui.CharPicker.prototype.updateGrid_ = function(grid, items) { |
| 790 |
if (grid == this.grid_) { |
| 791 |
/** |
| 792 |
* @desc The message used when there are invisible characters like space |
| 793 |
* or format control characters. |
| 794 |
*/ |
| 795 |
var MSG_PLEASE_HOVER = |
| 796 |
goog.getMsg('Please hover over each cell for the character name.'); |
| 797 |
|
| 798 |
goog.dom.setTextContent( |
| 799 |
this.notice_.getElement(), |
| 800 |
this.charNameFetcher_.isNameAvailable(items[0]) ? MSG_PLEASE_HOVER : |
| 801 |
''); |
| 802 |
this.items = items; |
| 803 |
if (this.stickwrap_.offsetHeight > 0) { |
| 804 |
this.stick_.style.height = |
| 805 |
this.stickwrap_.offsetHeight * items.length / this.gridsize_ + 'px'; |
| 806 |
} else { |
| 807 |
// This is the last ditch effort if height is not avaialble. |
| 808 |
// Maximum of 3em is assumed to the the cell height. Extra space after |
| 809 |
// last character in the grid is OK. |
| 810 |
this.stick_.style.height = |
| 811 |
3 * this.columnCount_ * items.length / this.gridsize_ + 'em'; |
| 812 |
} |
| 813 |
this.stickwrap_.scrollTop = 0; |
| 814 |
} |
| 815 |
|
| 816 |
this.modifyGridWithItems_(grid, items, 0); |
| 817 |
}; |
| 818 |
|
| 819 |
|
| 820 |
/** |
| 821 |
* Updates the grid with new character list for a given starting point. |
| 822 |
* @param {goog.ui.Component} grid The grid which is updated with a new set of |
| 823 |
* characters. |
| 824 |
* @param {Array<string>} items Characters to be added to the grid. |
| 825 |
* @param {number} start The index from which the characters should be |
| 826 |
* displayed. |
| 827 |
* @private |
| 828 |
*/ |
| 829 |
goog.ui.CharPicker.prototype.modifyGridWithItems_ = function( |
| 830 |
grid, items, start) { |
| 831 |
for (var buttonpos = 0, itempos = start; |
| 832 |
buttonpos < grid.buttoncount && itempos < items.length; |
| 833 |
buttonpos++, itempos++) { |
| 834 |
this.modifyCharNode_( |
| 835 |
/** @type {!goog.ui.Button} */ (grid.getChildAt(buttonpos)), |
| 836 |
items[itempos]); |
| 837 |
} |
| 838 |
|
| 839 |
for (; buttonpos < grid.buttoncount; buttonpos++) { |
| 840 |
grid.getChildAt(buttonpos).setVisible(false); |
| 841 |
} |
| 842 |
}; |
| 843 |
|
| 844 |
|
| 845 |
/** |
| 846 |
* Creates the grid for characters to displayed for selection. |
| 847 |
* @param {goog.ui.Component} grid The grid which is updated with a new set of |
| 848 |
* characters. |
| 849 |
* @private |
| 850 |
*/ |
| 851 |
goog.ui.CharPicker.prototype.populateGridWithButtons_ = function(grid) { |
| 852 |
for (var i = 0; i < grid.buttoncount; i++) { |
| 853 |
var button = new goog.ui.Button( |
| 854 |
' ', goog.ui.FlatButtonRenderer.getInstance(), this.getDomHelper()); |
| 855 |
|
| 856 |
// Dispatch the focus event so we can update the aria description while |
| 857 |
// the user tabs through the cells. |
| 858 |
button.setDispatchTransitionEvents(goog.ui.Component.State.FOCUSED, true); |
| 859 |
|
| 860 |
grid.addChild(button, true); |
| 861 |
button.setVisible(false); |
| 862 |
|
| 863 |
var buttonEl = button.getElement(); |
| 864 |
goog.asserts.assert(buttonEl, 'The button DOM element cannot be null.'); |
| 865 |
|
| 866 |
// Override the button role so the user doesn't hear "button" each time he |
| 867 |
// tabs through the cells. |
| 868 |
goog.a11y.aria.removeRole(buttonEl); |
| 869 |
} |
| 870 |
}; |
| 871 |
|
| 872 |
|
| 873 |
/** |
| 874 |
* Updates the grid cell with new character. |
| 875 |
* @param {goog.ui.Button} button This button is popped up for new character. |
| 876 |
* @param {string} ch Character to be displayed by the button. |
| 877 |
* @private |
| 878 |
*/ |
| 879 |
goog.ui.CharPicker.prototype.modifyCharNode_ = function(button, ch) { |
| 880 |
var text = this.displayChar_(ch); |
| 881 |
var buttonEl = button.getElement(); |
| 882 |
goog.dom.setTextContent(buttonEl, text); |
| 883 |
buttonEl.setAttribute('char', ch); |
| 884 |
button.setVisible(true); |
| 885 |
}; |
| 886 |
|
| 887 |
|
| 888 |
/** |
| 889 |
* Adds a given character to the recent character list. |
| 890 |
* @param {string} character Character to be added to the recent list. |
| 891 |
* @private |
| 892 |
*/ |
| 893 |
goog.ui.CharPicker.prototype.updateRecents_ = function(character) { |
| 894 |
if (character && character.charCodeAt(0) && |
| 895 |
!goog.array.contains(this.recents_, character)) { |
| 896 |
this.recents_.unshift(character); |
| 897 |
if (this.recents_.length > this.recentwidth_) { |
| 898 |
this.recents_.pop(); |
| 899 |
} |
| 900 |
this.updateGrid_(this.recentgrid_, this.recents_); |
| 901 |
} |
| 902 |
}; |
| 903 |
|
| 904 |
|
| 905 |
/** |
| 906 |
* Gets the user inputed unicode character. |
| 907 |
* @return {string} Unicode character inputed by user. |
| 908 |
*/ |
| 909 |
goog.ui.CharPicker.prototype.getInputChar = function() { |
| 910 |
var text = this.input_.getValue(); |
| 911 |
var code = parseInt(text, 16); |
| 912 |
return /** @type {string} */ (goog.i18n.uChar.fromCharCode(code)); |
| 913 |
}; |
| 914 |
|
| 915 |
|
| 916 |
/** |
| 917 |
* Gets the display character for the given character. |
| 918 |
* @param {string} ch Character whose display is fetched. |
| 919 |
* @return {string} The display of the given character. |
| 920 |
* @private |
| 921 |
*/ |
| 922 |
goog.ui.CharPicker.prototype.displayChar_ = function(ch) { |
| 923 |
return this.layoutAlteringChars_.contains(ch) ? '\u00A0' : ch; |
| 924 |
}; |