Path: blob/trunk/third_party/closure/goog/editor/plugins/emoticons.js
2868 views
// Copyright 2009 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.13// All Rights Reserved1415/**16* @fileoverview Plugin for generating emoticons.17*18* @author [email protected] (Nick Santos)19*/2021goog.provide('goog.editor.plugins.Emoticons');2223goog.require('goog.dom.TagName');24goog.require('goog.editor.Plugin');25goog.require('goog.editor.range');26goog.require('goog.functions');27goog.require('goog.ui.emoji.Emoji');28goog.require('goog.userAgent');29303132/**33* Plugin for generating emoticons.34*35* @constructor36* @extends {goog.editor.Plugin}37* @final38*/39goog.editor.plugins.Emoticons = function() {40goog.editor.plugins.Emoticons.base(this, 'constructor');41};42goog.inherits(goog.editor.plugins.Emoticons, goog.editor.Plugin);434445/** The emoticon command. */46goog.editor.plugins.Emoticons.COMMAND = '+emoticon';474849/** @override */50goog.editor.plugins.Emoticons.prototype.getTrogClassId =51goog.functions.constant(goog.editor.plugins.Emoticons.COMMAND);525354/** @override */55goog.editor.plugins.Emoticons.prototype.isSupportedCommand = function(command) {56return command == goog.editor.plugins.Emoticons.COMMAND;57};585960/**61* Inserts an emoticon into the editor at the cursor location. Places the62* cursor to the right of the inserted emoticon.63* @param {string} command Command to execute.64* @param {*=} opt_arg Emoji to insert.65* @return {!Object|undefined} The result of the command.66* @override67*/68goog.editor.plugins.Emoticons.prototype.execCommandInternal = function(69command, opt_arg) {70var emoji = /** @type {goog.ui.emoji.Emoji} */ (opt_arg);7172var styleProperties = 'margin:0 0.2ex;vertical-align:middle;';73var emojiHeight = emoji.getHeight();74styleProperties += emojiHeight ? 'height:' + emojiHeight + 'px;' : '';75var emojiWidth = emoji.getWidth();76styleProperties += emojiWidth ? 'width:' + emojiWidth + 'px;' : '';7778var dom = this.getFieldDomHelper();79var imgAttributes = {'src': emoji.getUrl(), 'style': styleProperties};80if (emoji.getAltText()) {81imgAttributes['alt'] = emoji.getAltText();82}83var img = dom.createDom(goog.dom.TagName.IMG, imgAttributes);8485img.setAttribute(goog.ui.emoji.Emoji.ATTRIBUTE, emoji.getId());86img.setAttribute(goog.ui.emoji.Emoji.DATA_ATTRIBUTE, emoji.getId());8788this.getFieldObject().getRange().replaceContentsWithNode(img);8990// IE8 does the right thing with the cursor, and has a js error when we try91// to place the cursor manually.92// IE9 loses the cursor when the window is focused, so focus first.93if (!goog.userAgent.IE || goog.userAgent.isDocumentModeOrHigher(9)) {94this.getFieldObject().focus();95goog.editor.range.placeCursorNextTo(img, false);96}97};9899100