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