JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
db982d5b2a0f1f2327a5426728bcba2d9e4e694a
[ckeditor.git] / _source / plugins / button / plugin.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.plugins.add( 'button',\r
7 {\r
8         beforeInit : function( editor )\r
9         {\r
10                 editor.ui.addHandler( CKEDITOR.UI_BUTTON, CKEDITOR.ui.button.handler );\r
11         }\r
12 });\r
13 \r
14 /**\r
15  * Button UI element.\r
16  * @constant\r
17  * @example\r
18  */\r
19 CKEDITOR.UI_BUTTON = 1;\r
20 \r
21 /**\r
22  * Represents a button UI element. This class should not be called directly. To\r
23  * create new buttons use {@link CKEDITOR.ui.prototype.addButton} instead.\r
24  * @constructor\r
25  * @param {Object} definition The button definition.\r
26  * @example\r
27  */\r
28 CKEDITOR.ui.button = function( definition )\r
29 {\r
30         // Copy all definition properties to this object.\r
31         CKEDITOR.tools.extend( this, definition,\r
32                 // Set defaults.\r
33                 {\r
34                         title           : definition.label,\r
35                         className       : definition.className || ( definition.command && 'cke_button_' + definition.command ) || '',\r
36                         click           : definition.click || function( editor )\r
37                                 {\r
38                                         editor.execCommand( definition.command );\r
39                                 }\r
40                 });\r
41 \r
42         this._ = {};\r
43 };\r
44 \r
45 /**\r
46  * Transforms a button definition in a {@link CKEDITOR.ui.button} instance.\r
47  * @type Object\r
48  * @example\r
49  */\r
50 CKEDITOR.ui.button.handler =\r
51 {\r
52         create : function( definition )\r
53         {\r
54                 return new CKEDITOR.ui.button( definition );\r
55         }\r
56 };\r
57 \r
58 CKEDITOR.ui.button.prototype =\r
59 {\r
60         canGroup : true,\r
61 \r
62         /**\r
63          * Renders the button.\r
64          * @param {CKEDITOR.editor} editor The editor instance which this button is\r
65          *              to be used by.\r
66          * @param {Array} output The output array to which append the HTML relative\r
67          *              to this button.\r
68          * @example\r
69          */\r
70         render : function( editor, output )\r
71         {\r
72                 var env = CKEDITOR.env;\r
73 \r
74                 var id = this._.id = 'cke_' + CKEDITOR.tools.getNextNumber();\r
75                 this._.editor = editor;\r
76 \r
77                 var instance =\r
78                 {\r
79                         id : id,\r
80                         button : this,\r
81                         editor : editor,\r
82                         focus : function()\r
83                         {\r
84                                 var element = CKEDITOR.document.getById( id );\r
85                                 element.focus();\r
86                         },\r
87                         execute : function()\r
88                         {\r
89                                 this.button.click( editor );\r
90                         }\r
91                 };\r
92 \r
93                 var clickFn = CKEDITOR.tools.addFunction( instance.execute, instance );\r
94 \r
95                 var index = CKEDITOR.ui.button._.instances.push( instance ) - 1;\r
96 \r
97                 var classes = '';\r
98 \r
99                 // Get the command name.\r
100                 var command = this.command;\r
101 \r
102                 if ( this.modes )\r
103                 {\r
104                         editor.on( 'mode', function()\r
105                                 {\r
106                                         this.setState( this.modes[ editor.mode ] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED );\r
107                                 }, this);\r
108                 }\r
109                 else if ( command )\r
110                 {\r
111                         // Get the command instance.\r
112                         command = editor.getCommand( command );\r
113 \r
114                         if ( command )\r
115                         {\r
116                                 command.on( 'state', function()\r
117                                         {\r
118                                                 this.setState( command.state );\r
119                                         }, this);\r
120 \r
121                                 classes += 'cke_' + (\r
122                                         command.state == CKEDITOR.TRISTATE_ON ? 'on' :\r
123                                         command.state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' :\r
124                                         'off' );\r
125                         }\r
126                 }\r
127 \r
128                 if ( !command )\r
129                         classes += 'cke_off';\r
130 \r
131                 if ( this.className )\r
132                         classes += ' ' + this.className;\r
133 \r
134                 output.push(\r
135                         '<span class="cke_button">',\r
136                         '<a id="', id, '"' +\r
137                                 ' class="', classes, '" href="javascript:void(\'', ( this.title || '' ).replace( "'", '' ), '\')"' +\r
138                                 ' title="', this.title, '"' +\r
139                                 ' tabindex="-1"' +\r
140                                 ' hidefocus="true"' );\r
141 \r
142                 // Some browsers don't cancel key events in the keydown but in the\r
143                 // keypress.\r
144                 // TODO: Check if really needed for Gecko+Mac.\r
145                 if ( env.opera || ( env.gecko && env.mac ) )\r
146                 {\r
147                         output.push(\r
148                                 ' onkeypress="return false;"' );\r
149                 }\r
150 \r
151                 // With Firefox, we need to force the button to redraw, otherwise it\r
152                 // will remain in the focus state.\r
153                 if ( env.gecko )\r
154                 {\r
155                         output.push(\r
156                                 ' onblur="this.style.cssText = this.style.cssText;"' );\r
157                 }\r
158 \r
159                 output.push(\r
160                                 ' onkeydown="return CKEDITOR.ui.button._.keydown(', index, ', event);"' +\r
161                                 ' onfocus="return CKEDITOR.ui.button._.focus(', index, ', event);"' +\r
162                                 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ', this); return false;">' +\r
163                                         '<span class="cke_icon"' );\r
164 \r
165                 if ( this.icon )\r
166                 {\r
167                         var offset = ( this.iconOffset || 0 ) * -16;\r
168                         output.push( ' style="background-image:url(', CKEDITOR.getUrl( this.icon ), ');background-position:0 ' + offset + 'px;"' );\r
169                 }\r
170 \r
171                 output.push(\r
172                                         '></span>' +\r
173                                         '<span class="cke_label">', this.label, '</span>' );\r
174 \r
175                 if ( this.hasArrow )\r
176                 {\r
177                         output.push(\r
178                                         '<span class="cke_buttonarrow"></span>' );\r
179                 }\r
180 \r
181                 output.push(\r
182                         '</a>',\r
183                         '</span>' );\r
184 \r
185                 if ( this.onRender )\r
186                         this.onRender();\r
187 \r
188                 return instance;\r
189         },\r
190 \r
191         setState : function( state )\r
192         {\r
193                 if ( this._.state == state )\r
194                         return;\r
195 \r
196                 var element = CKEDITOR.document.getById( this._.id );\r
197 \r
198                 if ( element )\r
199                 {\r
200                         element.setState( state );\r
201 \r
202                         var htmlTitle = this.title,\r
203                                 unavailable = this._.editor.lang.common.unavailable,\r
204                                 labelElement = element.getChild( 1 );\r
205 \r
206                         if ( state == CKEDITOR.TRISTATE_DISABLED )\r
207                                 htmlTitle = unavailable.replace( '%1', this.title );\r
208 \r
209                         labelElement.setHtml( htmlTitle );\r
210                 }\r
211 \r
212                 this._.state = state;\r
213         }\r
214 };\r
215 \r
216 /**\r
217  * Handles a button click.\r
218  * @private\r
219  */\r
220 CKEDITOR.ui.button._ =\r
221 {\r
222         instances : [],\r
223 \r
224         keydown : function( index, ev )\r
225         {\r
226                 var instance = CKEDITOR.ui.button._.instances[ index ];\r
227 \r
228                 if ( instance.onkey )\r
229                 {\r
230                         ev = new CKEDITOR.dom.event( ev );\r
231                         return ( instance.onkey( instance, ev.getKeystroke() ) !== false );\r
232                 }\r
233         },\r
234 \r
235         focus : function( index, ev )\r
236         {\r
237                 var instance = CKEDITOR.ui.button._.instances[ index ],\r
238                         retVal;\r
239 \r
240                 if ( instance.onfocus )\r
241                         retVal = ( instance.onfocus( instance, new CKEDITOR.dom.event( ev ) ) !== false );\r
242 \r
243                 // FF2: prevent focus event been bubbled up to editor container, which caused unexpected editor focus.\r
244                 if ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )\r
245                         ev.preventBubble();\r
246                 return retVal;\r
247         }\r
248 };\r
249 \r
250 /**\r
251  * Adds a button definition to the UI elements list.\r
252  * @param {String} The button name.\r
253  * @param {Object} The button definition.\r
254  * @example\r
255  * editorInstance.ui.addButton( 'MyBold',\r
256  *     {\r
257  *         label : 'My Bold',\r
258  *         command : 'bold'\r
259  *     });\r
260  */\r
261 CKEDITOR.ui.prototype.addButton = function( name, definition )\r
262 {\r
263         this.add( name, CKEDITOR.UI_BUTTON, definition );\r
264 };\r