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