server/frontend/static/js/app.js

362 lines
10 KiB
JavaScript
Raw Normal View History

2016-01-23 11:29:19 +00:00
angular.module("FICApp", ["ngRoute", "ngSanitize"])
2016-01-16 21:40:59 +00:00
.config(function($routeProvider, $locationProvider) {
$routeProvider
.when("/edit", {
controller: "MyTeamController",
templateUrl: "views/team-edit.html"
})
.when("/rank", {
2016-01-21 04:00:15 +00:00
controller: "RankController",
templateUrl: "views/rank.html"
2016-01-16 21:40:59 +00:00
})
.when("/:theme", {
2016-01-21 00:38:43 +00:00
controller: "ExerciceController",
2016-01-16 21:40:59 +00:00
templateUrl: "views/theme.html"
})
.when("/:theme/:exercice", {
controller: "ExerciceController",
templateUrl: "views/theme.html"
})
2016-01-21 00:38:43 +00:00
.when("/", {
2016-01-16 21:40:59 +00:00
controller: "HomeController",
templateUrl: "views/home.html"
2016-01-21 00:38:43 +00:00
})
.otherwise({
redirectTo: "/"
2016-01-16 21:40:59 +00:00
});
$locationProvider.html5Mode(true);
})
2016-01-23 11:29:19 +00:00
.run(function($rootScope, $timeout) {
2016-01-21 00:38:43 +00:00
$rootScope.current_theme = 0;
$rootScope.current_exercice = 0;
2016-01-23 11:29:19 +00:00
$rootScope.time = {};
function updTime() {
2016-01-25 02:09:22 +00:00
$timeout.cancel($rootScope.cbm);
$rootScope.cbm = $timeout(updTime, 1000);
2016-01-23 11:29:19 +00:00
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.start = time.st * 1000;
2016-01-24 14:22:53 +00:00
$rootScope.time.duration = time.du;
2016-01-23 11:29:19 +00:00
$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();
2016-01-16 21:40:59 +00:00
});
2016-01-24 13:32:46 +00:00
String.prototype.capitalize = function() {
return this
.toLowerCase()
.replace(
/(^|\s)([a-z])/g,
function(m,p1,p2) { return p1+p2.toUpperCase(); }
);
}
2016-01-16 21:40:59 +00:00
angular.module("FICApp")
2016-01-24 13:32:46 +00:00
.filter("capitalize", function() {
return function(input) {
return input.capitalize();
}
})
2016-01-23 11:29:19 +00:00
.filter("time", function() {
return function(input) {
if (input == undefined) {
return "--";
} else if (input >= 10) {
return input;
} else {
return "0" + input;
}
}
})
2016-12-04 18:13:44 +00:00
.filter("size", function() {
var units = [
"o",
"kio",
"Mio",
"Gio",
"Tio",
"Pio",
"Eio",
"Zio",
"Yio",
]
return function(input) {
var res = input;
var unit = 0;
while (res > 1024) {
unit += 1;
res = res / 1024;
}
return (Math.round(res * 100) / 100) + " " + units[unit];
}
})
2016-10-13 18:16:42 +00:00
.controller("DataController", function($sce, $scope, $http, $rootScope, $timeout) {
2016-01-21 00:38:43 +00:00
var actMenu = function() {
if ($scope.my && $scope.themes) {
angular.forEach($scope.themes, function(theme, key) {
$scope.themes[key].exercice_solved = 0;
angular.forEach(theme.exercices, function(exercice, k) {
if ($scope.my.exercices[k] && $scope.my.exercices[k].solved) {
$scope.themes[key].exercice_solved++;
}
});
});
}
}
2016-01-23 11:29:19 +00:00
$rootScope.refresh = function(justMy) {
if (!justMy) {
2016-01-25 02:09:22 +00:00
$timeout.cancel($scope.cbr);
$scope.cbr = $timeout($rootScope.refresh, 42000);
2016-01-23 11:29:19 +00:00
$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;
2016-01-24 14:22:53 +00:00
$scope.max_gain = 0;
2016-01-23 11:29:19 +00:00
angular.forEach(themes, function(theme, key) {
this[key].exercice_count = Object.keys(theme.exercices).length;
2016-01-24 14:22:53 +00:00
this[key].gain = 0;
angular.forEach(theme.exercices, function(ex, k) {
this.gain += ex.gain;
}, theme);
$scope.max_gain += theme.gain;
2016-01-23 11:29:19 +00:00
}, themes);
actMenu();
});
$http.get("/teams.json").success(function(teams) {
$scope.teams_count = Object.keys(teams).length
$scope.teams = teams;
2016-01-21 00:38:43 +00:00
2016-01-23 11:29:19 +00:00
$scope.rank = [];
angular.forEach($scope.teams, function(team, tid) {
team.id = tid;
this.push(team);
}, $scope.rank);
});
}
2016-01-21 00:38:43 +00:00
$http.get("/my.json").success(function(my) {
$scope.my = my;
2016-10-13 18:16:42 +00:00
angular.forEach($scope.my.exercices, function(exercice, eid) {
if (exercice.video_uri) {
exercice.video_uri = $sce.trustAsResourceUrl(exercice.video_uri);
}
});
2016-01-21 00:38:43 +00:00
actMenu();
});
console.log("refresh!");
}
2016-01-23 11:29:19 +00:00
$rootScope.refresh();
2016-01-21 00:38:43 +00:00
})
.controller("ExerciceController", function($scope, $routeParams, $http, $rootScope) {
2016-01-16 21:40:59 +00:00
$rootScope.current_theme = $routeParams.theme;
2016-01-21 00:38:43 +00:00
if ($routeParams.exercice) {
$rootScope.current_exercice = $routeParams.exercice;
} else {
if ($scope.themes && $scope.my && $scope.themes[$scope.current_theme]) {
var exos = Object.keys($scope.themes[$scope.current_theme].exercices);
var i = 0;
2016-01-24 13:32:46 +00:00
for (; i < exos.length; i++) {
2016-01-21 00:38:43 +00:00
if (!$scope.my.exercices[exos[i]] || !$scope.my.exercices[exos[i]].solved)
break;
}
2016-01-24 13:32:46 +00:00
if (i < exos.length) {
$rootScope.current_exercice = exos[i];
} else {
$rootScope.current_exercice = exos[0];
}
2016-01-21 00:38:43 +00:00
} else {
$rootScope.current_exercice = 0;
}
}
2016-01-23 11:29:19 +00:00
})
.controller("SubmissionController", function($scope, $http, $rootScope, $timeout) {
2016-12-04 18:08:46 +00:00
$scope.flags = [];
2016-01-24 13:32:46 +00:00
$rootScope.sberr = "";
2016-01-23 11:29:19 +00:00
var waitMy = function() {
if (!$scope.my || !$scope.my.exercices || !$scope.my.exercices[$rootScope.current_exercice]) {
2016-01-25 02:09:22 +00:00
$timeout.cancel($scope.cbs);
$scope.cbs = $timeout(waitMy, 420);
2016-01-23 11:29:19 +00:00
} else {
2016-12-04 18:08:46 +00:00
$scope.flags = [];
2016-01-23 11:29:19 +00:00
angular.forEach($scope.my.exercices[$rootScope.current_exercice].keys, function(key,kid) {
2016-12-04 18:08:46 +00:00
var o = {
2016-01-23 11:29:19 +00:00
id: kid,
name: key,
value: ""
2016-12-04 18:08:46 +00:00
};
if ($scope.my.exercices[$rootScope.current_exercice].solved_matrix != null)
o.found = $scope.my.exercices[$rootScope.current_exercice].solved_matrix[kid];
this.push(o);
2016-01-23 11:29:19 +00:00
}, $scope.flags);
2016-01-21 00:38:43 +00:00
}
2016-01-23 11:29:19 +00:00
}
waitMy();
$scope.ssubmit = function() {
var flgs = {}
angular.forEach($scope.flags, function(flag,kid) {
flgs[flag.name] = flag.value;
});
2016-01-21 00:38:43 +00:00
$http({
url: "/submit/" + $rootScope.current_exercice,
method: "POST",
2016-01-23 11:29:19 +00:00
data: flgs
2016-01-21 00:38:43 +00:00
}).success(function(data, status, header, config) {
2016-01-24 13:32:46 +00:00
$rootScope.messageClass = {"text-success": true};
2016-01-23 11:29:19 +00:00
$rootScope.message = data.errmsg;
2016-01-24 13:32:46 +00:00
$rootScope.sberr = "";
angular.forEach($scope.flags, function(flag,kid) {
flag.value = "";
});
2016-01-21 00:38:43 +00:00
2016-01-23 11:29:19 +00:00
var checkDiff = function() {
2016-01-21 00:38:43 +00:00
$http.get("/my.json").success(function(my) {
if ($scope.my.exercices[$rootScope.current_exercice].solved_time != my.exercices[$rootScope.current_exercice].solved_time) {
$rootScope.refresh();
2016-12-08 11:02:06 +00:00
waitMy();
2016-01-21 00:38:43 +00:00
} else {
2016-01-25 02:09:22 +00:00
$timeout.cancel($scope.cbd);
$scope.cbd = $timeout(checkDiff, 750);
2016-01-21 00:38:43 +00:00
}
});
};
checkDiff();
2016-01-21 00:38:43 +00:00
}).error(function(data, status, header, config) {
if (status >= 500) {
$scope.my.exercices[$rootScope.current_exercice].submitted = false;
}
2016-01-24 13:32:46 +00:00
$rootScope.messageClass = {"text-danger": true};
2016-01-23 11:29:19 +00:00
$rootScope.message = data.errmsg;
2016-01-24 13:32:46 +00:00
if (status != 402) {
$rootScope.sberr = "Une erreur est survenue lors de l'envoi. Veuillez réessayer dans quelques instants.";
}
2016-01-21 00:38:43 +00:00
});
$scope.my.exercices[$rootScope.current_exercice].submitted = true;
};
2016-01-16 21:40:59 +00:00
})
2016-01-24 13:23:04 +00:00
.controller("MyTeamController", function($scope, $http, $rootScope, $timeout) {
2016-01-16 21:40:59 +00:00
$rootScope.current_theme = 0;
$rootScope.current_exercice = 0;
2016-01-24 13:23:04 +00:00
if ($scope.my) {
$rootScope.title = $scope.my.name;
$rootScope.authors = $scope.my.members.map(function (cur) {
return cur.firstname.capitalize() + " " + cur.lastname.capitalize();
}).join(", ");
}
$scope.newName = "";
$rootScope.message = "";
$rootScope.sberr = "";
$scope.tsubmit = function() {
$rootScope.sberr = "";
if ($scope.newName.length < 1) {
$rootScope.messageClass = {"text-danger": true};
$rootScope.sberr = "Nom d'équipe invalide: pas d'entrée.";
return false;
}
else if ($scope.newName.length > 32) {
$rootScope.messageClass = {"text-danger": true};
$rootScope.sberr = "Nom d'équipe invalide: pas plus de 32 caractères.";
return false;
}
else if (!$scope.newName.match(/^[A-Za-z0-9 àéèêëîïôùûü_-]+$/)) {
$rootScope.messageClass = {"text-danger": true};
$rootScope.sberr = "Nom d'équipe invalide: seuls les caractères alpha-numériques sont autorisés.";
return false;
}
$http({
url: "/submit/name",
method: "POST",
data: {newName: $scope.newName}
}).success(function(data, status, header, config) {
$rootScope.messageClass = {"text-success": true};
$rootScope.message = data.errmsg;
var checkDiff = function() {
$http.get("/my.json").success(function(my) {
console.log(my.name);
if ($scope.my.name != my.name) {
$scope.newName = "";
$rootScope.message = "";
$rootScope.refresh();
} else {
2016-01-25 02:09:22 +00:00
$timeout.cancel($scope.cbt);
$scope.cbt = $timeout(checkDiff, 750);
2016-01-24 13:23:04 +00:00
}
});
};
checkDiff();
}).error(function(data, status, header, config) {
$rootScope.messageClass = {"text-danger": true};
$rootScope.message = data.errmsg;
if (status != 402) {
$rootScope.sberr = "Une erreur est survenue lors de l'envoi. Veuillez réessayer dans quelques instants.";
}
});
};
2016-01-16 21:40:59 +00:00
})
2016-01-21 00:38:43 +00:00
.controller("RankController", function($scope, $rootScope) {
$rootScope.current_theme = 0;
$rootScope.current_exercice = 0;
$rootScope.title = "Classement général";
2016-01-24 13:32:46 +00:00
$rootScope.authors = "";
2016-01-21 00:38:43 +00:00
$scope.fields = ["rank", "name", "score"];
$scope.rankOrder = "rank";
$scope.reverse = false;
$scope.order = function(fld) {
if ($scope.rankOrder == fld) {
$scope.reverse = !$scope.reverse;
} else {
$scope.rankOrder = fld;
$scope.reverse = false;
}
};
})
2016-01-16 21:40:59 +00:00
.controller("HomeController", function($scope, $rootScope) {
$rootScope.current_theme = 0;
$rootScope.current_exercice = 0;
$rootScope.title = "";
2016-01-24 13:32:46 +00:00
$rootScope.authors = "";
2016-01-16 21:40:59 +00:00
});
2016-01-21 00:38:43 +00:00
function sready() {
if ($("#solution").val().length) {
$("#sbmt").removeClass("disabled");
} else {
$("#sbmt").addClass("disabled");
}
};