exec-command.js revision dbf2c770f8178b12e8fe3c36bfa29df58ef13959
225N/A
225N/A /**
225N/A * Plugin for the frame module to handle execCommands for Editor
225N/A * @module editor
225N/A * @submodule exec-command
225N/A */
225N/A /**
225N/A * Plugin for the frame module to handle execCommands for Editor
225N/A * @class ExecCommand
225N/A * @extends Base
225N/A * @constructor
225N/A * @namespace Plugin
225N/A */
225N/A var ExecCommand = function() {
225N/A ExecCommand.superclass.constructor.apply(this, arguments);
225N/A };
225N/A
225N/A Y.extend(ExecCommand, Y.Base, {
225N/A /**
225N/A * An internal reference to the instance of the frame plugged into.
225N/A * @private
225N/A * @property _inst
3817N/A */
225N/A _inst: null,
225N/A /**
225N/A * Execute a command on the frame's document.
225N/A * @method command
225N/A * @param {String} action The action to perform (bold, italic, fontname)
1176N/A * @param {String} value The optional value (helvetica)
618N/A * @return {Node/NodeList} Should return the Node/Nodelist affected
225N/A */
225N/A command: function(action, value) {
844N/A var fn = ExecCommand.COMMANDS[action];
1176N/A
225N/A
1258N/A Y.log('execCommand(' + action + '): "' + value + '"', 'info', 'exec-command');
225N/A if (fn) {
2899N/A return fn.call(this, action, value);
2899N/A } else {
3817N/A return this._command(action, value);
3817N/A }
3817N/A },
225N/A /**
225N/A * The private version of execCommand that doesn't filter for overrides.
225N/A * @private
225N/A * @method _command
225N/A * @param {String} action The action to perform (bold, italic, fontname)
225N/A * @param {String} value The optional value (helvetica)
225N/A */
225N/A _command: function(action, value) {
225N/A var inst = this.getInstance();
225N/A try {
225N/A Y.log('Internal execCommand(' + action + '): "' + value + '"', 'info', 'exec-command');
225N/A inst.config.doc.execCommand(action, false, value);
225N/A } catch (e) {
225N/A Y.log(e.message, 'error', 'exec-command');
225N/A }
225N/A },
225N/A /**
225N/A * Get's the instance of YUI bound to the parent frame
225N/A * @method getInstance
225N/A * @return {YUI} The YUI instance bound to the parent frame
225N/A */
3846N/A getInstance: function() {
225N/A if (!this._inst) {
3846N/A this._inst = this.get('host').getInstance();
3846N/A }
3846N/A return this._inst;
3846N/A },
3846N/A initializer: function() {
3846N/A Y.mix(this.get('host'), {
3846N/A execCommand: function(action, value) {
225N/A return this.exec.command(action, value);
1989N/A },
1989N/A _execCommand: function(action, value) {
225N/A return this.exec._command(action, value);
3477N/A }
3477N/A });
225N/A }
225N/A }, {
225N/A /**
225N/A * execCommand
225N/A * @property NAME
225N/A * @static
225N/A */
225N/A NAME: 'execCommand',
225N/A /**
225N/A * exec
225N/A * @property NS
225N/A * @static
4337N/A */
4337N/A NS: 'exec',
225N/A ATTRS: {
225N/A host: {
225N/A value: false
225N/A }
897N/A },
897N/A /**
225N/A * Static object literal of execCommand overrides
3817N/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)
3817N/A * @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 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);
},
bidi: function() {
var inst = this.getInstance(),
sel = new inst.Selection(),
blockItem, dir,
blocks = 'p,div,li,body'; //More??
if (sel.anchorNode) {
blockItem = sel.anchorNode;
if (!sel.anchorNode.test(blocks)) {
blockItem = sel.anchorNode.ancestor(blocks);
}
dir = blockItem.getAttribute('dir');
if (dir === '') {
dir = inst.one('html').getAttribute('dir');
}
if (dir === 'rtl') {
dir = 'ltr';
} else {
dir = 'rtl';
}
blockItem.setAttribute('dir', dir);
}
return blockItem;
},
backcolor: function(cmd, val) {
if (Y.UA.gecko || Y.UA.opera) {
cmd = 'hilitecolor';
}
if (!Y.UA.ie) {
this._command('styleWithCSS', 'true');
}
this._command(cmd, val);
if (!Y.UA.ie) {
this._command('styleWithCSS', false);
}
},
hilitecolor: function() {
ExecCommand.COMMANDS.backcolor.apply(this, arguments);
}
}
});
Y.namespace('Plugin');
Y.Plugin.ExecCommand = ExecCommand;