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
74 var id = this._.id = 'cke_' + CKEDITOR.tools.getNextNumber();
\r
75 this._.editor = editor;
\r
84 var element = CKEDITOR.document.getById( id );
\r
87 execute : function()
\r
89 this.button.click( editor );
\r
93 var clickFn = CKEDITOR.tools.addFunction( instance.execute, instance );
\r
95 var index = CKEDITOR.ui.button._.instances.push( instance ) - 1;
\r
99 // Get the command name.
\r
100 var command = this.command;
\r
104 editor.on( 'mode', function()
\r
106 this.setState( this.modes[ editor.mode ] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED );
\r
109 else if ( command )
\r
111 // Get the command instance.
\r
112 command = editor.getCommand( command );
\r
116 command.on( 'state', function()
\r
118 this.setState( command.state );
\r
121 classes += 'cke_' + (
\r
122 command.state == CKEDITOR.TRISTATE_ON ? 'on' :
\r
123 command.state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' :
\r
129 classes += 'cke_off';
\r
131 if ( this.className )
\r
132 classes += ' ' + this.className;
\r
135 '<span class="cke_button">',
\r
136 '<a id="', id, '"' +
\r
137 ' class="', classes, '" href="javascript:void(\'', ( this.title || '' ).replace( "'", '' ), '\')"' +
\r
138 ' title="', this.title, '"' +
\r
140 ' hidefocus="true"' );
\r
142 // Some browsers don't cancel key events in the keydown but in the
\r
144 // TODO: Check if really needed for Gecko+Mac.
\r
145 if ( env.opera || ( env.gecko && env.mac ) )
\r
148 ' onkeypress="return false;"' );
\r
151 // With Firefox, we need to force the button to redraw, otherwise it
\r
152 // will remain in the focus state.
\r
156 ' onblur="this.style.cssText = this.style.cssText;"' );
\r
160 ' onkeydown="return CKEDITOR.ui.button._.keydown(', index, ', event);"' +
\r
161 ' onfocus="return CKEDITOR.ui.button._.focus(', index, ', event);"' +
\r
162 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ', this); return false;">' +
\r
163 '<span class="cke_icon"' );
\r
167 var offset = ( this.iconOffset || 0 ) * -16;
\r
168 output.push( ' style="background-image:url(', CKEDITOR.getUrl( this.icon ), ');background-position:0 ' + offset + 'px;"' );
\r
173 '<span class="cke_label">', this.label, '</span>' );
\r
175 if ( this.hasArrow )
\r
178 '<span class="cke_buttonarrow"></span>' );
\r
185 if ( this.onRender )
\r
191 setState : function( state )
\r
193 if ( this._.state == state )
\r
196 var element = CKEDITOR.document.getById( this._.id );
\r
200 element.setState( state );
\r
202 var htmlTitle = this.title,
\r
203 unavailable = this._.editor.lang.common.unavailable,
\r
204 labelElement = element.getChild( 1 );
\r
206 if ( state == CKEDITOR.TRISTATE_DISABLED )
\r
207 htmlTitle = unavailable.replace( '%1', this.title );
\r
209 labelElement.setHtml( htmlTitle );
\r
212 this._.state = state;
\r
217 * Handles a button click.
\r
220 CKEDITOR.ui.button._ =
\r
224 keydown : function( index, ev )
\r
226 var instance = CKEDITOR.ui.button._.instances[ index ];
\r
228 if ( instance.onkey )
\r
230 ev = new CKEDITOR.dom.event( ev );
\r
231 return ( instance.onkey( instance, ev.getKeystroke() ) !== false );
\r
235 focus : function( index, ev )
\r
237 var instance = CKEDITOR.ui.button._.instances[ index ],
\r
240 if ( instance.onfocus )
\r
241 retVal = ( instance.onfocus( instance, new CKEDITOR.dom.event( ev ) ) !== false );
\r
243 // FF2: prevent focus event been bubbled up to editor container, which caused unexpected editor focus.
\r
244 if ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )
\r
245 ev.preventBubble();
\r
251 * Adds a button definition to the UI elements list.
\r
252 * @param {String} The button name.
\r
253 * @param {Object} The button definition.
\r
255 * editorInstance.ui.addButton( 'MyBold',
\r
257 * label : 'My Bold',
\r
261 CKEDITOR.ui.prototype.addButton = function( name, definition )
\r
263 this.add( name, CKEDITOR.UI_BUTTON, definition );
\r