frontend: improve main interface

This commit is contained in:
nemunaire 2016-01-23 12:29:19 +01:00
parent 70b6b30067
commit 32e8f931b9
8 changed files with 477 additions and 354 deletions

View file

@ -1,4 +1,4 @@
angular.module("FICApp", ["ngRoute"])
angular.module("FICApp", ["ngRoute", "ngSanitize"])
.config(function($routeProvider, $locationProvider) {
$routeProvider
.when("/edit", {
@ -26,13 +26,56 @@ angular.module("FICApp", ["ngRoute"])
});
$locationProvider.html5Mode(true);
})
.run(function($rootScope) {
.run(function($rootScope, $timeout) {
$rootScope.current_theme = 0;
$rootScope.current_exercice = 0;
$rootScope.time = {};
function updTime() {
$timeout(updTime, 1000);
if (sessionStorage.userService) {
var time = angular.fromJson(sessionStorage.userService);
var srv_cur = (Date.now() + (time.cu * 1000 - time.he)) / 1000;
var remain = time.du;
if (time.st == Math.floor(srv_cur)) {
$rootScope.refresh(true);
}
if (time.st > 0 && time.st <= srv_cur) {
remain = time.st + time.du - srv_cur;
}
if (remain < 0) {
remain = 0;
$rootScope.time.end = true;
$rootScope.time.expired = true;
} else if (remain < 60) {
$rootScope.time.end = false;
$rootScope.time.expired = true;
} else {
$rootScope.time.end = false;
$rootScope.time.expired = false;
}
$rootScope.time.remaining = remain;
$rootScope.time.hours = Math.floor(remain / 3600);
$rootScope.time.minutes = Math.floor((remain % 3600) / 60);
$rootScope.time.seconds = Math.floor(remain % 60);
}
}
updTime();
});
angular.module("FICApp")
.controller("DataController", function($scope, $http, $rootScope) {
.filter("time", function() {
return function(input) {
if (input == undefined) {
return "--";
} else if (input >= 10) {
return input;
} else {
return "0" + input;
}
}
})
.controller("DataController", function($scope, $http, $rootScope, $timeout) {
var actMenu = function() {
if ($scope.my && $scope.themes) {
angular.forEach($scope.themes, function(theme, key) {
@ -45,32 +88,38 @@ angular.module("FICApp")
});
}
}
var repeat = function() {
$http.get("/themes.json").success(function(themes) {
$scope.themes = themes;
angular.forEach(themes, function(theme, key) {
this[key].exercice_count = Object.keys(theme.exercices).length;
}, themes);
actMenu();
});
$http.get("/teams.json").success(function(teams) {
$scope.teams_count = Object.keys(teams).length
$scope.teams = teams;
$rootScope.refresh = function(justMy) {
if (!justMy) {
$timeout($rootScope.refresh, 60000);
$http.get("/time.json").success(function(time) {
time.he = (new Date()).getTime();
sessionStorage.userService = angular.toJson(time);
});
$http.get("/themes.json").success(function(themes) {
$scope.themes = themes;
angular.forEach(themes, function(theme, key) {
this[key].exercice_count = Object.keys(theme.exercices).length;
}, themes);
actMenu();
});
$http.get("/teams.json").success(function(teams) {
$scope.teams_count = Object.keys(teams).length
$scope.teams = teams;
$scope.rank = [];
angular.forEach($scope.teams, function(team, tid) {
team.id = tid;
this.push(team);
}, $scope.rank);
});
$scope.rank = [];
angular.forEach($scope.teams, function(team, tid) {
team.id = tid;
this.push(team);
}, $scope.rank);
});
}
$http.get("/my.json").success(function(my) {
$scope.my = my;
actMenu();
});
console.log("refresh!");
}
repeat();
setInterval(repeat, 60000);
$rootScope.refresh();
})
.controller("ExerciceController", function($scope, $routeParams, $http, $rootScope) {
$rootScope.current_theme = $routeParams.theme;
@ -91,34 +140,56 @@ angular.module("FICApp")
}
}
$scope.ssubmit = function() {
if (!$("#solution").val().length) {
return false
})
.controller("SubmissionController", function($scope, $http, $rootScope, $timeout) {
$scope.flags = []
var waitMy = function() {
if (!$scope.my || !$scope.my.exercices || !$scope.my.exercices[$rootScope.current_exercice]) {
$timeout(waitMy, 420);
} else {
angular.forEach($scope.my.exercices[$rootScope.current_exercice].keys, function(key,kid) {
this.push({
id: kid,
name: key,
value: ""
});
}, $scope.flags);
}
}
waitMy();
$scope.ssubmit = function() {
var flgs = {}
angular.forEach($scope.flags, function(flag,kid) {
flgs[flag.name] = flag.value;
});
$http({
url: "/submit/" + $rootScope.current_exercice,
method: "POST",
data: $("#solution").val()
data: flgs
}).success(function(data, status, header, config) {
$scope.messageClass = {"alert-success": true};
$scope.message = data.errmsg;
$rootScope.messageClass = {"alert-success": true};
$rootScope.message = data.errmsg;
$scope.refresh = function() {
var checkDiff = function() {
$http.get("/my.json").success(function(my) {
if ($scope.my.exercices[$rootScope.current_exercice].solved_time != my.exercices[$rootScope.current_exercice].solved_time) {
$scope.my = my;
} else {
setTimeout($scope.refresh, 750);
$timeout(checkDiff, 750);
}
});
};
$scope.refresh()
checkDiff()
}).error(function(data, status, header, config) {
if (status >= 500) {
$scope.my.exercices[$rootScope.current_exercice].submitted = false;
}
$scope.messageClass = {"alert-danger": true};
$scope.message = data.errmsg;
$rootScope.messageClass = {"alert-danger": true};
$rootScope.message = data.errmsg;
});
$scope.my.exercices[$rootScope.current_exercice].submitted = true;
};