| 1 |
// Copyright 2007 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 Emoji Picker implementation. This provides a UI widget for |
| 17 |
* choosing an emoji from a grid of possible choices. |
| 18 |
* |
| 19 |
* @see ../demos/popupemojipicker.html for an example of how to instantiate |
| 20 |
* an emoji picker. |
| 21 |
* |
| 22 |
* Based on goog.ui.ColorPicker (colorpicker.js). |
| 23 |
* |
| 24 |
* @see ../../demos/popupemojipicker.html |
| 25 |
*/ |
| 26 |
|
| 27 |
goog.provide('goog.ui.emoji.EmojiPicker'); |
| 28 |
|
| 29 |
goog.require('goog.dom.TagName'); |
| 30 |
goog.require('goog.style'); |
| 31 |
goog.require('goog.ui.Component'); |
| 32 |
goog.require('goog.ui.TabPane'); |
| 33 |
goog.require('goog.ui.emoji.Emoji'); |
| 34 |
goog.require('goog.ui.emoji.EmojiPalette'); |
| 35 |
goog.require('goog.ui.emoji.EmojiPaletteRenderer'); |
| 36 |
goog.require('goog.ui.emoji.ProgressiveEmojiPaletteRenderer'); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Creates a new, empty emoji picker. An emoji picker is a grid of emoji, each |
| 42 |
* cell of the grid containing a single emoji. The picker may contain multiple |
| 43 |
* pages of emoji. |
| 44 |
* |
| 45 |
* When a user selects an emoji, by either clicking or pressing enter, the |
| 46 |
* picker fires a goog.ui.Component.EventType.ACTION event with the id. The |
| 47 |
* client listens on this event and in the handler can retrieve the id of the |
| 48 |
* selected emoji and do something with it, for instance, inserting an image |
| 49 |
* tag into a rich text control. An emoji picker does not maintain state. That |
| 50 |
* is, once an emoji is selected, the emoji picker does not remember which emoji |
| 51 |
* was selected. |
| 52 |
* |
| 53 |
* The emoji picker is implemented as a tabpane with each tabpage being a table. |
| 54 |
* Each of the tables are the same size to prevent jittering when switching |
| 55 |
* between pages. |
| 56 |
* |
| 57 |
* @param {string} defaultImgUrl Url of the img that should be used to fill up |
| 58 |
* the cells in the emoji table, to prevent jittering. Should be the same |
| 59 |
* size as the emoji. |
| 60 |
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper. |
| 61 |
* @extends {goog.ui.Component} |
| 62 |
* @constructor |
| 63 |
*/ |
| 64 |
goog.ui.emoji.EmojiPicker = function(defaultImgUrl, opt_domHelper) { |
| 65 |
goog.ui.Component.call(this, opt_domHelper); |
| 66 |
|
| 67 |
this.defaultImgUrl_ = defaultImgUrl; |
| 68 |
|
| 69 |
/** |
| 70 |
* Emoji that this picker displays. |
| 71 |
* |
| 72 |
* @type {Array<Object>} |
| 73 |
* @private |
| 74 |
*/ |
| 75 |
this.emoji_ = []; |
| 76 |
|
| 77 |
/** |
| 78 |
* Pages of this emoji picker. |
| 79 |
* |
| 80 |
* @type {Array<goog.ui.emoji.EmojiPalette>} |
| 81 |
* @private |
| 82 |
*/ |
| 83 |
this.pages_ = []; |
| 84 |
|
| 85 |
/** |
| 86 |
* Keeps track of which pages in the picker have been loaded. Used for delayed |
| 87 |
* loading of tabs. |
| 88 |
* |
| 89 |
* @type {Array<boolean>} |
| 90 |
* @private |
| 91 |
*/ |
| 92 |
this.pageLoadStatus_ = []; |
| 93 |
|
| 94 |
/** |
| 95 |
* Tabpane to hold the pages of this emojipicker. |
| 96 |
* |
| 97 |
* @type {goog.ui.TabPane} |
| 98 |
* @private |
| 99 |
*/ |
| 100 |
this.tabPane_ = null; |
| 101 |
|
| 102 |
this.getHandler().listen( |
| 103 |
this, goog.ui.Component.EventType.ACTION, this.onEmojiPaletteAction_); |
| 104 |
}; |
| 105 |
goog.inherits(goog.ui.emoji.EmojiPicker, goog.ui.Component); |
| 106 |
|
| 107 |
|
| 108 |
/** |
| 109 |
* Default number of rows per grid of emoji. |
| 110 |
* |
| 111 |
* @type {number} |
| 112 |
*/ |
| 113 |
goog.ui.emoji.EmojiPicker.DEFAULT_NUM_ROWS = 5; |
| 114 |
|
| 115 |
|
| 116 |
/** |
| 117 |
* Default number of columns per grid of emoji. |
| 118 |
* |
| 119 |
* @type {number} |
| 120 |
*/ |
| 121 |
goog.ui.emoji.EmojiPicker.DEFAULT_NUM_COLS = 10; |
| 122 |
|
| 123 |
|
| 124 |
/** |
| 125 |
* Default location of the tabs in relation to the emoji grids. |
| 126 |
* |
| 127 |
* @type {goog.ui.TabPane.TabLocation} |
| 128 |
*/ |
| 129 |
goog.ui.emoji.EmojiPicker.DEFAULT_TAB_LOCATION = |
| 130 |
goog.ui.TabPane.TabLocation.TOP; |
| 131 |
|
| 132 |
|
| 133 |
/** @private {goog.ui.emoji.Emoji} */ |
| 134 |
goog.ui.emoji.EmojiPicker.prototype.selectedEmoji_; |
| 135 |
|
| 136 |
|
| 137 |
/** @private {goog.ui.emoji.EmojiPaletteRenderer} */ |
| 138 |
goog.ui.emoji.EmojiPicker.prototype.renderer_; |
| 139 |
|
| 140 |
|
| 141 |
/** |
| 142 |
* Number of rows per grid of emoji. |
| 143 |
* |
| 144 |
* @type {number} |
| 145 |
* @private |
| 146 |
*/ |
| 147 |
goog.ui.emoji.EmojiPicker.prototype.numRows_ = |
| 148 |
goog.ui.emoji.EmojiPicker.DEFAULT_NUM_ROWS; |
| 149 |
|
| 150 |
|
| 151 |
/** |
| 152 |
* Number of columns per grid of emoji. |
| 153 |
* |
| 154 |
* @type {number} |
| 155 |
* @private |
| 156 |
*/ |
| 157 |
goog.ui.emoji.EmojiPicker.prototype.numCols_ = |
| 158 |
goog.ui.emoji.EmojiPicker.DEFAULT_NUM_COLS; |
| 159 |
|
| 160 |
|
| 161 |
/** |
| 162 |
* Whether the number of rows in the picker should be automatically determined |
| 163 |
* by the specified number of columns so as to minimize/eliminate jitter when |
| 164 |
* switching between tabs. |
| 165 |
* |
| 166 |
* @type {boolean} |
| 167 |
* @private |
| 168 |
*/ |
| 169 |
goog.ui.emoji.EmojiPicker.prototype.autoSizeByColumnCount_ = true; |
| 170 |
|
| 171 |
|
| 172 |
/** |
| 173 |
* Location of the tabs for the picker tabpane. |
| 174 |
* |
| 175 |
* @type {goog.ui.TabPane.TabLocation} |
| 176 |
* @private |
| 177 |
*/ |
| 178 |
goog.ui.emoji.EmojiPicker.prototype.tabLocation_ = |
| 179 |
goog.ui.emoji.EmojiPicker.DEFAULT_TAB_LOCATION; |
| 180 |
|
| 181 |
|
| 182 |
/** |
| 183 |
* Whether the component is focusable. |
| 184 |
* @type {boolean} |
| 185 |
* @private |
| 186 |
*/ |
| 187 |
goog.ui.emoji.EmojiPicker.prototype.focusable_ = true; |
| 188 |
|
| 189 |
|
| 190 |
/** |
| 191 |
* Url of the img that should be used for cells in the emoji picker that are |
| 192 |
* not filled with emoji, i.e., after all the emoji have already been placed |
| 193 |
* on a page. |
| 194 |
* |
| 195 |
* @type {string} |
| 196 |
* @private |
| 197 |
*/ |
| 198 |
goog.ui.emoji.EmojiPicker.prototype.defaultImgUrl_; |
| 199 |
|
| 200 |
|
| 201 |
/** |
| 202 |
* If present, indicates a prefix that should be prepended to all URLs |
| 203 |
* of images in this emojipicker. This provides an optimization if the URLs |
| 204 |
* are long, so that the client does not have to send a long string for each |
| 205 |
* emoji. |
| 206 |
* |
| 207 |
* @type {string|undefined} |
| 208 |
* @private |
| 209 |
*/ |
| 210 |
goog.ui.emoji.EmojiPicker.prototype.urlPrefix_; |
| 211 |
|
| 212 |
|
| 213 |
/** |
| 214 |
* If true, delay loading the images for the emojipalettes until after |
| 215 |
* construction. This gives a better user experience before the images are in |
| 216 |
* the cache, since other widgets waiting for construction of the emojipalettes |
| 217 |
* won't have to wait for all the images (which may be a substantial amount) to |
| 218 |
* load. |
| 219 |
* |
| 220 |
* @type {boolean} |
| 221 |
* @private |
| 222 |
*/ |
| 223 |
goog.ui.emoji.EmojiPicker.prototype.delayedLoad_ = false; |
| 224 |
|
| 225 |
|
| 226 |
/** |
| 227 |
* Whether to use progressive rendering in the emojipicker's palette, if using |
| 228 |
* sprited imgs. If true, then uses img tags, which most browsers render |
| 229 |
* progressively (i.e., as the data comes in). If false, then uses div tags |
| 230 |
* with the background-image, which some newer browsers render progressively |
| 231 |
* but older ones do not. |
| 232 |
* |
| 233 |
* @type {boolean} |
| 234 |
* @private |
| 235 |
*/ |
| 236 |
goog.ui.emoji.EmojiPicker.prototype.progressiveRender_ = false; |
| 237 |
|
| 238 |
|
| 239 |
/** |
| 240 |
* Whether to require the caller to manually specify when to start loading |
| 241 |
* animated emoji. This is primarily for unittests to be able to test the |
| 242 |
* structure of the emojipicker palettes before and after the animated emoji |
| 243 |
* have been loaded. |
| 244 |
* |
| 245 |
* @type {boolean} |
| 246 |
* @private |
| 247 |
*/ |
| 248 |
goog.ui.emoji.EmojiPicker.prototype.manualLoadOfAnimatedEmoji_ = false; |
| 249 |
|
| 250 |
|
| 251 |
/** |
| 252 |
* Index of the active page in the picker. |
| 253 |
* |
| 254 |
* @type {number} |
| 255 |
* @private |
| 256 |
*/ |
| 257 |
goog.ui.emoji.EmojiPicker.prototype.activePage_ = -1; |
| 258 |
|
| 259 |
|
| 260 |
/** |
| 261 |
* Adds a group of emoji to the picker. |
| 262 |
* |
| 263 |
* @param {string|Element} title Title for the group. |
| 264 |
* @param {Array<Array<string>>} emojiGroup A new group of emoji to be added |
| 265 |
* Each internal array contains [emojiUrl, emojiId]. |
| 266 |
*/ |
| 267 |
goog.ui.emoji.EmojiPicker.prototype.addEmojiGroup = function( |
| 268 |
title, emojiGroup) { |
| 269 |
this.emoji_.push({title: title, emoji: emojiGroup}); |
| 270 |
}; |
| 271 |
|
| 272 |
|
| 273 |
/** |
| 274 |
* Gets the number of rows per grid in the emoji picker. |
| 275 |
* |
| 276 |
* @return {number} number of rows per grid. |
| 277 |
*/ |
| 278 |
goog.ui.emoji.EmojiPicker.prototype.getNumRows = function() { |
| 279 |
return this.numRows_; |
| 280 |
}; |
| 281 |
|
| 282 |
|
| 283 |
/** |
| 284 |
* Gets the number of columns per grid in the emoji picker. |
| 285 |
* |
| 286 |
* @return {number} number of columns per grid. |
| 287 |
*/ |
| 288 |
goog.ui.emoji.EmojiPicker.prototype.getNumColumns = function() { |
| 289 |
return this.numCols_; |
| 290 |
}; |
| 291 |
|
| 292 |
|
| 293 |
/** |
| 294 |
* Sets the number of rows per grid in the emoji picker. This should only be |
| 295 |
* called before the picker has been rendered. |
| 296 |
* |
| 297 |
* @param {number} numRows Number of rows per grid. |
| 298 |
*/ |
| 299 |
goog.ui.emoji.EmojiPicker.prototype.setNumRows = function(numRows) { |
| 300 |
this.numRows_ = numRows; |
| 301 |
}; |
| 302 |
|
| 303 |
|
| 304 |
/** |
| 305 |
* Sets the number of columns per grid in the emoji picker. This should only be |
| 306 |
* called before the picker has been rendered. |
| 307 |
* |
| 308 |
* @param {number} numCols Number of columns per grid. |
| 309 |
*/ |
| 310 |
goog.ui.emoji.EmojiPicker.prototype.setNumColumns = function(numCols) { |
| 311 |
this.numCols_ = numCols; |
| 312 |
}; |
| 313 |
|
| 314 |
|
| 315 |
/** |
| 316 |
* Sets whether to automatically size the emojipicker based on the number of |
| 317 |
* columns and the number of emoji in each group, so as to reduce jitter. |
| 318 |
* |
| 319 |
* @param {boolean} autoSize Whether to automatically size the picker. |
| 320 |
*/ |
| 321 |
goog.ui.emoji.EmojiPicker.prototype.setAutoSizeByColumnCount = function( |
| 322 |
autoSize) { |
| 323 |
this.autoSizeByColumnCount_ = autoSize; |
| 324 |
}; |
| 325 |
|
| 326 |
|
| 327 |
/** |
| 328 |
* Sets the location of the tabs in relation to the emoji grids. This should |
| 329 |
* only be called before the picker has been rendered. |
| 330 |
* |
| 331 |
* @param {goog.ui.TabPane.TabLocation} tabLocation The location of the tabs. |
| 332 |
*/ |
| 333 |
goog.ui.emoji.EmojiPicker.prototype.setTabLocation = function(tabLocation) { |
| 334 |
this.tabLocation_ = tabLocation; |
| 335 |
}; |
| 336 |
|
| 337 |
|
| 338 |
/** |
| 339 |
* Sets whether loading of images should be delayed until after dom creation. |
| 340 |
* Thus, this function must be called before {@link #createDom}. If set to true, |
| 341 |
* the client must call {@link #loadImages} when they wish the images to be |
| 342 |
* loaded. |
| 343 |
* |
| 344 |
* @param {boolean} shouldDelay Whether to delay loading the images. |
| 345 |
*/ |
| 346 |
goog.ui.emoji.EmojiPicker.prototype.setDelayedLoad = function(shouldDelay) { |
| 347 |
this.delayedLoad_ = shouldDelay; |
| 348 |
}; |
| 349 |
|
| 350 |
|
| 351 |
/** |
| 352 |
* Sets whether to require the caller to manually specify when to start loading |
| 353 |
* animated emoji. This is primarily for unittests to be able to test the |
| 354 |
* structure of the emojipicker palettes before and after the animated emoji |
| 355 |
* have been loaded. This only affects sprited emojipickers with sprite data |
| 356 |
* for animated emoji. |
| 357 |
* |
| 358 |
* @param {boolean} manual Whether to load animated emoji manually. |
| 359 |
*/ |
| 360 |
goog.ui.emoji.EmojiPicker.prototype.setManualLoadOfAnimatedEmoji = function( |
| 361 |
manual) { |
| 362 |
this.manualLoadOfAnimatedEmoji_ = manual; |
| 363 |
}; |
| 364 |
|
| 365 |
|
| 366 |
/** |
| 367 |
* Returns true if the component is focusable, false otherwise. The default |
| 368 |
* is true. Focusable components always have a tab index and allocate a key |
| 369 |
* handler to handle keyboard events while focused. |
| 370 |
* @return {boolean} Whether the component is focusable. |
| 371 |
*/ |
| 372 |
goog.ui.emoji.EmojiPicker.prototype.isFocusable = function() { |
| 373 |
return this.focusable_; |
| 374 |
}; |
| 375 |
|
| 376 |
|
| 377 |
/** |
| 378 |
* Sets whether the component is focusable. The default is true. |
| 379 |
* Focusable components always have a tab index and allocate a key handler to |
| 380 |
* handle keyboard events while focused. |
| 381 |
* @param {boolean} focusable Whether the component is focusable. |
| 382 |
*/ |
| 383 |
goog.ui.emoji.EmojiPicker.prototype.setFocusable = function(focusable) { |
| 384 |
this.focusable_ = focusable; |
| 385 |
for (var i = 0; i < this.pages_.length; i++) { |
| 386 |
if (this.pages_[i]) { |
| 387 |
this.pages_[i].setSupportedState( |
| 388 |
goog.ui.Component.State.FOCUSED, focusable); |
| 389 |
} |
| 390 |
} |
| 391 |
}; |
| 392 |
|
| 393 |
|
| 394 |
/** |
| 395 |
* Sets the URL prefix for the emoji URLs. |
| 396 |
* |
| 397 |
* @param {string} urlPrefix Prefix that should be prepended to all URLs. |
| 398 |
*/ |
| 399 |
goog.ui.emoji.EmojiPicker.prototype.setUrlPrefix = function(urlPrefix) { |
| 400 |
this.urlPrefix_ = urlPrefix; |
| 401 |
}; |
| 402 |
|
| 403 |
|
| 404 |
/** |
| 405 |
* Sets the progressive rendering aspect of this emojipicker. Must be called |
| 406 |
* before createDom to have an effect. |
| 407 |
* |
| 408 |
* @param {boolean} progressive Whether this picker should render progressively. |
| 409 |
*/ |
| 410 |
goog.ui.emoji.EmojiPicker.prototype.setProgressiveRender = function( |
| 411 |
progressive) { |
| 412 |
this.progressiveRender_ = progressive; |
| 413 |
}; |
| 414 |
|
| 415 |
|
| 416 |
/** |
| 417 |
* Adjusts the number of rows to be the maximum row count out of all the emoji |
| 418 |
* groups, in order to prevent jitter in switching among the tabs. |
| 419 |
* |
| 420 |
* @private |
| 421 |
*/ |
| 422 |
goog.ui.emoji.EmojiPicker.prototype.adjustNumRowsIfNecessary_ = function() { |
| 423 |
var currentMax = 0; |
| 424 |
|
| 425 |
for (var i = 0; i < this.emoji_.length; i++) { |
| 426 |
var numEmoji = this.emoji_[i].emoji.length; |
| 427 |
var rowsNeeded = Math.ceil(numEmoji / this.numCols_); |
| 428 |
if (rowsNeeded > currentMax) { |
| 429 |
currentMax = rowsNeeded; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
this.setNumRows(currentMax); |
| 434 |
}; |
| 435 |
|
| 436 |
|
| 437 |
/** |
| 438 |
* Causes the emoji imgs to be loaded into the picker. Used for delayed loading. |
| 439 |
* No-op if delayed loading is not set. |
| 440 |
*/ |
| 441 |
goog.ui.emoji.EmojiPicker.prototype.loadImages = function() { |
| 442 |
if (!this.delayedLoad_) { |
| 443 |
return; |
| 444 |
} |
| 445 |
|
| 446 |
// Load the first page only |
| 447 |
this.loadPage_(0); |
| 448 |
this.activePage_ = 0; |
| 449 |
}; |
| 450 |
|
| 451 |
|
| 452 |
/** |
| 453 |
* @override |
| 454 |
* @suppress {deprecated} Using deprecated goog.ui.TabPane. |
| 455 |
*/ |
| 456 |
goog.ui.emoji.EmojiPicker.prototype.createDom = function() { |
| 457 |
this.setElementInternal(this.getDomHelper().createDom(goog.dom.TagName.DIV)); |
| 458 |
|
| 459 |
if (this.autoSizeByColumnCount_) { |
| 460 |
this.adjustNumRowsIfNecessary_(); |
| 461 |
} |
| 462 |
|
| 463 |
if (this.emoji_.length == 0) { |
| 464 |
throw new Error('Must add some emoji to the picker'); |
| 465 |
} |
| 466 |
|
| 467 |
// If there is more than one group of emoji, we construct a tabpane |
| 468 |
if (this.emoji_.length > 1) { |
| 469 |
// Give the tabpane a div to use as its content element, since tabpane |
| 470 |
// overwrites the CSS class of the element it's passed |
| 471 |
var div = this.getDomHelper().createDom(goog.dom.TagName.DIV); |
| 472 |
this.getElement().appendChild(div); |
| 473 |
this.tabPane_ = new goog.ui.TabPane( |
| 474 |
div, this.tabLocation_, this.getDomHelper(), true /* use MOUSEDOWN */); |
| 475 |
} |
| 476 |
|
| 477 |
this.renderer_ = this.progressiveRender_ ? |
| 478 |
new goog.ui.emoji.ProgressiveEmojiPaletteRenderer(this.defaultImgUrl_) : |
| 479 |
new goog.ui.emoji.EmojiPaletteRenderer(this.defaultImgUrl_); |
| 480 |
|
| 481 |
for (var i = 0; i < this.emoji_.length; i++) { |
| 482 |
var emoji = this.emoji_[i].emoji; |
| 483 |
var page = this.delayedLoad_ ? this.createPlaceholderEmojiPage_(emoji) : |
| 484 |
this.createEmojiPage_(emoji, i); |
| 485 |
this.pages_.push(page); |
| 486 |
} |
| 487 |
|
| 488 |
this.activePage_ = 0; |
| 489 |
this.getElement().tabIndex = 0; |
| 490 |
}; |
| 491 |
|
| 492 |
|
| 493 |
/** |
| 494 |
* Used by unittests to manually load the animated emoji for this picker. |
| 495 |
*/ |
| 496 |
goog.ui.emoji.EmojiPicker.prototype.manuallyLoadAnimatedEmoji = function() { |
| 497 |
for (var i = 0; i < this.pages_.length; i++) { |
| 498 |
this.pages_[i].loadAnimatedEmoji(); |
| 499 |
} |
| 500 |
}; |
| 501 |
|
| 502 |
|
| 503 |
/** |
| 504 |
* Creates a page if it has not already been loaded. This has the side effects |
| 505 |
* of setting the load status of the page to true. |
| 506 |
* |
| 507 |
* @param {Array<Array<string>>} emoji Emoji for this page. See |
| 508 |
* {@link addEmojiGroup} for more details. |
| 509 |
* @param {number} index Index of the page in the emojipicker. |
| 510 |
* @return {goog.ui.emoji.EmojiPalette} the emoji page. |
| 511 |
* @private |
| 512 |
*/ |
| 513 |
goog.ui.emoji.EmojiPicker.prototype.createEmojiPage_ = function(emoji, index) { |
| 514 |
// Safeguard against trying to create the same page twice |
| 515 |
if (this.pageLoadStatus_[index]) { |
| 516 |
return null; |
| 517 |
} |
| 518 |
|
| 519 |
var palette = new goog.ui.emoji.EmojiPalette( |
| 520 |
emoji, this.urlPrefix_, this.renderer_, this.getDomHelper()); |
| 521 |
if (!this.manualLoadOfAnimatedEmoji_) { |
| 522 |
palette.loadAnimatedEmoji(); |
| 523 |
} |
| 524 |
palette.setSize(this.numCols_, this.numRows_); |
| 525 |
palette.setSupportedState(goog.ui.Component.State.FOCUSED, this.focusable_); |
| 526 |
palette.createDom(); |
| 527 |
palette.setParent(this); |
| 528 |
|
| 529 |
this.pageLoadStatus_[index] = true; |
| 530 |
|
| 531 |
return palette; |
| 532 |
}; |
| 533 |
|
| 534 |
|
| 535 |
/** |
| 536 |
* Returns an array of emoji whose real URLs have been replaced with the |
| 537 |
* default img URL. Used for delayed loading. |
| 538 |
* |
| 539 |
* @param {Array<Array<string>>} emoji Original emoji array. |
| 540 |
* @return {!Array<!Array<string>>} emoji array with all emoji pointing to the |
| 541 |
* default img. |
| 542 |
* @private |
| 543 |
*/ |
| 544 |
goog.ui.emoji.EmojiPicker.prototype.getPlaceholderEmoji_ = function(emoji) { |
| 545 |
var placeholderEmoji = []; |
| 546 |
|
| 547 |
for (var i = 0; i < emoji.length; i++) { |
| 548 |
placeholderEmoji.push([this.defaultImgUrl_, emoji[i][1]]); |
| 549 |
} |
| 550 |
|
| 551 |
return placeholderEmoji; |
| 552 |
}; |
| 553 |
|
| 554 |
|
| 555 |
/** |
| 556 |
* Creates an emoji page using placeholder emoji pointing to the default |
| 557 |
* img instead of the real emoji. Used for delayed loading. |
| 558 |
* |
| 559 |
* @param {Array<Array<string>>} emoji Emoji for this page. See |
| 560 |
* {@link addEmojiGroup} for more details. |
| 561 |
* @return {!goog.ui.emoji.EmojiPalette} the emoji page. |
| 562 |
* @private |
| 563 |
*/ |
| 564 |
goog.ui.emoji.EmojiPicker.prototype.createPlaceholderEmojiPage_ = function( |
| 565 |
emoji) { |
| 566 |
var placeholderEmoji = this.getPlaceholderEmoji_(emoji); |
| 567 |
|
| 568 |
var palette = new goog.ui.emoji.EmojiPalette( |
| 569 |
placeholderEmoji, |
| 570 |
null, // no url prefix |
| 571 |
this.renderer_, this.getDomHelper()); |
| 572 |
palette.setSize(this.numCols_, this.numRows_); |
| 573 |
palette.setSupportedState(goog.ui.Component.State.FOCUSED, this.focusable_); |
| 574 |
palette.createDom(); |
| 575 |
palette.setParent(this); |
| 576 |
|
| 577 |
return palette; |
| 578 |
}; |
| 579 |
|
| 580 |
|
| 581 |
/** |
| 582 |
* EmojiPickers cannot be used to decorate pre-existing html, since the |
| 583 |
* structure they build is fairly complicated. |
| 584 |
* @param {Element} element Element to decorate. |
| 585 |
* @return {boolean} Returns always false. |
| 586 |
* @override |
| 587 |
*/ |
| 588 |
goog.ui.emoji.EmojiPicker.prototype.canDecorate = function(element) { |
| 589 |
return false; |
| 590 |
}; |
| 591 |
|
| 592 |
|
| 593 |
/** |
| 594 |
* @override |
| 595 |
* @suppress {deprecated} Using deprecated goog.ui.TabPane. |
| 596 |
*/ |
| 597 |
goog.ui.emoji.EmojiPicker.prototype.enterDocument = function() { |
| 598 |
goog.ui.emoji.EmojiPicker.superClass_.enterDocument.call(this); |
| 599 |
|
| 600 |
for (var i = 0; i < this.pages_.length; i++) { |
| 601 |
this.pages_[i].enterDocument(); |
| 602 |
var pageElement = this.pages_[i].getElement(); |
| 603 |
|
| 604 |
// Add a new tab to the tabpane if there's more than one group of emoji. |
| 605 |
// If there is just one group of emoji, then we simply use the single |
| 606 |
// page's element as the content for the picker |
| 607 |
if (this.pages_.length > 1) { |
| 608 |
// Create a simple default title containg the page number if the title |
| 609 |
// was not provided in the emoji group params |
| 610 |
var title = this.emoji_[i].title || (i + 1); |
| 611 |
this.tabPane_.addPage( |
| 612 |
new goog.ui.TabPane.TabPage(pageElement, title, this.getDomHelper())); |
| 613 |
} else { |
| 614 |
this.getElement().appendChild(pageElement); |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
// Initialize listeners. Note that we need to initialize this listener |
| 619 |
// after createDom, because addPage causes the goog.ui.TabPane.Events.CHANGE |
| 620 |
// event to fire, but we only want the handler (which loads delayed images) |
| 621 |
// to run after the picker has been constructed. |
| 622 |
if (this.tabPane_) { |
| 623 |
this.getHandler().listen( |
| 624 |
this.tabPane_, goog.ui.TabPane.Events.CHANGE, this.onPageChanged_); |
| 625 |
|
| 626 |
// Make the tabpane unselectable so that changing tabs doesn't disturb the |
| 627 |
// cursor |
| 628 |
goog.style.setUnselectable(this.tabPane_.getElement(), true); |
| 629 |
} |
| 630 |
|
| 631 |
this.getElement().unselectable = 'on'; |
| 632 |
}; |
| 633 |
|
| 634 |
|
| 635 |
/** @override */ |
| 636 |
goog.ui.emoji.EmojiPicker.prototype.exitDocument = function() { |
| 637 |
goog.ui.emoji.EmojiPicker.superClass_.exitDocument.call(this); |
| 638 |
for (var i = 0; i < this.pages_.length; i++) { |
| 639 |
this.pages_[i].exitDocument(); |
| 640 |
} |
| 641 |
}; |
| 642 |
|
| 643 |
|
| 644 |
/** @override */ |
| 645 |
goog.ui.emoji.EmojiPicker.prototype.disposeInternal = function() { |
| 646 |
goog.ui.emoji.EmojiPicker.superClass_.disposeInternal.call(this); |
| 647 |
|
| 648 |
if (this.tabPane_) { |
| 649 |
this.tabPane_.dispose(); |
| 650 |
this.tabPane_ = null; |
| 651 |
} |
| 652 |
|
| 653 |
for (var i = 0; i < this.pages_.length; i++) { |
| 654 |
this.pages_[i].dispose(); |
| 655 |
} |
| 656 |
this.pages_.length = 0; |
| 657 |
}; |
| 658 |
|
| 659 |
|
| 660 |
/** |
| 661 |
* @return {string} CSS class for the root element of EmojiPicker. |
| 662 |
*/ |
| 663 |
goog.ui.emoji.EmojiPicker.prototype.getCssClass = function() { |
| 664 |
return goog.getCssName('goog-ui-emojipicker'); |
| 665 |
}; |
| 666 |
|
| 667 |
|
| 668 |
/** |
| 669 |
* Returns the currently selected emoji from this picker. If the picker is |
| 670 |
* using the URL prefix optimization, allocates a new emoji object with the |
| 671 |
* full URL. This method is meant to be used by clients of the emojipicker, |
| 672 |
* e.g., in a listener on goog.ui.component.EventType.ACTION that wants to use |
| 673 |
* the just-selected emoji. |
| 674 |
* |
| 675 |
* @return {goog.ui.emoji.Emoji} The currently selected emoji from this picker. |
| 676 |
*/ |
| 677 |
goog.ui.emoji.EmojiPicker.prototype.getSelectedEmoji = function() { |
| 678 |
return this.urlPrefix_ ? |
| 679 |
new goog.ui.emoji.Emoji( |
| 680 |
this.urlPrefix_ + this.selectedEmoji_.getId(), |
| 681 |
this.selectedEmoji_.getId()) : |
| 682 |
this.selectedEmoji_; |
| 683 |
}; |
| 684 |
|
| 685 |
|
| 686 |
/** |
| 687 |
* Returns the number of emoji groups in this picker. |
| 688 |
* |
| 689 |
* @return {number} The number of emoji groups in this picker. |
| 690 |
*/ |
| 691 |
goog.ui.emoji.EmojiPicker.prototype.getNumEmojiGroups = function() { |
| 692 |
return this.emoji_.length; |
| 693 |
}; |
| 694 |
|
| 695 |
|
| 696 |
/** |
| 697 |
* Returns a page from the picker. This should be considered protected, and is |
| 698 |
* ONLY FOR TESTING. |
| 699 |
* |
| 700 |
* @param {number} index Index of the page to return. |
| 701 |
* @return {goog.ui.emoji.EmojiPalette?} the page at the specified index or null |
| 702 |
* if none exists. |
| 703 |
*/ |
| 704 |
goog.ui.emoji.EmojiPicker.prototype.getPage = function(index) { |
| 705 |
return this.pages_[index]; |
| 706 |
}; |
| 707 |
|
| 708 |
|
| 709 |
/** |
| 710 |
* Returns all the pages from the picker. This should be considered protected, |
| 711 |
* and is ONLY FOR TESTING. |
| 712 |
* |
| 713 |
* @return {Array<goog.ui.emoji.EmojiPalette>?} the pages in the picker or |
| 714 |
* null if none exist. |
| 715 |
*/ |
| 716 |
goog.ui.emoji.EmojiPicker.prototype.getPages = function() { |
| 717 |
return this.pages_; |
| 718 |
}; |
| 719 |
|
| 720 |
|
| 721 |
/** |
| 722 |
* Returns the tabpane if this is a multipage picker. This should be considered |
| 723 |
* protected, and is ONLY FOR TESTING. |
| 724 |
* |
| 725 |
* @return {goog.ui.TabPane} the tabpane if it is a multipage picker, |
| 726 |
* or null if it does not exist or is a single page picker. |
| 727 |
*/ |
| 728 |
goog.ui.emoji.EmojiPicker.prototype.getTabPane = function() { |
| 729 |
return this.tabPane_; |
| 730 |
}; |
| 731 |
|
| 732 |
|
| 733 |
/** |
| 734 |
* @return {goog.ui.emoji.EmojiPalette} The active page of the emoji picker. |
| 735 |
* @private |
| 736 |
*/ |
| 737 |
goog.ui.emoji.EmojiPicker.prototype.getActivePage_ = function() { |
| 738 |
return this.pages_[this.activePage_]; |
| 739 |
}; |
| 740 |
|
| 741 |
|
| 742 |
/** |
| 743 |
* Handles actions from the EmojiPalettes that this picker contains. |
| 744 |
* |
| 745 |
* @param {goog.ui.Component.EventType} e The event object. |
| 746 |
* @private |
| 747 |
*/ |
| 748 |
goog.ui.emoji.EmojiPicker.prototype.onEmojiPaletteAction_ = function(e) { |
| 749 |
this.selectedEmoji_ = this.getActivePage_().getSelectedEmoji(); |
| 750 |
}; |
| 751 |
|
| 752 |
|
| 753 |
/** |
| 754 |
* Handles changes in the active page in the tabpane. |
| 755 |
* |
| 756 |
* @param {goog.ui.TabPaneEvent} e The event object. |
| 757 |
* @private |
| 758 |
*/ |
| 759 |
goog.ui.emoji.EmojiPicker.prototype.onPageChanged_ = function(e) { |
| 760 |
var index = /** @type {number} */ (e.page.getIndex()); |
| 761 |
this.loadPage_(index); |
| 762 |
this.activePage_ = index; |
| 763 |
}; |
| 764 |
|
| 765 |
|
| 766 |
/** |
| 767 |
* Loads a page into the picker if it has not yet been loaded. |
| 768 |
* |
| 769 |
* @param {number} index Index of the page to load. |
| 770 |
* @private |
| 771 |
* @suppress {deprecated} Using deprecated goog.ui.TabPane. |
| 772 |
*/ |
| 773 |
goog.ui.emoji.EmojiPicker.prototype.loadPage_ = function(index) { |
| 774 |
if (index < 0 || index > this.pages_.length) { |
| 775 |
throw new Error('Index out of bounds'); |
| 776 |
} |
| 777 |
|
| 778 |
if (!this.pageLoadStatus_[index]) { |
| 779 |
var oldPage = this.pages_[index]; |
| 780 |
this.pages_[index] = this.createEmojiPage_(this.emoji_[index].emoji, index); |
| 781 |
this.pages_[index].enterDocument(); |
| 782 |
var pageElement = this.pages_[index].getElement(); |
| 783 |
if (this.pages_.length > 1) { |
| 784 |
this.tabPane_.removePage(index); |
| 785 |
var title = this.emoji_[index].title || (index + 1); |
| 786 |
this.tabPane_.addPage( |
| 787 |
new goog.ui.TabPane.TabPage(pageElement, title, this.getDomHelper()), |
| 788 |
index); |
| 789 |
this.tabPane_.setSelectedIndex(index); |
| 790 |
} else { |
| 791 |
var el = this.getElement(); |
| 792 |
el.appendChild(pageElement); |
| 793 |
} |
| 794 |
if (oldPage) { |
| 795 |
oldPage.dispose(); |
| 796 |
} |
| 797 |
} |
| 798 |
}; |