String.prototype.element = String.prototype.el = function(){ return document.getElementById(this) } /* eventオブジェクトの代わりに使う stop : イベントとアクションの停止 stopEvent : イベントの伝播を停止 stopAction : ブラウザのデフォルト動作を停止 */ function EventEmu(eve){ var TMP = function(){}; var common = { get : function(key){ return getter.own(key) ? getter[key].call(this.event) : this.event[key] }, stop : function(){ this.stopPropagation(); this.preventDefault()}, stopEvent : function(){this.stopPropagation()}, stopAction : function(){this.preventDefault()} }; if(window.event){ var getter = { target : function(){return this.srcElement} }; TMP.prototype = common; var e = new TMP; e.extend({ event : eve, stopPropagation : function() { window.event ? window.event.cancelBubble = true : 0}, preventDefault : function() { window.event ? window.event.returnValue = false : 0} }); e.extend({ type : window.event.type, target : window.event.srcElement, keyCode : window.event.keyCode, clientX : window.event.clientX, clientY : window.event.clientY, pageX : document.body.scrollLeft + window.event.clientX, pageY : document.body.scrollTop + window.event.clientY, shiftKey : window.event.shiftKey, ctrlKey : window.event.ctrlKey }); }else{ var getter = { wheelDelta : function(){return this.detail*-120} }; TMP.prototype = common; var e = new TMP; e.extend({ event: eve, stopPropagation : function() { this.event.stopPropagation() }, preventDefault : function() { this.event.preventDefault() } }); e.keyCode = eve.keyCode; } return e; } function DOMRef(obj){ this.ref = obj.isString ? obj.el() : obj } DOMRef.prototype = { ae : function(actions){ var self = this.ref; var synonym = { mousewheel : "DOMMouseScroll" }; actions.forEach(function(func,type){ self.addEventListener? self.addEventListener( synonym.own(type) ? synonym[type] : type, function(e){ var emu = new EventEmu(e); func(emu) } , false): self.attachEvent? self.attachEvent( 'on' + type, function(){ return func( new EventEmu(window.event).extend({ currentTarget : self })) } ):0 }) }, addEvent : function(type,func){ var self = this.ref; var synonym = { mousewheel : "DOMMouseScroll" }; (self.addEventListener) ? self.addEventListener( (synonym.own(type)) ? synonym[type] : type, function(e){ var emu = new EventEmu(e); func(emu) } , false): (self.attachEvent) ? self.attachEvent( 'on' + type, function(){ return func( new EventEmu(window.event).extend({ currentTarget : self })) } ) :0 }, set : function(o){ o.forEach(function(val,key){ this.ref[key] = val; },this) }, print : function(){ this.ref.innerHTML = Array.prototype.join.call(arguments,""); }, prev : function(){ return new DOMRef(this.ref.previousSibling); }, next : function(){ return new DOMRef(this.ref.nextSibling); }, parent : function(){ return new DOMRef(this.ref.parentNode); }, gw : function(){ return this.ref.offsetWidth }, gh : function(){ return this.ref.offsetHeight }, gy : function(){ return (function(ref,acc){ acc += ref.offsetTop; return ref.offsetParent? arguments.callee(ref.offsetParent,acc) : acc })(this.ref,0) }, gx : function(){ return (function(ref,acc){ acc += ref.offsetLeft; return ref.offsetParent? arguments.callee(ref.offsetParent,acc) : acc })(this.ref,0) }, ss : function(obj){ for(var i in obj){ if(!obj.hasOwnProperty(i)) continue; this.ref.style[i] = obj[i] } }, sv : function(val){ this.ref.value = val }, value : function(v){ return v ? this.ref.value = v : this.ref.value; }, getAll : function(){ return this.ref.getElementsByTagName("*") }, search : function(query){ return Array.prototype.filter.call( this.getAll(), function(node){ return query.every( function(value,key){return this[key].include(query[key])}, node ) } ); }, gtn : function(tag){ return this.ref.getElementsByTagName(tag) }, addClass : function(cl){ if(this.ref.className.include(cl)) return; this.ref.className = this.ref.className.split(/\s+/).append(cl).uniq().join(" ") }, delClass : function(cl){ if(!this.ref.className.include(cl)) return; this.ref.className = this.ref.className.split(/\s+/).remove(cl).join(" ") }, appendTo : function(obj){ obj.appendChild(this.ref) }, rc : function(obj){ this.ref.removeChild(obj.ref) }, remove : function(){ this.parent().rc(this) }, move : function(x,y){ this.ref.style.left = x + "px"; this.ref.style.top = y + "px"; }, resize : function(x,y){ x!=null?this.ref.style.width = x + "px":0; y!=null?this.ref.style.height = y + "px":0; } } function DOMArray(obj){ this.ref = obj; this.length = obj.length; } DOMArray.prototype = { push : function(v){ if(v.isString){ var tmp = document.createElement(this.ref[0].tagName); tmp.innerHTML = v; }else{ var tmp; var tagname; for(i in v){ if(v.own(i)){ tagname = i; tmp = document.createElement(i); tmp.innerHTML = v[i]; break; } } for(i in v){ if(v.own(i) && i != tagname){ if(v[i].isFunction){ var h = i.replace(/on/,"") new DOMRef(tmp).addEvent(h , v[i]) }else{ if(i.charAt(0) == "_"){ tmp.setAttribute(i.substr(1),v[i]) }else{ tmp[i] = v[i]; } } } } } // alert(this.ref[0].innerHTML); this.ref[0].parentNode.appendChild(tmp) }, pop : function(){ this.ref[0].parentNode.removeChild(this.ref[this.length-1]) }, shift : function(){ this.ref[0].parentNode.removeChild(this.ref[0]) }, unshift : function(v){ if(v.isString){ var tmp = document.createElement(this.ref[0].tagName); tmp.innerHTML = v; }else{ var tmp; var tagname; for(i in v){ if(v.own(i)){ tagname = i; tmp = document.createElement(i); tmp.innerHTML = v[i]; break; } } for(i in v){ if(v.own(i) && i != tagname) tmp[i] = v[i] } } this.ref[0].parentNode.insertBefore(tmp,this.ref[0]) } }