4 * http://d.hatena.ne.jp/uupaa/20100430/1272561922
8 if( window.addEventListener ){
9 X.Dom.Event = function( e, xnode ){
13 //http://www.quirksmode.org/js/events_properties.html
14 this.target = Node._getXNode( e.target.nodeType === 3 ? e.target.parentNode : e.target );// defeat Safari bug // xnode
16 this.currentTarget = xnode; // xnode
17 this.relatedTarget = Node._getXNode( e.relatedTarget ); // xnode
18 this.eventPhase = e.eventPhase;
20 this.clientX = e.clientX;
21 this.clientY = e.clientY;
22 //this.screenX = e.screenX;
23 //this.screenY = e.screenY;
26 this.offsetX = e.offsetX || e.layerX;
27 this.offsetY = e.offsetY || e.layerY;
29 this.keyCode = e.keyCode;
30 this.altKey = e.altKey;
31 this.ctrlKey = e.ctrlKey;
32 this.shiftKey = e.shiftKey;
34 // http://www.programming-magic.com/20090127231544/
35 this.which = e.which || ( e.button + 1 ); // 左:1, 中:2, 右:3
37 // https://developer.mozilla.org/ja/docs/DOM/DOM_event_reference/mousewheel
38 if( e.wheelDeltaY !== undefined ){
39 this.wheelDeltaX = e.wheelDeltaX / 12;
40 this.wheelDeltaY = e.wheelDeltaY / 12;
42 if( e.wheelDelta !== undefined ){
43 this.wheelDeltaX = this.wheelDeltaY = e.wheelDelta / 12;
45 if( e.detail !== undefined ){
46 this.wheelDeltaX = this.wheelDeltaY = - e.detail * 3;
48 this.wheelDeltaX = this.wheelDeltaY = 0;
51 if( e.constructor === window.TouchEvent ){
53 this.touches = e.touches;
54 this.changedTouches = e.changedTouches;
55 this.targetTouches = e.targetTouches;
56 this.metaKey = e.metaKey;
58 if( e.constructor === window.PointerEvent ){
60 this.currentPoint = e.currentPoint;
62 this.height = e.height;
63 this.timeStamp = e.timeStamp;
64 this.hwTimestamp = e.hwTimestamp;
65 this.intermediatePoints = e.intermediatePoints;
66 this.isPrimary = e.isPrimary;
67 this.pointerId = e.pointerId;
68 this.pointerType = e.pointerType;
69 this.pressure = e.pressure;
75 X.Dom.Event = function( e, xnode, element ){
80 this.target = Node._getXNode( e.srcElement ); // xnode
81 if( this.target && this.target._xnodeType === 3 ) this.target = this.target.parent; // ie4 の fake Textnode がヒットしていないか?
82 this.currentTarget = xnode; // xnode
83 this.relatedTarget = Node._getXNode( e.formElement ? e.formElement : e.toElement ); // xnode
84 this.eventPhase = e.srcElement === element ? 2: 3;
86 this.clientX = e.clientX;
87 this.clientY = e.clientY;
88 //this.screenX = e.screenX;
89 //this.screenY = e.screenY;
91 if( X.Dom._root ){ // uuu...
93 this.pageX = e.clientX + X.Dom._root.scrollLeft;
94 this.pageY = e.clientY + X.Dom._root.scrollTop;
97 //event.pageX = DOMAssistant.def(e.pageX)? e.pageX : (event.clientX + (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0));
98 //event.pageY = DOMAssistant.def(e.pageY)? e.pageY : (event.clientY + (de.scrollTop || b.scrollTop) - (de.clientTop || 0));
103 if( X.UA.IE && 5 <= X.UA.IE ){
104 this.offsetX = e.offsetX; // イベントターゲット左上からの座標
105 this.offsetY = e.offsetY;
107 //this.offsetX = e.x - e.srcElement.offsetLeft; // e.x はイベント発生要素の親要素を基準にした座標。
108 //this.offsetY = e.y - e.srcElement.offsetTop;
111 this.keyCode = e.keyCode;
112 this.altKey = e.altKey;
113 this.ctrlKey = e.ctrlKey;
114 this.shiftKey = e.shiftKey;
116 // http://www.programming-magic.com/20090127231544/
130 btn & 2 ? 3 : 0; // 左:1(click:0), 中:4, 右:2
132 this.wheelDeltaX = this.wheelDeltaY = e.wheelDelta / 12;
136 X.Dom.Event.DOM_PRE_INIT = 0;
137 X.Dom.Event.DOM_INIT = 1;
138 X.Dom.Event.XDOM_READY = 2;
139 X.Dom.Event.VIEW_ACTIVATE = 3;
140 X.Dom.Event.VIEW_DEACTIVATE = 4;
141 X.Dom.Event.VIEW_RESIZED = 5;
142 // on_screen_keyboard_show
143 // on_screen_keyboard_hide
144 // before_commit_update
145 X.Dom.Event.COMMIT_UPDATE = 6;
148 // X.Dom.Event.LOAD_BEFORE_STOP = 7;
149 X.Dom.Event.LOAD_ASSET_COMPLETE = 7;
150 X.Dom.Event.LOAD_ASSET_ERROR = 8;
152 X.Dom.Event.ANIME_BEFORE_START = 9;
153 X.Dom.Event.ANIME_START = 10;
154 X.Dom.Event.ANIME = 11;
155 X.Dom.Event.ANIME_END = 12;
156 X.Dom.Event.ANIME_BEFORE_STOP = 13; // xnode.stop() のみ、指定時間による停止では呼ばれない
157 X.Dom.Event.ANIME_STOP = 14;
158 X.Dom.Event._LAST_EVENT = 14; // ここに書いてあるイベントの最後の値 X.Dom.Event.ANIME_STOP と同じ値
161 X.Dom.Node.prototype.listen = function( type, arg2, arg3, arg4 /* [ listener || ( context + function ) || function ][ arguments ] */ ){
164 if( this._xnodeType === 0 || this._xnodeType === 3 || !arg2 ) return this;
166 ( !this._listeners || !this._listeners[ type ] ) && this._addEvent( type );
168 return typeof arg2 === 'function' ?
169 X.EventDispatcher.prototype.listen.call( this, type, this, arg2, arg3 ) :
170 X.EventDispatcher.prototype.listen.apply( this, arguments );
173 X.Dom.Node.prototype._addEvent =
174 document.removeEventListener ?
176 this._rawNode && this._rawNode.addEventListener( type, this, false );
178 document.detachEvent ?
180 if( !this._rawNode ) return;
181 this._handleEvent = this._handleEvent || X.Callback.create( this );
182 this._rawNode.attachEvent( 'on' + type, this._handleEvent );
185 var elm = this._ie4getRawNode();
187 this._handleEvent = elm[ 'on' + type ] = this._handleEvent || X.Callback.create( this );
191 X.Dom.Node.prototype.unlisten = function( type /* , arg2, arg3, arg4 */ ){
192 var list = this._listeners,
193 l = !this._dispatching && list && type !== undefined && list[ type ] && list[ type ].length;
195 X.EventDispatcher.prototype.unlisten.apply( this, arguments );
197 l && !list[ type ] && this._removeEvent( type );
202 X.Dom.Node.prototype._removeEvent =
203 document.removeEventListener ?
205 var elm = this._ie4getRawNode ? this._ie4getRawNode() : this._rawNode;
207 elm.removeEventListener( type, this, false );
209 document.detachEvent ?
211 var elm = this._ie4getRawNode ? this._ie4getRawNode() : this._rawNode;
213 elm.detachEvent( 'on' + type, this._handleEvent );
214 if( !this._listeners ){
215 X.Callback._correct( this._handleEvent );
216 delete this._handleEvent;
220 var elm = this._ie4getRawNode ? this._ie4getRawNode() : this._rawNode;
222 elm[ 'on' + type ] = X.emptyFunction;
223 elm[ 'on' + type ] = '';
224 if( !this._listeners ){
225 X.Callback._correct( this._handleEvent );
226 delete this._handleEvent;
231 X.Dom.Node.prototype.handleEvent =
232 document.removeEventListener ?
234 var ret = X.EventDispatcher.prototype.dispatch.call( this, new X.Dom.Event( e, this ) );
236 if( ret & X.Callback.STOP_PROPAGATION ){
239 if( ret & X.Callback.PREVENT_DEFAULT ){
245 var ret = X.EventDispatcher.prototype.dispatch.call( this, new X.Dom.Event( event, this, this._rawNode ) );
247 if( ret & X.Callback.STOP_PROPAGATION ){
248 event.cancelBubble = true;
250 if( ret & X.Callback.PREVENT_DEFAULT ){
251 return event.returnValue = false;
256 // イベントの退避、dom が画面から抜かれる場合に実施しておく
257 X.Dom.Node.prototype._migrateEvent = function(){
258 var hash = this._listeners,
262 this._removeEvent( type );
267 X.Dom.Node.prototype._restoreEvent = function(){
268 var hash = this._listeners,
272 this._addEvent( type );
278 /* -----------------------------------------------
280 * Dean Edwards/Matthias Miller/John Resig
282 /* for ie9+/Mozilla/Opera9 */
283 if( document.addEventListener ){
284 X.Dom.Node._document.listenOnce( 'DOMContentLoaded', X.Dom._init );
286 if( 5 <= X.UA.IE && X.inHead ){
287 // if this script in Head
288 document.write( "<script id=__ie_onload defer src=javascript:void(0)><\/script>" );
289 X.Dom._script = document.getElementById( "__ie_onload" );
290 X.Dom._script.onreadystatechange = function(){
291 this.readyState === 'complete' && X.Dom._init();
294 if( X.UA.WebKit ){ // sniff
295 X.Timer.add( 10, function(){
296 if( !X.Dom._init ) return X.Callback.UN_LISTEN;
297 if( 'loaded|complete'.indexOf( document.readyState ) !== -1 ) return X.Dom._init();
301 /* for other browsers */
302 X.Dom.Node._window.listenOnce( 'load', X.Dom._init );
305 X.Dom.listenOnce( X.Dom.Event.XDOM_READY, function(e){ console.log( 'X.Dom XDomReady ' + X.Dom.readyState ); } );
307 X.Dom.listenOnce( X.Dom.Event.VIEW_RESIZED, function(e){ console.log( 'X.Dom VIEW_RESIZED ' + e.w + 'x' + e.h ); } );
310 /* --------------------------------------
313 X.Dom.listenOnce( X.Dom.Event.DOM_INIT, function(){
315 Node._html = document.documentElement ?
316 new Node( document.documentElement ) :
317 document.getElementsByTagName ?
318 new Node( document.getElementsByTagName( 'html' )[ 0 ] ) :
320 new Node( document.all.tags( 'html' )[ 0 ] ) :
323 var r = Node.root = new Node( document.body ),
325 createTree, xnodes, s, i, n = 0;
326 r.appendTo = r.appendToRoot = r.before = r.after = r.clone = r.remove = r.destroy = r.prevNode = r.nextNode = new Function( 'return this' );
328 r._root = Node._html._root = r;
329 r.parent = Node._html;
330 //r.width = new Function( 'return X.Dom.getSize()[ 0 ]' );
331 //r.height = new Function( 'return X.Dom.getSize()[ 1 ]' );
333 // todo: cleanup tree
335 body.childNodes && (function( elm, skip, head ){
336 var me = arguments.callee,
337 moveToHead = 'style,bgsound,area,base,meta'.split( ',' ),
338 remove = 'script,noscript,noframes,comment,!,noembed,nolayer'.split( ',' ),
339 noncleanup = 'pre,textarea,code,kbd,samp,xmp,plaintext,listing'.split( ',' ),
340 nodes = elm.childNodes,
342 node, tag, textNode, content;
343 for( ; i < nodes.length; ){
345 switch( node.nodeType ){
347 tag = node.tagName.toLowerCase();
348 if( moveToHead.indexOf( tag ) !== -1 ){
349 head = head || document.getElementsByTagName( 'head' )[ 0 ];
350 head.appendChild( node );
353 if( remove.indexOf( tag ) !== -1 ){
354 node.parentNode.removeChild( node );
357 // pre タグ以下はスペースの置換は行わない
358 me( node, skip || noncleanup.indexOf( tag ) !== -1, head );
364 content = skip ? node.data : X.Dom.cleanupWhiteSpace( node.data );
365 //console.log( 'Delete space ' + node.data.length + ' => ' + content.length );
366 if( !textNode && content !== ' ' && content.length ){
373 textNode.data += content; // 直前が TextNode の場合 一本化して削除
375 // ブロック要素直下のスペースだけは削除??
377 //console.log( 'Remove type: ' + node.nodeType + ' value: ' + node.nodeValue );
378 elm.removeChild( node );
386 (function( xnode, childNodes, skipCleanup ){
389 child, _xnode, f, tag, text, _xtext;
390 childNodes = X.copyArray( childNodes );
392 for( ; i < childNodes.length; ++i ){
393 child = childNodes[ i ];
394 if( ( child.nodeType !== 1 && child.nodeType !== 3 ) || child.tagName === '!' ){
395 child.parentNode.removeChild( child );
399 while( xnode._xnodes && j < xnode._xnodes.length ){
400 _xnode = xnode._xnodes[ j ];
401 _xnode.parent = xnode;
403 if( _xnode._xnodeType === 1 ){
404 if( child.nodeType !== 1 ){
405 if( !( text = child.data ) || ( text = X.Dom.cleanupWhiteSpace( text ) ) === ' ' ){
406 child.parentNode.removeChild( child );
409 alert( '[' +xnode._tag + '>' +_xnode._tag + '] !== ' + child.nodeType + '\n' + child.data );
414 if( _xnode._tag.toUpperCase() !== tag ){
415 alert( '[' +xnode._tag + '>' +_xnode._tag + ' ' + (_xnode._xnodes ? _xnode._xnodes.length : '' ) + '] !== ' + child.tagName + ' ' + (child.childNodes ? child.childNodes.length : '' ) + '\n' + child.outerHTML );
419 _xnode._rawNode = child;
420 _xnode._root = xnode._root;
421 child.UID = _xnode._uid;
422 if( 0 <= X.Dom.cleanupTagNames.indexOf( tag.toLowerCase() ) || tag === 'SCRIPT' ){ // ie で body 内の script が2度よばれるのに対処
423 //alert( '[' +xnode._tag + '>' + _xnode._tag + '] remove ... ' );
428 //alert( '[' +xnode._tag + '>' + _xnode._tag + ' ' + (_xnode._xnodes ? _xnode._xnodes.length : '' ) + '] === ' + child.tagName + ' ' + (child.childNodes ? child.childNodes.length : '' ) + ' Hit\n' + child.outerHTML );
429 child.childNodes.length && createTree( _xnode, child.childNodes, skipCleanup || 0 <= X.Dom.skipCleanupTagNames.indexOf( tag.toLowerCase() ) );
437 if( _xnode._xnodeType === 3 ){
438 if( child.nodeType !== 3 ){
439 if( !( text = _xnode._text ) || ( text = X.Dom.cleanupWhiteSpace( text ) ) === ' ' ){
440 console.log( '[' +xnode._tag + '>' + _xnode._uid + '] destroy ... ' );
445 alert( xnode._tag + '>' + '"' + _xnode._text + '" !== ' + child.tagName + '\n' + child.outerHTML );
450 _xnode._rawNode = child;
451 _xnode._root = xnode._root;
453 if( !( text = _xnode._text ) || ( text = X.Dom.cleanupWhiteSpace( text ) ) === ' ' ){
454 console.log( '[' +xnode._tag + '>' + _xnode._uid + '] destroy ... ' );
460 _xtext.text( _xtext._text + text );
461 console.log( '[' +xnode._tag + '>' + _xnode._uid + '] xtext,destroy ... ' );
466 //alert( xnode._tag + '>' + '"' + text + '"\n' + child.data );
471 _xtext.text( _xtext._text + _xnode.text );
472 console.log( '[' +xnode._tag + '>' + _xnode._uid + '] xtext,destroy ... ' );
477 _xtext = _xtext || _xnode;
486 //if( !f ) alert( '**** ' + child.outerHTML );
488 while( xnode._xnodes && j < xnode._xnodes.length ){
489 _xnode = xnode._xnodes[ j ];
490 _xnode.parent = xnode;
497 (function( xnode, children, skipCleanup ){
499 xnodes = parent._xnodes,
500 l = xnodes && xnodes.length,
502 i = 0, j = 0, flag = 0,
503 elm, tag, xtext, text;
504 //children = X.copyArray( children );
506 for( ; i < xnodes.length; ++i ){
508 xnode.parent = parent;
510 if( xnode._xnodeType === 3 ){
511 //alert( X.Dom.cleanupWhiteSpace( xnode._text ) );
513 if( !( text = xnode._text ) || ( text = X.Dom.cleanupWhiteSpace( text ) ) === ' ' ){
518 //alert( 'xtext ' + text.charCodeAt( 0 ) + ' : ' + text.length );
519 xtext.text( xtext._text + text );
523 //alert( 'xnode ' + text.charCodeAt( 0 ) + ' : ' + text.length );
528 //alert( 'skip ' + text.charCodeAt( 0 ) + ' : ' + text.length );
529 xtext.text( xtext._text + xnode._text );
534 xtext = xtext || xnode;
538 if( xnode._xnodeType !== 1 ){
539 //alert( xnode._xnodeType )
550 if( xnode._tag !== tag ){
551 alert( xnode._tag + ' ' + xnode._xnodeType + ' !== ' + tag + '\n' + elm.outerHTML );
553 xnode._rawNode = elm;
554 xnode._root = parent._root;
555 if( 0 <= X.Dom.cleanupTagNames.indexOf( tag.toLowerCase() ) || tag === 'SCRIPT' ){
560 xnode._xnodes && xnode._xnodes.length && createTree( xnode, elm.children, skipCleanup || 0 <= X.Dom.skipCleanupTagNames.indexOf( tag.toLowerCase() ) );
562 !xnode._id && ( elm.id = 'ie4uid' + xnode._uid );
563 elm.setAttribute( 'UID', xnode._uid );
567 ( xnode._attrs = { type : 'text' } ) :
568 ( !xnode._attrs.type ) || ( xnode._attrs.type = 'text' )
577 if( !xnode._rawNode ){
578 alert( xnode._tag + ' ' + xnode._id + ' !== none...' );
585 ( flag & 6 ) && ( parent._dirty |= X.Dom.Dirty.IE4_TEXTNODE_FIX );
586 //( flag & 4 ) && ( parent._state |= X.Dom.Dirty.ONLY_TEXTNODE );
589 r._xnodes = xnodes = [];
592 Node.skipCreate = true;
594 * http://support.microsoft.com/kb/812417/ja
595 * PRB: outerHTML の HTML 要素のプロパティは、既定の属性は表示されません。
597 * body.innerHTML でなく、 body.outerHTML にはできなかった、、、
599 xnodes.push.apply( xnodes, X.Dom.parse( body.innerHTML, true ) );
600 delete Node.skipCreate;
602 //alert(n + ' ' + body.innerHTML);
604 createTree( r, body.childNodes || body.children );
605 //r._dirty = X.Dom.Dirty.IE4_TEXTNODE_FIX;
608 Node._systemNode = s = r.create( 'div' ).className( 'hidden-sysyem-node' );
609 //alert( i + ' -> ' + xnodes.length );
613 xnodes.splice( xnodes.indexOf( s ), 1 ); // hide from api user
615 //alert(n + ' ' + body.innerHTML);