LBF custom template (nation notes) fancybox replace.
[openemr.git] / library / custom_template / ckeditor / _source / plugins / button / plugin.js
blob632ff69ce8d3107bf5c4241e21d7ed0f6a8f780a
1 /*
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
4 */
6 CKEDITOR.plugins.add( 'button',
8         beforeInit : function( editor )
9         {
10                 editor.ui.addHandler( CKEDITOR.UI_BUTTON, CKEDITOR.ui.button.handler );
11         }
12 });
14 /**
15  * Button UI element.
16  * @constant
17  * @example
18  */
19 CKEDITOR.UI_BUTTON = 1;
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 )
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                 });
42         this._ = {};
45 /**
46  * Transforms a button definition in a {@link CKEDITOR.ui.button} instance.
47  * @type Object
48  * @example
49  */
50 CKEDITOR.ui.button.handler =
52         create : function( definition )
53         {
54                 return new CKEDITOR.ui.button( definition );
55         }
58 /**
59  * Handles a button click.
60  * @private
61  */
62 CKEDITOR.ui.button._ =
64         instances : [],
66         keydown : function( index, ev )
67         {
68                 var instance = CKEDITOR.ui.button._.instances[ index ];
70                 if ( instance.onkey )
71                 {
72                         ev = new CKEDITOR.dom.event( ev );
73                         return ( instance.onkey( instance, ev.getKeystroke() ) !== false );
74                 }
75         },
77         focus : function( index, ev )
78         {
79                 var instance = CKEDITOR.ui.button._.instances[ index ],
80                         retVal;
82                 if ( instance.onfocus )
83                         retVal = ( instance.onfocus( instance, new CKEDITOR.dom.event( ev ) ) !== false );
85                 // FF2: prevent focus event been bubbled up to editor container, which caused unexpected editor focus.
86                 if ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )
87                         ev.preventBubble();
88                 return retVal;
89         }
92 ( function()
94         var keydownFn = CKEDITOR.tools.addFunction( CKEDITOR.ui.button._.keydown, CKEDITOR.ui.button._ ),
95                 focusFn = CKEDITOR.tools.addFunction( CKEDITOR.ui.button._.focus, CKEDITOR.ui.button._ );
97 CKEDITOR.ui.button.prototype =
99         canGroup : true,
101         /**
102          * Renders the button.
103          * @param {CKEDITOR.editor} editor The editor instance which this button is
104          *              to be used by.
105          * @param {Array} output The output array to which append the HTML relative
106          *              to this button.
107          * @example
108          */
109         render : function( editor, output )
110         {
111                 var env = CKEDITOR.env,
112                         id = this._.id = CKEDITOR.tools.getNextId(),
113                         classes = '',
114                         command = this.command, // Get the command name.
115                         clickFn,
116                         index;
118                 this._.editor = editor;
120                 var instance =
121                 {
122                         id : id,
123                         button : this,
124                         editor : editor,
125                         focus : function()
126                         {
127                                 var element = CKEDITOR.document.getById( id );
128                                 element.focus();
129                         },
130                         execute : function()
131                         {
132                                 this.button.click( editor );
133                         }
134                 };
136                 instance.clickFn = clickFn = CKEDITOR.tools.addFunction( instance.execute, instance );
138                 instance.index = index = CKEDITOR.ui.button._.instances.push( instance ) - 1;
140                 // Indicate a mode sensitive button.
141                 if ( this.modes )
142                 {
143                         var modeStates = {};
144                         editor.on( 'beforeModeUnload', function()
145                                 {
146                                         modeStates[ editor.mode ] = this._.state;
147                                 }, this );
149                         editor.on( 'mode', function()
150                                 {
151                                         var mode = editor.mode;
152                                         // Restore saved button state.
153                                         this.setState( this.modes[ mode ] ?
154                                                 modeStates[ mode ] != undefined ? modeStates[ mode ] :
155                                                         CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED );
156                                 }, this);
157                 }
158                 else if ( command )
159                 {
160                         // Get the command instance.
161                         command = editor.getCommand( command );
163                         if ( command )
164                         {
165                                 command.on( 'state', function()
166                                         {
167                                                 this.setState( command.state );
168                                         }, this);
170                                 classes += 'cke_' + (
171                                         command.state == CKEDITOR.TRISTATE_ON ? 'on' :
172                                         command.state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' :
173                                         'off' );
174                         }
175                 }
177                 if ( !command )
178                         classes += 'cke_off';
180                 if ( this.className )
181                         classes += ' ' + this.className;
183                 output.push(
184                         '<span class="cke_button' + ( this.icon && this.icon.indexOf( '.png' ) == -1 ? ' cke_noalphafix' : '' ) + '">',
185                         '<a id="', id, '"' +
186                                 ' class="', classes, '"',
187                                 env.gecko && env.version >= 10900 && !env.hc  ? '' : '" href="javascript:void(\''+ ( this.title || '' ).replace( "'", '' )+ '\')"',
188                                 ' title="', this.title, '"' +
189                                 ' tabindex="-1"' +
190                                 ' hidefocus="true"' +
191                             ' role="button"' +
192                                 ' aria-labelledby="' + id + '_label"' +
193                                 ( this.hasArrow ?  ' aria-haspopup="true"' : '' ) );
195                 // Some browsers don't cancel key events in the keydown but in the
196                 // keypress.
197                 // TODO: Check if really needed for Gecko+Mac.
198                 if ( env.opera || ( env.gecko && env.mac ) )
199                 {
200                         output.push(
201                                 ' onkeypress="return false;"' );
202                 }
204                 // With Firefox, we need to force the button to redraw, otherwise it
205                 // will remain in the focus state.
206                 if ( env.gecko )
207                 {
208                         output.push(
209                                 ' onblur="this.style.cssText = this.style.cssText;"' );
210                 }
212                 output.push(
213                                         ' onkeydown="return CKEDITOR.tools.callFunction(', keydownFn, ', ', index, ', event);"' +
214                                         ' onfocus="return CKEDITOR.tools.callFunction(', focusFn,', ', index, ', event);"' +
215                                 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ', this); return false;">' +
216                                         '<span class="cke_icon"' );
218                 if ( this.icon )
219                 {
220                         var offset = ( this.iconOffset || 0 ) * -16;
221                         output.push( ' style="background-image:url(', CKEDITOR.getUrl( this.icon ), ');background-position:0 ' + offset + 'px;"' );
222                 }
224                 output.push(
225                                         '>&nbsp;</span>' +
226                                         '<span id="', id, '_label" class="cke_label">', this.label, '</span>' );
228                 if ( this.hasArrow )
229                 {
230                         output.push(
231                                         '<span class="cke_buttonarrow">'
232                                         // BLACK DOWN-POINTING TRIANGLE
233                                         + ( CKEDITOR.env.hc ? '&#9660;' : '&nbsp;' )
234                                         + '</span>' );
235                 }
237                 output.push(
238                         '</a>',
239                         '</span>' );
241                 if ( this.onRender )
242                         this.onRender();
244                 return instance;
245         },
247         setState : function( state )
248         {
249                 if ( this._.state == state )
250                         return false;
252                 this._.state = state;
254                 var element = CKEDITOR.document.getById( this._.id );
256                 if ( element )
257                 {
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' );
267                         return true;
268                 }
269                 else
270                         return false;
271         }
274 })();
277  * Adds a button definition to the UI elements list.
278  * @param {String} The button name.
279  * @param {Object} The button definition.
280  * @example
281  * editorInstance.ui.addButton( 'MyBold',
282  *     {
283  *         label : 'My Bold',
284  *         command : 'bold'
285  *     });
286  */
287 CKEDITOR.ui.prototype.addButton = function( name, definition )
289         this.add( name, CKEDITOR.UI_BUTTON, definition );
292 CKEDITOR.on( 'reset', function()
293         {
294                 CKEDITOR.ui.button._.instances = [];
295         });