| 1 |
// Copyright 2008 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 SpriteInfo implementation. This is a simple wrapper class to |
| 17 |
* hold CSS metadata needed for sprited emoji. |
| 18 |
* |
| 19 |
* @see ../demos/popupemojipicker.html or emojipicker_test.html for examples |
| 20 |
* of how to use this class. |
| 21 |
* |
| 22 |
*/ |
| 23 |
goog.provide('goog.ui.emoji.SpriteInfo'); |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
/** |
| 28 |
* Creates a SpriteInfo object with the specified properties. If the image is |
| 29 |
* sprited via CSS, then only the first parameter needs a value. If the image |
| 30 |
* is sprited via metadata, then the first parameter should be left null. |
| 31 |
* |
| 32 |
* @param {?string} cssClass CSS class to properly display the sprited image. |
| 33 |
* @param {string=} opt_url Url of the sprite image. |
| 34 |
* @param {number=} opt_width Width of the image being sprited. |
| 35 |
* @param {number=} opt_height Height of the image being sprited. |
| 36 |
* @param {number=} opt_xOffset Positive x offset of the image being sprited |
| 37 |
* within the sprite. |
| 38 |
* @param {number=} opt_yOffset Positive y offset of the image being sprited |
| 39 |
* within the sprite. |
| 40 |
* @param {boolean=} opt_animated Whether the sprite is animated. |
| 41 |
* @constructor |
| 42 |
* @final |
| 43 |
*/ |
| 44 |
goog.ui.emoji.SpriteInfo = function( |
| 45 |
cssClass, opt_url, opt_width, opt_height, opt_xOffset, opt_yOffset, |
| 46 |
opt_animated) { |
| 47 |
if (cssClass != null) { |
| 48 |
this.cssClass_ = cssClass; |
| 49 |
} else { |
| 50 |
if (opt_url == undefined || opt_width === undefined || |
| 51 |
opt_height === undefined || opt_xOffset == undefined || |
| 52 |
opt_yOffset === undefined) { |
| 53 |
throw new Error('Sprite info is not fully specified'); |
| 54 |
} |
| 55 |
|
| 56 |
this.url_ = opt_url; |
| 57 |
this.width_ = opt_width; |
| 58 |
this.height_ = opt_height; |
| 59 |
this.xOffset_ = opt_xOffset; |
| 60 |
this.yOffset_ = opt_yOffset; |
| 61 |
} |
| 62 |
|
| 63 |
this.animated_ = !!opt_animated; |
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
/** |
| 68 |
* Name of the CSS class to properly display the sprited image. |
| 69 |
* @type {string} |
| 70 |
* @private |
| 71 |
*/ |
| 72 |
goog.ui.emoji.SpriteInfo.prototype.cssClass_; |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* Url of the sprite image. |
| 77 |
* @type {string|undefined} |
| 78 |
* @private |
| 79 |
*/ |
| 80 |
goog.ui.emoji.SpriteInfo.prototype.url_; |
| 81 |
|
| 82 |
|
| 83 |
/** |
| 84 |
* Width of the image being sprited. |
| 85 |
* @type {number|undefined} |
| 86 |
* @private |
| 87 |
*/ |
| 88 |
goog.ui.emoji.SpriteInfo.prototype.width_; |
| 89 |
|
| 90 |
|
| 91 |
/** |
| 92 |
* Height of the image being sprited. |
| 93 |
* @type {number|undefined} |
| 94 |
* @private |
| 95 |
*/ |
| 96 |
goog.ui.emoji.SpriteInfo.prototype.height_; |
| 97 |
|
| 98 |
|
| 99 |
/** |
| 100 |
* Positive x offset of the image being sprited within the sprite. |
| 101 |
* @type {number|undefined} |
| 102 |
* @private |
| 103 |
*/ |
| 104 |
goog.ui.emoji.SpriteInfo.prototype.xOffset_; |
| 105 |
|
| 106 |
|
| 107 |
/** |
| 108 |
* Positive y offset of the image being sprited within the sprite. |
| 109 |
* @type {number|undefined} |
| 110 |
* @private |
| 111 |
*/ |
| 112 |
goog.ui.emoji.SpriteInfo.prototype.yOffset_; |
| 113 |
|
| 114 |
|
| 115 |
/** |
| 116 |
* Whether the emoji specified by the sprite is animated. |
| 117 |
* @type {boolean} |
| 118 |
* @private |
| 119 |
*/ |
| 120 |
goog.ui.emoji.SpriteInfo.prototype.animated_; |
| 121 |
|
| 122 |
|
| 123 |
/** |
| 124 |
* Returns the css class of the sprited image. |
| 125 |
* @return {?string} Name of the CSS class to properly display the sprited |
| 126 |
* image. |
| 127 |
*/ |
| 128 |
goog.ui.emoji.SpriteInfo.prototype.getCssClass = function() { |
| 129 |
return this.cssClass_ || null; |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
/** |
| 134 |
* Returns the url of the sprite image. |
| 135 |
* @return {?string} Url of the sprite image. |
| 136 |
*/ |
| 137 |
goog.ui.emoji.SpriteInfo.prototype.getUrl = function() { |
| 138 |
return this.url_ || null; |
| 139 |
}; |
| 140 |
|
| 141 |
|
| 142 |
/** |
| 143 |
* Returns whether the emoji specified by this sprite is animated. |
| 144 |
* @return {boolean} Whether the emoji is animated. |
| 145 |
*/ |
| 146 |
goog.ui.emoji.SpriteInfo.prototype.isAnimated = function() { |
| 147 |
return this.animated_; |
| 148 |
}; |
| 149 |
|
| 150 |
|
| 151 |
/** |
| 152 |
* Returns the width of the image being sprited, appropriate for a CSS value. |
| 153 |
* @return {string} The width of the image being sprited. |
| 154 |
*/ |
| 155 |
goog.ui.emoji.SpriteInfo.prototype.getWidthCssValue = function() { |
| 156 |
return goog.ui.emoji.SpriteInfo.getCssPixelValue_(this.width_); |
| 157 |
}; |
| 158 |
|
| 159 |
|
| 160 |
/** |
| 161 |
* Returns the height of the image being sprited, appropriate for a CSS value. |
| 162 |
* @return {string} The height of the image being sprited. |
| 163 |
*/ |
| 164 |
goog.ui.emoji.SpriteInfo.prototype.getHeightCssValue = function() { |
| 165 |
return goog.ui.emoji.SpriteInfo.getCssPixelValue_(this.height_); |
| 166 |
}; |
| 167 |
|
| 168 |
|
| 169 |
/** |
| 170 |
* Returns the x offset of the image being sprited within the sprite, |
| 171 |
* appropriate for a CSS value. |
| 172 |
* @return {string} The x offset of the image being sprited within the sprite. |
| 173 |
*/ |
| 174 |
goog.ui.emoji.SpriteInfo.prototype.getXOffsetCssValue = function() { |
| 175 |
return goog.ui.emoji.SpriteInfo.getOffsetCssValue_(this.xOffset_); |
| 176 |
}; |
| 177 |
|
| 178 |
|
| 179 |
/** |
| 180 |
* Returns the positive y offset of the image being sprited within the sprite, |
| 181 |
* appropriate for a CSS value. |
| 182 |
* @return {string} The y offset of the image being sprited within the sprite. |
| 183 |
*/ |
| 184 |
goog.ui.emoji.SpriteInfo.prototype.getYOffsetCssValue = function() { |
| 185 |
return goog.ui.emoji.SpriteInfo.getOffsetCssValue_(this.yOffset_); |
| 186 |
}; |
| 187 |
|
| 188 |
|
| 189 |
/** |
| 190 |
* Returns a string appropriate for use as a CSS value. If the value is zero, |
| 191 |
* then there is no unit appended. |
| 192 |
* |
| 193 |
* @param {number|undefined} value A number to be turned into a |
| 194 |
* CSS size/location value. |
| 195 |
* @return {string} A string appropriate for use as a CSS value. |
| 196 |
* @private |
| 197 |
*/ |
| 198 |
goog.ui.emoji.SpriteInfo.getCssPixelValue_ = function(value) { |
| 199 |
return !value ? '0' : value + 'px'; |
| 200 |
}; |
| 201 |
|
| 202 |
|
| 203 |
/** |
| 204 |
* Returns a string appropriate for use as a CSS value for a position offset, |
| 205 |
* such as the position argument for sprites. |
| 206 |
* |
| 207 |
* @param {number|undefined} posOffset A positive offset for a position. |
| 208 |
* @return {string} A string appropriate for use as a CSS value. |
| 209 |
* @private |
| 210 |
*/ |
| 211 |
goog.ui.emoji.SpriteInfo.getOffsetCssValue_ = function(posOffset) { |
| 212 |
var offset = goog.ui.emoji.SpriteInfo.getCssPixelValue_(posOffset); |
| 213 |
return offset == '0' ? offset : '-' + offset; |
| 214 |
}; |