2 Copyright (c) 2003-2010, 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
58 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
79 this._.editor = editor;
\r
88 var element = CKEDITOR.document.getById( id );
\r
91 execute : function()
\r
93 this.button.click( editor );
\r
97 instance.clickFn = clickFn = CKEDITOR.tools.addFunction( instance.execute, instance );
\r
99 instance.index = index = CKEDITOR.ui.button._.instances.push( instance ) - 1;
\r
101 // Indicate a mode sensitive button.
\r
104 var modeStates = {};
\r
105 editor.on( 'beforeModeUnload', function()
\r
107 modeStates[ editor.mode ] = this._.state;
\r
110 editor.on( 'mode', function()
\r
112 var mode = editor.mode;
\r
113 // Restore saved button state.
\r
114 this.setState( this.modes[ mode ] ?
\r
115 modeStates[ mode ] != undefined ? modeStates[ mode ] :
\r
116 CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED );
\r
119 else if ( command )
\r
121 // Get the command instance.
\r
122 command = editor.getCommand( command );
\r
126 command.on( 'state', function()
\r
128 this.setState( command.state );
\r
131 classes += 'cke_' + (
\r
132 command.state == CKEDITOR.TRISTATE_ON ? 'on' :
\r
133 command.state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' :
\r
139 classes += 'cke_off';
\r
141 if ( this.className )
\r
142 classes += ' ' + this.className;
\r
145 '<span class="cke_button">',
\r
146 '<a id="', id, '"' +
\r
147 ' class="', classes, '"',
\r
148 env.gecko && env.version >= 10900 && !env.hc ? '' : '" href="javascript:void(\''+ ( this.title || '' ).replace( "'", '' )+ '\')"',
\r
149 ' title="', this.title, '"' +
\r
151 ' hidefocus="true"' +
\r
153 ' aria-labelledby="' + id + '_label"' +
\r
154 ( this.hasArrow ? ' aria-haspopup="true"' : '' ) );
\r
156 // Some browsers don't cancel key events in the keydown but in the
\r
158 // TODO: Check if really needed for Gecko+Mac.
\r
159 if ( env.opera || ( env.gecko && env.mac ) )
\r
162 ' onkeypress="return false;"' );
\r
165 // With Firefox, we need to force the button to redraw, otherwise it
\r
166 // will remain in the focus state.
\r
170 ' onblur="this.style.cssText = this.style.cssText;"' );
\r
174 ' onkeydown="return CKEDITOR.ui.button._.keydown(', index, ', event);"' +
\r
175 ' onfocus="return CKEDITOR.ui.button._.focus(', index, ', event);"' +
\r
176 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ', this); return false;">' +
\r
177 '<span class="cke_icon"' );
\r
181 var offset = ( this.iconOffset || 0 ) * -16;
\r
182 output.push( ' style="background-image:url(', CKEDITOR.getUrl( this.icon ), ');background-position:0 ' + offset + 'px;"' );
\r
187 '<span id="', id, '_label" class="cke_label">', this.label, '</span>' );
\r
189 if ( this.hasArrow )
\r
192 '<span class="cke_buttonarrow">'
\r
193 // BLACK DOWN-POINTING TRIANGLE
\r
194 + ( CKEDITOR.env.hc ? '▼' : ' ' )
\r
202 if ( this.onRender )
\r
208 setState : function( state )
\r
210 if ( this._.state == state )
\r
213 this._.state = state;
\r
215 var element = CKEDITOR.document.getById( this._.id );
\r
219 element.setState( state );
\r
220 state == CKEDITOR.TRISTATE_DISABLED ?
\r
221 element.setAttribute( 'aria-disabled', true ) :
\r
222 element.removeAttribute( 'aria-disabled' );
\r
224 state == CKEDITOR.TRISTATE_ON ?
\r
225 element.setAttribute( 'aria-pressed', true ) :
\r
226 element.removeAttribute( 'aria-pressed' );
\r
236 * Handles a button click.
\r
239 CKEDITOR.ui.button._ =
\r
243 keydown : function( index, ev )
\r
245 var instance = CKEDITOR.ui.button._.instances[ index ];
\r
247 if ( instance.onkey )
\r
249 ev = new CKEDITOR.dom.event( ev );
\r
250 return ( instance.onkey( instance, ev.getKeystroke() ) !== false );
\r
254 focus : function( index, ev )
\r
256 var instance = CKEDITOR.ui.button._.instances[ index ],
\r
259 if ( instance.onfocus )
\r
260 retVal = ( instance.onfocus( instance, new CKEDITOR.dom.event( ev ) ) !== false );
\r
262 // FF2: prevent focus event been bubbled up to editor container, which caused unexpected editor focus.
\r
263 if ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )
\r
264 ev.preventBubble();
\r
270 * Adds a button definition to the UI elements list.
\r
271 * @param {String} The button name.
\r
272 * @param {Object} The button definition.
\r
274 * editorInstance.ui.addButton( 'MyBold',
\r
276 * label : 'My Bold',
\r
280 CKEDITOR.ui.prototype.addButton = function( name, definition )
\r
282 this.add( name, CKEDITOR.UI_BUTTON, definition );
\r
285 CKEDITOR.on( 'reset', function()
\r
287 CKEDITOR.ui.button._.instances = [];
\r