Debian Codegraph @

dojox/editor/plugins/Smiley.js

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