]> git.immae.eu Git - perso/Immae/Projets/packagist/connexionswing-ckeditor-component.git/blob - sources/plugins/button/plugin.js
Initial commit
[perso/Immae/Projets/packagist/connexionswing-ckeditor-component.git] / sources / plugins / button / plugin.js
1 /**
2 * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
3 * For licensing, see LICENSE.md or http://ckeditor.com/license
4 */
5
6 ( function() {
7 var template = '<a id="{id}"' +
8 ' class="cke_button cke_button__{name} cke_button_{state} {cls}"' +
9 ( CKEDITOR.env.gecko && !CKEDITOR.env.hc ? '' : ' href="javascript:void(\'{titleJs}\')"' ) +
10 ' title="{title}"' +
11 ' tabindex="-1"' +
12 ' hidefocus="true"' +
13 ' role="button"' +
14 ' aria-labelledby="{id}_label"' +
15 ' aria-haspopup="{hasArrow}"' +
16 ' aria-disabled="{ariaDisabled}"';
17
18 // Some browsers don't cancel key events in the keydown but in the
19 // keypress.
20 // TODO: Check if really needed.
21 if ( CKEDITOR.env.gecko && CKEDITOR.env.mac )
22 template += ' onkeypress="return false;"';
23
24 // With Firefox, we need to force the button to redraw, otherwise it
25 // will remain in the focus state.
26 if ( CKEDITOR.env.gecko )
27 template += ' onblur="this.style.cssText = this.style.cssText;"';
28
29 template += ' onkeydown="return CKEDITOR.tools.callFunction({keydownFn},event);"' +
30 ' onfocus="return CKEDITOR.tools.callFunction({focusFn},event);" ' +
31 ( CKEDITOR.env.ie ? 'onclick="return false;" onmouseup' : 'onclick' ) + // #188
32 '="CKEDITOR.tools.callFunction({clickFn},this);return false;">' +
33 '<span class="cke_button_icon cke_button__{iconName}_icon" style="{style}"';
34
35
36 template += '>&nbsp;</span>' +
37 '<span id="{id}_label" class="cke_button_label cke_button__{name}_label" aria-hidden="false">{label}</span>' +
38 '{arrowHtml}' +
39 '</a>';
40
41 var templateArrow = '<span class="cke_button_arrow">' +
42 // BLACK DOWN-POINTING TRIANGLE
43 ( CKEDITOR.env.hc ? '&#9660;' : '' ) +
44 '</span>';
45
46 var btnArrowTpl = CKEDITOR.addTemplate( 'buttonArrow', templateArrow ),
47 btnTpl = CKEDITOR.addTemplate( 'button', template );
48
49 CKEDITOR.plugins.add( 'button', {
50 lang: 'af,ar,bg,ca,cs,da,de,el,en,en-gb,eo,es,eu,fa,fi,fr,gl,he,hu,id,it,ja,km,ko,ku,lt,nb,nl,pl,pt,pt-br,ro,ru,sk,sl,sq,sv,tr,tt,ug,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE%
51 beforeInit: function( editor ) {
52 editor.ui.addHandler( CKEDITOR.UI_BUTTON, CKEDITOR.ui.button.handler );
53 }
54 } );
55
56 /**
57 * Button UI element.
58 *
59 * @readonly
60 * @property {String} [='button']
61 * @member CKEDITOR
62 */
63 CKEDITOR.UI_BUTTON = 'button';
64
65 /**
66 * Represents a button UI element. This class should not be called directly. To
67 * create new buttons use {@link CKEDITOR.ui#addButton} instead.
68 *
69 * @class
70 * @constructor Creates a button class instance.
71 * @param {Object} definition The button definition.
72 */
73 CKEDITOR.ui.button = function( definition ) {
74 CKEDITOR.tools.extend( this, definition,
75 // Set defaults.
76 {
77 title: definition.label,
78 click: definition.click ||
79 function( editor ) {
80 editor.execCommand( definition.command );
81 }
82 } );
83
84 this._ = {};
85 };
86
87 /**
88 * Represents the button handler object.
89 *
90 * @class
91 * @singleton
92 * @extends CKEDITOR.ui.handlerDefinition
93 */
94 CKEDITOR.ui.button.handler = {
95 /**
96 * Transforms a button definition in a {@link CKEDITOR.ui.button} instance.
97 *
98 * @member CKEDITOR.ui.button.handler
99 * @param {Object} definition
100 * @returns {CKEDITOR.ui.button}
101 */
102 create: function( definition ) {
103 return new CKEDITOR.ui.button( definition );
104 }
105 };
106
107 /** @class CKEDITOR.ui.button */
108 CKEDITOR.ui.button.prototype = {
109 /**
110 * Renders the button.
111 *
112 * @param {CKEDITOR.editor} editor The editor instance which this button is
113 * to be used by.
114 * @param {Array} output The output array to which the HTML code related to
115 * this button should be appended.
116 */
117 render: function( editor, output ) {
118 function updateState() {
119 // "this" is a CKEDITOR.ui.button instance.
120 var mode = editor.mode;
121
122 if ( mode ) {
123 // Restore saved button state.
124 var state = this.modes[ mode ] ? modeStates[ mode ] !== undefined ? modeStates[ mode ] : CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;
125
126 state = editor.readOnly && !this.readOnly ? CKEDITOR.TRISTATE_DISABLED : state;
127
128 this.setState( state );
129
130 // Let plugin to disable button.
131 if ( this.refresh )
132 this.refresh();
133 }
134 }
135
136 var env = CKEDITOR.env,
137 id = this._.id = CKEDITOR.tools.getNextId(),
138 stateName = '',
139 command = this.command,
140 // Get the command name.
141 clickFn;
142
143 this._.editor = editor;
144
145 var instance = {
146 id: id,
147 button: this,
148 editor: editor,
149 focus: function() {
150 var element = CKEDITOR.document.getById( id );
151 element.focus();
152 },
153 execute: function() {
154 this.button.click( editor );
155 },
156 attach: function( editor ) {
157 this.button.attach( editor );
158 }
159 };
160
161 var keydownFn = CKEDITOR.tools.addFunction( function( ev ) {
162 if ( instance.onkey ) {
163 ev = new CKEDITOR.dom.event( ev );
164 return ( instance.onkey( instance, ev.getKeystroke() ) !== false );
165 }
166 } );
167
168 var focusFn = CKEDITOR.tools.addFunction( function( ev ) {
169 var retVal;
170
171 if ( instance.onfocus )
172 retVal = ( instance.onfocus( instance, new CKEDITOR.dom.event( ev ) ) !== false );
173
174 return retVal;
175 } );
176
177 var selLocked = 0;
178
179 instance.clickFn = clickFn = CKEDITOR.tools.addFunction( function() {
180
181 // Restore locked selection in Opera.
182 if ( selLocked ) {
183 editor.unlockSelection( 1 );
184 selLocked = 0;
185 }
186 instance.execute();
187
188 // Fixed iOS focus issue when your press disabled button (#12381).
189 if ( env.iOS ) {
190 editor.focus();
191 }
192 } );
193
194
195 // Indicate a mode sensitive button.
196 if ( this.modes ) {
197 var modeStates = {};
198
199 editor.on( 'beforeModeUnload', function() {
200 if ( editor.mode && this._.state != CKEDITOR.TRISTATE_DISABLED )
201 modeStates[ editor.mode ] = this._.state;
202 }, this );
203
204 // Update status when activeFilter, mode or readOnly changes.
205 editor.on( 'activeFilterChange', updateState, this );
206 editor.on( 'mode', updateState, this );
207 // If this button is sensitive to readOnly state, update it accordingly.
208 !this.readOnly && editor.on( 'readOnly', updateState, this );
209
210 } else if ( command ) {
211 // Get the command instance.
212 command = editor.getCommand( command );
213
214 if ( command ) {
215 command.on( 'state', function() {
216 this.setState( command.state );
217 }, this );
218
219 stateName += ( command.state == CKEDITOR.TRISTATE_ON ? 'on' : command.state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' : 'off' );
220 }
221 }
222
223 // For button that has text-direction awareness on selection path.
224 if ( this.directional ) {
225 editor.on( 'contentDirChanged', function( evt ) {
226 var el = CKEDITOR.document.getById( this._.id ),
227 icon = el.getFirst();
228
229 var pathDir = evt.data;
230
231 // Make a minor direction change to become style-able for the skin icon.
232 if ( pathDir != editor.lang.dir )
233 el.addClass( 'cke_' + pathDir );
234 else
235 el.removeClass( 'cke_ltr' ).removeClass( 'cke_rtl' );
236
237 // Inline style update for the plugin icon.
238 icon.setAttribute( 'style', CKEDITOR.skin.getIconStyle( iconName, pathDir == 'rtl', this.icon, this.iconOffset ) );
239 }, this );
240 }
241
242 if ( !command )
243 stateName += 'off';
244
245 var name = this.name || this.command,
246 iconName = name;
247
248 // Check if we're pointing to an icon defined by another command. (#9555)
249 if ( this.icon && !( /\./ ).test( this.icon ) ) {
250 iconName = this.icon;
251 this.icon = null;
252 }
253
254 var params = {
255 id: id,
256 name: name,
257 iconName: iconName,
258 label: this.label,
259 cls: this.className || '',
260 state: stateName,
261 ariaDisabled: stateName == 'disabled' ? 'true' : 'false',
262 title: this.title,
263 titleJs: env.gecko && !env.hc ? '' : ( this.title || '' ).replace( "'", '' ),
264 hasArrow: this.hasArrow ? 'true' : 'false',
265 keydownFn: keydownFn,
266 focusFn: focusFn,
267 clickFn: clickFn,
268 style: CKEDITOR.skin.getIconStyle( iconName, ( editor.lang.dir == 'rtl' ), this.icon, this.iconOffset ),
269 arrowHtml: this.hasArrow ? btnArrowTpl.output() : ''
270 };
271
272 btnTpl.output( params, output );
273
274 if ( this.onRender )
275 this.onRender();
276
277 return instance;
278 },
279
280 /**
281 * Sets the button state.
282 *
283 * @param {Number} state Indicates the button state. One of {@link CKEDITOR#TRISTATE_ON},
284 * {@link CKEDITOR#TRISTATE_OFF}, or {@link CKEDITOR#TRISTATE_DISABLED}.
285 */
286 setState: function( state ) {
287 if ( this._.state == state )
288 return false;
289
290 this._.state = state;
291
292 var element = CKEDITOR.document.getById( this._.id );
293
294 if ( element ) {
295 element.setState( state, 'cke_button' );
296
297 state == CKEDITOR.TRISTATE_DISABLED ?
298 element.setAttribute( 'aria-disabled', true ) :
299 element.removeAttribute( 'aria-disabled' );
300
301 if ( !this.hasArrow ) {
302 // Note: aria-pressed attribute should not be added to menuButton instances. (#11331)
303 state == CKEDITOR.TRISTATE_ON ?
304 element.setAttribute( 'aria-pressed', true ) :
305 element.removeAttribute( 'aria-pressed' );
306 } else {
307 var newLabel = state == CKEDITOR.TRISTATE_ON ?
308 this._.editor.lang.button.selectedLabel.replace( /%1/g, this.label ) : this.label;
309 CKEDITOR.document.getById( this._.id + '_label' ).setText( newLabel );
310 }
311
312 return true;
313 } else {
314 return false;
315 }
316 },
317
318 /**
319 * Gets the button state.
320 *
321 * @returns {Number} The button state. One of {@link CKEDITOR#TRISTATE_ON},
322 * {@link CKEDITOR#TRISTATE_OFF}, or {@link CKEDITOR#TRISTATE_DISABLED}.
323 */
324 getState: function() {
325 return this._.state;
326 },
327
328 /**
329 * Returns this button's {@link CKEDITOR.feature} instance.
330 *
331 * It may be this button instance if it has at least one of
332 * `allowedContent` and `requiredContent` properties. Otherwise,
333 * if a command is bound to this button by the `command` property, then
334 * that command will be returned.
335 *
336 * This method implements the {@link CKEDITOR.feature#toFeature} interface method.
337 *
338 * @since 4.1
339 * @param {CKEDITOR.editor} Editor instance.
340 * @returns {CKEDITOR.feature} The feature.
341 */
342 toFeature: function( editor ) {
343 if ( this._.feature )
344 return this._.feature;
345
346 var feature = this;
347
348 // If button isn't a feature, return command if is bound.
349 if ( !this.allowedContent && !this.requiredContent && this.command )
350 feature = editor.getCommand( this.command ) || feature;
351
352 return this._.feature = feature;
353 }
354 };
355
356 /**
357 * Adds a button definition to the UI elements list.
358 *
359 * editorInstance.ui.addButton( 'MyBold', {
360 * label: 'My Bold',
361 * command: 'bold',
362 * toolbar: 'basicstyles,1'
363 * } );
364 *
365 * @member CKEDITOR.ui
366 * @param {String} name The button name.
367 * @param {Object} definition The button definition.
368 * @param {String} definition.label The textual part of the button (if visible) and its tooltip.
369 * @param {String} definition.command The command to be executed once the button is activated.
370 * @param {String} definition.toolbar The {@link CKEDITOR.config#toolbarGroups toolbar group} into which
371 * the button will be added. An optional index value (separated by a comma) determines the button position within the group.
372 */
373 CKEDITOR.ui.prototype.addButton = function( name, definition ) {
374 this.add( name, CKEDITOR.UI_BUTTON, definition );
375 };
376
377 } )();