server/frontend/static/js/app.js

161 lines
4.5 KiB
JavaScript
Raw Normal View History

2016-01-16 21:40:59 +00:00
angular.module("FICApp", ["ngRoute"])
.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-21 00:38:43 +00:00
.run(function($rootScope) {
$rootScope.current_theme = 0;
$rootScope.current_exercice = 0;
2016-01-16 21:40:59 +00:00
});
angular.module("FICApp")
2016-01-21 00:38:43 +00:00
.controller("DataController", function($scope, $http, $rootScope) {
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++;
}
});
});
}
}
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;
$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);
})
.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;
for (; i < exos.length - 1; i++) {
if (!$scope.my.exercices[exos[i]] || !$scope.my.exercices[exos[i]].solved)
break;
}
$rootScope.current_exercice = exos[i];
} else {
$rootScope.current_exercice = 0;
}
}
$scope.ssubmit = function() {
if (!$("#solution").val().length) {
return false
}
$http({
url: "/submit/" + $rootScope.current_exercice,
method: "POST",
data: $("#solution").val()
}).success(function(data, status, header, config) {
$scope.messageClass = {"alert-success": true};
$scope.message = data.errmsg;
$scope.refresh = 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);
}
});
};
$scope.refresh()
}).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;
});
$scope.my.exercices[$rootScope.current_exercice].submitted = true;
};
2016-01-16 21:40:59 +00:00
})
.controller("MyTeamController", function($scope, $rootScope) {
$rootScope.current_theme = 0;
$rootScope.current_exercice = 0;
$rootScope.title = "Edit team";
})
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";
$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-21 00:38:43 +00:00
function sready() {
if ($("#solution").val().length) {
$("#sbmt").removeClass("disabled");
} else {
$("#sbmt").addClass("disabled");
}
};