emoticons.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2009 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. // All Rights Reserved
  15. /**
  16. * @fileoverview Plugin for generating emoticons.
  17. *
  18. * @author nicksantos@google.com (Nick Santos)
  19. */
  20. goog.provide('goog.editor.plugins.Emoticons');
  21. goog.require('goog.dom.TagName');
  22. goog.require('goog.editor.Plugin');
  23. goog.require('goog.editor.range');
  24. goog.require('goog.functions');
  25. goog.require('goog.ui.emoji.Emoji');
  26. goog.require('goog.userAgent');
  27. /**
  28. * Plugin for generating emoticons.
  29. *
  30. * @constructor
  31. * @extends {goog.editor.Plugin}
  32. * @final
  33. */
  34. goog.editor.plugins.Emoticons = function() {
  35. goog.editor.plugins.Emoticons.base(this, 'constructor');
  36. };
  37. goog.inherits(goog.editor.plugins.Emoticons, goog.editor.Plugin);
  38. /** The emoticon command. */
  39. goog.editor.plugins.Emoticons.COMMAND = '+emoticon';
  40. /** @override */
  41. goog.editor.plugins.Emoticons.prototype.getTrogClassId =
  42. goog.functions.constant(goog.editor.plugins.Emoticons.COMMAND);
  43. /** @override */
  44. goog.editor.plugins.Emoticons.prototype.isSupportedCommand = function(command) {
  45. return command == goog.editor.plugins.Emoticons.COMMAND;
  46. };
  47. /**
  48. * Inserts an emoticon into the editor at the cursor location. Places the
  49. * cursor to the right of the inserted emoticon.
  50. * @param {string} command Command to execute.
  51. * @param {*=} opt_arg Emoji to insert.
  52. * @return {!Object|undefined} The result of the command.
  53. * @override
  54. */
  55. goog.editor.plugins.Emoticons.prototype.execCommandInternal = function(
  56. command, opt_arg) {
  57. var emoji = /** @type {goog.ui.emoji.Emoji} */ (opt_arg);
  58. var styleProperties = 'margin:0 0.2ex;vertical-align:middle;';
  59. var emojiHeight = emoji.getHeight();
  60. styleProperties += emojiHeight ? 'height:' + emojiHeight + 'px;' : '';
  61. var emojiWidth = emoji.getWidth();
  62. styleProperties += emojiWidth ? 'width:' + emojiWidth + 'px;' : '';
  63. var dom = this.getFieldDomHelper();
  64. var imgAttributes = {'src': emoji.getUrl(), 'style': styleProperties};
  65. if (emoji.getAltText()) {
  66. imgAttributes['alt'] = emoji.getAltText();
  67. }
  68. var img = dom.createDom(goog.dom.TagName.IMG, imgAttributes);
  69. img.setAttribute(goog.ui.emoji.Emoji.ATTRIBUTE, emoji.getId());
  70. img.setAttribute(goog.ui.emoji.Emoji.DATA_ATTRIBUTE, emoji.getId());
  71. this.getFieldObject().getRange().replaceContentsWithNode(img);
  72. // IE8 does the right thing with the cursor, and has a js error when we try
  73. // to place the cursor manually.
  74. // IE9 loses the cursor when the window is focused, so focus first.
  75. if (!goog.userAgent.IE || goog.userAgent.isDocumentModeOrHigher(9)) {
  76. this.getFieldObject().focus();
  77. goog.editor.range.placeCursorNextTo(img, false);
  78. }
  79. };