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