1 /* 2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved. 3 For licensing, see LICENSE.html or http://ckeditor.com/license 4 */ 5 6 CKEDITOR.command = function( editor, commandDefinition ) 7 { 8 this.exec = function( data ) 9 { 10 if ( this.state == CKEDITOR.TRISTATE_DISABLED ) 11 return false; 12 13 // The editor will always have the focus when executing a command. 14 editor.focus(); 15 16 return ( commandDefinition.exec.call( this, editor, data ) !== false ); 17 }; 18 19 CKEDITOR.tools.extend( this, commandDefinition, 20 // Defaults 21 { 22 modes : { wysiwyg : 1 }, 23 state : CKEDITOR.TRISTATE_OFF 24 }); 25 26 // Call the CKEDITOR.event constructor to initialize this instance. 27 CKEDITOR.event.call( this ); 28 }; 29 30 CKEDITOR.command.prototype = 31 { 32 enable : function() 33 { 34 if ( this.state == CKEDITOR.TRISTATE_DISABLED ) 35 this.setState( ( !this.preserveState || ( typeof this.previousState == 'undefined' ) ) ? CKEDITOR.TRISTATE_OFF : this.previousState ); 36 }, 37 38 disable : function() 39 { 40 this.setState( CKEDITOR.TRISTATE_DISABLED ); 41 }, 42 43 setState : function( newState ) 44 { 45 // Do nothing if there is no state change. 46 if ( this.state == newState ) 47 return false; 48 49 this.previousState = this.state; 50 51 // Set the new state. 52 this.state = newState; 53 54 // Fire the "state" event, so other parts of the code can react to the 55 // change. 56 this.fire( 'state' ); 57 58 return true; 59 }, 60 61 toggleState : function() 62 { 63 if ( this.state == CKEDITOR.TRISTATE_OFF ) 64 this.setState( CKEDITOR.TRISTATE_ON ); 65 else if ( this.state == CKEDITOR.TRISTATE_ON ) 66 this.setState( CKEDITOR.TRISTATE_OFF ); 67 } 68 }; 69 70 CKEDITOR.event.implementOn( CKEDITOR.command.prototype, true ); 71