server/frontend/static/js/public.js

113 lines
3.3 KiB
JavaScript

angular.module("FICApp", ["ngSanitize", "ngAnimate"]);
String.prototype.capitalize = function() {
return this
.toLowerCase()
.replace(
/(^|\s)([a-z])/g,
function(m,p1,p2) { return p1+p2.toUpperCase(); }
);
}
angular.module("FICApp")
.controller("TimeController", function($scope, $rootScope, $http, $timeout) {
$scope.time = {};
var initTime = function() {
$timeout.cancel($scope.cbi);
$scope.cbi = $timeout(initTime, 10000);
$http.get("/time.json").success(function(time) {
console.log("upd time");
time.he = (new Date()).getTime();
sessionStorage.userService = angular.toJson(time);
});
};
initTime();
function updTime() {
$timeout.cancel($scope.cb);
$scope.cb = $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)) {
$scope.refresh(true);
$rootScope.startIn = 0;
}
if (time.st > 0 && time.st <= srv_cur) {
remain = time.st + time.du - srv_cur;
} else if (time.st > 0) {
$rootScope.startIn = Math.floor(time.st - srv_cur);
}
if (remain < 0) {
remain = 0;
$scope.time.end = true;
$scope.time.expired = true;
} else if (remain < 60) {
$scope.time.end = false;
$scope.time.expired = true;
} else {
$scope.time.end = false;
$scope.time.expired = false;
}
$scope.time.start = time.st * 1000;
$scope.time.duration = time.du;
$scope.time.remaining = remain;
$scope.time.hours = Math.floor(remain / 3600);
$scope.time.minutes = Math.floor((remain % 3600) / 60);
$scope.time.seconds = Math.floor(remain % 60);
}
}
updTime();
})
.controller("DataController", function($scope, $http, $rootScope, $timeout) {
$rootScope.refresh = function() {
$timeout.cancel($scope.cbd);
$scope.cbd = $timeout($rootScope.refresh, 4200);
$http.get("/demo.json").success(function(demo) {
$scope.my = demo;
});
$http.get("/events.json").success(function(evts) {
var events = {};
var now = new Date();
angular.forEach(evts, function(event, key) {
events["e" + event.id] = event;
event.since = now.getTime() - now.getTimezoneOffset() * 60000 - Date.parse(event.time);
});
$scope.events = events;
});
$http.get("/public.json").success(function(scene) {
$scope.scene = scene;
});
$http.get("/stats.json").success(function(stats) {
$scope.stats = stats;
});
$http.get("/themes.json").success(function(themes) {
$scope.themes = themes;
$scope.max_gain = 0;
angular.forEach(themes, function(theme, key) {
this[key].exercice_count = Object.keys(theme.exercices).length;
this[key].gain = 0;
angular.forEach(theme.exercices, function(ex, k) {
this.gain += ex.gain;
}, theme);
$scope.max_gain += theme.gain;
}, themes);
});
$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;
if (team.rank) {
this.push(team);
}
}, $scope.rank);
});
console.log("refresh!");
}
$rootScope.refresh();
});