source: sandbox/3.0/phpgwapi/js/ckeditor/_source/core/event.js @ 2862

Revision 2862, 10.7 KB checked in by rodsouza, 14 years ago (diff)

Ticket #663 - Atualizando e centralizando o CKEditor (v. 3.2.1)

Line 
1/*
2Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
3For licensing, see LICENSE.html or http://ckeditor.com/license
4*/
5
6/**
7 * @fileOverview Defines the {@link CKEDITOR.event} class, which serves as the
8 *              base for classes and objects that require event handling features.
9 */
10
11if ( !CKEDITOR.event )
12{
13        /**
14         * This is a base class for classes and objects that require event handling
15         * features.
16         * @constructor
17         * @example
18         */
19        CKEDITOR.event = function()
20        {};
21
22        /**
23         * Implements the {@link CKEDITOR.event} features in an object.
24         * @param {Object} targetObject The object in which implement the features.
25         * @example
26         * var myObject = { message : 'Example' };
27         * <b>CKEDITOR.event.implementOn( myObject }</b>;
28         * myObject.on( 'testEvent', function()
29         *     {
30         *         alert( this.message );  // "Example"
31         *     });
32         * myObject.fire( 'testEvent' );
33         */
34        CKEDITOR.event.implementOn = function( targetObject, isTargetPrototype )
35        {
36                var eventProto = CKEDITOR.event.prototype;
37
38                for ( var prop in eventProto )
39                        if ( ! ( prop in Object.prototype ) )
40                        {
41                                if ( targetObject[ prop ] == undefined )
42                                        targetObject[ prop ] = eventProto[ prop ];
43                        }
44        };
45
46        CKEDITOR.event.prototype = (function()
47        {
48                // Returns the private events object for a given object.
49                var getPrivate = function( obj )
50                {
51                        var _ = ( obj.getPrivate && obj.getPrivate() ) || obj._ || ( obj._ = {} );
52                        return _.events || ( _.events = {} );
53                };
54
55                var eventEntry = function( eventName )
56                {
57                        this.name = eventName;
58                        this.listeners = [];
59                };
60
61                eventEntry.prototype =
62                {
63                        // Get the listener index for a specified function.
64                        // Returns -1 if not found.
65                        getListenerIndex : function( listenerFunction )
66                        {
67                                for ( var i = 0, listeners = this.listeners ; i < listeners.length ; i++ )
68                                {
69                                        if ( listeners[i].fn == listenerFunction )
70                                                return i;
71                                }
72                                return -1;
73                        }
74                };
75
76                return /** @lends CKEDITOR.event.prototype */ {
77                        /**
78                         * Registers a listener to a specific event in the current object.
79                         * @param {String} eventName The event name to which listen.
80                         * @param {Function} listenerFunction The function listening to the
81                         *              event. A single {@link CKEDITOR.eventInfo} object instanced
82                         *              is passed to this function containing all the event data.
83                         * @param {Object} [scopeObj] The object used to scope the listener
84                         *              call (the this object. If omitted, the current object is used.
85                         * @param {Object} [listenerData] Data to be sent as the
86                         *              {@link CKEDITOR.eventInfo#listenerData} when calling the
87                         *              listener.
88                         * @param {Number} [priority] The listener priority. Lower priority
89                         *              listeners are called first. Listeners with the same priority
90                         *              value are called in registration order. Defaults to 10.
91                         * @example
92                         * someObject.on( 'someEvent', function()
93                         *     {
94                         *         alert( this == someObject );  // "true"
95                         *     });
96                         * @example
97                         * someObject.on( 'someEvent', function()
98                         *     {
99                         *         alert( this == anotherObject );  // "true"
100                         *     }
101                         *     , anotherObject );
102                         * @example
103                         * someObject.on( 'someEvent', function( event )
104                         *     {
105                         *         alert( event.listenerData );  // "Example"
106                         *     }
107                         *     , null, 'Example' );
108                         * @example
109                         * someObject.on( 'someEvent', function() { ... } );                   // 2nd called
110                         * someObject.on( 'someEvent', function() { ... }, null, null, 100 );  // 3rd called
111                         * someObject.on( 'someEvent', function() { ... }, null, null, 1 );    // 1st called
112                         */
113                        on : function( eventName, listenerFunction, scopeObj, listenerData, priority )
114                        {
115                                // Get the event entry (create it if needed).
116                                var events = getPrivate( this ),
117                                        event = events[ eventName ] || ( events[ eventName ] = new eventEntry( eventName ) );
118
119                                if ( event.getListenerIndex( listenerFunction ) < 0 )
120                                {
121                                        // Get the listeners.
122                                        var listeners = event.listeners;
123
124                                        // Fill the scope.
125                                        if ( !scopeObj )
126                                                scopeObj = this;
127
128                                        // Default the priority, if needed.
129                                        if ( isNaN( priority ) )
130                                                priority = 10;
131
132                                        var me = this;
133
134                                        // Create the function to be fired for this listener.
135                                        var listenerFirer = function( editor, publisherData, stopFn, cancelFn )
136                                        {
137                                                var ev =
138                                                {
139                                                        name : eventName,
140                                                        sender : this,
141                                                        editor : editor,
142                                                        data : publisherData,
143                                                        listenerData : listenerData,
144                                                        stop : stopFn,
145                                                        cancel : cancelFn,
146                                                        removeListener : function()
147                                                        {
148                                                                me.removeListener( eventName, listenerFunction );
149                                                        }
150                                                };
151
152                                                listenerFunction.call( scopeObj, ev );
153
154                                                return ev.data;
155                                        };
156                                        listenerFirer.fn = listenerFunction;
157                                        listenerFirer.priority = priority;
158
159                                        // Search for the right position for this new listener, based on its
160                                        // priority.
161                                        for ( var i = listeners.length - 1 ; i >= 0 ; i-- )
162                                        {
163                                                // Find the item which should be before the new one.
164                                                if ( listeners[ i ].priority <= priority )
165                                                {
166                                                        // Insert the listener in the array.
167                                                        listeners.splice( i + 1, 0, listenerFirer );
168                                                        return;
169                                                }
170                                        }
171
172                                        // If no position has been found (or zero length), put it in
173                                        // the front of list.
174                                        listeners.unshift( listenerFirer );
175                                }
176                        },
177
178                        /**
179                         * Fires an specific event in the object. All registered listeners are
180                         * called at this point.
181                         * @function
182                         * @param {String} eventName The event name to fire.
183                         * @param {Object} [data] Data to be sent as the
184                         *              {@link CKEDITOR.eventInfo#data} when calling the
185                         *              listeners.
186                         * @param {CKEDITOR.editor} [editor] The editor instance to send as the
187                         *              {@link CKEDITOR.eventInfo#editor} when calling the
188                         *              listener.
189                         * @returns {Boolean|Object} A booloan indicating that the event is to be
190                         *              canceled, or data returned by one of the listeners.
191                         * @example
192                         * someObject.on( 'someEvent', function() { ... } );
193                         * someObject.on( 'someEvent', function() { ... } );
194                         * <b>someObject.fire( 'someEvent' )</b>;  // both listeners are called
195                         * @example
196                         * someObject.on( 'someEvent', function( event )
197                         *     {
198                         *         alert( event.data );  // "Example"
199                         *     });
200                         * <b>someObject.fire( 'someEvent', 'Example' )</b>;
201                         */
202                        fire : (function()
203                        {
204                                // Create the function that marks the event as stopped.
205                                var stopped = false;
206                                var stopEvent = function()
207                                {
208                                        stopped = true;
209                                };
210
211                                // Create the function that marks the event as canceled.
212                                var canceled = false;
213                                var cancelEvent = function()
214                                {
215                                        canceled = true;
216                                };
217
218                                return function( eventName, data, editor )
219                                {
220                                        // Get the event entry.
221                                        var event = getPrivate( this )[ eventName ];
222
223                                        // Save the previous stopped and cancelled states. We may
224                                        // be nesting fire() calls.
225                                        var previousStopped = stopped,
226                                                previousCancelled = canceled;
227
228                                        // Reset the stopped and canceled flags.
229                                        stopped = canceled = false;
230
231                                        if ( event )
232                                        {
233                                                var listeners = event.listeners;
234
235                                                if ( listeners.length )
236                                                {
237                                                        // As some listeners may remove themselves from the
238                                                        // event, the original array length is dinamic. So,
239                                                        // let's make a copy of all listeners, so we are
240                                                        // sure we'll call all of them.
241                                                        listeners = listeners.slice( 0 );
242
243                                                        // Loop through all listeners.
244                                                        for ( var i = 0 ; i < listeners.length ; i++ )
245                                                        {
246                                                                // Call the listener, passing the event data.
247                                                                var retData = listeners[i].call( this, editor, data, stopEvent, cancelEvent );
248
249                                                                if ( typeof retData != 'undefined' )
250                                                                        data = retData;
251
252                                                                // No further calls is stopped or canceled.
253                                                                if ( stopped || canceled )
254                                                                        break;
255                                                        }
256                                                }
257                                        }
258
259                                        var ret = canceled || ( typeof data == 'undefined' ? false : data );
260
261                                        // Restore the previous stopped and canceled states.
262                                        stopped = previousStopped;
263                                        canceled = previousCancelled;
264
265                                        return ret;
266                                };
267                        })(),
268
269                        /**
270                         * Fires an specific event in the object, releasing all listeners
271                         * registered to that event. The same listeners are not called again on
272                         * successive calls of it or of {@link #fire}.
273                         * @param {String} eventName The event name to fire.
274                         * @param {Object} [data] Data to be sent as the
275                         *              {@link CKEDITOR.eventInfo#data} when calling the
276                         *              listeners.
277                         * @param {CKEDITOR.editor} [editor] The editor instance to send as the
278                         *              {@link CKEDITOR.eventInfo#editor} when calling the
279                         *              listener.
280                         * @returns {Boolean|Object} A booloan indicating that the event is to be
281                         *              canceled, or data returned by one of the listeners.
282                         * @example
283                         * someObject.on( 'someEvent', function() { ... } );
284                         * someObject.fire( 'someEvent' );  // above listener called
285                         * <b>someObject.fireOnce( 'someEvent' )</b>;  // above listener called
286                         * someObject.fire( 'someEvent' );  // no listeners called
287                         */
288                        fireOnce : function( eventName, data, editor )
289                        {
290                                var ret = this.fire( eventName, data, editor );
291                                delete getPrivate( this )[ eventName ];
292                                return ret;
293                        },
294
295                        /**
296                         * Unregisters a listener function from being called at the specified
297                         *              event. No errors are thrown if the listener has not been
298                         *              registered previously.
299                         * @param {String} eventName The event name.
300                         * @param {Function} listenerFunction The listener function to unregister.
301                         * @example
302                         * var myListener = function() { ... };
303                         * someObject.on( 'someEvent', myListener );
304                         * someObject.fire( 'someEvent' );  // myListener called
305                         * <b>someObject.removeListener( 'someEvent', myListener )</b>;
306                         * someObject.fire( 'someEvent' );  // myListener not called
307                         */
308                        removeListener : function( eventName, listenerFunction )
309                        {
310                                // Get the event entry.
311                                var event = getPrivate( this )[ eventName ];
312
313                                if ( event )
314                                {
315                                        var index = event.getListenerIndex( listenerFunction );
316                                        if ( index >= 0 )
317                                                event.listeners.splice( index, 1 );
318                                }
319                        },
320
321                        /**
322                         * Checks if there is any listener registered to a given event.
323                         * @param {String} eventName The event name.
324                         * @example
325                         * var myListener = function() { ... };
326                         * someObject.on( 'someEvent', myListener );
327                         * alert( someObject.<b>hasListeners( 'someEvent' )</b> );  // "true"
328                         * alert( someObject.<b>hasListeners( 'noEvent' )</b> );    // "false"
329                         */
330                        hasListeners : function( eventName )
331                        {
332                                var event = getPrivate( this )[ eventName ];
333                                return ( event && event.listeners.length > 0 ) ;
334                        }
335                };
336        })();
337}
Note: See TracBrowser for help on using the repository browser.