2 Copyright (c) 2003-2013, 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 = 'button';
\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
60 CKEDITOR.ui.button.prototype =
\r
63 * Renders the button.
\r
64 * @param {CKEDITOR.editor} editor The editor instance which this button is
\r
66 * @param {Array} output The output array to which append the HTML relative
\r
70 render : function( editor, output )
\r
72 var env = CKEDITOR.env,
\r
73 id = this._.id = CKEDITOR.tools.getNextId(),
\r
75 command = this.command, // Get the command name.
\r
78 this._.editor = editor;
\r
87 var element = CKEDITOR.document.getById( id );
\r
90 execute : function()
\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
96 this.button.click( editor );
\r
100 var keydownFn = CKEDITOR.tools.addFunction( function( ev )
\r
102 if ( instance.onkey )
\r
104 ev = new CKEDITOR.dom.event( ev );
\r
105 return ( instance.onkey( instance, ev.getKeystroke() ) !== false );
\r
109 var focusFn = CKEDITOR.tools.addFunction( function( ev )
\r
113 if ( instance.onfocus )
\r
114 retVal = ( instance.onfocus( instance, new CKEDITOR.dom.event( ev ) ) !== false );
\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
122 instance.clickFn = clickFn = CKEDITOR.tools.addFunction( instance.execute, instance );
\r
125 // Indicate a mode sensitive button.
\r
128 var modeStates = {};
\r
130 function updateState()
\r
132 // "this" is a CKEDITOR.ui.button instance.
\r
134 var mode = editor.mode;
\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
142 this.setState( editor.readOnly && !this.readOnly ? CKEDITOR.TRISTATE_DISABLED : state );
\r
146 editor.on( 'beforeModeUnload', function()
\r
148 if ( editor.mode && this._.state != CKEDITOR.TRISTATE_DISABLED )
\r
149 modeStates[ editor.mode ] = this._.state;
\r
152 editor.on( 'mode', updateState, this);
\r
154 // If this button is sensitive to readOnly state, update it accordingly.
\r
155 !this.readOnly && editor.on( 'readOnly', updateState, this);
\r
157 else if ( command )
\r
159 // Get the command instance.
\r
160 command = editor.getCommand( command );
\r
164 command.on( 'state', function()
\r
166 this.setState( command.state );
\r
169 classes += 'cke_' + (
\r
170 command.state == CKEDITOR.TRISTATE_ON ? 'on' :
\r
171 command.state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' :
\r
177 classes += 'cke_off';
\r
179 if ( this.className )
\r
180 classes += ' ' + this.className;
\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
189 ' hidefocus="true"' +
\r
191 ' aria-labelledby="' + id + '_label"' +
\r
192 ( this.hasArrow ? ' aria-haspopup="true"' : '' ) );
\r
194 // Some browsers don't cancel key events in the keydown but in the
\r
196 // TODO: Check if really needed for Gecko+Mac.
\r
197 if ( env.opera || ( env.gecko && env.mac ) )
\r
200 ' onkeypress="return false;"' );
\r
203 // With Firefox, we need to force the button to redraw, otherwise it
\r
204 // will remain in the focus state.
\r
208 ' onblur="this.style.cssText = this.style.cssText;"' );
\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
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