2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
6 CKEDITOR
.plugins
.add( 'button',
8 beforeInit : function( editor
)
10 editor
.ui
.addHandler( CKEDITOR
.UI_BUTTON
, CKEDITOR
.ui
.button
.handler
);
19 CKEDITOR
.UI_BUTTON
= 1;
22 * Represents a button UI element. This class should not be called directly. To
23 * create new buttons use {@link CKEDITOR.ui.prototype.addButton} instead.
25 * @param {Object} definition The button definition.
28 CKEDITOR
.ui
.button = function( definition
)
30 // Copy all definition properties to this object.
31 CKEDITOR
.tools
.extend( this, definition
,
34 title
: definition
.label
,
35 className
: definition
.className
|| ( definition
.command
&& 'cke_button_' + definition
.command
) || '',
36 click
: definition
.click
|| function( editor
)
38 editor
.execCommand( definition
.command
);
46 * Transforms a button definition in a {@link CKEDITOR.ui.button} instance.
50 CKEDITOR
.ui
.button
.handler
=
52 create : function( definition
)
54 return new CKEDITOR
.ui
.button( definition
);
58 CKEDITOR
.ui
.button
.prototype =
64 * @param {CKEDITOR.editor} editor The editor instance which this button is
66 * @param {Array} output The output array to which append the HTML relative
70 render : function( editor
, output
)
72 var env
= CKEDITOR
.env
,
73 id
= this._
.id
= 'cke_' + CKEDITOR
.tools
.getNextNumber(),
75 command
= this.command
, // Get the command name.
79 this._
.editor
= editor
;
88 var element
= CKEDITOR
.document
.getById( id
);
93 this.button
.click( editor
);
97 instance
.clickFn
= clickFn
= CKEDITOR
.tools
.addFunction( instance
.execute
, instance
);
99 instance
.index
= index
= CKEDITOR
.ui
.button
._
.instances
.push( instance
) - 1;
103 editor
.on( 'mode', function()
105 this.setState( this.modes
[ editor
.mode
] ? CKEDITOR
.TRISTATE_OFF
: CKEDITOR
.TRISTATE_DISABLED
);
110 // Get the command instance.
111 command
= editor
.getCommand( command
);
115 command
.on( 'state', function()
117 this.setState( command
.state
);
120 classes
+= 'cke_' + (
121 command
.state
== CKEDITOR
.TRISTATE_ON
? 'on' :
122 command
.state
== CKEDITOR
.TRISTATE_DISABLED
? 'disabled' :
128 classes
+= 'cke_off';
130 if ( this.className
)
131 classes
+= ' ' + this.className
;
134 '<span class="cke_button">',
136 ' class="', classes
, '"',
137 env
.gecko
&& env
.version
>= 10900 && !env
.hc
? '' : '" href="javascript:void(\''+ ( this.title
|| '' ).replace( "'"+ '' )+ '\')"',
138 ' title="', this.title
, '"' +
140 ' hidefocus="true"' +
142 ' aria-labelledby="' + id
+ '_label"' +
143 ( this.hasArrow
? ' aria-haspopup="true"' : '' ) );
145 // Some browsers don't cancel key events in the keydown but in the
147 // TODO: Check if really needed for Gecko+Mac.
148 if ( env
.opera
|| ( env
.gecko
&& env
.mac
) )
151 ' onkeypress="return false;"' );
154 // With Firefox, we need to force the button to redraw, otherwise it
155 // will remain in the focus state.
159 ' onblur="this.style.cssText = this.style.cssText;"' );
163 ' onkeydown="return CKEDITOR.ui.button._.keydown(', index
, ', event);"' +
164 ' onfocus="return CKEDITOR.ui.button._.focus(', index
, ', event);"' +
165 ' onclick="CKEDITOR.tools.callFunction(', clickFn
, ', this); return false;">' +
166 '<span class="cke_icon"' );
170 var offset
= ( this.iconOffset
|| 0 ) * -16;
171 output
.push( ' style="background-image:url(', CKEDITOR
.getUrl( this.icon
), ');background-position:0 ' + offset
+ 'px;"' );
176 '<span id="', id
, '_label" class="cke_label">', this.label
, '</span>' );
181 '<span class="cke_buttonarrow">'
182 // BLACK DOWN-POINTING TRIANGLE
183 + ( CKEDITOR
.env
.hc
? '▼' : '' )
197 setState : function( state
)
199 if ( this._
.state
== state
)
202 this._
.state
= state
;
204 var element
= CKEDITOR
.document
.getById( this._
.id
);
208 element
.setState( state
);
209 state
== CKEDITOR
.TRISTATE_DISABLED
?
210 element
.setAttribute( 'aria-disabled', true ) :
211 element
.removeAttribute( 'aria-disabled' );
213 state
== CKEDITOR
.TRISTATE_ON
?
214 element
.setAttribute( 'aria-pressed', true ) :
215 element
.removeAttribute( 'aria-pressed' );
225 * Handles a button click.
228 CKEDITOR
.ui
.button
._
=
232 keydown : function( index
, ev
)
234 var instance
= CKEDITOR
.ui
.button
._
.instances
[ index
];
236 if ( instance
.onkey
)
238 ev
= new CKEDITOR
.dom
.event( ev
);
239 return ( instance
.onkey( instance
, ev
.getKeystroke() ) !== false );
243 focus : function( index
, ev
)
245 var instance
= CKEDITOR
.ui
.button
._
.instances
[ index
],
248 if ( instance
.onfocus
)
249 retVal
= ( instance
.onfocus( instance
, new CKEDITOR
.dom
.event( ev
) ) !== false );
251 // FF2: prevent focus event been bubbled up to editor container, which caused unexpected editor focus.
252 if ( CKEDITOR
.env
.gecko
&& CKEDITOR
.env
.version
< 10900 )
259 * Adds a button definition to the UI elements list.
260 * @param {String} The button name.
261 * @param {Object} The button definition.
263 * editorInstance.ui.addButton( 'MyBold',
269 CKEDITOR
.ui
.prototype.addButton = function( name
, definition
)
271 this.add( name
, CKEDITOR
.UI_BUTTON
, definition
);