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