nav updates

This commit is contained in:
Adam Bradley
2013-08-26 08:39:53 -05:00
parent a0ceba83d5
commit 7beb06dba5
3 changed files with 51 additions and 75 deletions

View File

@ -2,53 +2,29 @@
var var
x, x,
y, el;
link,
element;
// Add listeners to each link in the document // Add listeners to each link in the document
function addNavListeners() { function click(e) {
for(x = 0; x < document.links.length; x++) { // an element has been clicked. If its a link its good to go
link = document.links[x]; // if its not a link then jump up its parents until you find
// double check we didnt already add this event // its wrapping link. If you never find a link do nothing.
if(!link._hasClick) { if(e.target) {
link.addEventListener('click', linkClick, false); el = e.target;
link._hasClick = true; if(el.tagName === "A") {
return linkClick(e, el);
}
while(el.parentElement) {
el = el.parentElement;
if(el.tagName === "A") {
return linkClick(e, el);
} }
} }
} }
// Remove listeners from the links in the inactive page element
function removePages(e) {
var removeElements = document.body.getElementsByClassName("remove-element");
for(x = 0; x < removeElements.length; x++) {
element = removeElements[x];
links = element.querySelectorAll("a");
for(y = 0; y < links.length; y++) {
links[y].removeEventListener('click', linkClick, false);
}
element.parentNode.removeChild(element);
}
}
var _init = false;
function locationChange(e) {
if(!_init) {
_init = true;
return;
}
requestData({
url: location.href,
success: successPageLoad,
fail: failedPageLoad
});
} }
// A link has been clicked // A link has been clicked
function linkClick(e) { function linkClick(e, el) {
// if they clicked a link while scrolling don't nav to it // if they clicked a link while scrolling don't nav to it
if(framework.isScrolling) { if(framework.isScrolling) {
@ -58,27 +34,27 @@
// data-history-go="-1" // data-history-go="-1"
// shortcut if they just want to use window.history.go() // shortcut if they just want to use window.history.go()
if(e.currentTarget.dataset.historyGo) { if(el.dataset.historyGo) {
window.history.go( parseInt(e.currentTarget.dataset.historyGo, 10) ); window.history.go( parseInt(el.dataset.historyGo, 10) );
e.preventDefault(); e.preventDefault();
return false; return false;
} }
// only intercept the nav click if they're going to the same domain or page // only intercept the nav click if they're going to the same domain or page
if (location.protocol === e.currentTarget.protocol && location.host === e.currentTarget.host) { if (location.protocol === el.protocol && location.host === el.host) {
// trigger the event that a new page should be shown // trigger the event that a new page should be shown
framework.trigger("pageinit", e.currentTarget.href); framework.trigger("pageinit", el.href);
// decide how to handle this click depending on the href // decide how to handle this click depending on the href
if(e.currentTarget.getAttribute("href").indexOf("#") === 0) { if(el.getAttribute("href").indexOf("#") === 0) {
// this click is going to another element within this same page // this click is going to another element within this same page
} else { } else {
// this click is going to another page in the same domain // this click is going to another page in the same domain
requestData({ requestData({
url: e.currentTarget.href, url: el.href,
success: successPageLoad, success: successPageLoad,
fail: failedPageLoad fail: failedPageLoad
}); });
@ -91,6 +67,19 @@
} }
} }
function locationChange(e) {
if(!framework._initPopstate) {
framework._initPopstate = true;
return;
}
requestData({
url: location.href,
success: successPageLoad,
fail: failedPageLoad
});
}
function requestData(options) { function requestData(options) {
framework.isRequesting = true; framework.isRequesting = true;
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
@ -155,12 +144,8 @@
return data; return data;
} }
// after a page has been added to the DOM // listen to every click
framework.on("ready", addNavListeners); document.addEventListener("click", click, false);
framework.on("pagecreate", addNavListeners);
// before a page is about to be removed from the DOM
framework.on("pageremove", removePages);
// listen to when the location changes // listen to when the location changes
framework.on("popstate", locationChange); framework.on("popstate", locationChange);

View File

@ -11,11 +11,13 @@
el, el,
tmp, tmp,
emptyTemplates = [], emptyTemplates = [],
container; container,
templateElements;
// collect up all the templates currently in the DOM // collect up all the templates currently in the DOM
for(x=0; x<document.all.length; x++) { templateElements = document.body.querySelectorAll("[data-template]");
el = document.all[x]; for(x=0; x<templateElements.length; x++) {
el = templateElements[x];
if(el.dataset.template && !el.tSet) { if(el.dataset.template && !el.tSet) {
// this element is either supplying template // this element is either supplying template
@ -55,7 +57,7 @@
container = document.createElement("div"); container = document.createElement("div");
container.innerHTML = tmp; container.innerHTML = tmp;
el.parentNode.replaceChild(container.children[0], el); el.parentNode.replaceChild(container.children[0].cloneNode(true), el);
} }
} }

View File

@ -12,30 +12,19 @@
newMainElement.innerHTML = data.main; newMainElement.innerHTML = data.main;
var oldMainElement = document.querySelector("main"); var oldMainElement = document.querySelector("main");
oldMainElement.className += " hide remove-element";
insertPageIntoDom(newMainElement, oldMainElement, data); oldMainElement.parentNode.replaceChild(newMainElement, oldMainElement);
history.pushState({}, data.title, data.url);
framework.trigger("pageview");
framework.trigger("pageremove");
}
// insert the new main element before the old main element
function insertPageIntoDom(newMainElement, oldMainElement, data) {
oldMainElement.parentNode.insertBefore(newMainElement, oldMainElement);
// inform the framework that a new page has been added to the DOM
framework.trigger("pagecreate", { framework.trigger("pagecreate", {
id: data.id, id: data.id,
url: data.url, url: data.url,
title: data.title title: data.title
}); });
}
history.pushState({}, data.title, data.url);
framework.trigger("pageview");
}
framework.on("pageloaded", initTransition); framework.on("pageloaded", initTransition);
})(this, document, FM = this.FM || {}); })(this, document, FM = this.FM || {});