2 * MCTX3420 2013 GUI stuff.
4 * - Always end statements with semicolons
5 * - Egyptian brackets are highly recommended (*cough*).
6 * - Don't use synchronous stuff - hook events into callbacks
7 * - $.fn functions should return either themselves or some useful object
8 * to allow for chaining of method calls
12 //Don't use this in the final version
13 mctx.location = window.location.pathname;
14 mctx.location = mctx.location.substring(0, mctx.location.lastIndexOf('/')) + "/";
15 //mctx.location = location.protocol + "//" + location.host + "/";
16 mctx.api = location.protocol + "//" + location.host + "/" + "api/";
17 mctx.expected_api_version = 0;
18 mctx.has_control = false;
22 {'text' : 'Home', href : mctx.location + 'index.html'},
23 {'text' : 'Experiment control', href : mctx.location + 'control.html'},
24 {'text' : 'Pin debugging', href : mctx.location + 'pintest.html'},
25 {'text' : 'Help', href : mctx.location + 'help.html'}
36 mctx.statusCodesDescription = {
38 "-1" : "General error",
39 "-2" : "Unauthorized",
41 "-4" : "Already exists"
45 0 : {name : "Strain gauge 1"},
46 1 : {name : "Strain gauge 2"},
47 2 : {name : "Strain gauge 3"},
48 3 : {name : "Strain gauge 4"},
49 4 : {name : "Pressure sensor 1"},
50 5 : {name : "Pressure sensor 2"},
51 6 : {name : "Pressure sensor 3"}
55 0 : {name : "Solenoid 1"},
56 1 : {name : "Solenoid 2"},
57 2 : {name : "Solenoid 3"},
58 3 : {name : "Pressure regulator"}
62 mctx.actuator.pressure_regulator = 0;
64 mctx.strain_gauges = {};
65 mctx.strain_gauges.ids = [0, 1, 2, 3];
66 mctx.strain_gauges.time_limit = 20;
69 * Logs a message if mctx.debug is enabled. This function takes
70 * a variable number of arguments and passes them
71 * to alert or console.log (based on browser support).
72 * @returns {undefined}
74 function debugLog () {
76 if (typeof console === "undefined" || typeof console.log === "undefined") {
77 for (var i = 0; i < arguments.length; i++) {
82 console.log.apply(this, arguments);
85 for (var i = 0; i < arguments.length; i++) {
86 console.log(arguments[i]);
94 * Writes the current date to wherever it's called.
97 document.write((new Date()).toDateString());
101 * Should be run before the load of any GUI page.
102 * To hook events to be called after this function runs,
103 * use the 'always' method, e.g runBeforeLoad().always(function() {my stuff});
104 * @param {type} isLoginPage
105 * @returns The return value of calling $.ajax
107 function runBeforeLoad(isLoginPage) {
109 url : mctx.api + "identify"
110 }).done(function (data) {
111 if (data.logged_in && isLoginPage) {
113 debugLog("Redirect disabled!");
115 window.location = mctx.location;
117 } else if (!data.logged_in && !isLoginPage) {
119 debugLog("Redirect disabled!");
121 //Note: this only clears the nameless cookie
122 document.cookie = "";
123 window.location = mctx.location + "login.html";
126 mctx.friendlyName = data.user_name;
129 $(document).ready(function () {
131 $("#content").css("display", "block");
133 //Set the welcome bar
134 var name = " " + (mctx.friendlyName ? mctx.friendlyName : "");
135 $("#welcome-container").text("Welcome"+ name + "!");
136 $("#logout-container").css("display", "block");
137 //$("#menu-container").populateNavbar();
139 $("#logout").click(function () {
140 $("#logout").logout();
143 //Enable the error log, if present
144 $("#errorlog").setErrorLog();
146 }).fail(function (jqHXR) {
148 debugLog("Failed to ident server. Is API running?")
149 } else if (!isLoginPage) {
150 window.location = mctx.location + "login.html";
152 }).always(function () {
158 * Populates the navigation menu.
160 $.fn.populateNavMenu = function() {
161 var root = $("<ul/>")
162 for (var i = 0; i < mctx.menu.length; i++) {
163 var item = mctx.menu[i];
164 var entry = $("<li/>").append(
165 $("<a/>", {text : item.text, href: item.href})
169 $(this).append(root);
174 * Performs a login attempt.
175 * @returns The AJAX object of the login request */
176 $.fn.login = function () {
177 var username = this.find("input[name='username']").val();
178 var password = this.find("input[name='pass']").val();
179 var out = this.find("#result");
180 var redirect = function () {
181 window.location.href = mctx.location;
184 out.removeAttr("class");
185 out.text("Logging in...");
188 url : mctx.api + "bind",
189 data : {user: username, pass : password}
190 }).done(function (data) {
191 if (data.status < 0) {
192 mctx.has_control = false;
193 out.attr("class", "fail");
194 out.text("Login failed: " + data.description);
197 mctx.has_control = true;
198 out.attr("class", "pass");
199 out.text("Login ok!");
200 setTimeout(redirect, 800);
202 }).fail(function (jqXHR) {
203 mctx.has_control = false;
204 out.attr("class", "fail");
205 out.text("Login request failed - connection issues.")
210 * Performs a logout request. The nameless cookie is
211 * always cleared and the browser redirected to the login page,
212 * independent of whether or not logout succeeded.
213 * @returns The AJAX object of the logout request.
215 $.fn.logout = function () {
217 url : mctx.api + "unbind"
218 }).always(function () {
219 //Note: this only clears the nameless cookie
220 document.cookie = "";
221 window.location = mctx.location + "login.html";
226 * Sets the error log to continuously update.
228 $.fn.setErrorLog = function () {
229 var url = mctx.api + "errorlog";
232 if ($(this).length <= 0) {
233 //No error log, so do nothing.
237 var updater = function () {
238 $.ajax({url : url}).done(function (data) {
241 outdiv[0].scrollHeight - outdiv.height()
243 setTimeout(updater, 3000);
244 }).fail(function (jqXHR) {
245 if (jqXHR.status === 502 || jqXHR.status === 0) {
246 outdiv.text("Failed to retrieve the error log.");
248 setTimeout(updater, 10000); //poll at slower rate
256 $.fn.checkStatus = function(data) {
257 if (data.status !== mctx.status.OK) {
258 $(this).text(data.description).removeClass("pass").addClass("fail");
261 $(this).removeClass("fail");
265 $(document).ready(function () {
266 //Enable the hide/show clicks
267 $("#sidebar-hide").click(function () {
268 $("#sidebar").hide();
269 $("#sidebar-show").show();
273 $("#sidebar-show").click(function () {
274 $("#sidebar-show").hide();
275 $("#sidebar").show();
280 $(document).ajaxError(function (event, jqXHR) {
281 //console.log("AJAX query failed with: " + jqXHR.status + " (" + jqXHR.statusText + ")");