1 | /*
|
---|
2 | Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
|
---|
3 | For licensing, see LICENSE.html or http://ckeditor.com/license
|
---|
4 | */
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * @fileOverview Defines the {@link CKEDITOR.editor} class, which is the base
|
---|
8 | * for other classes representing DOM objects.
|
---|
9 | */
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Represents a DOM object. This class is not intended to be used directly. It
|
---|
13 | * serves as the base class for other classes representing specific DOM
|
---|
14 | * objects.
|
---|
15 | * @constructor
|
---|
16 | * @param {Object} nativeDomObject A native DOM object.
|
---|
17 | * @augments CKEDITOR.event
|
---|
18 | * @example
|
---|
19 | */
|
---|
20 | CKEDITOR.dom.domObject = function( nativeDomObject )
|
---|
21 | {
|
---|
22 | if ( nativeDomObject )
|
---|
23 | {
|
---|
24 | /**
|
---|
25 | * The native DOM object represented by this class instance.
|
---|
26 | * @type Object
|
---|
27 | * @example
|
---|
28 | * var element = new CKEDITOR.dom.element( 'span' );
|
---|
29 | * alert( element.$.nodeType ); // "1"
|
---|
30 | */
|
---|
31 | this.$ = nativeDomObject;
|
---|
32 | }
|
---|
33 | };
|
---|
34 |
|
---|
35 | CKEDITOR.dom.domObject.prototype = (function()
|
---|
36 | {
|
---|
37 | // Do not define other local variables here. We want to keep the native
|
---|
38 | // listener closures as clean as possible.
|
---|
39 |
|
---|
40 | var getNativeListener = function( domObject, eventName )
|
---|
41 | {
|
---|
42 | return function( domEvent )
|
---|
43 | {
|
---|
44 | // In FF, when reloading the page with the editor focused, it may
|
---|
45 | // throw an error because the CKEDITOR global is not anymore
|
---|
46 | // available. So, we check it here first. (#2923)
|
---|
47 | if ( typeof CKEDITOR != 'undefined' )
|
---|
48 | domObject.fire( eventName, new CKEDITOR.dom.event( domEvent ) );
|
---|
49 | };
|
---|
50 | };
|
---|
51 |
|
---|
52 | return /** @lends CKEDITOR.dom.domObject.prototype */ {
|
---|
53 |
|
---|
54 | getPrivate : function()
|
---|
55 | {
|
---|
56 | var priv;
|
---|
57 |
|
---|
58 | // Get the main private function from the custom data. Create it if not
|
---|
59 | // defined.
|
---|
60 | if ( !( priv = this.getCustomData( '_' ) ) )
|
---|
61 | this.setCustomData( '_', ( priv = {} ) );
|
---|
62 |
|
---|
63 | return priv;
|
---|
64 | },
|
---|
65 |
|
---|
66 | /** @ignore */
|
---|
67 | on : function( eventName )
|
---|
68 | {
|
---|
69 | // We customize the "on" function here. The basic idea is that we'll have
|
---|
70 | // only one listener for a native event, which will then call all listeners
|
---|
71 | // set to the event.
|
---|
72 |
|
---|
73 | // Get the listeners holder object.
|
---|
74 | var nativeListeners = this.getCustomData( '_cke_nativeListeners' );
|
---|
75 |
|
---|
76 | if ( !nativeListeners )
|
---|
77 | {
|
---|
78 | nativeListeners = {};
|
---|
79 | this.setCustomData( '_cke_nativeListeners', nativeListeners );
|
---|
80 | }
|
---|
81 |
|
---|
82 | // Check if we have a listener for that event.
|
---|
83 | if ( !nativeListeners[ eventName ] )
|
---|
84 | {
|
---|
85 | var listener = nativeListeners[ eventName ] = getNativeListener( this, eventName );
|
---|
86 |
|
---|
87 | if ( this.$.addEventListener )
|
---|
88 | this.$.addEventListener( eventName, listener, !!CKEDITOR.event.useCapture );
|
---|
89 | else if ( this.$.attachEvent )
|
---|
90 | this.$.attachEvent( 'on' + eventName, listener );
|
---|
91 | }
|
---|
92 |
|
---|
93 | // Call the original implementation.
|
---|
94 | return CKEDITOR.event.prototype.on.apply( this, arguments );
|
---|
95 | },
|
---|
96 |
|
---|
97 | /** @ignore */
|
---|
98 | removeListener : function( eventName )
|
---|
99 | {
|
---|
100 | // Call the original implementation.
|
---|
101 | CKEDITOR.event.prototype.removeListener.apply( this, arguments );
|
---|
102 |
|
---|
103 | // If we don't have listeners for this event, clean the DOM up.
|
---|
104 | if ( !this.hasListeners( eventName ) )
|
---|
105 | {
|
---|
106 | var nativeListeners = this.getCustomData( '_cke_nativeListeners' );
|
---|
107 | var listener = nativeListeners && nativeListeners[ eventName ];
|
---|
108 | if ( listener )
|
---|
109 | {
|
---|
110 | if ( this.$.removeEventListener )
|
---|
111 | this.$.removeEventListener( eventName, listener, false );
|
---|
112 | else if ( this.$.detachEvent )
|
---|
113 | this.$.detachEvent( 'on' + eventName, listener );
|
---|
114 |
|
---|
115 | delete nativeListeners[ eventName ];
|
---|
116 | }
|
---|
117 | }
|
---|
118 | },
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Removes any listener set on this object.
|
---|
122 | * To avoid memory leaks we must assure that there are no
|
---|
123 | * references left after the object is no longer needed.
|
---|
124 | */
|
---|
125 | removeAllListeners : function()
|
---|
126 | {
|
---|
127 | var nativeListeners = this.getCustomData( '_cke_nativeListeners' );
|
---|
128 | for ( var eventName in nativeListeners )
|
---|
129 | if ( ! ( eventName in Object.prototype ) )
|
---|
130 | {
|
---|
131 | var listener = nativeListeners[ eventName ];
|
---|
132 | if ( this.$.removeEventListener )
|
---|
133 | this.$.removeEventListener( eventName, listener, false );
|
---|
134 | else if ( this.$.detachEvent )
|
---|
135 | this.$.detachEvent( 'on' + eventName, listener );
|
---|
136 |
|
---|
137 | delete nativeListeners[ eventName ];
|
---|
138 | }
|
---|
139 | }
|
---|
140 | };
|
---|
141 | })();
|
---|
142 |
|
---|
143 | (function( domObjectProto )
|
---|
144 | {
|
---|
145 | var customData = {};
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Determines whether the specified object is equal to the current object.
|
---|
149 | * @name CKEDITOR.dom.domObject.prototype.equals
|
---|
150 | * @function
|
---|
151 | * @param {Object} object The object to compare with the current object.
|
---|
152 | * @returns {Boolean} "true" if the object is equal.
|
---|
153 | * @example
|
---|
154 | * var doc = new CKEDITOR.dom.document( document );
|
---|
155 | * alert( doc.equals( CKEDITOR.document ) ); // "true"
|
---|
156 | * alert( doc == CKEDITOR.document ); // "false"
|
---|
157 | */
|
---|
158 | domObjectProto.equals = function( object )
|
---|
159 | {
|
---|
160 | return ( object && object.$ === this.$ );
|
---|
161 | };
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Sets a data slot value for this object. These values are shared by all
|
---|
165 | * instances pointing to that same DOM object.
|
---|
166 | * @name CKEDITOR.dom.domObject.prototype.setCustomData
|
---|
167 | * @function
|
---|
168 | * @param {String} key A key used to identify the data slot.
|
---|
169 | * @param {Object} value The value to set to the data slot.
|
---|
170 | * @returns {CKEDITOR.dom.domObject} This DOM object instance.
|
---|
171 | * @see CKEDITOR.dom.domObject.prototype.getCustomData
|
---|
172 | * @example
|
---|
173 | * var element = new CKEDITOR.dom.element( 'span' );
|
---|
174 | * element.setCustomData( 'hasCustomData', true );
|
---|
175 | */
|
---|
176 | domObjectProto.setCustomData = function( key, value )
|
---|
177 | {
|
---|
178 | var expandoNumber = this.getUniqueId(),
|
---|
179 | dataSlot = customData[ expandoNumber ] || ( customData[ expandoNumber ] = {} );
|
---|
180 |
|
---|
181 | dataSlot[ key ] = value;
|
---|
182 |
|
---|
183 | return this;
|
---|
184 | };
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Gets the value set to a data slot in this object.
|
---|
188 | * @name CKEDITOR.dom.domObject.prototype.getCustomData
|
---|
189 | * @function
|
---|
190 | * @param {String} key The key used to identify the data slot.
|
---|
191 | * @returns {Object} This value set to the data slot.
|
---|
192 | * @see CKEDITOR.dom.domObject.prototype.setCustomData
|
---|
193 | * @example
|
---|
194 | * var element = new CKEDITOR.dom.element( 'span' );
|
---|
195 | * alert( element.getCustomData( 'hasCustomData' ) ); // e.g. 'true'
|
---|
196 | */
|
---|
197 | domObjectProto.getCustomData = function( key )
|
---|
198 | {
|
---|
199 | var expandoNumber = this.$._cke_expando,
|
---|
200 | dataSlot = expandoNumber && customData[ expandoNumber ];
|
---|
201 |
|
---|
202 | return dataSlot && dataSlot[ key ];
|
---|
203 | };
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * @name CKEDITOR.dom.domObject.prototype.removeCustomData
|
---|
207 | */
|
---|
208 | domObjectProto.removeCustomData = function( key )
|
---|
209 | {
|
---|
210 | var expandoNumber = this.$._cke_expando,
|
---|
211 | dataSlot = expandoNumber && customData[ expandoNumber ],
|
---|
212 | retval = dataSlot && dataSlot[ key ];
|
---|
213 |
|
---|
214 | if ( typeof retval != 'undefined' )
|
---|
215 | delete dataSlot[ key ];
|
---|
216 |
|
---|
217 | return retval || null;
|
---|
218 | };
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Removes any data stored on this object.
|
---|
222 | * To avoid memory leaks we must assure that there are no
|
---|
223 | * references left after the object is no longer needed.
|
---|
224 | * @name CKEDITOR.dom.domObject.prototype.clearCustomData
|
---|
225 | * @function
|
---|
226 | */
|
---|
227 | domObjectProto.clearCustomData = function()
|
---|
228 | {
|
---|
229 | // Clear all event listeners
|
---|
230 | this.removeAllListeners();
|
---|
231 |
|
---|
232 | var expandoNumber = this.$._cke_expando;
|
---|
233 | expandoNumber && delete customData[ expandoNumber ];
|
---|
234 | };
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * @name CKEDITOR.dom.domObject.prototype.getCustomData
|
---|
238 | */
|
---|
239 | domObjectProto.getUniqueId = function()
|
---|
240 | {
|
---|
241 | return this.$._cke_expando || ( this.$._cke_expando = CKEDITOR.tools.getNextNumber() );
|
---|
242 | };
|
---|
243 |
|
---|
244 | // Implement CKEDITOR.event.
|
---|
245 | CKEDITOR.event.implementOn( domObjectProto );
|
---|
246 |
|
---|
247 | })( CKEDITOR.dom.domObject.prototype );
|
---|