1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
/**
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
( function() {
var template = '<a id="{id}"' +
' class="cke_button cke_button__{name} cke_button_{state} {cls}"' +
( CKEDITOR.env.gecko && !CKEDITOR.env.hc ? '' : ' href="javascript:void(\'{titleJs}\')"' ) +
' title="{title}"' +
' tabindex="-1"' +
' hidefocus="true"' +
' role="button"' +
' aria-labelledby="{id}_label"' +
' aria-describedby="{id}_description"' +
' aria-haspopup="{hasArrow}"' +
' aria-disabled="{ariaDisabled}"';
// Some browsers don't cancel key events in the keydown but in the
// keypress.
// TODO: Check if really needed.
if ( CKEDITOR.env.gecko && CKEDITOR.env.mac )
template += ' onkeypress="return false;"';
// With Firefox, we need to force the button to redraw, otherwise it
// will remain in the focus state.
if ( CKEDITOR.env.gecko )
template += ' onblur="this.style.cssText = this.style.cssText;"';
// IE and Edge needs special click handler based on mouseup event with additional check
// of which mouse button was clicked (https://dev.ckeditor.com/ticket/188, #2565).
var specialClickHandler = '';
if ( CKEDITOR.env.ie ) {
specialClickHandler = 'return false;" onmouseup="CKEDITOR.tools.getMouseButton(event)==CKEDITOR.MOUSE_BUTTON_LEFT&&';
}
template += ' onkeydown="return CKEDITOR.tools.callFunction({keydownFn},event);"' +
' onfocus="return CKEDITOR.tools.callFunction({focusFn},event);" ' +
'onclick="' + specialClickHandler + 'CKEDITOR.tools.callFunction({clickFn},this);return false;">' +
'<span class="cke_button_icon cke_button__{iconName}_icon" style="{style}"';
template += '> </span>' +
'<span id="{id}_label" class="cke_button_label cke_button__{name}_label" aria-hidden="false">{label}</span>' +
'<span id="{id}_description" class="cke_button_label" aria-hidden="false">{ariaShortcutSpace}{ariaShortcut}</span>' +
'{arrowHtml}' +
'</a>';
var templateArrow = '<span class="cke_button_arrow">' +
// BLACK DOWN-POINTING TRIANGLE
( CKEDITOR.env.hc ? '▼' : '' ) +
'</span>';
var btnArrowTpl = CKEDITOR.addTemplate( 'buttonArrow', templateArrow ),
btnTpl = CKEDITOR.addTemplate( 'button', template );
CKEDITOR.plugins.add( 'button', {
beforeInit: function( editor ) {
editor.ui.addHandler( CKEDITOR.UI_BUTTON, CKEDITOR.ui.button.handler );
}
} );
/**
* Button UI element.
*
* @readonly
* @property {String} [='button']
* @member CKEDITOR
*/
CKEDITOR.UI_BUTTON = 'button';
/**
* Represents a button UI element. This class should not be called directly. To
* create new buttons use {@link CKEDITOR.ui#addButton} instead.
*
* @class
* @constructor Creates a button class instance.
* @param {Object} definition The button definition.
*/
CKEDITOR.ui.button = function( definition ) {
CKEDITOR.tools.extend( this, definition,
// Set defaults.
{
title: definition.label,
click: definition.click ||
function( editor ) {
editor.execCommand( definition.command );
}
} );
this._ = {};
};
/**
* Represents the button handler object.
*
* @class
* @singleton
* @extends CKEDITOR.ui.handlerDefinition
*/
CKEDITOR.ui.button.handler = {
/**
* Transforms a button definition into a {@link CKEDITOR.ui.button} instance.
*
* @member CKEDITOR.ui.button.handler
* @param {Object} definition
* @returns {CKEDITOR.ui.button}
*/
create: function( definition ) {
return new CKEDITOR.ui.button( definition );
}
};
/** @class CKEDITOR.ui.button */
CKEDITOR.ui.button.prototype = {
/**
* Renders the button.
*
* @param {CKEDITOR.editor} editor The editor instance which this button is
* to be used by.
* @param {Array} output The output array to which the HTML code related to
* this button should be appended.
*/
render: function( editor, output ) {
var modeStates = null;
function updateState() {
// "this" is a CKEDITOR.ui.button instance.
var mode = editor.mode;
if ( mode ) {
// Restore saved button state.
var state = this.modes[ mode ] ? modeStates[ mode ] !== undefined ? modeStates[ mode ] : CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;
state = editor.readOnly && !this.readOnly ? CKEDITOR.TRISTATE_DISABLED : state;
this.setState( state );
// Let plugin to disable button.
if ( this.refresh )
this.refresh();
}
}
var env = CKEDITOR.env,
id = this._.id = CKEDITOR.tools.getNextId(),
stateName = '',
command = this.command,
// Get the command name.
clickFn,
keystroke,
shortcut;
this._.editor = editor;
var instance = {
id: id,
button: this,
editor: editor,
focus: function() {
var element = CKEDITOR.document.getById( id );
element.focus();
},
execute: function() {
this.button.click( editor );
},
attach: function( editor ) {
this.button.attach( editor );
}
};
var keydownFn = CKEDITOR.tools.addFunction( function( ev ) {
if ( instance.onkey ) {
ev = new CKEDITOR.dom.event( ev );
return ( instance.onkey( instance, ev.getKeystroke() ) !== false );
}
} );
var focusFn = CKEDITOR.tools.addFunction( function( ev ) {
var retVal;
if ( instance.onfocus )
retVal = ( instance.onfocus( instance, new CKEDITOR.dom.event( ev ) ) !== false );
return retVal;
} );
var selLocked = 0;
instance.clickFn = clickFn = CKEDITOR.tools.addFunction( function() {
// Restore locked selection in Opera.
if ( selLocked ) {
editor.unlockSelection( 1 );
selLocked = 0;
}
instance.execute();
// Fixed iOS focus issue when your press disabled button (https://dev.ckeditor.com/ticket/12381).
if ( env.iOS ) {
editor.focus();
}
} );
// Indicate a mode sensitive button.
if ( this.modes ) {
modeStates = {};
editor.on( 'beforeModeUnload', function() {
if ( editor.mode && this._.state != CKEDITOR.TRISTATE_DISABLED )
modeStates[ editor.mode ] = this._.state;
}, this );
// Update status when activeFilter, mode or readOnly changes.
editor.on( 'activeFilterChange', updateState, this );
editor.on( 'mode', updateState, this );
// If this button is sensitive to readOnly state, update it accordingly.
!this.readOnly && editor.on( 'readOnly', updateState, this );
} else if ( command ) {
// Get the command instance.
command = editor.getCommand( command );
if ( command ) {
command.on( 'state', function() {
this.setState( command.state );
}, this );
stateName += ( command.state == CKEDITOR.TRISTATE_ON ? 'on' : command.state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' : 'off' );
}
}
var iconName;
// For button that has text-direction awareness on selection path.
if ( this.directional ) {
editor.on( 'contentDirChanged', function( evt ) {
var el = CKEDITOR.document.getById( this._.id ),
icon = el.getFirst();
var pathDir = evt.data;
// Make a minor direction change to become style-able for the skin icon.
if ( pathDir != editor.lang.dir )
el.addClass( 'cke_' + pathDir );
else
el.removeClass( 'cke_ltr' ).removeClass( 'cke_rtl' );
// Inline style update for the plugin icon.
icon.setAttribute( 'style', CKEDITOR.skin.getIconStyle( iconName, pathDir == 'rtl', this.icon, this.iconOffset ) );
}, this );
}
if ( !command ) {
stateName += 'off';
} else {
keystroke = editor.getCommandKeystroke( command );
if ( keystroke ) {
shortcut = CKEDITOR.tools.keystrokeToString( editor.lang.common.keyboard, keystroke );
}
}
var name = this.name || this.command,
iconPath = null,
overridePath = this.icon;
iconName = name;
// Check if we're pointing to an icon defined by another command. (https://dev.ckeditor.com/ticket/9555)
if ( this.icon && !( /\./ ).test( this.icon ) ) {
iconName = this.icon;
overridePath = null;
} else {
// Register and use custom icon for button (#1530).
if ( this.icon ) {
iconPath = this.icon;
}
if ( CKEDITOR.env.hidpi && this.iconHiDpi ) {
iconPath = this.iconHiDpi;
}
}
if ( iconPath ) {
CKEDITOR.skin.addIcon( iconPath, iconPath );
overridePath = null;
} else {
iconPath = iconName;
}
var params = {
id: id,
name: name,
iconName: iconName,
label: this.label,
// .cke_button_expandable enables additional styling for popup buttons (#2483).
cls: ( this.hasArrow ? 'cke_button_expandable ' : '' ) + ( this.className || '' ),
state: stateName,
ariaDisabled: stateName == 'disabled' ? 'true' : 'false',
title: this.title + ( shortcut ? ' (' + shortcut.display + ')' : '' ),
ariaShortcutSpace: shortcut ? ' ' : '',
ariaShortcut: shortcut ? editor.lang.common.keyboardShortcut + ' ' + shortcut.aria : '',
titleJs: env.gecko && !env.hc ? '' : ( this.title || '' ).replace( "'", '' ),
hasArrow: typeof this.hasArrow === 'string' && this.hasArrow || ( this.hasArrow ? 'true' : 'false' ),
keydownFn: keydownFn,
focusFn: focusFn,
clickFn: clickFn,
style: CKEDITOR.skin.getIconStyle( iconPath, ( editor.lang.dir == 'rtl' ), overridePath, this.iconOffset ),
arrowHtml: this.hasArrow ? btnArrowTpl.output() : ''
};
btnTpl.output( params, output );
if ( this.onRender )
this.onRender();
return instance;
},
/**
* Sets the button state.
*
* @param {Number} state Indicates the button state. One of {@link CKEDITOR#TRISTATE_ON},
* {@link CKEDITOR#TRISTATE_OFF}, or {@link CKEDITOR#TRISTATE_DISABLED}.
*/
setState: function( state ) {
if ( this._.state == state )
return false;
this._.state = state;
var element = CKEDITOR.document.getById( this._.id );
if ( element ) {
element.setState( state, 'cke_button' );
element.setAttribute( 'aria-disabled', state == CKEDITOR.TRISTATE_DISABLED );
if ( !this.hasArrow ) {
// Note: aria-pressed attribute should not be added to menuButton instances. (https://dev.ckeditor.com/ticket/11331)
if ( state === CKEDITOR.TRISTATE_ON ) {
element.setAttribute( 'aria-pressed', true );
} else {
element.removeAttribute( 'aria-pressed' );
}
} else {
// Indicates that menu button is opened (#421).
element.setAttribute( 'aria-expanded', state == CKEDITOR.TRISTATE_ON );
}
return true;
} else {
return false;
}
},
/**
* Gets the button state.
*
* @returns {Number} The button state. One of {@link CKEDITOR#TRISTATE_ON},
* {@link CKEDITOR#TRISTATE_OFF}, or {@link CKEDITOR#TRISTATE_DISABLED}.
*/
getState: function() {
return this._.state;
},
/**
* Returns this button's {@link CKEDITOR.feature} instance.
*
* It may be this button instance if it has at least one of
* `allowedContent` and `requiredContent` properties. Otherwise,
* if a command is bound to this button by the `command` property, then
* that command will be returned.
*
* This method implements the {@link CKEDITOR.feature#toFeature} interface method.
*
* @since 4.1.0
* @param {CKEDITOR.editor} Editor instance.
* @returns {CKEDITOR.feature} The feature.
*/
toFeature: function( editor ) {
if ( this._.feature )
return this._.feature;
var feature = this;
// If button isn't a feature, return command if is bound.
if ( !this.allowedContent && !this.requiredContent && this.command )
feature = editor.getCommand( this.command ) || feature;
return this._.feature = feature;
}
};
/**
* Adds a button definition to the UI elements list.
*
* editorInstance.ui.addButton( 'MyBold', {
* label: 'My Bold',
* command: 'bold',
* toolbar: 'basicstyles,1'
* } );
*
* @member CKEDITOR.ui
* @param {String} name The button name.
* @param {Object} definition The button definition.
* @param {String} definition.label The textual part of the button (if visible) and its tooltip.
* @param {String} definition.command The command to be executed once the button is activated.
* @param {String} definition.toolbar The {@link CKEDITOR.config#toolbarGroups toolbar group} into which
* the button will be added. An optional index value (separated by a comma) determines the button position within the group.
* @param {String} definition.icon The path to a custom icon or icon name registered by another plugin. Custom icon paths
* are supported since the **4.9.0** version.
*
* To use icon registered by another plugin, icon parameter should be used like:
*
* editor.ui.addButton( 'my_button', {
* icon: 'Link' // Uses link icon from Link plugin.
* } );
*
* If the plugin provides a HiDPI version of an icon, it will be used for HiDPI displays (so defining `iconHiDpi` is not needed
* in this case).
*
* To use a custom icon, the path to the icon should be provided:
*
* editor.ui.addButton( 'my_button', {
* icon: 'assets/icons/my_button.png'
* } )
*
* This icon will be used for both standard and HiDPI displays unless `iconHiDpi` is explicitly defined.
* **Important**: CKEditor will resolve relative paths based on {@link CKEDITOR#basePath}.
* @param {String} definition.iconHiDpi The path to the custom HiDPI icon version. Supported since **4.9.0** version.
* It will be used only in HiDPI environments. The usage is similar to the `icon` parameter:
*
* editor.ui.addButton( 'my_button', {
* iconHiDpi: 'assets/icons/my_button.hidpi.png'
* } )
* @param {String/Boolean} definition.hasArrow If Boolean, it indicates whether the button should have a dropdown. If a string, it acts
* as a value of the button's `aria-haspopup` attribute. Since **4.11.0** it supports the string as a value.
*/
CKEDITOR.ui.prototype.addButton = function( name, definition ) {
this.add( name, CKEDITOR.UI_BUTTON, definition );
};
} )();
|