JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
305a6f91ff5c15723941e53aa711e86460878bc4
[ckeditor.git] / _source / plugins / button / plugin.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.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                         id = this._.id = CKEDITOR.tools.getNextId(),\r
74                         classes = '',\r
75                         command = this.command, // Get the command name.\r
76                         clickFn,\r
77                         index;\r
78 \r
79                 this._.editor = editor;\r
80 \r
81                 var instance =\r
82                 {\r
83                         id : id,\r
84                         button : this,\r
85                         editor : editor,\r
86                         focus : function()\r
87                         {\r
88                                 var element = CKEDITOR.document.getById( id );\r
89                                 element.focus();\r
90                         },\r
91                         execute : function()\r
92                         {\r
93                                 this.button.click( editor );\r
94                         }\r
95                 };\r
96 \r
97                 instance.clickFn = clickFn = CKEDITOR.tools.addFunction( instance.execute, instance );\r
98 \r
99                 instance.index = index = CKEDITOR.ui.button._.instances.push( instance ) - 1;\r
100 \r
101                 if ( this.modes )\r
102                 {\r
103                         editor.on( 'mode', function()\r
104                                 {\r
105                                         this.setState( this.modes[ editor.mode ] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED );\r
106                                 }, this);\r
107                 }\r
108                 else if ( command )\r
109                 {\r
110                         // Get the command instance.\r
111                         command = editor.getCommand( command );\r
112 \r
113                         if ( command )\r
114                         {\r
115                                 command.on( 'state', function()\r
116                                         {\r
117                                                 this.setState( command.state );\r
118                                         }, this);\r
119 \r
120                                 classes += 'cke_' + (\r
121                                         command.state == CKEDITOR.TRISTATE_ON ? 'on' :\r
122                                         command.state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' :\r
123                                         'off' );\r
124                         }\r
125                 }\r
126 \r
127                 if ( !command )\r
128                         classes += 'cke_off';\r
129 \r
130                 if ( this.className )\r
131                         classes += ' ' + this.className;\r
132 \r
133                 output.push(\r
134                         '<span class="cke_button">',\r
135                         '<a id="', id, '"' +\r
136                                 ' class="', classes, '"',\r
137                                 env.gecko && env.version >= 10900 && !env.hc  ? '' : '" href="javascript:void(\''+ ( this.title || '' ).replace( "'", '' )+ '\')"',\r
138                                 ' title="', this.title, '"' +\r
139                                 ' tabindex="-1"' +\r
140                                 ' hidefocus="true"' +\r
141                             ' role="button"' +\r
142                                 ' aria-labelledby="' + id + '_label"' +\r
143                                 ( this.hasArrow ?  ' aria-haspopup="true"' : '' ) );\r
144 \r
145                 // Some browsers don't cancel key events in the keydown but in the\r
146                 // keypress.\r
147                 // TODO: Check if really needed for Gecko+Mac.\r
148                 if ( env.opera || ( env.gecko && env.mac ) )\r
149                 {\r
150                         output.push(\r
151                                 ' onkeypress="return false;"' );\r
152                 }\r
153 \r
154                 // With Firefox, we need to force the button to redraw, otherwise it\r
155                 // will remain in the focus state.\r
156                 if ( env.gecko )\r
157                 {\r
158                         output.push(\r
159                                 ' onblur="this.style.cssText = this.style.cssText;"' );\r
160                 }\r
161 \r
162                 output.push(\r
163                                 ' onkeydown="return CKEDITOR.ui.button._.keydown(', index, ', event);"' +\r
164                                 ' onfocus="return CKEDITOR.ui.button._.focus(', index, ', event);"' +\r
165                                 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ', this); return false;">' +\r
166                                         '<span class="cke_icon"' );\r
167 \r
168                 if ( this.icon )\r
169                 {\r
170                         var offset = ( this.iconOffset || 0 ) * -16;\r
171                         output.push( ' style="background-image:url(', CKEDITOR.getUrl( this.icon ), ');background-position:0 ' + offset + 'px;"' );\r
172                 }\r
173 \r
174                 output.push(\r
175                                         '>&nbsp;</span>' +\r
176                                         '<span id="', id, '_label" class="cke_label">', this.label, '</span>' );\r
177 \r
178                 if ( this.hasArrow )\r
179                 {\r
180                         output.push(\r
181                                         '<span class="cke_buttonarrow">'\r
182                                         // BLACK DOWN-POINTING TRIANGLE\r
183                                         + ( CKEDITOR.env.hc ? '&#9660;' : '&nbsp;' )\r
184                                         + '</span>' );\r
185                 }\r
186 \r
187                 output.push(\r
188                         '</a>',\r
189                         '</span>' );\r
190 \r
191                 if ( this.onRender )\r
192                         this.onRender();\r
193 \r
194                 return instance;\r
195         },\r
196 \r
197         setState : function( state )\r
198         {\r
199                 if ( this._.state == state )\r
200                         return false;\r
201 \r
202                 this._.state = state;\r
203 \r
204                 var element = CKEDITOR.document.getById( this._.id );\r
205 \r
206                 if ( element )\r
207                 {\r
208                         element.setState( state );\r
209                         state == CKEDITOR.TRISTATE_DISABLED ?\r
210                                 element.setAttribute( 'aria-disabled', true ) :\r
211                                 element.removeAttribute( 'aria-disabled' );\r
212 \r
213                         state == CKEDITOR.TRISTATE_ON ?\r
214                                 element.setAttribute( 'aria-pressed', true ) :\r
215                                 element.removeAttribute( 'aria-pressed' );\r
216 \r
217                         return true;\r
218                 }\r
219                 else\r
220                         return false;\r
221         }\r
222 };\r
223 \r
224 /**\r
225  * Handles a button click.\r
226  * @private\r
227  */\r
228 CKEDITOR.ui.button._ =\r
229 {\r
230         instances : [],\r
231 \r
232         keydown : function( index, ev )\r
233         {\r
234                 var instance = CKEDITOR.ui.button._.instances[ index ];\r
235 \r
236                 if ( instance.onkey )\r
237                 {\r
238                         ev = new CKEDITOR.dom.event( ev );\r
239                         return ( instance.onkey( instance, ev.getKeystroke() ) !== false );\r
240                 }\r
241         },\r
242 \r
243         focus : function( index, ev )\r
244         {\r
245                 var instance = CKEDITOR.ui.button._.instances[ index ],\r
246                         retVal;\r
247 \r
248                 if ( instance.onfocus )\r
249                         retVal = ( instance.onfocus( instance, new CKEDITOR.dom.event( ev ) ) !== false );\r
250 \r
251                 // FF2: prevent focus event been bubbled up to editor container, which caused unexpected editor focus.\r
252                 if ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )\r
253                         ev.preventBubble();\r
254                 return retVal;\r
255         }\r
256 };\r
257 \r
258 /**\r
259  * Adds a button definition to the UI elements list.\r
260  * @param {String} The button name.\r
261  * @param {Object} The button definition.\r
262  * @example\r
263  * editorInstance.ui.addButton( 'MyBold',\r
264  *     {\r
265  *         label : 'My Bold',\r
266  *         command : 'bold'\r
267  *     });\r
268  */\r
269 CKEDITOR.ui.prototype.addButton = function( name, definition )\r
270 {\r
271         this.add( name, CKEDITOR.UI_BUTTON, definition );\r
272 };\r
273 \r
274 CKEDITOR.on( 'reset', function()\r
275         {\r
276                 CKEDITOR.ui.button._.instances = [];\r
277         });\r