| 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 Palette implementation. This provides a UI widget for |
| 17 |
* choosing an emoji from a palette of possible choices. EmojiPalettes are |
| 18 |
* contained within EmojiPickers. |
| 19 |
* |
| 20 |
* See ../demos/popupemojipicker.html for an example of how to instantiate |
| 21 |
* an emoji picker. |
| 22 |
* |
| 23 |
* Based on goog.ui.ColorPicker (colorpicker.js). |
| 24 |
* |
| 25 |
*/ |
| 26 |
|
| 27 |
goog.provide('goog.ui.emoji.EmojiPalette'); |
| 28 |
|
| 29 |
goog.require('goog.events.EventType'); |
| 30 |
goog.require('goog.net.ImageLoader'); |
| 31 |
goog.require('goog.ui.Palette'); |
| 32 |
goog.require('goog.ui.emoji.Emoji'); |
| 33 |
goog.require('goog.ui.emoji.EmojiPaletteRenderer'); |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* A page of emoji to be displayed in an EmojiPicker. |
| 39 |
* |
| 40 |
* @param {Array<Array<?>>} emoji List of emoji for this page. |
| 41 |
* @param {?string=} opt_urlPrefix Prefix that should be prepended to all URL. |
| 42 |
* @param {goog.ui.PaletteRenderer=} opt_renderer Renderer used to render or |
| 43 |
* decorate the palette; defaults to {@link goog.ui.PaletteRenderer}. |
| 44 |
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper. |
| 45 |
* @extends {goog.ui.Palette} |
| 46 |
* @constructor |
| 47 |
* @final |
| 48 |
*/ |
| 49 |
goog.ui.emoji.EmojiPalette = function( |
| 50 |
emoji, opt_urlPrefix, opt_renderer, opt_domHelper) { |
| 51 |
goog.ui.Palette.call( |
| 52 |
this, null, opt_renderer || new goog.ui.emoji.EmojiPaletteRenderer(null), |
| 53 |
opt_domHelper); |
| 54 |
/** |
| 55 |
* All the different emoji that this palette can display. Maps emoji ids |
| 56 |
* (string) to the goog.ui.emoji.Emoji for that id. |
| 57 |
* |
| 58 |
* @type {Object} |
| 59 |
* @private |
| 60 |
*/ |
| 61 |
this.emojiCells_ = {}; |
| 62 |
|
| 63 |
/** |
| 64 |
* Map of emoji id to index into this.emojiCells_. |
| 65 |
* |
| 66 |
* @type {Object} |
| 67 |
* @private |
| 68 |
*/ |
| 69 |
this.emojiMap_ = {}; |
| 70 |
|
| 71 |
/** |
| 72 |
* List of the animated emoji in this palette. Each internal array is of type |
| 73 |
* [HTMLDivElement, goog.ui.emoji.Emoji], and represents the palette item |
| 74 |
* for that animated emoji, and the Emoji object. |
| 75 |
* |
| 76 |
* @type {Array<Array<(HTMLDivElement|goog.ui.emoji.Emoji)>>} |
| 77 |
* @private |
| 78 |
*/ |
| 79 |
this.animatedEmoji_ = []; |
| 80 |
|
| 81 |
this.urlPrefix_ = opt_urlPrefix || ''; |
| 82 |
|
| 83 |
/** |
| 84 |
* Palette items that are displayed on this page of the emoji picker. Each |
| 85 |
* item is a div wrapped around a div or an img. |
| 86 |
* |
| 87 |
* @type {Array<HTMLDivElement>} |
| 88 |
* @private |
| 89 |
*/ |
| 90 |
this.emoji_ = this.getEmojiArrayFromProperties_(emoji); |
| 91 |
|
| 92 |
this.setContent(this.emoji_); |
| 93 |
}; |
| 94 |
goog.inherits(goog.ui.emoji.EmojiPalette, goog.ui.Palette); |
| 95 |
|
| 96 |
|
| 97 |
/** |
| 98 |
* Indicates a prefix that should be prepended to all URLs of images in this |
| 99 |
* emojipalette. This provides an optimization if the URLs are long, so that |
| 100 |
* the client does not have to send a long string for each emoji. |
| 101 |
* |
| 102 |
* @type {string} |
| 103 |
* @private |
| 104 |
*/ |
| 105 |
goog.ui.emoji.EmojiPalette.prototype.urlPrefix_ = ''; |
| 106 |
|
| 107 |
|
| 108 |
/** |
| 109 |
* Whether the emoji images have been loaded. |
| 110 |
* |
| 111 |
* @type {boolean} |
| 112 |
* @private |
| 113 |
*/ |
| 114 |
goog.ui.emoji.EmojiPalette.prototype.imagesLoaded_ = false; |
| 115 |
|
| 116 |
|
| 117 |
/** |
| 118 |
* Image loader for loading animated emoji. |
| 119 |
* |
| 120 |
* @type {goog.net.ImageLoader} |
| 121 |
* @private |
| 122 |
*/ |
| 123 |
goog.ui.emoji.EmojiPalette.prototype.imageLoader_; |
| 124 |
|
| 125 |
|
| 126 |
/** |
| 127 |
* Helps create an array of emoji palette items from an array of emoji |
| 128 |
* properties. Each element will be either a div with background-image set to |
| 129 |
* a sprite, or an img element pointing directly to an emoji, and all elements |
| 130 |
* are wrapped with an outer div for alignment issues (i.e., this allows |
| 131 |
* centering the inner div). |
| 132 |
* |
| 133 |
* @param {Object} emojiGroup The group of emoji for this page. |
| 134 |
* @return {!Array<!HTMLDivElement>} The emoji items. |
| 135 |
* @private |
| 136 |
*/ |
| 137 |
goog.ui.emoji.EmojiPalette.prototype.getEmojiArrayFromProperties_ = function( |
| 138 |
emojiGroup) { |
| 139 |
var emojiItems = []; |
| 140 |
|
| 141 |
for (var i = 0; i < emojiGroup.length; i++) { |
| 142 |
var url = emojiGroup[i][0]; |
| 143 |
var id = emojiGroup[i][1]; |
| 144 |
var spriteInfo = emojiGroup[i][2]; |
| 145 |
var displayUrl = spriteInfo ? spriteInfo.getUrl() : this.urlPrefix_ + url; |
| 146 |
|
| 147 |
var item = this.getRenderer().createPaletteItem( |
| 148 |
this.getDomHelper(), id, spriteInfo, displayUrl); |
| 149 |
emojiItems.push(item); |
| 150 |
|
| 151 |
var emoji = new goog.ui.emoji.Emoji(url, id); |
| 152 |
this.emojiCells_[id] = emoji; |
| 153 |
this.emojiMap_[id] = i; |
| 154 |
|
| 155 |
// Keep track of sprited emoji that are animated, for later loading. |
| 156 |
if (spriteInfo && spriteInfo.isAnimated()) { |
| 157 |
this.animatedEmoji_.push([item, emoji]); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// Create the image loader now so that tests can access it before it has |
| 162 |
// started loading images. |
| 163 |
if (this.animatedEmoji_.length > 0) { |
| 164 |
this.imageLoader_ = new goog.net.ImageLoader(); |
| 165 |
} |
| 166 |
|
| 167 |
this.imagesLoaded_ = true; |
| 168 |
return emojiItems; |
| 169 |
}; |
| 170 |
|
| 171 |
|
| 172 |
/** |
| 173 |
* Sends off requests for all the animated emoji and replaces their static |
| 174 |
* sprites when the images are done downloading. |
| 175 |
*/ |
| 176 |
goog.ui.emoji.EmojiPalette.prototype.loadAnimatedEmoji = function() { |
| 177 |
if (this.animatedEmoji_.length > 0) { |
| 178 |
for (var i = 0; i < this.animatedEmoji_.length; i++) { |
| 179 |
var emoji = |
| 180 |
/** @type {goog.ui.emoji.Emoji} */ (this.animatedEmoji_[i][1]); |
| 181 |
var url = this.urlPrefix_ + emoji.getUrl(); |
| 182 |
|
| 183 |
this.imageLoader_.addImage(emoji.getId(), url); |
| 184 |
} |
| 185 |
|
| 186 |
this.getHandler().listen( |
| 187 |
this.imageLoader_, goog.events.EventType.LOAD, this.handleImageLoad_); |
| 188 |
this.imageLoader_.start(); |
| 189 |
} |
| 190 |
}; |
| 191 |
|
| 192 |
|
| 193 |
/** |
| 194 |
* Handles image load events from the ImageLoader. |
| 195 |
* |
| 196 |
* @param {goog.events.Event} e The event object. |
| 197 |
* @private |
| 198 |
*/ |
| 199 |
goog.ui.emoji.EmojiPalette.prototype.handleImageLoad_ = function(e) { |
| 200 |
var id = e.target.id; |
| 201 |
var url = e.target.src; |
| 202 |
// Just to be safe, we check to make sure we have an id and src url from |
| 203 |
// the event target, which the ImageLoader sets to an Image object. |
| 204 |
if (id && url) { |
| 205 |
var item = this.emoji_[this.emojiMap_[id]]; |
| 206 |
if (item) { |
| 207 |
this.getRenderer().updateAnimatedPaletteItem(item, e.target); |
| 208 |
} |
| 209 |
} |
| 210 |
}; |
| 211 |
|
| 212 |
|
| 213 |
/** |
| 214 |
* Returns the image loader that this palette uses. Used for testing. |
| 215 |
* |
| 216 |
* @return {goog.net.ImageLoader} the image loader. |
| 217 |
*/ |
| 218 |
goog.ui.emoji.EmojiPalette.prototype.getImageLoader = function() { |
| 219 |
return this.imageLoader_; |
| 220 |
}; |
| 221 |
|
| 222 |
|
| 223 |
/** @override */ |
| 224 |
goog.ui.emoji.EmojiPalette.prototype.disposeInternal = function() { |
| 225 |
goog.ui.emoji.EmojiPalette.superClass_.disposeInternal.call(this); |
| 226 |
|
| 227 |
if (this.imageLoader_) { |
| 228 |
this.imageLoader_.dispose(); |
| 229 |
this.imageLoader_ = null; |
| 230 |
} |
| 231 |
this.animatedEmoji_ = null; |
| 232 |
this.emojiCells_ = null; |
| 233 |
this.emojiMap_ = null; |
| 234 |
this.emoji_ = null; |
| 235 |
}; |
| 236 |
|
| 237 |
|
| 238 |
/** |
| 239 |
* Returns a goomoji id from an img or the containing td, or null if none |
| 240 |
* exists for that element. |
| 241 |
* |
| 242 |
* @param {Element} el The element to get the Goomoji id from. |
| 243 |
* @return {?string} A goomoji id from an img or the containing td, or null if |
| 244 |
* none exists for that element. |
| 245 |
* @private |
| 246 |
*/ |
| 247 |
goog.ui.emoji.EmojiPalette.prototype.getGoomojiIdFromElement_ = function(el) { |
| 248 |
if (!el) { |
| 249 |
return null; |
| 250 |
} |
| 251 |
|
| 252 |
var item = this.getRenderer().getContainingItem(this, el); |
| 253 |
if (item) { |
| 254 |
return item.getAttribute(goog.ui.emoji.Emoji.ATTRIBUTE) != '' ? |
| 255 |
item.getAttribute(goog.ui.emoji.Emoji.ATTRIBUTE) : |
| 256 |
item.getAttribute(goog.ui.emoji.Emoji.DATA_ATTRIBUTE); |
| 257 |
} |
| 258 |
return null; |
| 259 |
}; |
| 260 |
|
| 261 |
|
| 262 |
/** |
| 263 |
* @return {goog.ui.emoji.Emoji} The currently selected emoji from this palette. |
| 264 |
*/ |
| 265 |
goog.ui.emoji.EmojiPalette.prototype.getSelectedEmoji = function() { |
| 266 |
var elem = /** @type {Element} */ (this.getSelectedItem()); |
| 267 |
var goomojiId = this.getGoomojiIdFromElement_(elem); |
| 268 |
return this.emojiCells_[goomojiId]; |
| 269 |
}; |
| 270 |
|
| 271 |
|
| 272 |
/** |
| 273 |
* @return {number} The number of emoji managed by this palette. |
| 274 |
*/ |
| 275 |
goog.ui.emoji.EmojiPalette.prototype.getNumberOfEmoji = function() { |
| 276 |
return this.emojiCells_.length; |
| 277 |
}; |
| 278 |
|
| 279 |
|
| 280 |
/** |
| 281 |
* Returns the index of the specified emoji within this palette. |
| 282 |
* |
| 283 |
* @param {string} id Id of the emoji to look up. |
| 284 |
* @return {number} The index of the specified emoji within this palette. |
| 285 |
*/ |
| 286 |
goog.ui.emoji.EmojiPalette.prototype.getEmojiIndex = function(id) { |
| 287 |
return this.emojiMap_[id]; |
| 288 |
}; |