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