define(function() { 'use strict'; var all_pages = 'home studying curriculum campus career'.split(' '); /* loads a page into view and execute scripts */ function loadContent(name, text) { var contentElement = document.getElementById('content'); contentElement.innerHTML = text; document.body.dataset.page = name; // execute scripts in page [].forEach.call(contentElement.getElementsByTagName('script'), function(origScript) { var script = document.createElement('script'); // copy attributes [].forEach.call(origScript.attributes, function(a) { script.setAttribute(a.name, a.value); }); // copy contents script.textContent = origScript.textContent; origScript.parentNode.replaceChild(script, origScript); }); } /* tries to navigate to a page */ function selectPage(page) { if (all_pages.indexOf(page) == -1) { // TODO: 404 console.log("404 " + page); return false; } console.log("Loading " + page); var http = new XMLHttpRequest(); http.onload = function() { loadContent(page, http.responseText); }; http.open('get', 'pages/' + page + '.html'); http.send(null); return true; } addEventListener('hashchange', function(ev) { var m = /#\/(.+)/.exec(ev.newURL); if (m) { selectPage(m[1]); } else if (!/#/.test(ev.newURL)) { location.hash = '#/home'; } }); // Tries to load the current page, falling back to "home" for unknown URLs. if (!selectPage(location.hash.replace(/^#\//, ''))) { location.hash = '#/home'; } });