-/**
- * Populates the navigation bar
- */
-$.fn.populateNavbar = function () {
- var menu = $("<ul/>", {"class" : "menu"});
- var sensorTranslator = function(item, items) {
- var href = mctx.api + "sensors?start_time=0&format=tsv&id=" + item;
- return {text : items[item].name, href : href};
- };
- var actuatorTranslator = function(item, items) {
- var href = mctx.api + "actuators?start_time=0&format=tsv&id=" + item;
- return {text : items[item].name, href : href};
- };
-
- menu.populateSubmenu("Sensor data", mctx.sensors, sensorTranslator);
- menu.populateSubmenu("Actuator data", mctx.actuators, actuatorTranslator);
- menu.appendTo(this);
- return this;
+/**
+* Should be run before the load of any GUI page.
+* To hook events to be called after this function runs,
+* use the 'always' method, e.g runBeforeLoad().always(function() {my stuff});
+* @param {type} isLoginPage
+* @returns The return value of calling $.ajax
+*/
+function runBeforeLoad(isLoginPage) {
+ return $.ajax({
+ url : mctx.api + "identify"
+ }).done(function (data) {
+ if (data.logged_in && isLoginPage) {
+ if (mctx.debug) {
+ debugLog("Redirect disabled!");
+ } else {
+ window.location = mctx.location;
+ }
+ } else if (!data.logged_in && !isLoginPage) {
+ if (mctx.debug) {
+ debugLog("Redirect disabled!");
+ } else {
+ //Note: this only clears the nameless cookie
+ document.cookie = "";
+ window.location = mctx.location + "login.html";
+ }
+ } else {
+ mctx.friendlyName = data.friendly_name;
+ }
+ }).fail(function (jqHXR) {
+ if (mctx.debug) {
+ debugLog("Failed to ident server. Is API running?")
+ } else if (!isLoginPage) {
+ window.location = mctx.location + "login.html";
+ }
+ }).always(function () {
+
+ });