Smiley.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. define("dojox/editor/plugins/Smiley", [
  2. "dojo",
  3. "dijit",
  4. "dojox",
  5. "dijit/_editor/_Plugin",
  6. "dijit/form/DropDownButton",
  7. "dojo/_base/connect",
  8. "dojo/_base/declare",
  9. "dojo/i18n",
  10. "dojox/editor/plugins/_SmileyPalette",
  11. "dojox/html/format",
  12. "dojo/i18n!dojox/editor/plugins/nls/Smiley"
  13. ], function(dojo, dijit, dojox) {
  14. dojo.experimental("dojox.editor.plugins.Smiley");
  15. dojo.declare("dojox.editor.plugins.Smiley", dijit._editor._Plugin, {
  16. // summary:
  17. // This plugin allows the user to select from emoticons or "smileys"
  18. // to insert at the current cursor position.
  19. //
  20. // description:
  21. // The commands provided by this plugin are:
  22. // * smiley - inserts the selected emoticon
  23. // iconClassPrefix: [const] String
  24. // The CSS class name for the button node is formed from `iconClassPrefix` and `command`
  25. iconClassPrefix: "dijitAdditionalEditorIcon",
  26. // a marker for emoticon wrap like [:-)] for regexp convienent
  27. // when a message which contains an emoticon stored in a database or view source, this marker include also
  28. // but when user enter an emoticon by key board, user don't need to enter this marker.
  29. // also emoticon definition character set can not contains this marker
  30. emoticonMarker: '[]',
  31. emoticonImageClass: 'dojoEditorEmoticon',
  32. _initButton: function(){
  33. // summary:
  34. //
  35. this.dropDown = new dojox.editor.plugins._SmileyPalette();
  36. this.connect(this.dropDown, "onChange", function(ascii){
  37. this.button.closeDropDown();
  38. this.editor.focus();
  39. //
  40. ascii = this.emoticonMarker.charAt(0) + ascii + this.emoticonMarker.charAt(1);
  41. this.editor.execCommand("inserthtml", ascii);
  42. });
  43. this.i18n = dojo.i18n.getLocalization("dojox.editor.plugins", "Smiley");
  44. this.button = new dijit.form.DropDownButton({
  45. label: this.i18n.smiley,
  46. showLabel: false,
  47. iconClass: this.iconClassPrefix + " " + this.iconClassPrefix + "Smiley",
  48. tabIndex: "-1",
  49. dropDown: this.dropDown
  50. });
  51. this.emoticonImageRegexp = new RegExp("class=(\"|\')" + this.emoticonImageClass + "(\"|\')");
  52. },
  53. updateState: function(){
  54. // summary:
  55. // Over-ride for button state control for disabled to work.
  56. this.button.set("disabled", this.get("disabled"));
  57. },
  58. setEditor: function(editor){
  59. // summary:
  60. // Over-ride for the setting of the editor.
  61. // editor: Object
  62. // The editor to configure for this plugin to use.
  63. this.editor = editor;
  64. this._initButton();
  65. this.editor.contentPreFilters.push(dojo.hitch(this, this._preFilterEntities));
  66. this.editor.contentPostFilters.push(dojo.hitch(this, this._postFilterEntities));
  67. if(dojo.isFF){
  68. // This is a workaround for a really odd Firefox bug with
  69. // leaving behind phantom cursors when deleting smiley images.
  70. // See: #13299
  71. var deleteHandler = dojo.hitch(this, function(){
  72. var editor = this.editor;
  73. // have to use timers here because the event has to happen
  74. // (bubble), then we can poke the dom.
  75. setTimeout(function(){
  76. if(editor.editNode){
  77. dojo.style(editor.editNode, "opacity", "0.99");
  78. // Allow it to apply, then undo it to trigger cleanup of those
  79. // phantoms.
  80. setTimeout(function(){if(editor.editNode) { dojo.style(editor.editNode, "opacity", "");} }, 0);
  81. }
  82. }, 0);
  83. return true;
  84. })
  85. this.editor.onLoadDeferred.addCallback(dojo.hitch(this, function(){
  86. this.editor.addKeyHandler(dojo.keys.DELETE, false, false, deleteHandler);
  87. this.editor.addKeyHandler(dojo.keys.BACKSPACE, false, false, deleteHandler);
  88. }));
  89. }
  90. },
  91. _preFilterEntities: function(/*String content passed in*/ value){
  92. // summary:
  93. // A function to filter out emoticons into their UTF-8 character form
  94. // displayed in the editor. It gets registered with the preFilters
  95. // of the editor.
  96. // tags:
  97. // private.
  98. //
  99. //
  100. return value.replace(/\[([^\]]*)\]/g, dojo.hitch(this, this._decode));
  101. },
  102. _postFilterEntities: function(/*String content passed in*/ value){
  103. // summary:
  104. // A function to filter out emoticons into encoded form so they
  105. // are properly displayed in the editor. It gets registered with the
  106. // postFilters of the editor.
  107. // tags:
  108. // private.
  109. return value.replace(/<img [^>]*>/gi, dojo.hitch(this, this._encode));
  110. },
  111. _decode: function(str, ascii){
  112. // summary:
  113. // Pre-filter for editor to convert strings like [:-)] into an <img> of the corresponding smiley
  114. var emoticon = dojox.editor.plugins.Emoticon.fromAscii(ascii);
  115. return emoticon ? emoticon.imgHtml(this.emoticonImageClass) : str;
  116. },
  117. _encode: function(str){
  118. // summary:
  119. // Post-filter for editor to convert <img> nodes of smileys into strings like [:-)]
  120. // Each <img> node has an alt tag with it's ascii representation, so just use that.
  121. // TODO: wouldn't this be easier as a postDomFilter ?
  122. if(str.search(this.emoticonImageRegexp) > -1){
  123. return this.emoticonMarker.charAt(0) + str.replace(/(<img [^>]*)alt="([^"]*)"([^>]*>)/, "$2") + this.emoticonMarker.charAt(1);
  124. }
  125. else{
  126. return str;
  127. }
  128. }
  129. });
  130. // Register this plugin.
  131. dojo.subscribe(dijit._scopeName + ".Editor.getPlugin",null,function(o){
  132. if(o.plugin){ return; }
  133. if(o.args.name === "smiley"){
  134. o.plugin = new dojox.editor.plugins.Smiley();
  135. }
  136. });
  137. return dojox.editor.plugins.Smiley;
  138. });