JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.1
[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                                 // IE 6 needs some time before execution (#7922)\r
93                                 if ( CKEDITOR.env.ie && CKEDITOR.env.version < 7 )\r
94                                         CKEDITOR.tools.setTimeout( function(){ this.button.click( editor ); }, 0, this );\r
95                                 else\r
96                                         this.button.click( editor );\r
97                         }\r
98                 };\r
99 \r
100                 var keydownFn = CKEDITOR.tools.addFunction( function( ev )\r
101                         {\r
102                                 if ( instance.onkey )\r
103                                 {\r
104                                         ev = new CKEDITOR.dom.event( ev );\r
105                                         return ( instance.onkey( instance, ev.getKeystroke() ) !== false );\r
106                                 }\r
107                         });\r
108 \r
109                 var focusFn = CKEDITOR.tools.addFunction( function( ev )\r
110                         {\r
111                                 var retVal;\r
112 \r
113                                 if ( instance.onfocus )\r
114                                           retVal = ( instance.onfocus( instance, new CKEDITOR.dom.event( ev ) ) !== false );\r
115 \r
116                                 // FF2: prevent focus event been bubbled up to editor container, which caused unexpected editor focus.\r
117                                 if ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )\r
118                                           ev.preventBubble();\r
119                                 return retVal;\r
120                         });\r
121 \r
122                 instance.clickFn = clickFn = CKEDITOR.tools.addFunction( instance.execute, instance );\r
123 \r
124 \r
125                 // Indicate a mode sensitive button.\r
126                 if ( this.modes )\r
127                 {\r
128                         var modeStates = {};\r
129 \r
130                         function updateState()\r
131                         {\r
132                                 // "this" is a CKEDITOR.ui.button instance.\r
133 \r
134                                 var mode = editor.mode;\r
135 \r
136                                 if ( mode )\r
137                                 {\r
138                                         // Restore saved button state.\r
139                                         var state = this.modes[ mode ] ? modeStates[ mode ] != undefined ? modeStates[ mode ] :\r
140                                                         CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;\r
141 \r
142                                         this.setState( editor.readOnly && !this.readOnly ? CKEDITOR.TRISTATE_DISABLED : state );\r
143                                 }\r
144                         }\r
145 \r
146                         editor.on( 'beforeModeUnload', function()\r
147                                 {\r
148                                         if ( editor.mode && this._.state != CKEDITOR.TRISTATE_DISABLED )\r
149                                                 modeStates[ editor.mode ] = this._.state;\r
150                                 }, this );\r
151 \r
152                         editor.on( 'mode', updateState, this);\r
153 \r
154                         // If this button is sensitive to readOnly state, update it accordingly.\r
155                         !this.readOnly && editor.on( 'readOnly', updateState, this);\r
156                 }\r
157                 else if ( command )\r
158                 {\r
159                         // Get the command instance.\r
160                         command = editor.getCommand( command );\r
161 \r
162                         if ( command )\r
163                         {\r
164                                 command.on( 'state', function()\r
165                                         {\r
166                                                 this.setState( command.state );\r
167                                         }, this);\r
168 \r
169                                 classes += 'cke_' + (\r
170                                         command.state == CKEDITOR.TRISTATE_ON ? 'on' :\r
171                                         command.state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' :\r
172                                         'off' );\r
173                         }\r
174                 }\r
175 \r
176                 if ( !command )\r
177                         classes += 'cke_off';\r
178 \r
179                 if ( this.className )\r
180                         classes += ' ' + this.className;\r
181 \r
182                 output.push(\r
183                         '<span class="cke_button' + ( this.icon && this.icon.indexOf( '.png' ) == -1 ? ' cke_noalphafix' : '' ) + '">',\r
184                         '<a id="', id, '"' +\r
185                                 ' class="', classes, '"',\r
186                                 env.gecko && env.version >= 10900 && !env.hc  ? '' : '" href="javascript:void(\''+ ( this.title || '' ).replace( "'", '' )+ '\')"',\r
187                                 ' title="', this.title, '"' +\r
188                                 ' tabindex="-1"' +\r
189                                 ' hidefocus="true"' +\r
190                             ' role="button"' +\r
191                                 ' aria-labelledby="' + id + '_label"' +\r
192                                 ( this.hasArrow ?  ' aria-haspopup="true"' : '' ) );\r
193 \r
194                 // Some browsers don't cancel key events in the keydown but in the\r
195                 // keypress.\r
196                 // TODO: Check if really needed for Gecko+Mac.\r
197                 if ( env.opera || ( env.gecko && env.mac ) )\r
198                 {\r
199                         output.push(\r
200                                 ' onkeypress="return false;"' );\r
201                 }\r
202 \r
203                 // With Firefox, we need to force the button to redraw, otherwise it\r
204                 // will remain in the focus state.\r
205                 if ( env.gecko )\r
206                 {\r
207                         output.push(\r
208                                 ' onblur="this.style.cssText = this.style.cssText;"' );\r
209                 }\r
210 \r
211                 output.push(\r
212                                         ' onkeydown="return CKEDITOR.tools.callFunction(', keydownFn, ', event);"' +\r
213                                         ' onfocus="return CKEDITOR.tools.callFunction(', focusFn,', event);" ' +\r
214                                         ( CKEDITOR.env.ie ? 'onclick="return false;" onmouseup' : 'onclick' ) +         // #188\r
215                                                 '="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