summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimotej Lazar <timotej.lazar@fri.uni-lj.si>2016-03-01 19:42:29 +0100
committerTimotej Lazar <timotej.lazar@fri.uni-lj.si>2016-03-01 19:42:29 +0100
commit0dbbbf9b10628d1aa51dee7c7155fa24e04848ab (patch)
tree802c9d2c0523866320ecbc56703f35d5a6ad17ff
parent868733bb9f0721fbaebc51dfa766d329ed7ce840 (diff)
Clean up navigation.js
-rw-r--r--js/codeq/navigation.js117
1 files changed, 49 insertions, 68 deletions
diff --git a/js/codeq/navigation.js b/js/codeq/navigation.js
index 1406d55..9725781 100644
--- a/js/codeq/navigation.js
+++ b/js/codeq/navigation.js
@@ -1,5 +1,5 @@
/* CodeQ: an online programming tutor.
- Copyright (C) 2015 UL FRI
+ Copyright (C) 2015,2016 UL FRI
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License as published by the Free
@@ -14,74 +14,52 @@ details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
-/**
- * Created by robert on 9/15/15.
- *
- */
-
(function() {
"use strict";
- codeq.makeStateMachine = function (def) {
- var currState = null;
- return {
- 'transition': function (name) {
- var newState = def[name];
- if (!newState) {
- codeq.log.error('Cannot transition to state ' + name + ': it is not defined');
- return;
- }
- if (newState === currState) {
- codeq.log.info('Will not transition between identical states: ' + name);
- return;
- }
- if (currState !== null) currState.exit();
- currState = newState;
- currState.enter.apply(currState, Array.prototype.slice.apply(arguments, [1]));
- },
- 'destroy': function () {
- if (currState !== null) currState.exit();
- currState = null;
- },
- 'register': function (name, state) {
- if (name in def) codeq.log.error('The state ' + name + ' is already registered, overriding');
- def[name] = state;
- }
- }
- };
-
-
- //the global state machine is a bit different - so it'll be implemented here
var makeGlobalStateMachine = function (def) {
var currState = null,
waitingState = null; // state hidden with a modal form
var stateChangeFun = function historyStateChangeHandler() {
var historyState = History.getState();
- codeq.globalStateMachine.actualTransition.apply(codeq.globalStateMachine, $.merge([historyState.data.state] ,historyState.data.params));
+ codeq.globalStateMachine.actualTransition.apply(
+ codeq.globalStateMachine,
+ $.merge([historyState.data.state],
+ historyState.data.params));
};
+
History.Adapter.bind(window, 'statechange', stateChangeFun);
- //prepare the history state change listener
+ // prepare the history state change listener
return {
'transition': function (name) {
var newState = def[name];
if (!newState) {
- codeq.log.error('Cannot transition to state ' + name + ': it is not defined');
+ codeq.log.error('Cannot transition to undefined state: ' + name);
return;
}
if (newState === currState) {
codeq.log.info('Will not transition between identical states: ' + name);
return;
}
-
- if(History.getState().data.state === name) codeq.globalStateMachine.actualTransition.apply(codeq.globalStateMachine,Array.prototype.slice.apply(arguments, [0]));//special case which happens if the user refreshes while in the login screen (history state doesn't change because it goes from login to login and the above listener doesn't trigger)
+ if (History.getState().data.state === name) {
+ // special case which happens if the user refreshes while
+ // in the login screen (history state doesn't change
+ // because it goes from login to login and the above
+ // listener doesn't trigger)
+ codeq.globalStateMachine.actualTransition.apply(
+ codeq.globalStateMachine,
+ Array.prototype.slice.apply(arguments, [0]));
+ }
try {
- History.pushState({'state': name, 'params': Array.prototype.slice.apply(arguments, [1])}, 'CodeQ', '?s=' + name);
+ History.pushState(
+ {'state': name, 'params': Array.prototype.slice.apply(arguments, [1])},
+ 'CodeQ', '?s=' + name);
}
catch (e) {
- codeq.log.error('init: History.pushState() failed for new state ' + name+'. Error:'+e, e);
+ codeq.log.error('History.pushState() failed for new state ' + name + '. ' +
+ 'Error:' + e, e);
}
},
'destroy': function () {
- var i;
if (currState) currState.exit();
currState = null;
if (waitingState) waitingState.exit();
@@ -89,19 +67,26 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */
History.Adapter.unbind(window, 'statechange', stateChangeFun);
},
'register': function (name, state, props) {
- if (name in def) codeq.log.error('The state ' + name + ' is already registered, overriding');
+ if (name in def) {
+ codeq.log.error('State ' + name + ' is already registered, overriding.');
+ }
def[name] = state;
},
'actualTransition': function (name) {
- //check if we are logged in - if we aren't we will always transition to the login state
- if(!codeq.comms.getSid() && name !== "signup" && name !== "aailogin"){//we can of course enter the signup/aailogin state even if not logged in
- name = 'login';//this will cause the following code to transition to the login state instead of the original one given
+ // always transition to 'login' state if no session exists
+ // (except for 'signup'/'aailogin' states)
+ if (!codeq.comms.getSid() && name !== "signup" && name !== "aailogin") {
+ name = 'login';
}
- var newState = def[name];//if the newState is not the same as the old or if it doesn't exist at all is already checked at this point
+ // we have already checked that newState exists, and that it is
+ // different from the current state
+ var newState = def[name];
if (currState) {
if (newState.isModal) {
- if (currState.isModal) currState.exit();
+ if (currState.isModal) {
+ currState.exit();
+ }
else {
waitingState = currState;
waitingState.jqScreen.css('display', 'none');
@@ -134,8 +119,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */
};
codeq.globalStateMachine = makeGlobalStateMachine({});
- //setup all the buttons in the banner
- $('#navigation-home').on('click', function(e){
+ // setup the navigation buttons
+ $('#navigation-home').on('click', function (e) {
codeq.globalStateMachine.transition('language');
e.preventDefault();
});
@@ -143,20 +128,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */
codeq.globalStateMachine.transition('problem_list');
e.preventDefault();
});
- $('#navigation-python').on('click', function(e){
+ $('#navigation-python').on('click', function (e) {
codeq.globalStateMachine.transition('python');
e.preventDefault();
});
- $('#navigation-prolog').on('click', function(e){
+ $('#navigation-prolog').on('click', function (e) {
codeq.globalStateMachine.transition('prolog');
e.preventDefault();
});
- $('#navigation-robot').on('click', function(e){
+ $('#navigation-robot').on('click', function (e) {
codeq.globalStateMachine.transition('robot');
e.preventDefault();
});
- $('#navigation-logout').on('click', function(e){
-
+ $('#navigation-logout').on('click', function (e) {
codeq.comms.logout()
.then(function (data) {
codeq.comms.disconnect();
@@ -166,7 +150,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */
})
.done();
- /*leaving this here if we later decide to enable saml_logout
+ /* leaving this here if we later decide to enable saml_logout
if(codeq.samlLogin){
codeq.comms.samlLogout()
.then(function (data) {
@@ -190,25 +174,22 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */
}*/
codeq.globalStateMachine.transition('login');
-
- //codeq.globalStateMachine.transition('login');
- e.preventDefault();//prevent this since we'll trigger a page reload otherwise
+ e.preventDefault();
});
- $('#navigation-profile').on('click', function(e){
+ $('#navigation-profile').on('click', function (e) {
codeq.globalStateMachine.transition('profile');
- e.preventDefault();//prevent this since we'll trigger a page reload otherwise
+ e.preventDefault();
});
- $('#navigation-about').on('click', function(e){
+ $('#navigation-about').on('click', function (e) {
codeq.globalStateMachine.transition('about');
- e.preventDefault();//prevent this since we'll trigger a page reload otherwise
+ e.preventDefault();
});
- $('#navigation-change-password').on('click', function(e){
+ $('#navigation-change-password').on('click', function (e) {
codeq.globalStateMachine.transition('changePassword');
- e.preventDefault();//prevent this since we'll trigger a page reload otherwise
+ e.preventDefault();
});
$('#navigation-settings').on('click',function(e){
codeq.globalStateMachine.transition('settings');
e.preventDefault();
});
-
})();