2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
7 * Creates a command class instance.
\r
8 * @class Represents a command that can be executed on an editor instance.
\r
9 * @param {CKEDITOR.editor} editor The editor instance this command will be
\r
11 * @param {CKEDITOR.commandDefinition} commandDefinition The command
\r
13 * @augments CKEDITOR.event
\r
15 * var command = new CKEDITOR.command( editor,
\r
17 * exec : function( editor )
\r
19 * alert( editor.document.getBody().getHtml() );
\r
23 CKEDITOR.command = function( editor, commandDefinition )
\r
26 * Lists UI items that are associated to this command. This list can be
\r
27 * used to interact with the UI on command execution (by the execution code
\r
28 * itself, for example).
\r
31 * alert( 'Number of UI items associated to this command: ' + command.<b>uiItems</b>.length );
\r
36 * Executes the command.
\r
37 * @param {Object} [data] Any data to pass to the command. Depends on the
\r
38 * command implementation and requirements.
\r
39 * @returns {Boolean} A boolean indicating that the command has been
\r
40 * successfully executed.
\r
42 * command.<b>exec()</b>; // The command gets executed.
\r
44 this.exec = function( data )
\r
46 if ( this.state == CKEDITOR.TRISTATE_DISABLED )
\r
49 if ( this.editorFocus ) // Give editor focus if necessary (#4355).
\r
52 return ( commandDefinition.exec.call( this, editor, data ) !== false );
\r
55 CKEDITOR.tools.extend( this, commandDefinition,
\r
57 /** @lends CKEDITOR.command.prototype */
\r
60 * The editor modes within which the command can be executed. The
\r
61 * execution will have no action if the current mode is not listed
\r
64 * @default { wysiwyg : 1 }
\r
65 * @see CKEDITOR.editor.prototype.mode
\r
67 * // Enable the command in both WYSIWYG and Source modes.
\r
68 * command.<b>modes</b> = { wysiwyg : 1, source : 1 };
\r
70 * // Enable the command in Source mode only.
\r
71 * command.<b>modes</b> = { source : 1 };
\r
73 modes : { wysiwyg : 1 },
\r
76 * Indicates that the editor will get the focus before executing
\r
81 * // Do not force the editor to have focus when executing the command.
\r
82 * command.<b>editorFocus</b> = false;
\r
87 * Indicates the editor state. Possible values are:
\r
89 * <li>{@link CKEDITOR.TRISTATE_DISABLED}: the command is
\r
90 * disabled. It's execution will have no effect. Same as
\r
91 * {@link disable}.</li>
\r
92 * <li>{@link CKEDITOR.TRISTATE_ON}: the command is enabled
\r
93 * and currently active in the editor (for context sensitive commands,
\r
94 * for example).</li>
\r
95 * <li>{@link CKEDITOR.TRISTATE_OFF}: the command is enabled
\r
96 * and currently inactive in the editor (for context sensitive
\r
97 * commands, for example).</li>
\r
99 * Do not set this property directly, using the {@link #setState}
\r
102 * @default {@link CKEDITOR.TRISTATE_OFF}
\r
104 * if ( command.<b>state</b> == CKEDITOR.TRISTATE_DISABLED )
\r
105 * alert( 'This command is disabled' );
\r
107 state : CKEDITOR.TRISTATE_OFF
\r
110 // Call the CKEDITOR.event constructor to initialize this instance.
\r
111 CKEDITOR.event.call( this );
\r
114 CKEDITOR.command.prototype =
\r
117 * Enables the command for execution. The command state (see
\r
118 * {@link CKEDITOR.command.prototype.state}) available before disabling it
\r
121 * command.<b>enable()</b>;
\r
122 * command.exec(); // Execute the command.
\r
124 enable : function()
\r
126 if ( this.state == CKEDITOR.TRISTATE_DISABLED )
\r
127 this.setState( ( !this.preserveState || ( typeof this.previousState == 'undefined' ) ) ? CKEDITOR.TRISTATE_OFF : this.previousState );
\r
131 * Disables the command for execution. The command state (see
\r
132 * {@link CKEDITOR.command.prototype.state}) will be set to
\r
133 * {@link CKEDITOR.TRISTATE_DISABLED}.
\r
135 * command.<b>disable()</b>;
\r
136 * command.exec(); // "false" - Nothing happens.
\r
138 disable : function()
\r
140 this.setState( CKEDITOR.TRISTATE_DISABLED );
\r
144 * Sets the command state.
\r
145 * @param {Number} newState The new state. See {@link #state}.
\r
146 * @returns {Boolean} Returns "true" if the command state changed.
\r
148 * command.<b>setState( CKEDITOR.TRISTATE_ON )</b>;
\r
149 * command.exec(); // Execute the command.
\r
150 * command.<b>setState( CKEDITOR.TRISTATE_DISABLED )</b>;
\r
151 * command.exec(); // "false" - Nothing happens.
\r
152 * command.<b>setState( CKEDITOR.TRISTATE_OFF )</b>;
\r
153 * command.exec(); // Execute the command.
\r
155 setState : function( newState )
\r
157 // Do nothing if there is no state change.
\r
158 if ( this.state == newState )
\r
161 this.previousState = this.state;
\r
163 // Set the new state.
\r
164 this.state = newState;
\r
166 // Fire the "state" event, so other parts of the code can react to the
\r
168 this.fire( 'state' );
\r
174 * Toggles the on/off (active/inactive) state of the command. This is
\r
175 * mainly used internally by context sensitive commands.
\r
177 * command.<b>toggleState()</b>;
\r
179 toggleState : function()
\r
181 if ( this.state == CKEDITOR.TRISTATE_OFF )
\r
182 this.setState( CKEDITOR.TRISTATE_ON );
\r
183 else if ( this.state == CKEDITOR.TRISTATE_ON )
\r
184 this.setState( CKEDITOR.TRISTATE_OFF );
\r
188 CKEDITOR.event.implementOn( CKEDITOR.command.prototype, true );
\r
191 * Indicates the previous command state.
\r
192 * @name CKEDITOR.command.prototype.previousState
\r
196 * alert( command.<b>previousState</b> );
\r
200 * Fired when the command state changes.
\r
201 * @name CKEDITOR.command#state
\r
204 * command.on( <b>'state'</b> , function( e )
\r
206 * // Alerts the new state.
\r
207 * alert( this.state );
\r