[3019] | 1 | /*
|
---|
| 2 | Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
|
---|
| 3 | For licensing, see LICENSE.html or http://ckeditor.com/license
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | CKEDITOR.plugins.add( 'button',
|
---|
| 7 | {
|
---|
| 8 | beforeInit : function( editor )
|
---|
| 9 | {
|
---|
| 10 | editor.ui.addHandler( CKEDITOR.UI_BUTTON, CKEDITOR.ui.button.handler );
|
---|
| 11 | }
|
---|
| 12 | });
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
| 15 | * Button UI element.
|
---|
| 16 | * @constant
|
---|
| 17 | * @example
|
---|
| 18 | */
|
---|
| 19 | CKEDITOR.UI_BUTTON = 1;
|
---|
| 20 |
|
---|
| 21 | /**
|
---|
| 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.
|
---|
| 24 | * @constructor
|
---|
| 25 | * @param {Object} definition The button definition.
|
---|
| 26 | * @example
|
---|
| 27 | */
|
---|
| 28 | CKEDITOR.ui.button = function( definition )
|
---|
| 29 | {
|
---|
| 30 | // Copy all definition properties to this object.
|
---|
| 31 | CKEDITOR.tools.extend( this, definition,
|
---|
| 32 | // Set defaults.
|
---|
| 33 | {
|
---|
| 34 | title : definition.label,
|
---|
| 35 | className : definition.className || ( definition.command && 'cke_button_' + definition.command ) || '',
|
---|
| 36 | click : definition.click || function( editor )
|
---|
| 37 | {
|
---|
| 38 | editor.execCommand( definition.command );
|
---|
| 39 | }
|
---|
| 40 | });
|
---|
| 41 |
|
---|
| 42 | this._ = {};
|
---|
| 43 | };
|
---|
| 44 |
|
---|
| 45 | /**
|
---|
| 46 | * Transforms a button definition in a {@link CKEDITOR.ui.button} instance.
|
---|
| 47 | * @type Object
|
---|
| 48 | * @example
|
---|
| 49 | */
|
---|
| 50 | CKEDITOR.ui.button.handler =
|
---|
| 51 | {
|
---|
| 52 | create : function( definition )
|
---|
| 53 | {
|
---|
| 54 | return new CKEDITOR.ui.button( definition );
|
---|
| 55 | }
|
---|
| 56 | };
|
---|
| 57 |
|
---|
| 58 | CKEDITOR.ui.button.prototype =
|
---|
| 59 | {
|
---|
| 60 | canGroup : true,
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | * Renders the button.
|
---|
| 64 | * @param {CKEDITOR.editor} editor The editor instance which this button is
|
---|
| 65 | * to be used by.
|
---|
| 66 | * @param {Array} output The output array to which append the HTML relative
|
---|
| 67 | * to this button.
|
---|
| 68 | * @example
|
---|
| 69 | */
|
---|
| 70 | render : function( editor, output )
|
---|
| 71 | {
|
---|
| 72 | var env = CKEDITOR.env;
|
---|
| 73 |
|
---|
| 74 | var id = this._.id = 'cke_' + CKEDITOR.tools.getNextNumber();
|
---|
| 75 | this._.editor = editor;
|
---|
| 76 |
|
---|
| 77 | var instance =
|
---|
| 78 | {
|
---|
| 79 | id : id,
|
---|
| 80 | button : this,
|
---|
| 81 | editor : editor,
|
---|
| 82 | focus : function()
|
---|
| 83 | {
|
---|
| 84 | var element = CKEDITOR.document.getById( id );
|
---|
| 85 | element.focus();
|
---|
| 86 | },
|
---|
| 87 | execute : function()
|
---|
| 88 | {
|
---|
| 89 | this.button.click( editor );
|
---|
| 90 | }
|
---|
| 91 | };
|
---|
| 92 |
|
---|
| 93 | var clickFn = CKEDITOR.tools.addFunction( instance.execute, instance );
|
---|
| 94 |
|
---|
| 95 | var index = CKEDITOR.ui.button._.instances.push( instance ) - 1;
|
---|
| 96 |
|
---|
| 97 | var classes = '';
|
---|
| 98 |
|
---|
| 99 | // Get the command name.
|
---|
| 100 | var command = this.command;
|
---|
| 101 |
|
---|
| 102 | if ( this.modes )
|
---|
| 103 | {
|
---|
| 104 | editor.on( 'mode', function()
|
---|
| 105 | {
|
---|
| 106 | this.setState( this.modes[ editor.mode ] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED );
|
---|
| 107 | }, this);
|
---|
| 108 | }
|
---|
| 109 | else if ( command )
|
---|
| 110 | {
|
---|
| 111 | // Get the command instance.
|
---|
| 112 | command = editor.getCommand( command );
|
---|
| 113 |
|
---|
| 114 | if ( command )
|
---|
| 115 | {
|
---|
| 116 | command.on( 'state', function()
|
---|
| 117 | {
|
---|
| 118 | this.setState( command.state );
|
---|
| 119 | }, this);
|
---|
| 120 |
|
---|
| 121 | classes += 'cke_' + (
|
---|
| 122 | command.state == CKEDITOR.TRISTATE_ON ? 'on' :
|
---|
| 123 | command.state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' :
|
---|
| 124 | 'off' );
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | if ( !command )
|
---|
| 129 | classes += 'cke_off';
|
---|
| 130 |
|
---|
| 131 | if ( this.className )
|
---|
| 132 | classes += ' ' + this.className;
|
---|
| 133 |
|
---|
| 134 | output.push(
|
---|
| 135 | '<span class="cke_button">',
|
---|
| 136 | '<a id="', id, '"' +
|
---|
| 137 | ' class="', classes, '" href="javascript:void(\'', ( this.title || '' ).replace( "'", '' ), '\')"' +
|
---|
| 138 | ' title="', this.title, '"' +
|
---|
| 139 | ' tabindex="-1"' +
|
---|
| 140 | ' hidefocus="true"' );
|
---|
| 141 |
|
---|
| 142 | // Some browsers don't cancel key events in the keydown but in the
|
---|
| 143 | // keypress.
|
---|
| 144 | // TODO: Check if really needed for Gecko+Mac.
|
---|
| 145 | if ( env.opera || ( env.gecko && env.mac ) )
|
---|
| 146 | {
|
---|
| 147 | output.push(
|
---|
| 148 | ' onkeypress="return false;"' );
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | // With Firefox, we need to force the button to redraw, otherwise it
|
---|
| 152 | // will remain in the focus state.
|
---|
| 153 | if ( env.gecko )
|
---|
| 154 | {
|
---|
| 155 | output.push(
|
---|
| 156 | ' onblur="this.style.cssText = this.style.cssText;"' );
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | output.push(
|
---|
| 160 | ' onkeydown="return CKEDITOR.ui.button._.keydown(', index, ', event);"' +
|
---|
| 161 | ' onfocus="return CKEDITOR.ui.button._.focus(', index, ', event);"' +
|
---|
| 162 | ' onclick="CKEDITOR.tools.callFunction(', clickFn, ', this); return false;">' +
|
---|
| 163 | '<span class="cke_icon"' );
|
---|
| 164 |
|
---|
| 165 | if ( this.icon )
|
---|
| 166 | {
|
---|
| 167 | var offset = ( this.iconOffset || 0 ) * -16;
|
---|
| 168 | output.push( ' style="background-image:url(', CKEDITOR.getUrl( this.icon ), ');background-position:0 ' + offset + 'px;"' );
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | output.push(
|
---|
| 172 | '></span>' +
|
---|
| 173 | '<span class="cke_label">', this.label, '</span>' );
|
---|
| 174 |
|
---|
| 175 | if ( this.hasArrow )
|
---|
| 176 | {
|
---|
| 177 | output.push(
|
---|
| 178 | '<span class="cke_buttonarrow"></span>' );
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | output.push(
|
---|
| 182 | '</a>',
|
---|
| 183 | '</span>' );
|
---|
| 184 |
|
---|
| 185 | if ( this.onRender )
|
---|
| 186 | this.onRender();
|
---|
| 187 |
|
---|
| 188 | return instance;
|
---|
| 189 | },
|
---|
| 190 |
|
---|
| 191 | setState : function( state )
|
---|
| 192 | {
|
---|
| 193 | if ( this._.state == state )
|
---|
| 194 | return;
|
---|
| 195 |
|
---|
| 196 | var element = CKEDITOR.document.getById( this._.id );
|
---|
| 197 |
|
---|
| 198 | if ( element )
|
---|
| 199 | {
|
---|
| 200 | element.setState( state );
|
---|
| 201 |
|
---|
| 202 | var htmlTitle = this.title,
|
---|
| 203 | unavailable = this._.editor.lang.common.unavailable,
|
---|
| 204 | labelElement = element.getChild( 1 );
|
---|
| 205 |
|
---|
| 206 | if ( state == CKEDITOR.TRISTATE_DISABLED )
|
---|
| 207 | htmlTitle = unavailable.replace( '%1', this.title );
|
---|
| 208 |
|
---|
| 209 | labelElement.setHtml( htmlTitle );
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | this._.state = state;
|
---|
| 213 | }
|
---|
| 214 | };
|
---|
| 215 |
|
---|
| 216 | /**
|
---|
| 217 | * Handles a button click.
|
---|
| 218 | * @private
|
---|
| 219 | */
|
---|
| 220 | CKEDITOR.ui.button._ =
|
---|
| 221 | {
|
---|
| 222 | instances : [],
|
---|
| 223 |
|
---|
| 224 | keydown : function( index, ev )
|
---|
| 225 | {
|
---|
| 226 | var instance = CKEDITOR.ui.button._.instances[ index ];
|
---|
| 227 |
|
---|
| 228 | if ( instance.onkey )
|
---|
| 229 | {
|
---|
| 230 | ev = new CKEDITOR.dom.event( ev );
|
---|
| 231 | return ( instance.onkey( instance, ev.getKeystroke() ) !== false );
|
---|
| 232 | }
|
---|
| 233 | },
|
---|
| 234 |
|
---|
| 235 | focus : function( index, ev )
|
---|
| 236 | {
|
---|
| 237 | var instance = CKEDITOR.ui.button._.instances[ index ],
|
---|
| 238 | retVal;
|
---|
| 239 |
|
---|
| 240 | if ( instance.onfocus )
|
---|
| 241 | retVal = ( instance.onfocus( instance, new CKEDITOR.dom.event( ev ) ) !== false );
|
---|
| 242 |
|
---|
| 243 | // FF2: prevent focus event been bubbled up to editor container, which caused unexpected editor focus.
|
---|
| 244 | if ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )
|
---|
| 245 | ev.preventBubble();
|
---|
| 246 | return retVal;
|
---|
| 247 | }
|
---|
| 248 | };
|
---|
| 249 |
|
---|
| 250 | /**
|
---|
| 251 | * Adds a button definition to the UI elements list.
|
---|
| 252 | * @param {String} The button name.
|
---|
| 253 | * @param {Object} The button definition.
|
---|
| 254 | * @example
|
---|
| 255 | * editorInstance.ui.addButton( 'MyBold',
|
---|
| 256 | * {
|
---|
| 257 | * label : 'My Bold',
|
---|
| 258 | * command : 'bold'
|
---|
| 259 | * });
|
---|
| 260 | */
|
---|
| 261 | CKEDITOR.ui.prototype.addButton = function( name, definition )
|
---|
| 262 | {
|
---|
| 263 | this.add( name, CKEDITOR.UI_BUTTON, definition );
|
---|
| 264 | };
|
---|