4 * 1. as3 の EventDispatcher ライクなクラス。そのまま使ったり、継承したり。コールバック中にイベントを追加したら?削除したら?にも対処している。
\r
5 * 2. _rawObject メンバがいる場合、addEventListener, attachEvent, on 等で生のブラウザオブジェクトにリスナを登録する。
\r
6 * window, document, HTMLElement, Image, XHR などが _rawObject
\r
10 * https://developer.mozilla.org/ja/docs/Web/API/EventTarget.addEventListener
\r
12 * EventListener がイベント処理中に EventTarget に追加された場合、それが現在のアクションによって実行されることはありませんが、浮上フェーズのように、後の段階のイベントフローで実行されるかもしれません。
\r
14 * https://developer.mozilla.org/ja/docs/Web/API/EventTarget.removeEventListener
\r
15 * イベントリスナーが イベントを処理中であるイベントターゲットから削除された場合、現在のアクションによってそのイベントリスナーが実行されることはありません。
\r
16 * イベントリスナーは、決して削除された後に実行されることはありません。
\r
17 * イベントターゲット上にある現在のどのイベントリスナーも指定していない引数付きの removeEventListener は、何の効果もありません。
\r
29 // KILL_INSTANCE_CANCELED
\r
33 // --- define / local variables ----------------------------
\r
34 var x_eventdispatcher_once = false,
\r
35 x_eventdispatcher_needsIndex = false,
\r
36 x_eventdispatcher_temp = {};
\r
39 x_eventdispatcher_temp.DOM_W3C = true;
\r
40 x_eventdispatcher_temp.EVENT_DOM0 = true;
\r
42 if( X.UA.IE4 ){ // ie4 & iemobi4
\r
43 x_eventdispatcher_temp.DOM_IE4 = true;
\r
44 x_eventdispatcher_temp.EVENT_DOM0 = true;
\r
46 if( document.getElementById ){
\r
47 x_eventdispatcher_temp.DOM_W3C = true;
\r
48 if( document.addEventListener ){
\r
49 x_eventdispatcher_temp.EVENT_W3C = true;
\r
51 if( document.attachEvent ){
\r
52 x_eventdispatcher_temp.EVENT_IE = true;
\r
54 x_eventdispatcher_temp.EVENT_DOM0 = true;
\r
58 x_eventdispatcher_temp.DOM_IE4 = true;
\r
59 x_eventdispatcher_temp.EVENT_DOM0 = true;
\r
61 if( document.layers ){
\r
68 * イベントターゲットをラップする場合(widnow, document, Image, XHR 等)、通常は new 時に渡します。参照:コンストラクタ実体 {@link X.EventDispatcher.Constructor}
\r
69 * アプリケーション独自のイベントをやり取りしたいだけ、という場合、イベントターゲットは不要です。
\r
71 * @classdesc EventTarget オブジェクトをラップしたり、アプリケーションで独自に定義したイベントを発信するためのクラスです。
\r
72 * listen, unlisten, dispatch という addEventListener, removeEventListener, dispatchEvent に対応する関数を持ちます。さらに listening という as3 の hasEventListener に相当する関数を持ちます。
\r
73 * as3 の EventDispatcher に相当する機能に加え、イベントターゲットオブジェクト(widnow, document, HTMLElement, XHR 等)が設定されていた場合に、それらへ実際のイベント登録・解除も行います。
\r
74 * このイベントの登録・解除はクロスブラウザ対策が施されていて、IE5~8の独自イベントの差異を吸収し、DOM0 に対しても複数のイベントリスナを登録します。
\r
75 * さらに、コールバックに対して、this コンテキストや、追加の引数を指定できます。 this コンテキストを指定しなかった場合、EventDispatcher インスタンスがコールバック関数の this になります。
\r
76 * @param {object=} opt_argument
\r
85 * @memberof X.EventDispatcher
\r
89 * イベントリスナをイベント名(string)や数値(1~,フレームワーク内で定義)をキーとするArrayで記憶します。
\r
90 * Arrayには、{k:種類,x:コンテキスト(thisObject),f:関数,s:サプリメントする引数の配列} というハッシュ、または関数が蓄えられています。
\r
92 * @type {Object.<(number|string), Array.<(callbackDef|function)>>}
\r
97 * _rawObject には HTMLElement, window, document, XHR といったイベントターゲットオブジェクトを設定します。
\r
98 * _rawObject が設定されていると on(), off() 時に addEventListener(DOM Level2) や detachEvent(ie5~8), on~(DOM0) 等を操作します。
\r
99 * _rawObject は最初の on() 前に設定しておかないと addEventListener 等が意図したように行われません。
\r
100 * X.Dom.Node では非同期に HTMLElement を生成していて、要素生成以前に on, off を呼び出すことができます。これは適宜に migrateEvent, restoreEvent を呼んで解決しているためです。
\r
105 _handleEvent : null,
\r
107 _dispatching : 0, // dispatch 中の unlisten で使用
\r
108 _unlistens : null, // dispatch 中の unlisten で使用
\r
109 _reserves : null, // dispatch中に unlisten されたイベントリスナ
\r
110 _killReserved : false,
\r
113 * X.EventDispatcher のコンストラクタの実体。
\r
115 * @this {X.EventDispatcher}
\r
117 Constructor : function( rawObject ){
\r
119 this._rawNode = rawObject;
\r
125 * @this {X.EventDispatcher}
\r
127 on : x_eventdispatcher_on,
\r
128 listen : x_eventdispatcher_on,
\r
130 listenOnce : function( type, arg1, arg2, arg3 ){
\r
131 x_eventdispatcher_once = true;
\r
132 this.listen( type, arg1, arg2, arg3 );
\r
133 x_eventdispatcher_once = false;
\r
137 off : x_eventdispatcher_off,
\r
138 unlisten : x_eventdispatcher_off,
\r
140 listening : function( type, arg1, arg2, arg3 ){
\r
141 var list = this._listeners, unlistens, i, f, hash;
\r
142 if( type === undefined ) return !!list;
\r
143 if( !list || !( list = list[ type ] ) ) return false;
\r
144 if( arg1 === undefined ) return true;
\r
149 hash = X.Callback._classifyCallbackArgs( arg1, arg2, arg3, this );
\r
152 if( ( unlistens = this._unlistens ) && ( unlistens = unlistens[ type ] ) ){
\r
153 for( i = unlistens.length; i; ){
\r
154 f = unlistens[ --i ];
\r
155 if( f === hash || ( f.x === hash.x && f.f === hash.f && f.s === hash.s ) ) return false;
\r
158 for( i = list.length; i; ){
\r
160 if( f === hash || ( f.x === hash.x && f.f === hash.f && f.s === hash.s ) ) return x_eventdispatcher_needsIndex ? i : true;
\r
165 * dispatch 中に dispatch が呼ばれるケースがあるため、
\r
166 * _dispatching では その深さを保存する
\r
167 * _dispatching が 0 のときに unlistens を削除する
\r
170 dispatch : function( e ){
\r
171 // dispatch 中の listen は?
\r
172 var list = this._listeners,
\r
173 ret = X.Callback.NONE,
\r
175 unlistens, i, l, f, r, sysOnly;
\r
177 if( !list ) return ret;
\r
180 if( !type ) e = { type : type = e };
\r
181 e.target = e.target || this;
\r
183 if( !( list = list[ type ] ) ) return ret;
\r
185 ++this._dispatching;
\r
189 this._unlistens = this._unlistens || {};
\r
190 unlistens = this._unlistens[ type ];
\r
192 for( i = 0; i < list.length; ++i ){
\r
195 unlistens = this._unlistens[ type ];
\r
197 if( unlistens && unlistens.indexOf( f ) !== -1 ) continue;
\r
201 r = X.Callback._proxyCallback( f );
\r
203 r = f.call( this, e );
\r
206 if( f.once || r & X.Callback.UN_LISTEN ){
\r
207 // dispatch 中に unlisten が作られることがある
\r
209 unlistens = this._unlistens || ( this._unlistens = {} );
\r
210 unlistens = unlistens[ type ] || ( unlistens[ type ] = [] );
\r
212 unlistens.indexOf( f ) === -1 && ( unlistens[ unlistens.length ] = f );
\r
215 if( r & X.Callback.STOP_NOW ){
\r
221 if( ( --this._dispatching ) === 0 ){
\r
222 // dispatch 中に unlisten された要素の削除
\r
223 unlistens = this._unlistens;
\r
224 delete this._dispatching;
\r
225 delete this._unlistens;
\r
227 for( type in unlistens ){
\r
228 list = unlistens[ type ];
\r
229 for( i = list.length; i; ){
\r
230 this.unlisten( type, list[ --i ] );
\r
233 delete unlistens[ type ];
\r
236 if( this._killReserved ){
\r
239 if( list = this._reserves ){
\r
240 for( i = 0, l = list.length; i < l; ++i ){
\r
242 x_eventdispatcher_once = f[ 4 ];
\r
243 this.listen( f[ 0 ], f[ 1 ], f[ 2 ], f[ 3 ] );
\r
244 x_eventdispatcher_once = false;
\r
248 delete this._reserves;
\r
255 onKill : function(){
\r
256 if( this._dispatching ){
\r
257 this._killReserved = true;
\r
260 this._listeners && this.unlisten();
\r
263 asyncDispatch : function( delay, e ){
\r
264 return X.Timer.add( delay, 1, this, this.dispatch, [ e ] );
\r
270 // --- implements ------------------------------------------
\r
271 function x_eventdispatcher_on( type, arg1, arg2, arg3 ){
\r
272 var list = this._listeners,
\r
275 if( this._dispatching ){
\r
276 if( !this._reserves ) this._reserves = [];
\r
277 this._reserves[ this._reserves.length ] = [ type, arg1, arg2, arg3, x_eventdispatcher_once ];
\r
281 if( X.Type.isArray( type ) ){
\r
282 for( i = type.length; i; ){
\r
283 this.listen( type[ --i ], arg1, arg2, arg3 );
\r
288 ( !list || !list[ type ] ) && X.Type.isString( type ) && x_eventdispatcher_actualAddEvent( this, type );
\r
290 if( this.listening( type, arg1, arg2, arg3 ) ) return this;
\r
292 if( !list ) list = this._listeners = {};
\r
293 if( !( list = list[ type ] ) ) list = this._listeners[ type ] = [];
\r
295 f = X.Callback._classifyCallbackArgs( arg1, arg2, arg3, this );
\r
296 list[ list.length ] = f;
\r
297 f.once = x_eventdispatcher_once;
\r
302 function x_eventdispatcher_off( type, arg1, arg2, arg3 ){
\r
303 var list = this._listeners,
\r
304 _list, reserves, unlistens, i, f;
\r
305 if( !list ) return this;
\r
307 if( X.Type.isArray( type ) ){
\r
308 for( i = type.length; i; ){
\r
309 this.unlisten( type[ --i ], arg1, arg2, arg3 );
\r
314 if( type === undefined ){
\r
316 for( type in list ){
\r
317 _list = list[ type ];
\r
318 for( i = _list.length; i; ){
\r
319 this.unlisten( type, _list[ --i ] ); // override されていることがあるので、必ず unlisten を使用
\r
321 // this.unlisten( type ); これは無茶!
\r
325 if( arg1 === undefined ){
\r
327 if( _list = list[ type ] ){
\r
328 for( i = _list.length; i; ){
\r
329 this.unlisten( type, _list[ --i ] ); // override されていることがあるので、必ず unlisten を使用
\r
334 if( reserves = this._reserves ){
\r
335 for( i = reserves.length; i; ){
\r
336 f = reserves[ --i ];
\r
337 if( f[ 0 ] === type && f[ 1 ] === arg1 && f[ 2 ] === arg2 && f[ 3 ] === arg3 ){
\r
338 reserves.splice( i, 1 );
\r
339 if( !reserves.legth ) delete this._reserves;
\r
345 x_eventdispatcher_needsIndex = true;
\r
346 i = this.listening( type, arg1, arg2, arg3 );
\r
347 x_eventdispatcher_needsIndex = false;
\r
348 if( i === false ) return this;
\r
350 f = ( _list = list[ type ] )[ i ];
\r
351 if( unlistens = this._unlistens ){
\r
352 ( unlistens = unlistens[ type ] ) ?
\r
353 ( unlistens[ unlistens.length ] = f ) :
\r
354 ( this._unlistens[ type ] = [ f ] );
\r
357 f.kill === X.Callback._kill && f.kill();
\r
358 _list.splice( i, 1 );
\r
359 if( !_list.length ){
\r
360 delete this._listeners[ type ];
\r
361 X.Type.isString( type ) && x_eventdispatcher_actualRemoveEvent( this, type );
\r
362 if( X.isEmptyObject( this._listeners ) ) delete this._listeners;
\r
369 x_eventdispatcher_actualAddEvent =
\r
370 // Days on the Moon DOM Events とブラウザの実装
\r
371 // http://nanto.asablo.jp/blog/2007/03/23/1339502
\r
372 // Safari 2 では関数オブジェクトしか EventListener として使えませんが、Safari のナイトリービルドでは handleEvent メソッドを持つオブジェクトも EventListener として使えるようです。
\r
373 x_eventdispatcher_temp.EVENT_W3C && ( X.UA.WebKit < 525.13 || X.UA.Opera7 || X.UA.NetFront < 4 ) ? // Safari3-
\r
374 (function( that, type ){
\r
375 var raw = that._rawNode;
\r
377 that._handleEvent = that._handleEvent || X.Callback.create( that );
\r
378 type = X.Dom.Event.Rename[ type ] || type;
\r
379 if( raw.addEventListener ){
\r
380 raw.addEventListener( type, that._handleEvent, false );
\r
382 // Safari は Image, Opera7 は window
\r
383 raw[ 'on' + type ] = that._handleEvent;
\r
386 x_eventdispatcher_temp.EVENT_W3C ?
\r
387 (function( that, type ){
\r
388 that._rawNode && that._rawNode.addEventListener( X.Dom.Event.Rename[ type ] || type, that, false );
\r
390 x_eventdispatcher_temp.EVENT_IE ?
\r
391 (function( that, type ){
\r
392 var raw = that._rawNode;
\r
394 type = X.Dom.Event.Rename[ type ] || type;
\r
395 //if( type === 'load' && that._tag && X.Dom.Event._LOAD_FIX_TAGS[ that._tag ] ){
\r
396 // type = 'readystatechange';
\r
398 that._handleEvent = that._handleEvent || X.Callback.create( that );
\r
399 if( raw.attachEvent ){
\r
400 raw.attachEvent( 'on' + type, that._handleEvent );
\r
402 raw[ 'on' + type ] = that._handleEvent;
\r
405 (function( that, type ){
\r
406 var raw = that._rawNode || ( that._ie4getRawNode && that._ie4getRawNode() );
\r
408 raw[ 'on' + ( X.Dom.Event.Rename[ type ] || type ) ] = that._handleEvent = that._handleEvent || X.Callback.create( that );
\r
412 x_eventdispatcher_actualRemoveEvent =
\r
413 x_eventdispatcher_temp.EVENT_W3C && ( X.UA.WebKit < 525.13 || X.UA.Opera7 || X.UA.NetFront < 4 ) ? // Safari3-
\r
414 (function( that, type ){
\r
415 var raw = that._rawNode;
\r
417 type = X.Dom.Event.Rename[ type ] || type;
\r
419 if( raw.addEventListener ){ // Image
\r
420 raw.removeEventListener( type, that._handleEvent, false );
\r
422 raw[ 'on' + type ] = null;
\r
424 if( !that._listeners ){
\r
425 X.Callback._correct( that._handleEvent );
\r
426 delete that._handleEvent;
\r
429 x_eventdispatcher_temp.EVENT_W3C ?
\r
430 (function( that, type ){
\r
431 var raw = that._rawNode;
\r
433 raw.removeEventListener( X.Dom.Event.Rename[ type ] || type, that, false );
\r
435 x_eventdispatcher_temp.EVENT_IE ?
\r
436 (function( that, type ){
\r
437 var raw = that._rawNode;
\r
439 type = X.Dom.Event.Rename[ type ] || type;
\r
440 //if( type === 'load' && that._tag && X.Dom.Event._LOAD_FIX_TAGS[ that._tag ] ){
\r
441 // type = 'readystatechange';
\r
443 if( raw.attachEvent ){
\r
444 raw.detachEvent( 'on' + type, that._handleEvent );
\r
446 raw[ 'on' + type ] = X.emptyFunction;
\r
447 raw[ 'on' + type ] = '';
\r
449 if( !that._listeners ){
\r
450 X.Callback._correct( that._handleEvent );
\r
451 delete that._handleEvent;
\r
454 (function( that, type ){
\r
455 var raw = that._rawNode || ( that._ie4getRawNode && that._ie4getRawNode() );
\r
457 type = X.Dom.Event.Rename[ type ] || type;
\r
458 raw[ 'on' + type ] = X.emptyFunction;
\r
459 raw[ 'on' + type ] = '';
\r
460 if( !that._listeners ){
\r
461 X.Callback._correct( that._handleEvent );
\r
462 delete that._handleEvent;
\r
467 console.log( 'X.Core.EventDispatcher' );
\r