Skip to content

Instantly share code, notes, and snippets.

@jasdeepkhalsa
Last active September 17, 2015 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdeepkhalsa/4d8919013a906042bea1 to your computer and use it in GitHub Desktop.
Save jasdeepkhalsa/4d8919013a906042bea1 to your computer and use it in GitHub Desktop.
Navigation
var goingBack = false;
function navigate(page, html, comment, cleanComponents, cb) {
if (comment) console.debug(comment);
if (!goingBack) navigation.add(page);
appHtml.innerHTML = html;
if (cleanComponents) {
app.components.clean();
}
if (cb) cb();
}
/**
* Navigation provides an API to navigate between pages in the app
* Navigation uses the this.history [array] to add and remove pages
* @type {Object}
*/
var navigation = {
history: [],
forward: function(page) {
this.add(page);
},
back: function(canGoBack, noBack) {
if (this.history.length > 1) {
if (canGoBack) {
canGoBack(this.history[this.history.length - 2]);
}
this.remove();
} else {
if (noBack) noBack();
}
},
add: function(page) {
this.history.push(page);
},
remove: function() {
this.history.pop();
}
};
var history = [];
var navigation = {
back: function() {
if (history.length > 1) {
this.show(history[history.length - 2]);
history.pop();
}
},
forward: function(menu) {
history.push(menu);
this.show(menu);
},
restore: function() {
this.show(history[history.length - 1]);
},
hideAll: function(){
for(var i=0;i<history.length;i++){
var elem = document.querySelector(history[i]);
elem.style.display = 'none';
}
},
show: function(menu) {
this.hideAll();
var elem = document.querySelector(menu);
elem.style.display = 'block';
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment