| 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 Popup Emoji Picker implementation. This provides a UI widget |
| 17 |
* for choosing an emoji from a grid of possible choices. The widget is a popup, |
| 18 |
* so it is suitable for a toolbar, for instance the TrogEdit toolbar. |
| 19 |
* |
| 20 |
* @see ../demos/popupemojipicker.html for an example of how to instantiate |
| 21 |
* an emoji picker. |
| 22 |
* |
| 23 |
* See goog.ui.emoji.EmojiPicker in emojipicker.js for more details. |
| 24 |
* |
| 25 |
* Based on goog.ui.PopupColorPicker (popupcolorpicker.js). |
| 26 |
* |
| 27 |
* @see ../../demos/popupemojipicker.html |
| 28 |
*/ |
| 29 |
|
| 30 |
goog.provide('goog.ui.emoji.PopupEmojiPicker'); |
| 31 |
|
| 32 |
goog.require('goog.events.EventType'); |
| 33 |
goog.require('goog.positioning.AnchoredPosition'); |
| 34 |
goog.require('goog.positioning.Corner'); |
| 35 |
goog.require('goog.ui.Component'); |
| 36 |
goog.require('goog.ui.Popup'); |
| 37 |
goog.require('goog.ui.emoji.EmojiPicker'); |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* Constructs a popup emoji picker widget. |
| 43 |
* |
| 44 |
* @param {string} defaultImgUrl Url of the img that should be used to fill up |
| 45 |
* the cells in the emoji table, to prevent jittering. Should be the same |
| 46 |
* size as the emoji. |
| 47 |
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper. |
| 48 |
* @extends {goog.ui.Component} |
| 49 |
* @constructor |
| 50 |
* @final |
| 51 |
*/ |
| 52 |
goog.ui.emoji.PopupEmojiPicker = function(defaultImgUrl, opt_domHelper) { |
| 53 |
goog.ui.Component.call(this, opt_domHelper); |
| 54 |
|
| 55 |
this.emojiPicker_ = |
| 56 |
new goog.ui.emoji.EmojiPicker(defaultImgUrl, opt_domHelper); |
| 57 |
this.addChild(this.emojiPicker_); |
| 58 |
|
| 59 |
this.getHandler().listen( |
| 60 |
this.emojiPicker_, goog.ui.Component.EventType.ACTION, |
| 61 |
this.onEmojiPicked_); |
| 62 |
}; |
| 63 |
goog.inherits(goog.ui.emoji.PopupEmojiPicker, goog.ui.Component); |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* Instance of an emoji picker control. |
| 68 |
* @type {goog.ui.emoji.EmojiPicker} |
| 69 |
* @private |
| 70 |
*/ |
| 71 |
goog.ui.emoji.PopupEmojiPicker.prototype.emojiPicker_ = null; |
| 72 |
|
| 73 |
|
| 74 |
/** |
| 75 |
* Instance of goog.ui.Popup used to manage the behavior of the emoji picker. |
| 76 |
* @type {goog.ui.Popup} |
| 77 |
* @private |
| 78 |
*/ |
| 79 |
goog.ui.emoji.PopupEmojiPicker.prototype.popup_ = null; |
| 80 |
|
| 81 |
|
| 82 |
/** |
| 83 |
* Reference to the element that triggered the last popup. |
| 84 |
* @type {Element} |
| 85 |
* @private |
| 86 |
*/ |
| 87 |
goog.ui.emoji.PopupEmojiPicker.prototype.lastTarget_ = null; |
| 88 |
|
| 89 |
|
| 90 |
/** |
| 91 |
* Whether the emoji picker can accept focus. |
| 92 |
* @type {boolean} |
| 93 |
* @private |
| 94 |
*/ |
| 95 |
goog.ui.emoji.PopupEmojiPicker.prototype.focusable_ = true; |
| 96 |
|
| 97 |
|
| 98 |
/** |
| 99 |
* If true, then the emojipicker will toggle off if it is already visible. |
| 100 |
* Default is true. |
| 101 |
* @type {boolean} |
| 102 |
* @private |
| 103 |
*/ |
| 104 |
goog.ui.emoji.PopupEmojiPicker.prototype.toggleMode_ = true; |
| 105 |
|
| 106 |
|
| 107 |
/** |
| 108 |
* Adds a group of emoji to the picker. |
| 109 |
* |
| 110 |
* @param {string|Element} title Title for the group. |
| 111 |
* @param {Array<Array<?>>} emojiGroup A new group of emoji to be added. Each |
| 112 |
* internal array contains [emojiUrl, emojiId]. |
| 113 |
*/ |
| 114 |
goog.ui.emoji.PopupEmojiPicker.prototype.addEmojiGroup = function( |
| 115 |
title, emojiGroup) { |
| 116 |
this.emojiPicker_.addEmojiGroup(title, emojiGroup); |
| 117 |
}; |
| 118 |
|
| 119 |
|
| 120 |
/** |
| 121 |
* Sets whether the emoji picker should toggle if it is already open. |
| 122 |
* @param {boolean} toggle The toggle mode to use. |
| 123 |
*/ |
| 124 |
goog.ui.emoji.PopupEmojiPicker.prototype.setToggleMode = function(toggle) { |
| 125 |
this.toggleMode_ = toggle; |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
/** |
| 130 |
* Gets whether the emojipicker is in toggle mode |
| 131 |
* @return {boolean} toggle. |
| 132 |
*/ |
| 133 |
goog.ui.emoji.PopupEmojiPicker.prototype.getToggleMode = function() { |
| 134 |
return this.toggleMode_; |
| 135 |
}; |
| 136 |
|
| 137 |
|
| 138 |
/** |
| 139 |
* Sets whether loading of images should be delayed until after dom creation. |
| 140 |
* Thus, this function must be called before {@link #createDom}. If set to true, |
| 141 |
* the client must call {@link #loadImages} when they wish the images to be |
| 142 |
* loaded. |
| 143 |
* |
| 144 |
* @param {boolean} shouldDelay Whether to delay loading the images. |
| 145 |
*/ |
| 146 |
goog.ui.emoji.PopupEmojiPicker.prototype.setDelayedLoad = function( |
| 147 |
shouldDelay) { |
| 148 |
if (this.emojiPicker_) { |
| 149 |
this.emojiPicker_.setDelayedLoad(shouldDelay); |
| 150 |
} |
| 151 |
}; |
| 152 |
|
| 153 |
|
| 154 |
/** |
| 155 |
* Sets whether the emoji picker can accept focus. |
| 156 |
* @param {boolean} focusable Whether the emoji picker should accept focus. |
| 157 |
*/ |
| 158 |
goog.ui.emoji.PopupEmojiPicker.prototype.setFocusable = function(focusable) { |
| 159 |
this.focusable_ = focusable; |
| 160 |
if (this.emojiPicker_) { |
| 161 |
// TODO(user): In next revision sort the behavior of passing state to |
| 162 |
// children correctly |
| 163 |
this.emojiPicker_.setFocusable(focusable); |
| 164 |
} |
| 165 |
}; |
| 166 |
|
| 167 |
|
| 168 |
/** |
| 169 |
* Sets the URL prefix for the emoji URLs. |
| 170 |
* |
| 171 |
* @param {string} urlPrefix Prefix that should be prepended to all URLs. |
| 172 |
*/ |
| 173 |
goog.ui.emoji.PopupEmojiPicker.prototype.setUrlPrefix = function(urlPrefix) { |
| 174 |
this.emojiPicker_.setUrlPrefix(urlPrefix); |
| 175 |
}; |
| 176 |
|
| 177 |
|
| 178 |
/** |
| 179 |
* Sets the location of the tabs in relation to the emoji grids. This should |
| 180 |
* only be called before the picker has been rendered. |
| 181 |
* |
| 182 |
* @param {goog.ui.TabPane.TabLocation} tabLocation The location of the tabs. |
| 183 |
*/ |
| 184 |
goog.ui.emoji.PopupEmojiPicker.prototype.setTabLocation = function( |
| 185 |
tabLocation) { |
| 186 |
this.emojiPicker_.setTabLocation(tabLocation); |
| 187 |
}; |
| 188 |
|
| 189 |
|
| 190 |
/** |
| 191 |
* Sets the number of rows per grid in the emoji picker. This should only be |
| 192 |
* called before the picker has been rendered. |
| 193 |
* |
| 194 |
* @param {number} numRows Number of rows per grid. |
| 195 |
*/ |
| 196 |
goog.ui.emoji.PopupEmojiPicker.prototype.setNumRows = function(numRows) { |
| 197 |
this.emojiPicker_.setNumRows(numRows); |
| 198 |
}; |
| 199 |
|
| 200 |
|
| 201 |
/** |
| 202 |
* Sets the number of columns per grid in the emoji picker. This should only be |
| 203 |
* called before the picker has been rendered. |
| 204 |
* |
| 205 |
* @param {number} numCols Number of columns per grid. |
| 206 |
*/ |
| 207 |
goog.ui.emoji.PopupEmojiPicker.prototype.setNumColumns = function(numCols) { |
| 208 |
this.emojiPicker_.setNumColumns(numCols); |
| 209 |
}; |
| 210 |
|
| 211 |
|
| 212 |
/** |
| 213 |
* Sets the progressive rendering aspect of this emojipicker. Must be called |
| 214 |
* before createDom to have an effect. |
| 215 |
* |
| 216 |
* @param {boolean} progressive Whether the picker should render progressively. |
| 217 |
*/ |
| 218 |
goog.ui.emoji.PopupEmojiPicker.prototype.setProgressiveRender = function( |
| 219 |
progressive) { |
| 220 |
if (this.emojiPicker_) { |
| 221 |
this.emojiPicker_.setProgressiveRender(progressive); |
| 222 |
} |
| 223 |
}; |
| 224 |
|
| 225 |
|
| 226 |
/** |
| 227 |
* Returns the number of emoji groups in this picker. |
| 228 |
* |
| 229 |
* @return {number} The number of emoji groups in this picker. |
| 230 |
*/ |
| 231 |
goog.ui.emoji.PopupEmojiPicker.prototype.getNumEmojiGroups = function() { |
| 232 |
return this.emojiPicker_.getNumEmojiGroups(); |
| 233 |
}; |
| 234 |
|
| 235 |
|
| 236 |
/** |
| 237 |
* Causes the emoji imgs to be loaded into the picker. Used for delayed loading. |
| 238 |
*/ |
| 239 |
goog.ui.emoji.PopupEmojiPicker.prototype.loadImages = function() { |
| 240 |
if (this.emojiPicker_) { |
| 241 |
this.emojiPicker_.loadImages(); |
| 242 |
} |
| 243 |
}; |
| 244 |
|
| 245 |
|
| 246 |
/** @override */ |
| 247 |
goog.ui.emoji.PopupEmojiPicker.prototype.createDom = function() { |
| 248 |
goog.ui.emoji.PopupEmojiPicker.superClass_.createDom.call(this); |
| 249 |
|
| 250 |
this.emojiPicker_.createDom(); |
| 251 |
|
| 252 |
this.getElement().className = goog.getCssName('goog-ui-popupemojipicker'); |
| 253 |
this.getElement().appendChild(this.emojiPicker_.getElement()); |
| 254 |
|
| 255 |
this.popup_ = new goog.ui.Popup(this.getElement()); |
| 256 |
this.getElement().unselectable = 'on'; |
| 257 |
}; |
| 258 |
|
| 259 |
|
| 260 |
/** @override */ |
| 261 |
goog.ui.emoji.PopupEmojiPicker.prototype.disposeInternal = function() { |
| 262 |
goog.ui.emoji.PopupEmojiPicker.superClass_.disposeInternal.call(this); |
| 263 |
this.emojiPicker_ = null; |
| 264 |
this.lastTarget_ = null; |
| 265 |
if (this.popup_) { |
| 266 |
this.popup_.dispose(); |
| 267 |
this.popup_ = null; |
| 268 |
} |
| 269 |
}; |
| 270 |
|
| 271 |
|
| 272 |
/** |
| 273 |
* Attaches the popup emoji picker to an element. |
| 274 |
* |
| 275 |
* @param {Element} element The element to attach to. |
| 276 |
*/ |
| 277 |
goog.ui.emoji.PopupEmojiPicker.prototype.attach = function(element) { |
| 278 |
// TODO(user): standardize event type, popups should use MOUSEDOWN, but |
| 279 |
// currently apps are using click. |
| 280 |
this.getHandler().listen(element, goog.events.EventType.CLICK, this.show_); |
| 281 |
}; |
| 282 |
|
| 283 |
|
| 284 |
/** |
| 285 |
* Detatches the popup emoji picker from an element. |
| 286 |
* |
| 287 |
* @param {Element} element The element to detach from. |
| 288 |
*/ |
| 289 |
goog.ui.emoji.PopupEmojiPicker.prototype.detach = function(element) { |
| 290 |
this.getHandler().unlisten(element, goog.events.EventType.CLICK, this.show_); |
| 291 |
}; |
| 292 |
|
| 293 |
|
| 294 |
/** |
| 295 |
* @return {goog.ui.emoji.EmojiPicker} The emoji picker instance. |
| 296 |
*/ |
| 297 |
goog.ui.emoji.PopupEmojiPicker.prototype.getEmojiPicker = function() { |
| 298 |
return this.emojiPicker_; |
| 299 |
}; |
| 300 |
|
| 301 |
|
| 302 |
/** |
| 303 |
* Returns whether the Popup dismisses itself when the user clicks outside of |
| 304 |
* it. |
| 305 |
* @return {boolean} Whether the Popup autohides on an external click. |
| 306 |
*/ |
| 307 |
goog.ui.emoji.PopupEmojiPicker.prototype.getAutoHide = function() { |
| 308 |
return !!this.popup_ && this.popup_.getAutoHide(); |
| 309 |
}; |
| 310 |
|
| 311 |
|
| 312 |
/** |
| 313 |
* Sets whether the Popup dismisses itself when the user clicks outside of it - |
| 314 |
* must be called after the Popup has been created (in createDom()), |
| 315 |
* otherwise it does nothing. |
| 316 |
* |
| 317 |
* @param {boolean} autoHide Whether to autohide on an external click. |
| 318 |
*/ |
| 319 |
goog.ui.emoji.PopupEmojiPicker.prototype.setAutoHide = function(autoHide) { |
| 320 |
if (this.popup_) { |
| 321 |
this.popup_.setAutoHide(autoHide); |
| 322 |
} |
| 323 |
}; |
| 324 |
|
| 325 |
|
| 326 |
/** |
| 327 |
* Returns the region inside which the Popup dismisses itself when the user |
| 328 |
* clicks, or null if it was not set. Null indicates the entire document is |
| 329 |
* the autohide region. |
| 330 |
* @return {Element} The DOM element for autohide, or null if it hasn't been |
| 331 |
* set. |
| 332 |
*/ |
| 333 |
goog.ui.emoji.PopupEmojiPicker.prototype.getAutoHideRegion = function() { |
| 334 |
return this.popup_ && this.popup_.getAutoHideRegion(); |
| 335 |
}; |
| 336 |
|
| 337 |
|
| 338 |
/** |
| 339 |
* Sets the region inside which the Popup dismisses itself when the user |
| 340 |
* clicks - must be called after the Popup has been created (in createDom()), |
| 341 |
* otherwise it does nothing. |
| 342 |
* |
| 343 |
* @param {Element} element The DOM element for autohide. |
| 344 |
*/ |
| 345 |
goog.ui.emoji.PopupEmojiPicker.prototype.setAutoHideRegion = function(element) { |
| 346 |
if (this.popup_) { |
| 347 |
this.popup_.setAutoHideRegion(element); |
| 348 |
} |
| 349 |
}; |
| 350 |
|
| 351 |
|
| 352 |
/** |
| 353 |
* Returns the {@link goog.ui.PopupBase} from this picker. Returns null if the |
| 354 |
* popup has not yet been created. |
| 355 |
* |
| 356 |
* NOTE: This should *ONLY* be called from tests. If called before createDom(), |
| 357 |
* this should return null. |
| 358 |
* |
| 359 |
* @return {goog.ui.PopupBase?} The popup, or null if it hasn't been created. |
| 360 |
*/ |
| 361 |
goog.ui.emoji.PopupEmojiPicker.prototype.getPopup = function() { |
| 362 |
return this.popup_; |
| 363 |
}; |
| 364 |
|
| 365 |
|
| 366 |
/** |
| 367 |
* @return {Element} The last element that triggered the popup. |
| 368 |
*/ |
| 369 |
goog.ui.emoji.PopupEmojiPicker.prototype.getLastTarget = function() { |
| 370 |
return this.lastTarget_; |
| 371 |
}; |
| 372 |
|
| 373 |
|
| 374 |
/** |
| 375 |
* @return {goog.ui.emoji.Emoji} The currently selected emoji. |
| 376 |
*/ |
| 377 |
goog.ui.emoji.PopupEmojiPicker.prototype.getSelectedEmoji = function() { |
| 378 |
return this.emojiPicker_.getSelectedEmoji(); |
| 379 |
}; |
| 380 |
|
| 381 |
|
| 382 |
/** |
| 383 |
* Handles click events on the element this picker is attached to and shows the |
| 384 |
* emoji picker in a popup. |
| 385 |
* |
| 386 |
* @param {goog.events.BrowserEvent} e The browser event. |
| 387 |
* @private |
| 388 |
*/ |
| 389 |
goog.ui.emoji.PopupEmojiPicker.prototype.show_ = function(e) { |
| 390 |
if (this.popup_.isOrWasRecentlyVisible() && this.toggleMode_ && |
| 391 |
this.lastTarget_ == e.currentTarget) { |
| 392 |
this.popup_.setVisible(false); |
| 393 |
return; |
| 394 |
} |
| 395 |
|
| 396 |
this.lastTarget_ = /** @type {Element} */ (e.currentTarget); |
| 397 |
this.popup_.setPosition( |
| 398 |
new goog.positioning.AnchoredPosition( |
| 399 |
this.lastTarget_, goog.positioning.Corner.BOTTOM_LEFT)); |
| 400 |
this.popup_.setVisible(true); |
| 401 |
}; |
| 402 |
|
| 403 |
|
| 404 |
/** |
| 405 |
* Handles selection of an emoji. |
| 406 |
* |
| 407 |
* @param {goog.events.Event} e The event object. |
| 408 |
* @private |
| 409 |
*/ |
| 410 |
goog.ui.emoji.PopupEmojiPicker.prototype.onEmojiPicked_ = function(e) { |
| 411 |
this.popup_.setVisible(false); |
| 412 |
}; |