JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.0
[ckeditor.git] / _source / core / command.js
diff --git a/_source/core/command.js b/_source/core/command.js
new file mode 100644 (file)
index 0000000..fe64f21
--- /dev/null
@@ -0,0 +1,70 @@
+/*\r
+Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+For licensing, see LICENSE.html or http://ckeditor.com/license\r
+*/\r
+\r
+CKEDITOR.command = function( editor, commandDefinition )\r
+{\r
+       this.exec = function( data )\r
+       {\r
+               if ( this.state == CKEDITOR.TRISTATE_DISABLED )\r
+                       return false;\r
+\r
+               // The editor will always have the focus when executing a command.\r
+               editor.focus();\r
+\r
+               return ( commandDefinition.exec.call( this, editor, data ) !== false );\r
+       };\r
+\r
+       CKEDITOR.tools.extend( this, commandDefinition,\r
+               // Defaults\r
+               {\r
+                       modes : { wysiwyg : 1 },\r
+                       state : CKEDITOR.TRISTATE_OFF\r
+               });\r
+\r
+       // Call the CKEDITOR.event constructor to initialize this instance.\r
+       CKEDITOR.event.call( this );\r
+};\r
+\r
+CKEDITOR.command.prototype =\r
+{\r
+       enable : function()\r
+       {\r
+               if ( this.state == CKEDITOR.TRISTATE_DISABLED )\r
+                       this.setState( ( !this.preserveState || ( typeof this.previousState == 'undefined' ) ) ? CKEDITOR.TRISTATE_OFF : this.previousState );\r
+       },\r
+\r
+       disable : function()\r
+       {\r
+               this.setState( CKEDITOR.TRISTATE_DISABLED );\r
+       },\r
+\r
+       setState : function( newState )\r
+       {\r
+               // Do nothing if there is no state change.\r
+               if ( this.state == newState )\r
+                       return false;\r
+\r
+               this.previousState = this.state;\r
+\r
+               // Set the new state.\r
+               this.state = newState;\r
+\r
+               // Fire the "state" event, so other parts of the code can react to the\r
+               // change.\r
+               this.fire( 'state' );\r
+\r
+               return true;\r
+       },\r
+\r
+       toggleState : function()\r
+       {\r
+               if ( this.state == CKEDITOR.TRISTATE_OFF )\r
+                       this.setState( CKEDITOR.TRISTATE_ON );\r
+               else if ( this.state == CKEDITOR.TRISTATE_ON )\r
+                       this.setState( CKEDITOR.TRISTATE_OFF );\r
+       }\r
+};\r
+\r
+CKEDITOR.event.implementOn( CKEDITOR.command.prototype, true );\r