2
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3
Code licensed under the BSD License:
4
http://developer.yahoo.com/yui/license.html
8
YUI.add('exec-command', function(Y) {
12
* Plugin for the frame module to handle execCommands for Editor
14
* @submodule exec-command
17
* Plugin for the frame module to handle execCommands for Editor
18
* @class Plugin.ExecCommand
22
var ExecCommand = function() {
23
ExecCommand.superclass.constructor.apply(this, arguments);
26
Y.extend(ExecCommand, Y.Base, {
28
* An internal reference to the instance of the frame plugged into.
34
* Execute a command on the frame's document.
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
40
command: function(action, value) {
41
var fn = ExecCommand.COMMANDS[action];
44
return fn.call(this, action, value);
46
return this._command(action, value);
50
* The private version of execCommand that doesn't filter for overrides.
53
* @param {String} action The action to perform (bold, italic, fontname)
54
* @param {String} value The optional value (helvetica)
56
_command: function(action, value) {
57
var inst = this.getInstance();
59
inst.config.doc.execCommand(action, false, value);
64
* Get's the instance of YUI bound to the parent frame
66
* @return {YUI} The YUI instance bound to the parent frame
68
getInstance: function() {
70
this._inst = this.get('host').getInstance();
74
initializer: function() {
75
Y.mix(this.get('host'), {
76
execCommand: function(action, value) {
77
return this.exec.command(action, value);
79
_execCommand: function(action, value) {
80
return this.exec._command(action, value);
103
* Static object literal of execCommand overrides
109
* Wraps the content with a new element of type (tag)
110
* @method COMMANDS.wrap
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.
116
wrap: function(cmd, tag) {
117
var inst = this.getInstance();
118
return (new inst.Selection()).wrapContent(tag);
121
* Inserts the provided HTML at the cursor, should be a single element.
122
* @method COMMANDS.inserthtml
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.
128
inserthtml: function(cmd, html) {
129
var inst = this.getInstance();
130
return (new inst.Selection()).insertContent(html);
133
* Inserts the provided HTML at the cursor, and focuses the cursor afterwards.
134
* @method COMMANDS.insertandfocus
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.
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);
149
* Inserts a BR at the current cursor position
150
* @method COMMANDS.insertbr
152
* @param {String} cmd The command executed: insertbr
154
insertbr: function(cmd) {
155
var inst = this.getInstance(), cur,
156
sel = new inst.Selection();
159
cur = sel.getCursor();
160
cur.insert('<br>', 'before');
161
sel.focusCursor(true, false);
162
return cur.previous();
165
* Inserts an image at the cursor position
166
* @method COMMANDS.insertimage
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.
172
insertimage: function(cmd, img) {
173
return this.command('inserthtml', '<img src="' + img + '">');
176
* Add a class to all of the elements in the selection
177
* @method COMMANDS.addclass
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.
183
addclass: function(cmd, cls) {
184
var inst = this.getInstance();
185
return (new inst.Selection()).getSelected().addClass(cls);
188
* Remove a class from all of the elements in the selection
189
* @method COMMANDS.removeclass
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.
195
removeclass: function(cmd, cls) {
196
var inst = this.getInstance();
197
return (new inst.Selection()).getSelected().removeClass(cls);
200
* Adds a background color to the current selection, or creates a new element and applies it
201
* @method COMMANDS.backcolor
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.
207
forecolor: function(cmd, val) {
208
var inst = this.getInstance(),
209
sel = new inst.Selection(), n;
212
this._command('styleWithCSS', 'true');
214
if (sel.isCollapsed) {
215
if (sel.anchorNode && (sel.anchorNode.get('innerHTML') === ' ')) {
216
sel.anchorNode.setStyle('color', val);
219
n = this.command('inserthtml', '<span style="color: ' + val + '">' + inst.Selection.CURSOR + '</span>');
220
sel.focusCursor(true, true);
224
return this._command(cmd, val);
227
this._command('styleWithCSS', false);
230
backcolor: function(cmd, val) {
231
var inst = this.getInstance(),
232
sel = new inst.Selection(), n;
234
if (Y.UA.gecko || Y.UA.opera) {
238
this._command('styleWithCSS', 'true');
240
if (sel.isCollapsed) {
241
if (sel.anchorNode && (sel.anchorNode.get('innerHTML') === ' ')) {
242
sel.anchorNode.setStyle('backgroundColor', val);
245
n = this.command('inserthtml', '<span style="background-color: ' + val + '">' + inst.Selection.CURSOR + '</span>');
246
sel.focusCursor(true, true);
250
return this._command(cmd, val);
253
this._command('styleWithCSS', false);
257
* Sugar method, calles backcolor
258
* @method COMMANDS.hilitecolor
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.
264
hilitecolor: function() {
265
return ExecCommand.COMMANDS.backcolor.apply(this, arguments);
268
* Adds a font name to the current selection, or creates a new element and applies it
269
* @method COMMANDS.fontname
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.
275
fontname: function(cmd, val) {
276
var inst = this.getInstance(),
277
sel = new inst.Selection(), n;
279
if (sel.isCollapsed) {
280
if (sel.anchorNode && (sel.anchorNode.get('innerHTML') === ' ')) {
281
sel.anchorNode.setStyle('fontFamily', val);
284
n = this.command('inserthtml', '<span style="font-family: ' + val + '">' + inst.Selection.CURSOR + '</span>');
285
sel.focusCursor(true, true);
289
return this._command('fontname', val);
293
* Adds a fontsize to the current selection, or creates a new element and applies it
294
* @method COMMANDS.fontsize
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.
300
fontsize: function(cmd, val) {
301
var inst = this.getInstance(),
302
sel = new inst.Selection(), n, prev;
304
if (sel.isCollapsed) {
305
n = this.command('inserthtml', '<font size="' + val + '"> </font>');
306
prev = n.get('previousSibling');
307
if (prev && prev.get('nodeType') === 3) {
308
if (prev.get('length') < 2) {
312
sel.selectNode(n.get('firstChild'), true, false);
315
return this._command('fontsize', val);
321
Y.namespace('Plugin');
322
Y.Plugin.ExecCommand = ExecCommand;
326
}, '3.2.0' ,{skinnable:false, requires:['frame']});