// Whether to use javascript caching or not var useCache = true; // assoc array of pages interval pollers var pagePollers = new Array(); // the debug-mode history browser var histWin; // Array of visited pages - used for 'back' button var visited = new Array(''); // Array of visited-visited pages (used for pages looked at using the 'back' button) var revisited = new Array(); // Array of cached pages var pageCache = new Array(); // deprecated vars used for handling user's hard-back button var lastPage; var preventedUnload = false; // initialise ajax objects as global @todo: use array var pageContentManager = false; var pageTitleManager = false; /* * @desc Debug function, could be removed from debug menu * and instead intrinsic for admin users, or called * by hooked back/forward controls * * @author Leo Brown * */ function stopCaching(){ useCache = false; //alert('Pages will no longer be cached.'); } /* * @desc Check if the user has hit enter on the search box * if so, get the results from the search page with the * relevant text value * * @author Leo Brown * */ function checkEnter(box,e) { var characterCode if(e && e.which) { e=e characterCode = e.which } else { e = event characterCode = e.keyCode } if(characterCode == 13) { getPage('search.php?s='+box.value,'',true) return false; } } /* * @desc Faux ajax back function, using array of previously * (re-)visited pages * @author Leo Brown */ function prevPage(){ // Knock the first (current) page off the top //revisited.push( visited.pop() ); visited.pop(); // Check if the last is valid, if so navigate // note that this function should not be available // from the interface if there are no elements last = visited.pop(); if(last != undefined){ getPage(last,'',true); } // User backed right up? if (visited.length == 1){ document.getElementById('backButton').style.visibility='hidden'; } } /* * @todo Make sensible :| * @todo Not use SMC style comments :O * */ function nextPage(){ next = revisited.pop(); if(next != undefined){ getPage(next ,'',true); } } /* @desc Central ajax controller to load the page content into the right place * also controls some knock-on interface elements * @author Leo Brown * @param page the new page (content) * @param watermark string of a jpeg in /img/watermark * @param dontRandomise (whether or not to pass a random get param) */ function getPage(page,watermark,dontRandomise) { // remove hostname which javascript may have 'cleverly' entered page = page.replace(document.location.protocol, ''); page = page.replace(document.location.host, ''); // directory to our content page = 'content/' + page; // Get rid of any per page polling for (i in pagePollers){ clearInterval(pagePollers[i]); } pagePollers = new Array(); visited.push(page); //document.getElementById('backButton').style.visibility='visible'; lastPage = page; // set the right watermark, if given if(watermark!=undefined){ document.body.style.backgroundImage = 'url(/img/watermark/'+watermark+'.jpg)'; } // use cache to get page, if poss if (useCache && pageCache[page]){ document.getElementById('content').innerHTML = pageCache[page]; parseScript(document.getElementById('content')); //try{init()} catch(e){} // Draw page title again getPageTitle(page); return; } var self = this; if (window.XMLHttpRequest) { self.pageContentManager = new XMLHttpRequest(); } else if (window.ActiveXObject) { if ( ! self.pageContentManager ){ self.pageContentManager = new ActiveXObject("Microsoft.XMLHTTP"); } } //added rqst param to stop IE caching if (!dontRandomise){ getString = page+'?randomSeed='+Math.random(); } else{ getString = page; } // General options // Get page content self.pageContentManager.abort(); self.pageContentManager.open('GET', getString, true); self.pageContentManager.setRequestHeader('Content-Type', 'text/html'); self.pageContentManager.onreadystatechange = function() { if (self.pageContentManager.readyState == 4) { document.getElementById('content').innerHTML = self.pageContentManager.responseText; parseScript(document.getElementById('content')); try{init()} catch(e){} getPageTitle(page); pageCache[page] = self.pageContentManager.responseText; } } self.pageContentManager.send(''); } /* * * @desc Script block post-parser for AJAX innerHTML response * Can't believe this has to be made. * @author Leo Brown * */ function parseScript( results ) { var children = results.childNodes; var child; var index; for (index=0; index tag... * @author Leo Brown * */ function getPageTitle(page){ // Get page title var self = this; if (window.XMLHttpRequest) { self.pageTitleManager = new XMLHttpRequest(); } else if (window.ActiveXObject) { if ( ! self.pageTitleManager ){ self.pageTitleManager = new ActiveXObject("Microsoft.XMLHTTP"); } } self.pageTitleManager.open('GET', '/system/getTitle.php?p='+page, true); self.pageTitleManager.setRequestHeader('Content-Type', 'text/html'); self.pageTitleManager.onreadystatechange = function() { if (self.pageTitleManager.readyState == 4) { if(document.getElementById('pageTitle')){ document.getElementById('pageTitle').innerHTML = self.pageTitleManager.responseText; } } } self.pageTitleManager.send(''); } /* * @desc 'Hardback' override function, to hook document.body.unload. * designed to overcome the limitations of AJAX content * such as browser 'back' buttons that will suprise users * @author Leo Brown * */ function fauxBack(){ if(!preventedUnload){ // If there is no recorded movement, get current page if (visited.length < 2){ alert('nowhere to go...'); lastPage = '&allowBack=1'; } else{ visited.pop(); lastPage = visited.pop(); } //alert('Hooking page unload...'); window.location.replace('/?page='+lastPage); } else{ //alert('Not hooking page unload...'); } return false; } /* * * @desc Debug page history function * @author Leo Brown * */ function showPageHistory(){ // define the start of the history window content (could do this using a fixed file) var pageHistory = ""+ "

Page History

  1. " + visited.join("
  2. "); // Dont bother making another open() call, if we already have a reference // browsers seem unhappy reopening a window if we're resuing the object... // but if we use a new object and the same win ref, then the content is blank when reusing, but okay reloading //histWin=null; if(!histWin){ try{ histWin = window.open('about:blank','HistWindow','left='+(screen.width-250)+',width=220,height=600,scrollbars=1',true); } catch(e){} } // Great, MS dont yet support document.clear() - this is their suggested hack: histWin.document.write(''); histWin.document.close(); // Write the history window contents to the history browser and set focus histWin.document.write(pageHistory); histWin.focus(); } /* * * @desc Body keyboard hook, primarily for quick debug functions * @author Leo Brown * */ function keyListener(){ // alert(event.keyCode); var KEY_LEFTBRACKET = 91; var KEY_RIGHTBRACKET = 93; var KEY_NUMPAD5 = 53; switch(event.keyCode){ case KEY_LEFTBRACKET: prevPage(); break; case KEY_RIGHTBRACKET: nextPage(); break; case KEY_NUMPAD5: getPage(visited[visited.length-1]); break; } } /* * * @desc Create a new blank page showing the content from the main display area * @author Leo Brown, Elwood Casey * */ function debugContent() { win = window.open('about:blank'); win.document.write(''); win.document.write(document.getElementById('content').innerHTML); } // deprecated page body load fn - pls grep for usage function loadPage(page,watermark){ document.getElementById('content').innerHTML = page; document.body.style.backgroundImage = 'url(/img/watermark/'+watermark+'.jpg)'; }