exec-command.js revision 9002d9b85f99bb503c702f12a20f829f732d22f8
124N/AYUI.add('exec-command', function(Y) {
124N/A
124N/A /**
124N/A * Plugin for the frame module to handle execCommands for Editor
124N/A * @module editor
124N/A * @submodule exec-command
124N/A */
124N/A /**
124N/A * Plugin for the frame module to handle execCommands for Editor
124N/A * @class Plugin.ExecCommand
124N/A * @extends Base
124N/A * @constructor
124N/A */
124N/A var ExecCommand = function() {
124N/A ExecCommand.superclass.constructor.apply(this, arguments);
124N/A };
124N/A
124N/A Y.extend(ExecCommand, Y.Base, {
124N/A /**
124N/A * An internal reference to the instance of the frame plugged into.
5680N/A * @private
5680N/A * @property _inst
5230N/A */
124N/A _inst: null,
5680N/A /**
124N/A * Execute a command on the frame's document.
124N/A * @method command
124N/A * @param {String} action The action to perform (bold, italic, fontname)
5871N/A * @param {String} value The optional value (helvetica)
618N/A * @return {Node/NodeList} Should return the Node/Nodelist affected
124N/A */
844N/A command: function(action, value) {
5871N/A var fn = ExecCommand.COMMANDS[action];
618N/A
124N/A Y.log('execCommand(' + action + '): "' + value + '"', 'info', 'exec-command');
5871N/A if (fn) {
2899N/A return fn.call(this, action, value);
5648N/A } else {
5648N/A return this._command(action, value);
5648N/A }
5648N/A },
5648N/A /**
5648N/A * The private version of execCommand that doesn't filter for overrides.
5648N/A * @private
5680N/A * @method _command
5680N/A * @param {String} action The action to perform (bold, italic, fontname)
124N/A * @param {String} value The optional value (helvetica)
2960N/A */
2960N/A _command: function(action, value) {
2960N/A var inst = this.getInstance();
2960N/A try {
124N/A Y.log('Internal execCommand(' + action + '): "' + value + '"', 'info', 'exec-command');
124N/A inst.config.doc.execCommand(action, false, value);
3069N/A } catch (e) {
3069N/A Y.log(e.message, 'error', 'exec-command');
3069N/A }
5935N/A },
5935N/A /**
5935N/A * Get's the instance of YUI bound to the parent frame
5935N/A * @method getInstance
5680N/A * @return {YUI} The YUI instance bound to the parent frame
5680N/A */
124N/A getInstance: function() {
5617N/A if (!this._inst) {
124N/A this._inst = this.get('host').getInstance();
124N/A }
124N/A return this._inst;
3878N/A },
3878N/A initializer: function() {
5935N/A Y.mix(this.get('host'), {
5935N/A execCommand: function(action, value) {
124N/A return this.exec.command(action, value);
765N/A },
765N/A _execCommand: function(action, value) {
765N/A return this.exec._command(action, value);
765N/A }
765N/A });
1792N/A }
1792N/A }, {
1792N/A /**
765N/A * execCommand
765N/A * @property NAME
765N/A * @static
765N/A */
765N/A NAME: 'execCommand',
3817N/A /**
3817N/A * exec
3817N/A * @property NS
3817N/A * @static
3817N/A */
3817N/A NS: 'exec',
3817N/A ATTRS: {
3817N/A host: {
5111N/A value: false
3817N/A }
3817N/A },
3817N/A /**
6067N/A * Static object literal of execCommand overrides
6067N/A * @property COMMANDS
3817N/A * @static
3817N/A */
3817N/A COMMANDS: {
3817N/A /**
3817N/A * Wraps the content with a new element of type (tag)
* @method COMMANDS.wrap
* @static
* @param {String} cmd The command executed: wrap
* @param {String} tag The tag to wrap the selection with
* @return {NodeList} NodeList of the items touched by this command.
*/
wrap: function(cmd, tag) {
var inst = this.getInstance();
return (new inst.Selection()).wrapContent(tag);
},
/**
* Inserts the provided HTML at the cursor, should be a single element.
* @method COMMANDS.inserthtml
* @static
* @param {String} cmd The command executed: inserthtml
* @param {String} html The html to insert
* @return {Node} Node instance of the item touched by this command.
*/
inserthtml: function(cmd, html) {
var inst = this.getInstance();
return (new inst.Selection()).insertContent(html);
},
/**
* Inserts the provided HTML at the cursor, and focuses the cursor afterwards.
* @method COMMANDS.insertandfocus
* @static
* @param {String} cmd The command executed: insertandfocus
* @param {String} html The html to insert
* @return {Node} Node instance of the item touched by this command.
*/
insertandfocus: function(cmd, html) {
var inst = this.getInstance(), out, sel;
html += inst.Selection.CURSOR;
out = this.command('inserthtml', html);
sel = new inst.Selection();
sel.focusCursor(true, true);
return out;
},
/**
* Inserts a BR at the current cursor position
* @method COMMANDS.insertbr
* @static
* @param {String} cmd The command executed: insertbr
*/
insertbr: function(cmd) {
var inst = this.getInstance(), cur,
sel = new inst.Selection();
sel.setCursor();
cur = sel.getCursor();
cur.insert('<br>', 'before');
sel.focusCursor(true, false);
return cur.previous();
},
/**
* Inserts an image at the cursor position
* @method COMMANDS.insertimage
* @static
* @param {String} cmd The command executed: insertimage
* @param {String} img The url of the image to be inserted
* @return {Node} Node instance of the item touched by this command.
*/
insertimage: function(cmd, img) {
return this.command('inserthtml', '<img src="' + img + '">');
},
/**
* Add a class to all of the elements in the selection
* @method COMMANDS.addclass
* @static
* @param {String} cmd The command executed: addclass
* @param {String} cls The className to add
* @return {NodeList} NodeList of the items touched by this command.
*/
addclass: function(cmd, cls) {
var inst = this.getInstance();
return (new inst.Selection()).getSelected().addClass(cls);
},
/**
* Remove a class from all of the elements in the selection
* @method COMMANDS.removeclass
* @static
* @param {String} cmd The command executed: removeclass
* @param {String} cls The className to remove
* @return {NodeList} NodeList of the items touched by this command.
*/
removeclass: function(cmd, cls) {
var inst = this.getInstance();
return (new inst.Selection()).getSelected().removeClass(cls);
},
/**
* Adds a background color to the current selection, or creates a new element and applies it
* @method COMMANDS.backcolor
* @static
* @param {String} cmd The command executed: backcolor
* @param {String} val The color value to apply
* @return {NodeList} NodeList of the items touched by this command.
*/
backcolor: function(cmd, val) {
var inst = this.getInstance(),
sel = new inst.Selection(), n;
if (Y.UA.gecko || Y.UA.opera) {
cmd = 'hilitecolor';
}
if (!Y.UA.ie) {
this._command('styleWithCSS', 'true');
}
if (sel.isCollapsed) {
n = this.command('inserthtml', '<span style="background-color: ' + val + '"><span>&nbsp;</span>&nbsp;</span>');
inst.Selection.filterBlocks();
sel.selectNode(n.get('firstChild'));
return n;
} else {
return this._command(cmd, val);
}
if (!Y.UA.ie) {
this._command('styleWithCSS', false);
}
},
/**
* Sugar method, calles backcolor
* @method COMMANDS.hilitecolor
* @static
* @param {String} cmd The command executed: backcolor
* @param {String} val The color value to apply
* @return {NodeList} NodeList of the items touched by this command.
*/
hilitecolor: function() {
return ExecCommand.COMMANDS.backcolor.apply(this, arguments);
},
/**
* Adds a font name to the current selection, or creates a new element and applies it
* @method COMMANDS.fontname
* @static
* @param {String} cmd The command executed: fontname
* @param {String} val The font name to apply
* @return {NodeList} NodeList of the items touched by this command.
*/
fontname: function(cmd, val) {
var inst = this.getInstance(),
sel = new inst.Selection(), n;
if (sel.isCollapsed) {
n = this.command('inserthtml', '<span style="font-family: ' + val + '">&nbsp;</span>');
sel.selectNode(n.get('firstChild'), true);
return n;
} else {
return this._command('fontname', val);
}
},
/**
* Adds a fontsize to the current selection, or creates a new element and applies it
* @method COMMANDS.fontsize
* @static
* @param {String} cmd The command executed: fontsize
* @param {String} val The font size to apply
* @return {NodeList} NodeList of the items touched by this command.
*/
fontsize: function(cmd, val) {
var inst = this.getInstance(),
sel = new inst.Selection(), n;
if (sel.isCollapsed) {
n = this.command('inserthtml', '<font size="' + val + '">&nbsp;</font>');
sel.selectNode(n.get('firstChild'), true);
return n;
} else {
return this._command('fontsize', val);
}
}
}
});
Y.namespace('Plugin');
Y.Plugin.ExecCommand = ExecCommand;
}, '@VERSION@', { requires: [ 'frame' ], skinnable: false });