diff options
author | Yorhel <git@yorhel.nl> | 2015-08-15 11:54:46 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2015-08-15 11:54:46 +0200 |
commit | 8c1c8311f5910d8bf62eee449c20d87b62412239 (patch) | |
tree | befc63bc9500886fe03d41d7e3538aacfa64d22e | |
parent | e94d0df3b2f0eaefe189295413d524cea6e1f467 (diff) |
js: Wrap included files in anonymous function
This removes the need to indent all files and add the anonymous function
manually, and it also provides clean and consistent semantics.
I already rewrote the library-like files earlier on to add their public
interfaces to the window object, so everything should keep working after
this change. It's still possible that some files use use a function from
another non-library file. Those will break, but I'm sure such cases will
be found soon enough, if they exist.
-rw-r--r-- | data/js/dateselector.js | 117 | ||||
-rw-r--r-- | data/js/dropdown.js | 126 | ||||
-rw-r--r-- | data/js/dropdownsearch.js | 282 | ||||
-rw-r--r-- | data/js/iv.js | 198 | ||||
-rw-r--r-- | data/js/lib.js | 86 | ||||
-rw-r--r-- | data/js/main.js | 34 | ||||
-rw-r--r-- | data/js/tabs.js | 52 | ||||
-rwxr-xr-x | util/jsgen.pl | 2 |
8 files changed, 436 insertions, 461 deletions
diff --git a/data/js/dateselector.js b/data/js/dateselector.js index c746ea50..1ba12eca 100644 --- a/data/js/dateselector.js +++ b/data/js/dateselector.js @@ -16,72 +16,69 @@ * obj.date_val: Always contains the currently selected date. * obj.dateSet(val): Change the selected date */ -(function(){ +function load(obj, serfunc) { + var i; + var selops = {style: 'width: 70px', onfocus:serfunc, onchange: serialize, tabIndex: 10}; - function load(obj, serfunc) { - var i; - var selops = {style: 'width: 70px', onfocus:serfunc, onchange: serialize, tabIndex: 10}; + var year = tag('select', selops, + tag('option', {value:0}, mt('_js_date_year')), + tag('option', {value:9999}, 'TBA') + ); + for(i=(new Date()).getFullYear()+5; i>=1980; i--) + year.appendChild(tag('option', {value: i}, i)); - var year = tag('select', selops, - tag('option', {value:0}, mt('_js_date_year')), - tag('option', {value:9999}, 'TBA') - ); - for(i=(new Date()).getFullYear()+5; i>=1980; i--) - year.appendChild(tag('option', {value: i}, i)); + var month = tag('select', selops, + tag('option', {value:99}, mt('_js_date_month')) + ); + for(i=1; i<=12; i++) + month.appendChild(tag('option', {value: i}, i)); - var month = tag('select', selops, - tag('option', {value:99}, mt('_js_date_month')) - ); - for(i=1; i<=12; i++) - month.appendChild(tag('option', {value: i}, i)); + var day = tag('select', selops, + tag('option', {value:99}, mt('_js_date_day')) + ); + for(i=1; i<=31; i++) + day.appendChild(tag('option', {value: i}, i)); - var day = tag('select', selops, - tag('option', {value:99}, mt('_js_date_day')) - ); - for(i=1; i<=31; i++) - day.appendChild(tag('option', {value: i}, i)); + var div = tag('div', { + date_obj: obj, + date_serfunc: serfunc, + date_val: obj ? obj.value : 0 + }, year, month, day); + div.dateSet = function(v){ set(div, v) }; - var div = tag('div', { - date_obj: obj, - date_serfunc: serfunc, - date_val: obj ? obj.value : 0 - }, year, month, day); - div.dateSet = function(v){ set(div, v) }; + set(div, div.date_val); + return obj ? obj.parentNode.insertBefore(div, obj) : div; +} - set(div, div.date_val); - return obj ? obj.parentNode.insertBefore(div, obj) : div; - } - - function set(div, val) { - val = +val || 0; - val = [ Math.floor(val/10000), Math.floor(val/100)%100, val%100 ]; - if(val[1] == 0) val[1] = 99; - if(val[2] == 0) val[2] = 99; - var l = byName(div, 'select'); - for(var i=0; i<l.length; i++) - for(var j=0; j<l[i].options.length; j++) - l[i].options[j].selected = l[i].options[j].value == val[i]; - serialize(div, true); - } +function set(div, val) { + val = +val || 0; + val = [ Math.floor(val/10000), Math.floor(val/100)%100, val%100 ]; + if(val[1] == 0) val[1] = 99; + if(val[2] == 0) val[2] = 99; + var l = byName(div, 'select'); + for(var i=0; i<l.length; i++) + for(var j=0; j<l[i].options.length; j++) + l[i].options[j].selected = l[i].options[j].value == val[i]; + serialize(div, true); +} - function serialize(div, nonotify) { - div = div.dateSet ? div : this.parentNode; - var sel = byName(div, 'select'); - var val = [ - sel[0].options[sel[0].selectedIndex].value*1, - sel[1].options[sel[1].selectedIndex].value*1, - sel[2].options[sel[2].selectedIndex].value*1 - ]; - div.date_val = val[0] == 0 ? 0 : val[0] == 9999 ? 99999999 : val[0]*10000+val[1]*100+(val[1]==99?99:val[2]); - if(div.date_obj) - div.date_obj.value = div.date_val; - if(!nonotify && div.date_serfunc) - div.date_serfunc(div); - } +function serialize(div, nonotify) { + div = div.dateSet ? div : this.parentNode; + var sel = byName(div, 'select'); + var val = [ + sel[0].options[sel[0].selectedIndex].value*1, + sel[1].options[sel[1].selectedIndex].value*1, + sel[2].options[sel[2].selectedIndex].value*1 + ]; + div.date_val = val[0] == 0 ? 0 : val[0] == 9999 ? 99999999 : val[0]*10000+val[1]*100+(val[1]==99?99:val[2]); + if(div.date_obj) + div.date_obj.value = div.date_val; + if(!nonotify && div.date_serfunc) + div.date_serfunc(div); +} - var l = byClass('input', 'dateinput'); - for(var i=0; i<l.length; i++) - load(l[i]); +var l = byClass('input', 'dateinput'); +for(var i=0; i<l.length; i++) + load(l[i]); - window.dateLoad = load; -})(); +window.dateLoad = load; diff --git a/data/js/dropdown.js b/data/js/dropdown.js index 222a7110..76be3f35 100644 --- a/data/js/dropdown.js +++ b/data/js/dropdown.js @@ -16,78 +16,76 @@ * ddHide(); Hides the box * ddRefresh(); Refreshes the box contents */ -(function(){ - var box; +var box; - function init(obj, align, contents) { - obj.dd_align = align; - obj.dd_contents = contents; - obj.onmouseover = show; - } +function init(obj, align, contents) { + obj.dd_align = align; + obj.dd_contents = contents; + obj.onmouseover = show; +} - function show() { - if(!box) { - box = tag('div', {id:'dd_box', 'class':'hidden'}); - addBody(box); - } - box.dd_lnk = this; - document.onmousemove = mouse; - document.onscroll = hide; - refresh(); +function show() { + if(!box) { + box = tag('div', {id:'dd_box', 'class':'hidden'}); + addBody(box); } + box.dd_lnk = this; + document.onmousemove = mouse; + document.onscroll = hide; + refresh(); +} - function hide() { - if(box) { - setText(box, ''); - setClass(box, 'hidden', true); - box.dd_lnk = document.onmousemove = document.onscroll = null; - } +function hide() { + if(box) { + setText(box, ''); + setClass(box, 'hidden', true); + box.dd_lnk = document.onmousemove = document.onscroll = null; } +} - function mouse(e) { - e = e || window.event; - // Don't hide if the cursor is on the link - for(var lnk = e.target || e.srcElement; lnk; lnk=lnk.parentNode) - if(lnk == box.dd_lnk) - return; +function mouse(e) { + e = e || window.event; + // Don't hide if the cursor is on the link + for(var lnk = e.target || e.srcElement; lnk; lnk=lnk.parentNode) + if(lnk == box.dd_lnk) + return; - // Hide if it's 10px outside of the box - var mouseX = e.pageX || (e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft); - var mouseY = e.pageY || (e.clientY + document.body.scrollTop + document.documentElement.scrollTop); - if(mouseX < box.dd_x-10 || mouseX > box.dd_x+box.offsetWidth+10 || mouseY < box.dd_y-10 || mouseY > box.dd_y+box.offsetHeight+10) - hide(); - } + // Hide if it's 10px outside of the box + var mouseX = e.pageX || (e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft); + var mouseY = e.pageY || (e.clientY + document.body.scrollTop + document.documentElement.scrollTop); + if(mouseX < box.dd_x-10 || mouseX > box.dd_x+box.offsetWidth+10 || mouseY < box.dd_y-10 || mouseY > box.dd_y+box.offsetHeight+10) + hide(); +} - function refresh() { - if(!box || !box.dd_lnk) - return hide(); - var lnk = box.dd_lnk; - var content = lnk.dd_contents(lnk, box); - if(content == null) - return hide(); - setContent(box, content); - setClass(box, 'hidden', false); +function refresh() { + if(!box || !box.dd_lnk) + return hide(); + var lnk = box.dd_lnk; + var content = lnk.dd_contents(lnk, box); + if(content == null) + return hide(); + setContent(box, content); + setClass(box, 'hidden', false); - var o = lnk; - ddx = ddy = 0; - do { - ddx += o.offsetLeft; - ddy += o.offsetTop; - } while(o = o.offsetParent); + var o = lnk; + ddx = ddy = 0; + do { + ddx += o.offsetLeft; + ddy += o.offsetTop; + } while(o = o.offsetParent); - if(lnk.dd_align == 'left') - ddx -= box.offsetWidth; - if(lnk.dd_align == 'tagmod') - ddx += lnk.offsetWidth-35; - if(lnk.dd_align == 'bottom') - ddy += lnk.offsetHeight; - box.dd_x = ddx; - box.dd_y = ddy; - box.style.left = ddx+'px'; - box.style.top = ddy+'px'; - } + if(lnk.dd_align == 'left') + ddx -= box.offsetWidth; + if(lnk.dd_align == 'tagmod') + ddx += lnk.offsetWidth-35; + if(lnk.dd_align == 'bottom') + ddy += lnk.offsetHeight; + box.dd_x = ddx; + box.dd_y = ddy; + box.style.left = ddx+'px'; + box.style.top = ddy+'px'; +} - window.ddInit = init; - window.ddHide = hide; - window.ddRefresh = refresh; -})(); +window.ddInit = init; +window.ddHide = hide; +window.ddRefresh = refresh; diff --git a/data/js/dropdownsearch.js b/data/js/dropdownsearch.js index 7d0f82ad..35bd1443 100644 --- a/data/js/dropdownsearch.js +++ b/data/js/dropdownsearch.js @@ -28,169 +28,167 @@ * worth investigating whether the additional flexibility offered by * retfunc() is actually necessary, and remove the callback if not. */ -(function(){ - var boxobj; - - function box() { - if(!boxobj) { - boxobj = tag('div', {id: 'ds_box', 'class':'hidden'}, tag('b', mt('_js_loading'))); - addBody(boxobj); - } - return boxobj; - } +var boxobj; - function init(obj, url, trfunc, serfunc, retfunc) { - obj.setAttribute('autocomplete', 'off'); - obj.onkeydown = keydown; - obj.onblur = blur; - obj.ds_returnFunc = retfunc; - obj.ds_trFunc = trfunc; - obj.ds_serFunc = serfunc; - obj.ds_searchURL = url; - obj.ds_selectedId = 0; +function box() { + if(!boxobj) { + boxobj = tag('div', {id: 'ds_box', 'class':'hidden'}, tag('b', mt('_js_loading'))); + addBody(boxobj); + } + return boxobj; +} + +function init(obj, url, trfunc, serfunc, retfunc) { + obj.setAttribute('autocomplete', 'off'); + obj.onkeydown = keydown; + obj.onblur = blur; + obj.ds_returnFunc = retfunc; + obj.ds_trFunc = trfunc; + obj.ds_serFunc = serfunc; + obj.ds_searchURL = url; + obj.ds_selectedId = 0; + obj.ds_dosearch = null; +} + +function blur() { + setTimeout(function () { + setClass(box(), 'hidden', true) + }, 500) +} + +function setselected(obj, id) { + obj.ds_selectedId = id; + var l = byName(box(), 'tr'); + for(var i=0; i<l.length; i++) + setClass(l[i], 'selected', id && l[i].id == 'ds_box_'+id); +} + +function setvalue(obj) { + if(obj.ds_selectedId != 0) + obj.value = obj.ds_serFunc(byId('ds_box_'+obj.ds_selectedId).ds_itemData, obj); + if(obj.ds_returnFunc) + obj.ds_returnFunc(obj); + + setClass(box(), 'hidden', true); + setContent(box(), tag('b', mt('_js_loading'))); + setselected(obj, 0); + if(obj.ds_dosearch) { + clearTimeout(obj.ds_dosearch); obj.ds_dosearch = null; } - - function blur() { - setTimeout(function () { - setClass(box(), 'hidden', true) - }, 500) +} + +function enter(obj) { + // Make sure the form doesn't submit when enter is pressed. + // This solution is a hack, but it's simple and reliable. + var frm = obj; + while(frm && frm.nodeName.toLowerCase() != 'form') + frm = frm.parentNode; + if(frm) { + var oldsubmit = frm.onsubmit; + frm.onsubmit = function() { return false }; + setTimeout(function() { frm.onsubmit = oldsubmit }, 100); } - function setselected(obj, id) { - obj.ds_selectedId = id; - var l = byName(box(), 'tr'); - for(var i=0; i<l.length; i++) - setClass(l[i], 'selected', id && l[i].id == 'ds_box_'+id); - } + setvalue(obj); + return false; +} - function setvalue(obj) { - if(obj.ds_selectedId != 0) - obj.value = obj.ds_serFunc(byId('ds_box_'+obj.ds_selectedId).ds_itemData, obj); - if(obj.ds_returnFunc) - obj.ds_returnFunc(obj); +function updown(obj, up) { + var i, sel, l = byName(box(), 'tr'); + if(l.length < 1) + return true; - setClass(box(), 'hidden', true); - setContent(box(), tag('b', mt('_js_loading'))); - setselected(obj, 0); - if(obj.ds_dosearch) { - clearTimeout(obj.ds_dosearch); - obj.ds_dosearch = null; - } - } + if(obj.ds_selectedId == 0) + sel = up ? l.length-1 : 0; + else + for(i=0; i<l.length; i++) + if(l[i].id == 'ds_box_'+obj.ds_selectedId) + sel = up ? (i>0 ? i-1 : l.length-1) : (l[i+1] ? i+1 : 0); - function enter(obj) { - // Make sure the form doesn't submit when enter is pressed. - // This solution is a hack, but it's simple and reliable. - var frm = obj; - while(frm && frm.nodeName.toLowerCase() != 'form') - frm = frm.parentNode; - if(frm) { - var oldsubmit = frm.onsubmit; - frm.onsubmit = function() { return false }; - setTimeout(function() { frm.onsubmit = oldsubmit }, 100); - } - - setvalue(obj); - return false; - } + setselected(obj, l[sel].id.substr(7)); + return false; +} - function updown(obj, up) { - var i, sel, l = byName(box(), 'tr'); - if(l.length < 1) - return true; +function keydown(ev) { + var c = document.layers ? ev.which : document.all ? event.keyCode : ev.keyCode; + var obj = this; - if(obj.ds_selectedId == 0) - sel = up ? l.length-1 : 0; - else - for(i=0; i<l.length; i++) - if(l[i].id == 'ds_box_'+obj.ds_selectedId) - sel = up ? (i>0 ? i-1 : l.length-1) : (l[i+1] ? i+1 : 0); + if(c == 9) // tab + return true; - setselected(obj, l[sel].id.substr(7)); - return false; - } + if(c == 13) // enter + return enter(obj); - function keydown(ev) { - var c = document.layers ? ev.which : document.all ? event.keyCode : ev.keyCode; - var obj = this; + if(c == 38 || c == 40) // up / down + return updown(obj, c == 38); - if(c == 9) // tab - return true; + // perform search after a timeout + if(obj.ds_dosearch) + clearTimeout(obj.ds_dosearch); + obj.ds_dosearch = setTimeout(function() { + search(obj); + }, 500); - if(c == 13) // enter - return enter(obj); + return true; +} - if(c == 38 || c == 40) // up / down - return updown(obj, c == 38); +function search(obj) { + var b = box(); + var val = obj.value; - // perform search after a timeout - if(obj.ds_dosearch) - clearTimeout(obj.ds_dosearch); - obj.ds_dosearch = setTimeout(function() { - search(obj); - }, 500); + clearTimeout(obj.ds_dosearch); + obj.ds_dosearch = null; - return true; + // hide the ds_box div if the search string is too short + if(val.length < 2) { + setClass(b, 'hidden', true); + setContent(b, tag('b', mt('_js_loading'))); + setselected(obj, 0); + return; } - function search(obj) { - var b = box(); - var val = obj.value; + // position the div + var ddx=0; + var ddy=obj.offsetHeight; + var o = obj; + do { + ddx += o.offsetLeft; + ddy += o.offsetTop; + } while(o = o.offsetParent); + + b.style.position = 'absolute'; + b.style.left = ddx+'px'; + b.style.top = ddy+'px'; + b.style.width = obj.offsetWidth+'px'; + setClass(b, 'hidden', false); + + // perform search + ajax(obj.ds_searchURL + encodeURIComponent(val), function(hr) { results(hr, obj) }); +} + +function results(hr, obj) { + var lst = hr.responseXML.getElementsByTagName('item'); + var b = box(); + if(lst.length < 1) { + setContent(b, tag('b', mt('_js_ds_noresults'))); + setselected(obj, 0); + return; + } - clearTimeout(obj.ds_dosearch); - obj.ds_dosearch = null; + var tb = tag('tbody', null); + for(var i=0; i<lst.length; i++) { + var id = lst[i].getAttribute('id'); + var tr = tag('tr', {id: 'ds_box_'+id, ds_itemData: lst[i]} ); - // hide the ds_box div if the search string is too short - if(val.length < 2) { - setClass(b, 'hidden', true); - setContent(b, tag('b', mt('_js_loading'))); - setselected(obj, 0); - return; - } - - // position the div - var ddx=0; - var ddy=obj.offsetHeight; - var o = obj; - do { - ddx += o.offsetLeft; - ddy += o.offsetTop; - } while(o = o.offsetParent); - - b.style.position = 'absolute'; - b.style.left = ddx+'px'; - b.style.top = ddy+'px'; - b.style.width = obj.offsetWidth+'px'; - setClass(b, 'hidden', false); - - // perform search - ajax(obj.ds_searchURL + encodeURIComponent(val), function(hr) { results(hr, obj) }); - } + tr.onmouseover = function() { setselected(obj, this.id.substr(7)) }; + tr.onmousedown = function() { setselected(obj, this.id.substr(7)); setvalue(obj) }; - function results(hr, obj) { - var lst = hr.responseXML.getElementsByTagName('item'); - var b = box(); - if(lst.length < 1) { - setContent(b, tag('b', mt('_js_ds_noresults'))); - setselected(obj, 0); - return; - } - - var tb = tag('tbody', null); - for(var i=0; i<lst.length; i++) { - var id = lst[i].getAttribute('id'); - var tr = tag('tr', {id: 'ds_box_'+id, ds_itemData: lst[i]} ); - - tr.onmouseover = function() { setselected(obj, this.id.substr(7)) }; - tr.onmousedown = function() { setselected(obj, this.id.substr(7)); setvalue(obj) }; - - obj.ds_trFunc(lst[i], tr); - tb.appendChild(tr); - } - setContent(b, tag('table', tb)); - setselected(obj, obj.ds_selectedId != 0 && !byId('ds_box_'+obj.ds_selectedId) ? 0 : obj.ds_selectedId); + obj.ds_trFunc(lst[i], tr); + tb.appendChild(tr); } + setContent(b, tag('table', tb)); + setselected(obj, obj.ds_selectedId != 0 && !byId('ds_box_'+obj.ds_selectedId) ? 0 : obj.ds_selectedId); +} - window.dsInit = init; -})(); +window.dsInit = init; diff --git a/data/js/iv.js b/data/js/iv.js index b45b40f4..75754a90 100644 --- a/data/js/iv.js +++ b/data/js/iv.js @@ -10,117 +10,115 @@ * ivInit() should be called when links with "iv:" tags are dynamically added * or removed from the DOM. */ -(function(){ - // Cache of image categories and the list of associated link objects. Used to - // quickly generate the next/prev links. - var cats; +// Cache of image categories and the list of associated link objects. Used to +// quickly generate the next/prev links. +var cats; - function init() { - cats = {}; - var n = 0; - var l = byName('a'); - for(var i=0;i<l.length;i++) { - var o = l[i]; - if(o.rel.substr(0,3) == 'iv:' && o.id != 'ivprev' && o.id != 'ivnext') { - n++; - o.onclick = show; - var cat = o.rel.split(':')[2]; - if(cat) { - if(!cats[cat]) - cats[cat] = []; - o.iv_i = cats[cat].length; - cats[cat].push(o); - } +function init() { + cats = {}; + var n = 0; + var l = byName('a'); + for(var i=0;i<l.length;i++) { + var o = l[i]; + if(o.rel.substr(0,3) == 'iv:' && o.id != 'ivprev' && o.id != 'ivnext') { + n++; + o.onclick = show; + var cat = o.rel.split(':')[2]; + if(cat) { + if(!cats[cat]) + cats[cat] = []; + o.iv_i = cats[cat].length; + cats[cat].push(o); } } - - if(n && !byId('iv_view')) { - addBody(tag('div', {id: 'iv_view','class':'hidden'}, - tag('b', {id:'ivimg'}, ''), - tag('br', null), - tag('a', {href:'#', id:'ivfull'}, ''), - tag('a', {href:'#', onclick: close, id:'ivclose'}, mt('_js_close')), - tag('a', {href:'#', onclick: show, id:'ivprev'}, '« '+mt('_js_iv_prev')), - tag('a', {href:'#', onclick: show, id:'ivnext'}, mt('_js_iv_next')+' »') - )); - addBody(tag('b', {id:'ivimgload','class':'hidden'}, mt('_js_loading'))); - } } - // Find the next (dir=1) or previous (dir=-1) non-hidden link object for the category. - function findnav(cat, i, dir) { - for(var j=i+dir; j>=0 && j<cats[cat].length; j+=dir) - if(!hasClass(cats[cat][j], 'hidden')) - return cats[cat][j]; - return 0 + if(n && !byId('iv_view')) { + addBody(tag('div', {id: 'iv_view','class':'hidden'}, + tag('b', {id:'ivimg'}, ''), + tag('br', null), + tag('a', {href:'#', id:'ivfull'}, ''), + tag('a', {href:'#', onclick: close, id:'ivclose'}, mt('_js_close')), + tag('a', {href:'#', onclick: show, id:'ivprev'}, '« '+mt('_js_iv_prev')), + tag('a', {href:'#', onclick: show, id:'ivnext'}, mt('_js_iv_next')+' »') + )); + addBody(tag('b', {id:'ivimgload','class':'hidden'}, mt('_js_loading'))); } +} - // fix properties of the prev/next links - function fixnav(lnk, cat, i, dir) { - var a = cat ? findnav(cat, i, dir) : 0; - lnk.style.visibility = a ? 'visible' : 'hidden'; - lnk.href = a ? a.href : '#'; - lnk.rel = a ? a.rel : ''; - lnk.iv_i = a ? a.iv_i : 0; - } +// Find the next (dir=1) or previous (dir=-1) non-hidden link object for the category. +function findnav(cat, i, dir) { + for(var j=i+dir; j>=0 && j<cats[cat].length; j+=dir) + if(!hasClass(cats[cat][j], 'hidden')) + return cats[cat][j]; + return 0 +} - function show() { - var u = this.href; - var opt = this.rel.split(':'); - var idx = this.iv_i; - var view = byId('iv_view'); - var full = byId('ivfull'); +// fix properties of the prev/next links +function fixnav(lnk, cat, i, dir) { + var a = cat ? findnav(cat, i, dir) : 0; + lnk.style.visibility = a ? 'visible' : 'hidden'; + lnk.href = a ? a.href : '#'; + lnk.rel = a ? a.rel : ''; + lnk.iv_i = a ? a.iv_i : 0; +} - fixnav(byId('ivprev'), opt[2], idx, -1); - fixnav(byId('ivnext'), opt[2], idx, 1); +function show() { + var u = this.href; + var opt = this.rel.split(':'); + var idx = this.iv_i; + var view = byId('iv_view'); + var full = byId('ivfull'); - // calculate dimensions - var w = Math.floor(opt[1].split('x')[0]); - var h = Math.floor(opt[1].split('x')[1]); - var ww = typeof(window.innerWidth) == 'number' ? window.innerWidth : document.documentElement.clientWidth; - var wh = typeof(window.innerHeight) == 'number' ? window.innerHeight : document.documentElement.clientHeight; - var st = typeof(window.pageYOffset) == 'number' ? window.pageYOffset : document.body && document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop; - if(w+100 > ww || h+70 > wh) { - full.href = u; - setText(full, w+'x'+h); - full.style.visibility = 'visible'; - if(w/h > ww/wh) { // width++ - h *= (ww-100)/w; - w = ww-100; - } else { // height++ - w *= (wh-70)/h; - h = wh-70; - } - } else - full.style.visibility = 'hidden'; - var dw = w; - var dh = h+20; - dw = dw < 200 ? 200 : dw; + fixnav(byId('ivprev'), opt[2], idx, -1); + fixnav(byId('ivnext'), opt[2], idx, 1); - // update document - setClass(view, 'hidden', false); - setContent(byId('ivimg'), tag('img', {src:u, onclick:close, - onload: function() { setClass(byId('ivimgload'), 'hidden', true); }, - style: 'width: '+w+'px; height: '+h+'px' - })); - view.style.width = dw+'px'; - view.style.height = dh+'px'; - view.style.left = ((ww - dw) / 2 - 10)+'px'; - view.style.top = ((wh - dh) / 2 + st - 20)+'px'; - byId('ivimgload').style.left = ((ww - 100) / 2 - 10)+'px'; - byId('ivimgload').style.top = ((wh - 20) / 2 + st)+'px'; - setClass(byId('ivimgload'), 'hidden', false); - return false; - } + // calculate dimensions + var w = Math.floor(opt[1].split('x')[0]); + var h = Math.floor(opt[1].split('x')[1]); + var ww = typeof(window.innerWidth) == 'number' ? window.innerWidth : document.documentElement.clientWidth; + var wh = typeof(window.innerHeight) == 'number' ? window.innerHeight : document.documentElement.clientHeight; + var st = typeof(window.pageYOffset) == 'number' ? window.pageYOffset : document.body && document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop; + if(w+100 > ww || h+70 > wh) { + full.href = u; + setText(full, w+'x'+h); + full.style.visibility = 'visible'; + if(w/h > ww/wh) { // width++ + h *= (ww-100)/w; + w = ww-100; + } else { // height++ + w *= (wh-70)/h; + h = wh-70; + } + } else + full.style.visibility = 'hidden'; + var dw = w; + var dh = h+20; + dw = dw < 200 ? 200 : dw; - function close() { - setClass(byId('iv_view'), 'hidden', true); - setClass(byId('ivimgload'), 'hidden', true); - setText(byId('ivimg'), ''); - return false; - } + // update document + setClass(view, 'hidden', false); + setContent(byId('ivimg'), tag('img', {src:u, onclick:close, + onload: function() { setClass(byId('ivimgload'), 'hidden', true); }, + style: 'width: '+w+'px; height: '+h+'px' + })); + view.style.width = dw+'px'; + view.style.height = dh+'px'; + view.style.left = ((ww - dw) / 2 - 10)+'px'; + view.style.top = ((wh - dh) / 2 + st - 20)+'px'; + byId('ivimgload').style.left = ((ww - 100) / 2 - 10)+'px'; + byId('ivimgload').style.top = ((wh - 20) / 2 + st)+'px'; + setClass(byId('ivimgload'), 'hidden', false); + return false; +} + +function close() { + setClass(byId('iv_view'), 'hidden', true); + setClass(byId('ivimgload'), 'hidden', true); + setText(byId('ivimg'), ''); + return false; +} - window.ivInit = init; - init(); -})(); +window.ivInit = init; +init(); diff --git a/data/js/lib.js b/data/js/lib.js index c13d7b43..8e79f37a 100644 --- a/data/js/lib.js +++ b/data/js/lib.js @@ -1,13 +1,14 @@ -var expanded_icon = '▾', - collapsed_icon = '▸'; +window.expanded_icon = '▾', +window.collapsed_icon = '▸'; -function ajax(url, func, async) { - if(!async && ajax.req) - ajax.req.abort(); +var ajax_req; +window.ajax = function(url, func, async) { + if(!async && ajax_req) + ajax_req.abort(); var req = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest(); if(!async) - ajax.req = req; + ajax_req = req; req.onreadystatechange = function() { if(!req || req.readyState != 4 || !req.responseText) return; @@ -18,15 +19,16 @@ function ajax(url, func, async) { url += (url.indexOf('?')>=0 ? ';' : '?')+(Math.floor(Math.random()*999)+1); req.open('GET', url, true); req.send(null); -} +}; -function setCookie(n,v) { +window.setCookie = function(n,v) { var date = new Date(); date.setTime(date.getTime()+(365*24*60*60*1000)); document.cookie = VARS.cookie_prefix+n+'='+v+'; expires='+date.toGMTString()+'; path=/'; -} -function getCookie(n) { +}; + +window.getCookie = function(n) { var l = document.cookie.split(';'); n = VARS.cookie_prefix+n; for(var i=0; i<l.length; i++) { @@ -37,18 +39,20 @@ function getCookie(n) { return c.substring(n.length+1,c.length); } return null; -} +}; -function byId(n) { +window.byId = function(n) { return document.getElementById(n) -} -function byName(){ +}; + +window.byName = function(){ var d = arguments.length > 1 ? arguments[0] : document; var n = arguments.length > 1 ? arguments[1] : arguments[0]; return d.getElementsByTagName(n); -} -function byClass() { // [class], [parent, class], [tagname, class], [parent, tagname, class] +}; + +window.byClass = function() { // [class], [parent, class], [tagname, class], [parent, tagname, class] var par = typeof arguments[0] == 'object' ? arguments[0] : document; var t = arguments.length == 2 && typeof arguments[0] == 'string' ? arguments[0] : arguments.length == 3 ? arguments[1] : '*'; var c = arguments[arguments.length-1]; @@ -58,7 +62,7 @@ function byClass() { // [class], [parent, class], [tagname, class], [parent, tag if(hasClass(l[i], c)) ret[ret.length] = l[i]; return ret; -} +}; /* wrapper around DOM element creation @@ -66,7 +70,7 @@ function byClass() { // [class], [parent, class], [tagname, class], [parent, tag * tag('tagname', tag(), 'string', ..) -> createElement(), appendChild(), .. * tag('tagname', { class: 'meh', title: 'Title' }) -> createElement(), setAttribute().. * tag('tagname', { <attributes> }, <elements>) -> create, setattr, append */ -function tag() { +window.tag = function() { if(arguments.length == 1) return typeof arguments[0] != 'object' ? document.createTextNode(arguments[0]) : arguments[0]; var el = typeof document.createElementNS != 'undefined' @@ -86,53 +90,53 @@ function tag() { el.appendChild(tag(arguments[i])); } return el; -} +}; -function addBody(el) { +window.addBody = function(el) { if(document.body.appendChild) document.body.appendChild(el); else if(document.documentElement.appendChild) document.documentElement.appendChild(el); else if(document.appendChild) document.appendChild(el); -} +}; -function setContent() { +window.setContent = function() { setText(arguments[0], ''); for(var i=1; i<arguments.length; i++) if(arguments[i] != null) arguments[0].appendChild(tag(arguments[i])); -} +}; -function getText(obj) { +window.getText = function(obj) { return obj.textContent || obj.innerText || ''; -} +}; -function setText(obj, txt) { +window.setText = function(obj, txt) { if(obj.textContent != null) obj.textContent = txt; else obj.innerText = txt; -} +}; -function listClass(obj) { +window.listClass = function(obj) { var n = obj.className; if(!n) return []; return n.split(/ /); -} +}; -function hasClass(obj, c) { +window.hasClass = function(obj, c) { var l = listClass(obj); for(var i=0; i<l.length; i++) if(l[i] == c) return true; return false; -} +}; -function setClass(obj, c, set) { +window.setClass = function(obj, c, set) { var l = listClass(obj); var n = []; if(set) { @@ -145,9 +149,9 @@ function setClass(obj, c, set) { n[n.length] = l[i]; } obj.className = n.join(' '); -} +}; -function onSubmit(form, handler) { +window.onSubmit = function(form, handler) { var prev_handler = form.onsubmit; form.onsubmit = function(e) { if(prev_handler) @@ -155,19 +159,19 @@ function onSubmit(form, handler) { return false; return handler(e); } -} +}; -function shorten(v, l) { +window.shorten = function(v, l) { return v.length > l ? v.substr(0, l-3)+'...' : v; -} +}; /* maketext function, less powerful than the Perl equivalent: * - Only supports [_n], ~[, ~] * - When it finds [quant,_n,..], it will only return the first argument (and doesn't support ~ in an argument) */ -function mt() { +window.mt = function() { var key = arguments[0]; var val = VARS.l10n_str[key] ? VARS.l10n_str[key] : key; for(var i=1; i<arguments.length; i++) { @@ -179,9 +183,9 @@ function mt() { while(val.indexOf('~[') >= 0 || val.indexOf('~]') >= 0) val = val.replace('~[', '[').replace('~]', ']'); return val; -} +}; -function jsonParse(s) { +window.jsonParse = function(s) { return s ? JSON.parse(s) : ''; -} +}; diff --git a/data/js/main.js b/data/js/main.js index e1ea13e6..dc1d3b27 100644 --- a/data/js/main.js +++ b/data/js/main.js @@ -20,36 +20,18 @@ * * In the case of an mt('<key>') without any extra arguments, the entire * function call may be replaced by the TL string. - * - * - * Currently, most of the JS code dumps its functions and variables in the - * global namespace. This should be fixed, but for now, these are the prefixes - * being used: - * - * ctr -> Character <-> trait linking - * cvn -> Character <-> VN linking - * date -> Date selector - * dd -> dropdown - * ds -> dropdown search - * fil -> Filter selector - * iv -> image viewer - * jt -> Javascript Tabs - * med -> Release media selector - * prr -> Producer relation editor - * rl -> Release List dropdown - * rpr -> Release <-> producer linking - * rvn -> Release <-> visual novel linking - * scr -> VN screenshot uploader - * tgl -> VN tag linking - * tvs -> VN page tag spoilers - * vnr -> VN relation editor - * vns -> VN staff - * vnc -> VN cast - * sal -> Staff aliases editor */ +// Variables from jsgen.pl VARS = /*VARS*/; +/* The include directives below automatically wrap the file contents inside an + * anonymous function, so each file has its own local namespace. Included files + * can't access variables or functions from other files, unless these variables + * are explicitely shared in DOM objects or (more commonly) the global 'window' + * object. + */ + // Reusable library functions //include lib.js diff --git a/data/js/tabs.js b/data/js/tabs.js index 861cb9ff..470bd077 100644 --- a/data/js/tabs.js +++ b/data/js/tabs.js @@ -16,36 +16,34 @@ * * Only one jt_select list-of-tabs can be used on a single page. */ -(function(){ - var links = byId('jt_select') ? byName(byId('jt_select'), 'a') : []; +var links = byId('jt_select') ? byName(byId('jt_select'), 'a') : []; - function init() { - var sel; - var first; - for(var i=0; i<links.length; i++) { - links[i].onclick = function() { set(this.id); return false }; - if(!first) - first = links[i].id; - if(location.hash && links[i].id == 'jt_sel_'+location.hash.substr(1)) - sel = links[i].id; - } - if(first) - set(sel||first, 1); +function init() { + var sel; + var first; + for(var i=0; i<links.length; i++) { + links[i].onclick = function() { set(this.id); return false }; + if(!first) + first = links[i].id; + if(location.hash && links[i].id == 'jt_sel_'+location.hash.substr(1)) + sel = links[i].id; } + if(first) + set(sel||first, 1); +} - function set(which, nolink) { - which = which.substr(7); +function set(which, nolink) { + which = which.substr(7); - for(var i=0; i<links.length; i++) { - var name = links[i].id.substr(7); - if(name != 'all') - setClass(byId('jt_box_'+name), 'hidden', which != 'all' && which != name); - setClass(links[i].parentNode, 'tabselected', name == which); - } - - if(!nolink) - location.href = '#'+which; + for(var i=0; i<links.length; i++) { + var name = links[i].id.substr(7); + if(name != 'all') + setClass(byId('jt_box_'+name), 'hidden', which != 'all' && which != name); + setClass(links[i].parentNode, 'tabselected', name == which); } - init(); -})() + if(!nolink) + location.href = '#'+which; +} + +init(); diff --git a/util/jsgen.pl b/util/jsgen.pl index d8095839..e2f8af87 100755 --- a/util/jsgen.pl +++ b/util/jsgen.pl @@ -122,7 +122,7 @@ sub readjs { local $/ = undef; local $_ = <$JS>; close $JS; - s{^//include (.+)$}{readjs($1)}meg; + s{^//include (.+)$}{'(function(){'.readjs($1).'})();'}meg; $_; } |