JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
0a8d0988635a2d77f0172d73da18a13dfef32f32
[ckeditor.git] / command.js
1 /*\r
2 Copyright (c) 2003-2010, 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.uiItems = [];\r
9 \r
10         this.exec = function( data )\r
11         {\r
12                 if ( this.state == CKEDITOR.TRISTATE_DISABLED )\r
13                         return false;\r
14 \r
15                 if ( this.editorFocus )     // Give editor focus if necessary (#4355).\r
16                         editor.focus();\r
17 \r
18                 return ( commandDefinition.exec.call( this, editor, data ) !== false );\r
19         };\r
20 \r
21         CKEDITOR.tools.extend( this, commandDefinition,\r
22                 // Defaults\r
23                 {\r
24                         modes : { wysiwyg : 1 },\r
25                         editorFocus : true,\r
26                         state : CKEDITOR.TRISTATE_OFF\r
27                 });\r
28 \r
29         // Call the CKEDITOR.event constructor to initialize this instance.\r
30         CKEDITOR.event.call( this );\r
31 };\r
32 \r
33 CKEDITOR.command.prototype =\r
34 {\r
35         enable : function()\r
36         {\r
37                 if ( this.state == CKEDITOR.TRISTATE_DISABLED )\r
38                         this.setState( ( !this.preserveState || ( typeof this.previousState == 'undefined' ) ) ? CKEDITOR.TRISTATE_OFF : this.previousState );\r
39         },\r
40 \r
41         disable : function()\r
42         {\r
43                 this.setState( CKEDITOR.TRISTATE_DISABLED );\r
44         },\r
45 \r
46         setState : function( newState )\r
47         {\r
48                 // Do nothing if there is no state change.\r
49                 if ( this.state == newState )\r
50                         return false;\r
51 \r
52                 this.previousState = this.state;\r
53 \r
54                 // Set the new state.\r
55                 this.state = newState;\r
56 \r
57                 // Fire the "state" event, so other parts of the code can react to the\r
58                 // change.\r
59                 this.fire( 'state' );\r
60 \r
61                 return true;\r
62         },\r
63 \r
64         toggleState : function()\r
65         {\r
66                 if ( this.state == CKEDITOR.TRISTATE_OFF )\r
67                         this.setState( CKEDITOR.TRISTATE_ON );\r
68                 else if ( this.state == CKEDITOR.TRISTATE_ON )\r
69                         this.setState( CKEDITOR.TRISTATE_OFF );\r
70         }\r
71 };\r
72 \r
73 CKEDITOR.event.implementOn( CKEDITOR.command.prototype, true );\r