~ted/lazr-js/annoying-debug-message

« back to all changes in this revision

Viewing changes to src-js/lazrjs/yui/editor/exec-command.js

  • Committer: Launchpad Patch Queue Manager
  • Date: 2010-09-09 14:20:30 UTC
  • mfrom: (182.1.3 yui-3.2)
  • Revision ID: launchpad@pqm.canonical.com-20100909142030-13w6vo0ixfysxc15
[r=beuno] Update lazr-js to yui-3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
 
3
Code licensed under the BSD License:
 
4
http://developer.yahoo.com/yui/license.html
 
5
version: 3.2.0
 
6
build: 2676
 
7
*/
 
8
YUI.add('exec-command', function(Y) {
 
9
 
 
10
 
 
11
    /**
 
12
     * Plugin for the frame module to handle execCommands for Editor
 
13
     * @module editor
 
14
     * @submodule exec-command
 
15
     */     
 
16
    /**
 
17
     * Plugin for the frame module to handle execCommands for Editor
 
18
     * @class Plugin.ExecCommand
 
19
     * @extends Base
 
20
     * @constructor
 
21
     */
 
22
        var ExecCommand = function() {
 
23
            ExecCommand.superclass.constructor.apply(this, arguments);
 
24
        };
 
25
 
 
26
        Y.extend(ExecCommand, Y.Base, {
 
27
            /**
 
28
            * An internal reference to the instance of the frame plugged into.
 
29
            * @private
 
30
            * @property _inst
 
31
            */
 
32
            _inst: null,
 
33
            /**
 
34
            * Execute a command on the frame's document.
 
35
            * @method command
 
36
            * @param {String} action The action to perform (bold, italic, fontname)
 
37
            * @param {String} value The optional value (helvetica)
 
38
            * @return {Node/NodeList} Should return the Node/Nodelist affected
 
39
            */
 
40
            command: function(action, value) {
 
41
                var fn = ExecCommand.COMMANDS[action];
 
42
 
 
43
                if (fn) {
 
44
                    return fn.call(this, action, value);
 
45
                } else {
 
46
                    return this._command(action, value);
 
47
                }
 
48
            },
 
49
            /**
 
50
            * The private version of execCommand that doesn't filter for overrides.
 
51
            * @private
 
52
            * @method _command
 
53
            * @param {String} action The action to perform (bold, italic, fontname)
 
54
            * @param {String} value The optional value (helvetica)
 
55
            */
 
56
            _command: function(action, value) {
 
57
                var inst = this.getInstance();
 
58
                try {
 
59
                    inst.config.doc.execCommand(action, false, value);
 
60
                } catch (e) {
 
61
                }
 
62
            },
 
63
            /**
 
64
            * Get's the instance of YUI bound to the parent frame
 
65
            * @method getInstance
 
66
            * @return {YUI} The YUI instance bound to the parent frame
 
67
            */
 
68
            getInstance: function() {
 
69
                if (!this._inst) {
 
70
                    this._inst = this.get('host').getInstance();
 
71
                }
 
72
                return this._inst;
 
73
            },
 
74
            initializer: function() {
 
75
                Y.mix(this.get('host'), {
 
76
                    execCommand: function(action, value) {
 
77
                        return this.exec.command(action, value);
 
78
                    },
 
79
                    _execCommand: function(action, value) {
 
80
                        return this.exec._command(action, value);
 
81
                    }
 
82
                });
 
83
            }
 
84
        }, {
 
85
            /**
 
86
            * execCommand
 
87
            * @property NAME
 
88
            * @static
 
89
            */
 
90
            NAME: 'execCommand',
 
91
            /**
 
92
            * exec
 
93
            * @property NS
 
94
            * @static
 
95
            */
 
96
            NS: 'exec',
 
97
            ATTRS: {
 
98
                host: {
 
99
                    value: false
 
100
                }
 
101
            },
 
102
            /**
 
103
            * Static object literal of execCommand overrides
 
104
            * @property COMMANDS
 
105
            * @static
 
106
            */
 
107
            COMMANDS: {
 
108
                /**
 
109
                * Wraps the content with a new element of type (tag)
 
110
                * @method COMMANDS.wrap
 
111
                * @static
 
112
                * @param {String} cmd The command executed: wrap
 
113
                * @param {String} tag The tag to wrap the selection with
 
114
                * @return {NodeList} NodeList of the items touched by this command.
 
115
                */
 
116
                wrap: function(cmd, tag) {
 
117
                    var inst = this.getInstance();
 
118
                    return (new inst.Selection()).wrapContent(tag);
 
119
                },
 
120
                /**
 
121
                * Inserts the provided HTML at the cursor, should be a single element.
 
122
                * @method COMMANDS.inserthtml
 
123
                * @static
 
124
                * @param {String} cmd The command executed: inserthtml
 
125
                * @param {String} html The html to insert
 
126
                * @return {Node} Node instance of the item touched by this command.
 
127
                */
 
128
                inserthtml: function(cmd, html) {
 
129
                    var inst = this.getInstance();
 
130
                    return (new inst.Selection()).insertContent(html);
 
131
                },
 
132
                /**
 
133
                * Inserts the provided HTML at the cursor, and focuses the cursor afterwards.
 
134
                * @method COMMANDS.insertandfocus
 
135
                * @static
 
136
                * @param {String} cmd The command executed: insertandfocus
 
137
                * @param {String} html The html to insert
 
138
                * @return {Node} Node instance of the item touched by this command.
 
139
                */
 
140
                insertandfocus: function(cmd, html) {
 
141
                    var inst = this.getInstance(), out, sel;
 
142
                    html += inst.Selection.CURSOR;
 
143
                    out = this.command('inserthtml', html);
 
144
                    sel = new inst.Selection();
 
145
                    sel.focusCursor(true, true);
 
146
                    return out;
 
147
                },
 
148
                /**
 
149
                * Inserts a BR at the current cursor position
 
150
                * @method COMMANDS.insertbr
 
151
                * @static
 
152
                * @param {String} cmd The command executed: insertbr
 
153
                */
 
154
                insertbr: function(cmd) {
 
155
                    var inst = this.getInstance(), cur,
 
156
                        sel = new inst.Selection();
 
157
 
 
158
                    sel.setCursor();
 
159
                    cur = sel.getCursor();
 
160
                    cur.insert('<br>', 'before');
 
161
                    sel.focusCursor(true, false);
 
162
                    return cur.previous();
 
163
                },
 
164
                /**
 
165
                * Inserts an image at the cursor position
 
166
                * @method COMMANDS.insertimage
 
167
                * @static
 
168
                * @param {String} cmd The command executed: insertimage
 
169
                * @param {String} img The url of the image to be inserted
 
170
                * @return {Node} Node instance of the item touched by this command.
 
171
                */
 
172
                insertimage: function(cmd, img) {
 
173
                    return this.command('inserthtml', '<img src="' + img + '">');
 
174
                },
 
175
                /**
 
176
                * Add a class to all of the elements in the selection
 
177
                * @method COMMANDS.addclass
 
178
                * @static
 
179
                * @param {String} cmd The command executed: addclass
 
180
                * @param {String} cls The className to add
 
181
                * @return {NodeList} NodeList of the items touched by this command.
 
182
                */
 
183
                addclass: function(cmd, cls) {
 
184
                    var inst = this.getInstance();
 
185
                    return (new inst.Selection()).getSelected().addClass(cls);
 
186
                },
 
187
                /**
 
188
                * Remove a class from all of the elements in the selection
 
189
                * @method COMMANDS.removeclass
 
190
                * @static
 
191
                * @param {String} cmd The command executed: removeclass
 
192
                * @param {String} cls The className to remove
 
193
                * @return {NodeList} NodeList of the items touched by this command.
 
194
                */
 
195
                removeclass: function(cmd, cls) {
 
196
                    var inst = this.getInstance();
 
197
                    return (new inst.Selection()).getSelected().removeClass(cls);
 
198
                },
 
199
                /**
 
200
                * Adds a background color to the current selection, or creates a new element and applies it
 
201
                * @method COMMANDS.backcolor
 
202
                * @static
 
203
                * @param {String} cmd The command executed: backcolor
 
204
                * @param {String} val The color value to apply
 
205
                * @return {NodeList} NodeList of the items touched by this command.
 
206
                */
 
207
                forecolor: function(cmd, val) {
 
208
                    var inst = this.getInstance(),
 
209
                        sel = new inst.Selection(), n;
 
210
 
 
211
                    if (!Y.UA.ie) {
 
212
                        this._command('styleWithCSS', 'true');
 
213
                    }
 
214
                    if (sel.isCollapsed) {
 
215
                        if (sel.anchorNode && (sel.anchorNode.get('innerHTML') === '&nbsp;')) {
 
216
                            sel.anchorNode.setStyle('color', val);
 
217
                            n = sel.anchorNode;
 
218
                        } else {
 
219
                            n = this.command('inserthtml', '<span style="color: ' + val + '">' + inst.Selection.CURSOR + '</span>');
 
220
                            sel.focusCursor(true, true);
 
221
                        }
 
222
                        return n;
 
223
                    } else {
 
224
                        return this._command(cmd, val);
 
225
                    }
 
226
                    if (!Y.UA.ie) {
 
227
                        this._command('styleWithCSS', false);
 
228
                    }
 
229
                },
 
230
                backcolor: function(cmd, val) {
 
231
                    var inst = this.getInstance(),
 
232
                        sel = new inst.Selection(), n;
 
233
 
 
234
                    if (Y.UA.gecko || Y.UA.opera) {
 
235
                        cmd = 'hilitecolor';
 
236
                    }
 
237
                    if (!Y.UA.ie) {
 
238
                        this._command('styleWithCSS', 'true');
 
239
                    }
 
240
                    if (sel.isCollapsed) {
 
241
                        if (sel.anchorNode && (sel.anchorNode.get('innerHTML') === '&nbsp;')) {
 
242
                            sel.anchorNode.setStyle('backgroundColor', val);
 
243
                            n = sel.anchorNode;
 
244
                        } else {
 
245
                            n = this.command('inserthtml', '<span style="background-color: ' + val + '">' + inst.Selection.CURSOR + '</span>');
 
246
                            sel.focusCursor(true, true);
 
247
                        }
 
248
                        return n;
 
249
                    } else {
 
250
                        return this._command(cmd, val);
 
251
                    }
 
252
                    if (!Y.UA.ie) {
 
253
                        this._command('styleWithCSS', false);
 
254
                    }
 
255
                },
 
256
                /**
 
257
                * Sugar method, calles backcolor
 
258
                * @method COMMANDS.hilitecolor
 
259
                * @static
 
260
                * @param {String} cmd The command executed: backcolor
 
261
                * @param {String} val The color value to apply
 
262
                * @return {NodeList} NodeList of the items touched by this command.
 
263
                */
 
264
                hilitecolor: function() {
 
265
                    return ExecCommand.COMMANDS.backcolor.apply(this, arguments);
 
266
                },
 
267
                /**
 
268
                * Adds a font name to the current selection, or creates a new element and applies it
 
269
                * @method COMMANDS.fontname
 
270
                * @static
 
271
                * @param {String} cmd The command executed: fontname
 
272
                * @param {String} val The font name to apply
 
273
                * @return {NodeList} NodeList of the items touched by this command.
 
274
                */
 
275
                fontname: function(cmd, val) {
 
276
                    var inst = this.getInstance(),
 
277
                        sel = new inst.Selection(), n;
 
278
 
 
279
                    if (sel.isCollapsed) {
 
280
                        if (sel.anchorNode && (sel.anchorNode.get('innerHTML') === '&nbsp;')) {
 
281
                            sel.anchorNode.setStyle('fontFamily', val);
 
282
                            n = sel.anchorNode;
 
283
                        } else {
 
284
                            n = this.command('inserthtml', '<span style="font-family: ' + val + '">' + inst.Selection.CURSOR + '</span>');
 
285
                            sel.focusCursor(true, true);
 
286
                        }
 
287
                        return n;
 
288
                    } else {
 
289
                        return this._command('fontname', val);
 
290
                    }
 
291
                },
 
292
                /**
 
293
                * Adds a fontsize to the current selection, or creates a new element and applies it
 
294
                * @method COMMANDS.fontsize
 
295
                * @static
 
296
                * @param {String} cmd The command executed: fontsize
 
297
                * @param {String} val The font size to apply
 
298
                * @return {NodeList} NodeList of the items touched by this command.
 
299
                */
 
300
                fontsize: function(cmd, val) {
 
301
                    var inst = this.getInstance(),
 
302
                        sel = new inst.Selection(), n, prev;
 
303
 
 
304
                    if (sel.isCollapsed) {
 
305
                        n = this.command('inserthtml', '<font size="' + val + '">&nbsp;</font>');
 
306
                        prev = n.get('previousSibling');
 
307
                        if (prev && prev.get('nodeType') === 3) {
 
308
                            if (prev.get('length') < 2) {
 
309
                                prev.remove();
 
310
                            }
 
311
                        }
 
312
                        sel.selectNode(n.get('firstChild'), true, false);
 
313
                        return n;
 
314
                    } else {
 
315
                        return this._command('fontsize', val);
 
316
                    }
 
317
                }
 
318
            }
 
319
        });
 
320
 
 
321
        Y.namespace('Plugin');
 
322
        Y.Plugin.ExecCommand = ExecCommand;
 
323
 
 
324
 
 
325
 
 
326
}, '3.2.0' ,{skinnable:false, requires:['frame']});