JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2
[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 \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, '"',\r
138                                 env.gecko && env.version >= 10900 && !env.hc  ? '' : '" href="javascript:void(\''+ ( this.title || '' ).replace( "'"+ '' )+ '\')"',\r
139                                 ' title="', this.title, '"' +\r
140                                 ' tabindex="-1"' +\r
141                                 ' hidefocus="true"' +\r
142                             ' role="button"' +\r
143                                 ' aria-labelledby="' + id + '_label"' +\r
144                                 ( this.hasArrow ?  ' aria-haspopup="true"' : '' ) );\r
145 \r
146                 // Some browsers don't cancel key events in the keydown but in the\r
147                 // keypress.\r
148                 // TODO: Check if really needed for Gecko+Mac.\r
149                 if ( env.opera || ( env.gecko && env.mac ) )\r
150                 {\r
151                         output.push(\r
152                                 ' onkeypress="return false;"' );\r
153                 }\r
154 \r
155                 // With Firefox, we need to force the button to redraw, otherwise it\r
156                 // will remain in the focus state.\r
157                 if ( env.gecko )\r
158                 {\r
159                         output.push(\r
160                                 ' onblur="this.style.cssText = this.style.cssText;"' );\r
161                 }\r
162 \r
163                 output.push(\r
164                                 ' onkeydown="return CKEDITOR.ui.button._.keydown(', index, ', event);"' +\r
165                                 ' onfocus="return CKEDITOR.ui.button._.focus(', index, ', event);"' +\r
166                                 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ', this); return false;">' +\r
167                                         '<span class="cke_icon"' );\r
168 \r
169                 if ( this.icon )\r
170                 {\r
171                         var offset = ( this.iconOffset || 0 ) * -16;\r
172                         output.push( ' style="background-image:url(', CKEDITOR.getUrl( this.icon ), ');background-position:0 ' + offset + 'px;"' );\r
173                 }\r
174 \r
175                 output.push(\r
176                                         '></span>' +\r
177                                         '<span id="', id, '_label" class="cke_label">', this.label, '</span>' );\r
178 \r
179                 if ( this.hasArrow )\r
180                 {\r
181                         output.push(\r
182                                         '<span class="cke_buttonarrow"></span>' );\r
183                 }\r
184 \r
185                 output.push(\r
186                         '</a>',\r
187                         '</span>' );\r
188 \r
189                 if ( this.onRender )\r
190                         this.onRender();\r
191 \r
192                 return instance;\r
193         },\r
194 \r
195         setState : function( state )\r
196         {\r
197                 if ( this._.state == state )\r
198                         return false;\r
199 \r
200                 this._.state = state;\r
201 \r
202                 var element = CKEDITOR.document.getById( this._.id );\r
203 \r
204                 if ( element )\r
205                 {\r
206                         element.setState( state );\r
207                         state == CKEDITOR.TRISTATE_DISABLED ?\r
208                                 element.setAttribute( 'aria-disabled', true ) :\r
209                                 element.removeAttribute( 'aria-disabled' );\r
210 \r
211                         state == CKEDITOR.TRISTATE_ON ?\r
212                                 element.setAttribute( 'aria-pressed', true ) :\r
213                                 element.removeAttribute( 'aria-pressed' );\r
214 \r
215                         return true;\r
216                 }\r
217                 else\r
218                         return false;\r
219         }\r
220 };\r
221 \r
222 /**\r
223  * Handles a button click.\r
224  * @private\r
225  */\r
226 CKEDITOR.ui.button._ =\r
227 {\r
228         instances : [],\r
229 \r
230         keydown : function( index, ev )\r
231         {\r
232                 var instance = CKEDITOR.ui.button._.instances[ index ];\r
233 \r
234                 if ( instance.onkey )\r
235                 {\r
236                         ev = new CKEDITOR.dom.event( ev );\r
237                         return ( instance.onkey( instance, ev.getKeystroke() ) !== false );\r
238                 }\r
239         },\r
240 \r
241         focus : function( index, ev )\r
242         {\r
243                 var instance = CKEDITOR.ui.button._.instances[ index ],\r
244                         retVal;\r
245 \r
246                 if ( instance.onfocus )\r
247                         retVal = ( instance.onfocus( instance, new CKEDITOR.dom.event( ev ) ) !== false );\r
248 \r
249                 // FF2: prevent focus event been bubbled up to editor container, which caused unexpected editor focus.\r
250                 if ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )\r
251                         ev.preventBubble();\r
252                 return retVal;\r
253         }\r
254 };\r
255 \r
256 /**\r
257  * Adds a button definition to the UI elements list.\r
258  * @param {String} The button name.\r
259  * @param {Object} The button definition.\r
260  * @example\r
261  * editorInstance.ui.addButton( 'MyBold',\r
262  *     {\r
263  *         label : 'My Bold',\r
264  *         command : 'bold'\r
265  *     });\r
266  */\r
267 CKEDITOR.ui.prototype.addButton = function( name, definition )\r
268 {\r
269         this.add( name, CKEDITOR.UI_BUTTON, definition );\r
270 };\r