Template:Amplino
From 2012e.igem.org
(Difference between revisions)
Line 15: | Line 15: | ||
<script> | <script> | ||
+ | //Global Vars | ||
+ | var currentPage; | ||
+ | var in_frame_test = 1; | ||
+ | var ampersandStr = String.fromCharCode(38); | ||
+ | var loading_home = false; | ||
+ | |||
+ | function dbgout(msg) { | ||
+ | var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1; | ||
+ | if(is_chrome) console.log(msg); | ||
+ | } | ||
+ | |||
$(document).ready(function() { | $(document).ready(function() { | ||
+ | |||
+ | //Move iGEM default content blocks | ||
$('#search-controls').appendTo('#search-form'); | $('#search-controls').appendTo('#search-form'); | ||
$('#menubar').appendTo('#user-menu'); | $('#menubar').appendTo('#user-menu'); | ||
+ | |||
+ | // Initialize history plugin. | ||
+ | $(window).hashchange(historyCallback); | ||
+ | //rewriteLinks($("#navlist")); | ||
+ | |||
+ | historyCallback(); | ||
+ | |||
}); | }); | ||
+ | |||
+ | function isDefined(variable) | ||
+ | { | ||
+ | return typeof(window[variable]) != "undefined"; | ||
+ | } | ||
+ | |||
+ | function splitHash(hash) { | ||
+ | var kvpairs = hash.split(ampersandStr); | ||
+ | var i; | ||
+ | var kvmap = {}; | ||
+ | for(i=0;i<kvpairs.length;i++) { | ||
+ | var s = kvpairs[i].split('='); | ||
+ | kvmap[s[0]] = s[1]; | ||
+ | } | ||
+ | return kvmap; | ||
+ | } | ||
+ | |||
+ | function makeHash(page, kvmap) { | ||
+ | var str = '#page=' + page; | ||
+ | var i; | ||
+ | if (kvmap) { | ||
+ | for(i in kvmap) { | ||
+ | str += ampersandStr + i + '=' + kvmap[i]; | ||
+ | } | ||
+ | } | ||
+ | return str; | ||
+ | } | ||
+ | |||
+ | function setHash(page, kvmap) { | ||
+ | location.hash = makeHash(page,kvmap); | ||
+ | } | ||
+ | |||
+ | |||
+ | function include_js(file, cb) { | ||
+ | var html_doc = document.getElementsByTagName('head')[0]; | ||
+ | var js = document.createElement('script'); | ||
+ | js.setAttribute('type', 'text/javascript'); | ||
+ | js.setAttribute('src', file); | ||
+ | html_doc.appendChild(js); | ||
+ | |||
+ | js.onreadystatechange = function () { | ||
+ | if (js.readyState == 'complete') | ||
+ | cb(); | ||
+ | } | ||
+ | |||
+ | js.onload = cb; | ||
+ | } | ||
+ | |||
+ | function loadScript(src, callback) | ||
+ | { | ||
+ | include_js(src, callback); | ||
+ | } | ||
+ | |||
+ | function moveToAnchor(anchor) { | ||
+ | try { | ||
+ | var pos = $('#Amplino-container > a[name='+anchor+']').offset(); | ||
+ | dbgout('anchor: '+anchor+'; pos=' + pos.left + ','+pos.top ); | ||
+ | window.scroll(pos.left, pos.top); | ||
+ | } catch (err) { | ||
+ | dbgout('moveToAnchor error: ' + err); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | function loadPage(page, anchor) | ||
+ | { | ||
+ | var showLoadAnim = !!currentPage; | ||
+ | currentPage = page; | ||
+ | var url = wgServer + "/Team:Amplino/" + page + "?action=render"; | ||
+ | if(page.substring(0, 5) == "User:") { | ||
+ | url = wgServer + "/" + page + "?action=render"; | ||
+ | } | ||
+ | $(window).trigger('page_close'); | ||
+ | |||
+ | if(showLoadAnim) { | ||
+ | $("#Amplino-loader").show(); | ||
+ | $("#Amplino-container").fadeTo(200,0.25); | ||
+ | $('html, body').animate({scrollTop:0}, 'slow'); | ||
+ | } | ||
+ | loading_home = false; | ||
+ | |||
+ | var processPage = function(next) { | ||
+ | dbgout('processPage: ' + page); | ||
+ | $(window).trigger('page_init'); | ||
+ | rewriteLinks($("#Amplino-container")); | ||
+ | updateContentEditLink(); | ||
+ | $("#Amplino-container").fadeTo(200,1).delay(200).queue(function(n) { | ||
+ | if(anchor) moveToAnchor(anchor); n(); | ||
+ | }); | ||
+ | next(); | ||
+ | } | ||
+ | |||
+ | $.get(url, function(data) { | ||
+ | $("#Amplino-container").html(data).queue(processPage); | ||
+ | //buildBreadCrumbTrail(page); | ||
+ | //Cufon.replace('h2'); // Works without a selector engine | ||
+ | //Cufon.replace('h3'); // Works without a selector engine | ||
+ | //Cufon.replace('#sub1'); // Requires a selector engine for IE 6-7, see above | ||
+ | //$("tr:nth-child(odd)").addClass("odd"); | ||
+ | |||
+ | $("#Amplino-loader").hide(); | ||
+ | //$("#loading-overlay").hide(); | ||
+ | }); | ||
+ | } | ||
+ | |||
+ | function historyCallback() { | ||
+ | var hash = location.hash; | ||
+ | if(hash) { | ||
+ | hash = hash.substring(1); | ||
+ | if(hash.split('=').length > 1) { | ||
+ | var kvmap = splitHash(hash); | ||
+ | var changepage; | ||
+ | |||
+ | // looks a little clumsy, but js AND operator conflicts with mediawiki markup | ||
+ | if (kvmap.page) if(kvmap.page != currentPage) changepage = kvmap.page; | ||
+ | |||
+ | if (changepage) { | ||
+ | loadPage(changepage, kvmap.anchor); | ||
+ | } else { | ||
+ | if(kvmap.anchor) moveToAnchor(kvmap.anchor); | ||
+ | $(window).trigger('hashupdate'); | ||
+ | } | ||
+ | } | ||
+ | } else { | ||
+ | setHash('Home'); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | function rewriteLinks(elem) { | ||
+ | $("a",elem).each(function() { | ||
+ | var txt = $(this).text(); | ||
+ | var url = this.href; | ||
+ | |||
+ | if(this.hash) { | ||
+ | var anchor = this.hash.substring(1); | ||
+ | |||
+ | if (anchor.substring(0,5)!='page=') | ||
+ | this.href = '#page=' + currentPage + ampersandStr + 'anchor=' + anchor; | ||
+ | } else if(txt != "edit") | ||
+ | this.href = this.href.replace("https://2012e.igem.org/Team:Amplino/", "#page="); | ||
+ | |||
+ | // dbgout('rewriting ' + url + ' to ' + this.href); | ||
+ | }); | ||
+ | } | ||
+ | |||
</script> | </script> | ||
Line 174: | Line 338: | ||
</div> | </div> | ||
</div> | </div> | ||
+ | |||
+ | <div id="Amplino-loader"> | ||
+ | </div> | ||
+ | |||
+ | <div id="Amplino-container"> | ||
+ | </div> | ||
+ | |||
</html> | </html> |
Revision as of 13:17, 29 April 2012