2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
6 CKEDITOR.plugins.add( 'button',
\r
8 beforeInit : function( editor )
\r
10 editor.ui.addHandler( CKEDITOR.UI_BUTTON, CKEDITOR.ui.button.handler );
\r
15 * Button UI element.
\r
19 CKEDITOR.UI_BUTTON = 1;
\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
25 * @param {Object} definition The button definition.
\r
28 CKEDITOR.ui.button = function( definition )
\r
30 // Copy all definition properties to this object.
\r
31 CKEDITOR.tools.extend( this, definition,
\r
34 title : definition.label,
\r
35 className : definition.className || ( definition.command && 'cke_button_' + definition.command ) || '',
\r
36 click : definition.click || function( editor )
\r
38 editor.execCommand( definition.command );
\r
46 * Transforms a button definition in a {@link CKEDITOR.ui.button} instance.
\r
50 CKEDITOR.ui.button.handler =
\r
52 create : function( definition )
\r
54 return new CKEDITOR.ui.button( definition );
\r
59 * Handles a button click.
\r
62 CKEDITOR.ui.button._ =
\r
66 keydown : function( index, ev )
\r
68 var instance = CKEDITOR.ui.button._.instances[ index ];
\r
70 if ( instance.onkey )
\r
72 ev = new CKEDITOR.dom.event( ev );
\r
73 return ( instance.onkey( instance, ev.getKeystroke() ) !== false );
\r
77 focus : function( index, ev )
\r
79 var instance = CKEDITOR.ui.button._.instances[ index ],
\r
82 if ( instance.onfocus )
\r
83 retVal = ( instance.onfocus( instance, new CKEDITOR.dom.event( ev ) ) !== false );
\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
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
97 CKEDITOR.ui.button.prototype =
\r
102 * Renders the button.
\r
103 * @param {CKEDITOR.editor} editor The editor instance which this button is
\r
105 * @param {Array} output The output array to which append the HTML relative
\r
109 render : function( editor, output )
\r
111 var env = CKEDITOR.env,
\r
112 id = this._.id = CKEDITOR.tools.getNextId(),
\r
114 command = this.command, // Get the command name.
\r
118 this._.editor = editor;
\r
127 var element = CKEDITOR.document.getById( id );
\r
130 execute : function()
\r
132 this.button.click( editor );
\r
136 instance.clickFn = clickFn = CKEDITOR.tools.addFunction( instance.execute, instance );
\r
138 instance.index = index = CKEDITOR.ui.button._.instances.push( instance ) - 1;
\r
140 // Indicate a mode sensitive button.
\r
143 var modeStates = {};
\r
144 editor.on( 'beforeModeUnload', function()
\r
146 modeStates[ editor.mode ] = this._.state;
\r
149 editor.on( 'mode', function()
\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
158 else if ( command )
\r
160 // Get the command instance.
\r
161 command = editor.getCommand( command );
\r
165 command.on( 'state', function()
\r
167 this.setState( command.state );
\r
170 classes += 'cke_' + (
\r
171 command.state == CKEDITOR.TRISTATE_ON ? 'on' :
\r
172 command.state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' :
\r
178 classes += 'cke_off';
\r
180 if ( this.className )
\r
181 classes += ' ' + this.className;
\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
190 ' hidefocus="true"' +
\r
192 ' aria-labelledby="' + id + '_label"' +
\r
193 ( this.hasArrow ? ' aria-haspopup="true"' : '' ) );
\r
195 // Some browsers don't cancel key events in the keydown but in the
\r
197 // TODO: Check if really needed for Gecko+Mac.
\r
198 if ( env.opera || ( env.gecko && env.mac ) )
\r
201 ' onkeypress="return false;"' );
\r
204 // With Firefox, we need to force the button to redraw, otherwise it
\r
205 // will remain in the focus state.
\r
209 ' onblur="this.style.cssText = this.style.cssText;"' );
\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
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
226 '<span id="', id, '_label" class="cke_label">', this.label, '</span>' );
\r
228 if ( this.hasArrow )
\r
231 '<span class="cke_buttonarrow">'
\r
232 // BLACK DOWN-POINTING TRIANGLE
\r
233 + ( CKEDITOR.env.hc ? '▼' : ' ' )
\r
241 if ( this.onRender )
\r
247 setState : function( state )
\r
249 if ( this._.state == state )
\r
252 this._.state = state;
\r
254 var element = CKEDITOR.document.getById( this._.id );
\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
263 state == CKEDITOR.TRISTATE_ON ?
\r
264 element.setAttribute( 'aria-pressed', true ) :
\r
265 element.removeAttribute( 'aria-pressed' );
\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
281 * editorInstance.ui.addButton( 'MyBold',
\r
283 * label : 'My Bold',
\r
287 CKEDITOR.ui.prototype.addButton = function( name, definition )
\r
289 this.add( name, CKEDITOR.UI_BUTTON, definition );
\r
292 CKEDITOR.on( 'reset', function()
\r
294 CKEDITOR.ui.button._.instances = [];
\r