2 Copyright (c) 2003-2011, 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
= 'button';
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
);
60 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
= CKEDITOR
.tools
.getNextId(),
75 command
= this.command
, // Get the command name.
78 this._
.editor
= editor
;
87 var element
= CKEDITOR
.document
.getById( id
);
92 // IE 6 needs some time before execution (#7922)
93 if ( CKEDITOR
.env
.ie
&& CKEDITOR
.env
.version
< 7 )
94 CKEDITOR
.tools
.setTimeout( function(){ this.button
.click( editor
); }, 0, this );
96 this.button
.click( editor
);
100 var keydownFn
= CKEDITOR
.tools
.addFunction( function( ev
)
102 if ( instance
.onkey
)
104 ev
= new CKEDITOR
.dom
.event( ev
);
105 return ( instance
.onkey( instance
, ev
.getKeystroke() ) !== false );
109 var focusFn
= CKEDITOR
.tools
.addFunction( function( ev
)
113 if ( instance
.onfocus
)
114 retVal
= ( instance
.onfocus( instance
, new CKEDITOR
.dom
.event( ev
) ) !== false );
116 // FF2: prevent focus event been bubbled up to editor container, which caused unexpected editor focus.
117 if ( CKEDITOR
.env
.gecko
&& CKEDITOR
.env
.version
< 10900 )
122 instance
.clickFn
= clickFn
= CKEDITOR
.tools
.addFunction( instance
.execute
, instance
);
125 // Indicate a mode sensitive button.
130 function updateState()
132 // "this" is a CKEDITOR.ui.button instance.
134 var mode
= editor
.mode
;
138 // Restore saved button state.
139 var state
= this.modes
[ mode
] ? modeStates
[ mode
] != undefined ? modeStates
[ mode
] :
140 CKEDITOR
.TRISTATE_OFF
: CKEDITOR
.TRISTATE_DISABLED
;
142 this.setState( editor
.readOnly
&& !this.readOnly
? CKEDITOR
.TRISTATE_DISABLED
: state
);
146 editor
.on( 'beforeModeUnload', function()
148 if ( editor
.mode
&& this._
.state
!= CKEDITOR
.TRISTATE_DISABLED
)
149 modeStates
[ editor
.mode
] = this._
.state
;
152 editor
.on( 'mode', updateState
, this);
154 // If this button is sensitive to readOnly state, update it accordingly.
155 !this.readOnly
&& editor
.on( 'readOnly', updateState
, this);
159 // Get the command instance.
160 command
= editor
.getCommand( command
);
164 command
.on( 'state', function()
166 this.setState( command
.state
);
169 classes
+= 'cke_' + (
170 command
.state
== CKEDITOR
.TRISTATE_ON
? 'on' :
171 command
.state
== CKEDITOR
.TRISTATE_DISABLED
? 'disabled' :
177 classes
+= 'cke_off';
179 if ( this.className
)
180 classes
+= ' ' + this.className
;
183 '<span class="cke_button' + ( this.icon
&& this.icon
.indexOf( '.png' ) == -1 ? ' cke_noalphafix' : '' ) + '">',
185 ' class="', classes
, '"',
186 env
.gecko
&& env
.version
>= 10900 && !env
.hc
? '' : '" href="javascript:void(\''+ ( this.title
|| '' ).replace( "'", '' )+ '\')"',
187 ' title="', this.title
, '"' +
189 ' hidefocus="true"' +
191 ' aria-labelledby="' + id
+ '_label"' +
192 ( this.hasArrow
? ' aria-haspopup="true"' : '' ) );
194 // Some browsers don't cancel key events in the keydown but in the
196 // TODO: Check if really needed for Gecko+Mac.
197 if ( env
.opera
|| ( env
.gecko
&& env
.mac
) )
200 ' onkeypress="return false;"' );
203 // With Firefox, we need to force the button to redraw, otherwise it
204 // will remain in the focus state.
208 ' onblur="this.style.cssText = this.style.cssText;"' );
212 ' onkeydown="return CKEDITOR.tools.callFunction(', keydownFn
, ', event);"' +
213 ' onfocus="return CKEDITOR.tools.callFunction(', focusFn
,', event);" ' +
214 ( CKEDITOR
.env
.ie
? 'onclick="return false;" onmouseup' : 'onclick' ) + // #188
215 '="CKEDITOR.tools.callFunction(', clickFn
, ', this); return false;">' +
216 '<span class="cke_icon"' );
220 var offset
= ( this.iconOffset
|| 0 ) * -16;
221 output
.push( ' style="background-image:url(', CKEDITOR
.getUrl( this.icon
), ');background-position:0 ' + offset
+ 'px;"' );
226 '<span id="', id
, '_label" class="cke_label">', this.label
, '</span>' );
231 '<span class="cke_buttonarrow">'
232 // BLACK DOWN-POINTING TRIANGLE
233 + ( CKEDITOR
.env
.hc
? '▼' : ' ' )
247 setState : function( state
)
249 if ( this._
.state
== state
)
252 this._
.state
= state
;
254 var element
= CKEDITOR
.document
.getById( this._
.id
);
258 element
.setState( state
);
259 state
== CKEDITOR
.TRISTATE_DISABLED
?
260 element
.setAttribute( 'aria-disabled', true ) :
261 element
.removeAttribute( 'aria-disabled' );
263 state
== CKEDITOR
.TRISTATE_ON
?
264 element
.setAttribute( 'aria-pressed', true ) :
265 element
.removeAttribute( 'aria-pressed' );
277 * Adds a button definition to the UI elements list.
278 * @param {String} name The button name.
279 * @param {Object} definition The button definition.
281 * editorInstance.ui.addButton( 'MyBold',
287 CKEDITOR
.ui
.prototype.addButton = function( name
, definition
)
289 this.add( name
, CKEDITOR
.UI_BUTTON
, definition
);