From d64416ef51ac75e59dd399b4a3bbae57607abcdf Mon Sep 17 00:00:00 2001 From: Saivann Date: Sun, 16 Nov 2014 00:42:32 -0500 Subject: [PATCH] Replace javascript syntax by a more standard and readable one --- js/main.js | 763 ++++++++++++++++++++++++++++------------------------- 1 file changed, 399 insertions(+), 364 deletions(-) diff --git a/js/main.js b/js/main.js index cf67a7d2..60189dde 100644 --- a/js/main.js +++ b/js/main.js @@ -1,157 +1,172 @@ -function addEvent(a,b,c){ +function addEvent(a, b, c) { //Attach event to a DOM node. //Ex. addEvent(node,'click',function); -return (a.addEventListener)?a.addEventListener(b,c,false):(a.attachEvent)?a.attachEvent('on'+b,c):false; +return (a.addEventListener) ? a.addEventListener(b, c, false) : (a.attachEvent) ? a.attachEvent('on' + b, c) : false; } -function removeEvent(a,b,c){ +function removeEvent(a, b, c) { //Detach event from a DOM node. //Ex. removeEvent(node,'click',function); -return (a.removeEventListener)?a.removeEventListener(b,c,false):(a.detachEvent)?a.detachEvent('on'+b,c):false; +return (a.removeEventListener) ? a.removeEventListener(b, c, false) : (a.detachEvent) ? a.detachEvent('on' + b, c) : false; } -function cancelEvent(e){ +function cancelEvent(e) { //Cancel current event. //Ex. cancelEvent(event); -if(!e)var e=window.event;(e.preventDefault)?e.preventDefault():e.returnValue=false; +if (!e) var e = window.event; +(e.preventDefault) ? e.preventDefault(): e.returnValue = false; } -function getEventTarget(e){ +function getEventTarget(e) { //Return target DOM node on which the event is triggered. //Ex. getEventTarget(event); -if(!e)var e=window.event;return (e.target&&e.target.nodeType==3)?e.target.parentNode:(e.target)?e.target:e.srcElement; +if (!e) var e = window.event; +return (e.target && e.target.nodeType == 3) ? e.target.parentNode : (e.target) ? e.target : e.srcElement; } -function getStyle(a,b){ +function getStyle(a, b) { //Return the value of the computed style on a DOM node. //Ex. getStyle(node,'padding-bottom'); -if(window.getComputedStyle)return document.defaultView.getComputedStyle(a,null).getPropertyValue(b); -var n=b.indexOf('-'); -if(n!==-1)b=b.substr(0,n)+b.substr(n+1,1).toUpperCase()+b.substr(n+2); +if (window.getComputedStyle) return document.defaultView.getComputedStyle(a, null).getPropertyValue(b); +var n = b.indexOf('-'); +if (n !== -1) b = b.substr(0, n) + b.substr(n + 1, 1).toUpperCase() + b.substr(n + 2); return a.currentStyle[b]; } -function getWidth(a){ +function getWidth(a) { //Return the integer value of the computed width of a DOM node. //Ex. getWidth(node); -var w=getStyle(a,'width'); -if(w.indexOf('px')!==-1)return parseInt(w.replace('px','')); -var p=[getStyle(a,'padding-top'),getStyle(a,'padding-right'),getStyle(a,'padding-bottom'),getStyle(a,'padding-left')]; -for(var i=0;i<4;i++){ - if(p[i].indexOf('px')!==-1)p[i]=parseInt(p[i]); - else p[i]=0; +var w = getStyle(a, 'width'); +if (w.indexOf('px') !== -1) return parseInt(w.replace('px', '')); +var p = [getStyle(a, 'padding-top'), getStyle(a, 'padding-right'), getStyle(a, 'padding-bottom'), getStyle(a, 'padding-left')]; +for (var i = 0; i < 4; i++) { + if (p[i].indexOf('px') !== -1) p[i] = parseInt(p[i]); + else p[i] = 0; } -return Math.max(0,a.offsetWidth-p[1]-p[3]); +return Math.max(0, a.offsetWidth - p[1] - p[3]); } -function getHeight(a){ +function getHeight(a) { //Return the integer value of the computed height of a DOM node. //Ex. getHeight(node); -var h=getStyle(a,'height'); -if(h.indexOf('px')!==-1)return parseInt(h.replace('px','')); -var p=[getStyle(a,'padding-top'),getStyle(a,'padding-right'),getStyle(a,'padding-bottom'),getStyle(a,'padding-left')]; -for(var i=0;i<4;i++){ - if(p[i].indexOf('px')!==-1)p[i]=parseInt(p[i]); - else p[i]=0; +var h = getStyle(a, 'height'); +if (h.indexOf('px') !== -1) return parseInt(h.replace('px', '')); +var p = [getStyle(a, 'padding-top'), getStyle(a, 'padding-right'), getStyle(a, 'padding-bottom'), getStyle(a, 'padding-left')]; +for (var i = 0; i < 4; i++) { + if (p[i].indexOf('px') !== -1) p[i] = parseInt(p[i]); + else p[i] = 0; } -return Math.max(0,a.offsetHeight-p[0]-p[2]); +return Math.max(0, a.offsetHeight - p[0] - p[2]); } -function getLeft(a){ +function getLeft(a) { //Return the integer value of the computed distance between given node and the browser window. //Ex. getLeft(node); -var b=a.offsetLeft; -while(a.offsetParent){a=a.offsetParent;b+=a.offsetLeft;} +var b = a.offsetLeft; +while (a.offsetParent) { + a = a.offsetParent; + b += a.offsetLeft; +} return b; } -function getTop(a){ +function getTop(a) { //Return the integer value of the computed distance between given node and the browser window. //Ex. getTop(node); -var b=a.offsetTop; -while(a.offsetParent){a=a.offsetParent;b+=a.offsetTop;} +var b = a.offsetTop; +while (a.offsetParent) { + a = a.offsetParent; + b += a.offsetTop; +} return b; } -function getPageYOffset(){ +function getPageYOffset() { //Return the integer value for the vertical position of the scroll bar. -return (window.pageYOffset)?window.pageYOffset:document.documentElement.scrollTop; +return (window.pageYOffset) ? window.pageYOffset : document.documentElement.scrollTop; } -function getPageXOffset(){ +function getPageXOffset() { //Return the integer value for the horizontal position of the scroll bar. -return (window.pageXOffset)?window.pageXOffset:document.documentElement.scrollLeft; +return (window.pageXOffset) ? window.pageXOffset : document.documentElement.scrollLeft; } -function getWindowY(){ +function getWindowY() { //Return the integer value for the browser window height. -return (window.innerHeight)?window.innerHeight:document.documentElement.clientHeight; +return (window.innerHeight) ? window.innerHeight : document.documentElement.clientHeight; } -function getWindowX(){ +function getWindowX() { //Return the integer value for the browser window width. -return (window.innerWidth)?window.innerWidth:document.documentElement.clientWidth; +return (window.innerWidth) ? window.innerWidth : document.documentElement.clientWidth; } -function isMobile(){ +function isMobile() { //Return true if the mobile CSS stylesheet is used. -if(getStyle(document.getElementById('detectmobile'),'display')!='none')return true; +if (getStyle(document.getElementById('detectmobile'), 'display') != 'none') return true; return false; } -function addClass(node,data){ +function addClass(node, data) { //Add class to node. -var cl=node.className.split(' '); -for(var i=0,n=cl.length;i=delay){ - window.scrollTo(x,fy); +if (document.body.getAttribute('data-scrollstatus') != null) { + clearInterval(document.body.getAttribute('data-scrollstatus')); + document.body.removeAttribute('data-scrollstatus'); +} +var delay = 800; +var py = getPageYOffset(); +var fy = getTop(t) +var dy = fy - py; +var x = getPageXOffset(); +var oti = new Date().getTime(); +document.body.setAttribute('data-scrollstatus', setInterval(function() { + var nti = new Date().getTime() - oti; + if (nti >= delay) { + window.scrollTo(x, fy); clearInterval(document.body.getAttribute('data-scrollstatus')); document.body.removeAttribute('data-scrollstatus'); return; } - var p=nti/delay; - p=p*(1+(0.5*(1-p))); - window.scrollTo(x,(py+(dy*p)).toFixed(0)); -},10)); + var p = nti / delay; + p = p * (1 + (0.5 * (1 - p))); + window.scrollTo(x, (py + (dy * p)).toFixed(0)); +}, 10)); } -function supportCSS(id){ +function supportCSS(id) { //Return true if the browser supports given CSS feature. -var x=domPrefixes='Webkit Moz ms O'.split(' '); -var nd=document.createElement('DIV'); -id=id.toLowerCase(); -if(nd.style[id]!==undefined)return true; -idc=id.charAt(0).toUpperCase()+id.substr(1); -for(var i=0,n=domPrefixes.length;i0){ - var ul=t.getElementsByTagName('UL')[0]; - addClass(ul,'hover'); +var p = t = getEventTarget(e); +if (t.nodeName != 'A') return; +while (p.parentNode.nodeName != 'DIV') p = p.parentNode; +while (t.nodeName != 'LI' || t.parentNode != p) t = t.parentNode; +var ul = null; +if (t.getElementsByTagName('UL').length > 0) { + var ul = t.getElementsByTagName('UL')[0]; + addClass(ul, 'hover'); } -setTimeout(function(){ -for(var i=0,nd=p.getElementsByTagName('UL'),n=nd.length;ilast[1])last=[nodes[i],top]; - if(topcloser[1])closer=[nodes[i],top]; + for (var i = 0, n = nodes.length; i < n; i++) { + if (!nodes[i].id) continue; + var top = getTop(nodes[i]); + if (top < first[1]) first = [nodes[i], top]; + if (top > last[1]) last = [nodes[i], top]; + if (top < pageoffset + 10 && top > closer[1]) closer = [nodes[i], top]; } //Set closer title to first or last title if at the top or bottom of the page - if(pageoffset=getHeight(document.body))closer=[last[0],last[1]]; + if (pageoffset < first[1]) closer = [first[0], first[1]]; + if (windowy + pageoffset >= getHeight(document.body)) closer = [last[0], last[1]]; } //Update toc position and set active toc entry -var updatetoc=function(){ +var updatetoc = function() { //Set bottom and top to fit within window and not overflow its parent node - var div=toc.getElementsByTagName('DIV')[0]; - if(pageoffset>getTop(toc)){ - addClass(div,'scroll'); - div.style.top='20px'; - div.style.bottom=Math.max((pageoffset+windowy)-(getHeight(toc.parentNode)+getTop(toc.parentNode)),20)+'px'; - } - else removeClass(div,'scroll'); + var div = toc.getElementsByTagName('DIV')[0]; + if (pageoffset > getTop(toc)) { + addClass(div, 'scroll'); + div.style.top = '20px'; + div.style.bottom = Math.max((pageoffset + windowy) - (getHeight(toc.parentNode) + getTop(toc.parentNode)), 20) + 'px'; + } else removeClass(div, 'scroll'); //Remove .active class from toc and find new active toc entry - var a=false; - for(var i=0,t=toc.getElementsByTagName('*'),n=t.length;i0||bdiff>0)div.scrollTop-=tdiff; + var nd = a; + var otop = nd.offsetTop; + while (nd.offsetParent != div && nd.offsetParent) { + nd = nd.offsetParent; + otop += nd.offsetTop; + } + var bdiff = getHeight(a) + otop - div.scrollTop - getHeight(div); + var tdiff = getHeight(a) - otop + div.scrollTop; + if (tdiff > 0 || bdiff > 0) div.scrollTop -= tdiff; } //Update browser url -var updatehistory=function(){ +var updatehistory = function() { //Don't call window.history if not supported - if(!window.history||!window.history.replaceState)return; + if (!window.history || !window.history.replaceState) return; //Don't update window url when it doesn't need to be updated - if(new RegExp('#'+closer[0].id+'$').test(window.location.href.toString()))return; + if (new RegExp('#' + closer[0].id + '$').test(window.location.href.toString())) return; //Don't update window url when the window is over the first title in the page - if(pageoffsetnew Date().getTime()-1000)return; - window.history.replaceState(null,null,'#'+closer[0].id); + if (!toc.hasAttribute('data-timestamp') || toc.getAttribute('data-timestamp') > new Date().getTime() - 1000) return; + window.history.replaceState(null, null, '#' + closer[0].id); } //Update the toc when the page stops scrolling -var evtimeout=function(){ - toc=document.getElementById('toc'); +var evtimeout = function() { + toc = document.getElementById('toc'); clearTimeout(toc.getAttribute('data-timeout')); - toc.setAttribute('data-timeout',setTimeout(init,1)); + toc.setAttribute('data-timeout', setTimeout(init, 1)); } //Reset timestamp on page load and each time the user clicks a url -var evtimestamp=function(){ - toc=document.getElementById('toc'); - document.getElementById('toc').setAttribute('data-timestamp',new Date().getTime()); +var evtimestamp = function() { +toc = document.getElementById('toc'); +document.getElementById('toc').setAttribute('data-timestamp', new Date().getTime()); } -addEvent(window,'scroll',evtimeout); -addEvent(window,'popstate',evtimestamp); -addEvent(window,'load',evtimestamp); +addEvent(window, 'scroll', evtimeout); +addEvent(window, 'popstate', evtimestamp); +addEvent(window, 'load', evtimestamp); init(); } -function addAnchorLinks(){ +function addAnchorLinks() { //Apply anchor links icon on each title displayed on CSS hover -var nodes=[]; -var tags=['H2','H3','H4','H5','H6']; -for(var i=0,n=tags.length;i0)return; - addClass(nodes[i],'anchorAf'); - var anc=document.createElement('A'); - anc.href='#'+nodes[i].id; - nodes[i].insertBefore(anc,nodes[i].firstChild); +for (var i = 0, n = nodes.length; i < n; i++) { + if (!nodes[i].id) continue; + if (nodes[i].getElementsByTagName('A').length > 0) return; + addClass(nodes[i], 'anchorAf'); + var anc = document.createElement('A'); + anc.href = '#' + nodes[i].id; + nodes[i].insertBefore(anc, nodes[i].firstChild); } } -function updateIssue(e){ +function updateIssue(e) { //Update GitHub issue link with pre-filled with current page location -var t=getEventTarget(e); -t.href='https://github.com/bitcoin/bitcoin.org/issues/new?body='+encodeURIComponent('Location: '+window.location.href.toString()+"\n\n"); +var t = getEventTarget(e); +t.href = 'https://github.com/bitcoin/bitcoin.org/issues/new?body=' + encodeURIComponent('Location: ' + window.location.href.toString() + "\n\n"); } -function disclaimerClose(e){ +function disclaimerClose(e) { //Auto close temporary disclaimer in devel-docs -if(e)cancelEvent(e); -var t=document.getElementById('develdocdisclaimer') +if (e) cancelEvent(e); +var t = document.getElementById('develdocdisclaimer') t.parentNode.removeChild(t); -if(typeof(Storage)==='undefined')return; -sessionStorage.setItem('develdocdisclaimerclose','1'); +if (typeof(Storage) === 'undefined') return; +sessionStorage.setItem('develdocdisclaimerclose', '1'); } -function disclaimerAutoClose(){ +function disclaimerAutoClose() { //Auto close temporary disclaimer in devel-docs if session says so -if(typeof(Storage)==='undefined')return; -if(sessionStorage.getItem('develdocdisclaimerclose')==='1')disclaimerClose(); +if (typeof(Storage) === 'undefined') return; +if (sessionStorage.getItem('develdocdisclaimerclose') === '1') disclaimerClose(); } -function walletListener(e){ +function walletListener(e) { //Listen events on the wallets categories menu and hide / show / select wallets accordingly. -var t=getEventTarget(e); -switch(e.type){ -case 'click': - if(t.nodeName=='A')walletSelectPlatform(t); -break; -case 'mouseover': - if(t.nodeName=='A')walletShowPlatform(t.getAttribute('data-walletcompat')); - if(t.nodeName=='A')clearTimeout(document.getElementById('walletmenu').getAttribute('data-wallettimeout')); -break; -case 'mouseout': - clearTimeout(document.getElementById('walletmenu').getAttribute('data-wallettimeout')); - document.getElementById('walletmenu').setAttribute('data-wallettimeout',setTimeout(walletFallbackPlatform,100)); -break; +var t = getEventTarget(e); +switch (e.type) { + case 'click': + if (t.nodeName == 'A') walletSelectPlatform(t); + break; + case 'mouseover': + if (t.nodeName == 'A') walletShowPlatform(t.getAttribute('data-walletcompat')); + if (t.nodeName == 'A') clearTimeout(document.getElementById('walletmenu').getAttribute('data-wallettimeout')); + break; + case 'mouseout': + clearTimeout(document.getElementById('walletmenu').getAttribute('data-wallettimeout')); + document.getElementById('walletmenu').setAttribute('data-wallettimeout', setTimeout(walletFallbackPlatform, 100)); + break; } } -function walletSelectPlatform(t){ +function walletSelectPlatform(t) { //Select wallets platform when the mouse clicks on the menu. -var p=t; -while(p.nodeName!='DIV')p=p.parentNode; -for(var i=0,nds=p.getElementsByTagName('A'),n=nds.length;i