197 lines
5.4 KiB
JavaScript
197 lines
5.4 KiB
JavaScript
angular.module("AtsebaytApp", ["ngRoute", "ngResource", "ngSanitize"])
|
|
.config(function($routeProvider, $locationProvider) {
|
|
$routeProvider
|
|
.when("/auth", {
|
|
controller: "AuthController",
|
|
templateUrl: "views/auth.html"
|
|
})
|
|
.when("/surveys", {
|
|
controller: "SurveysController",
|
|
templateUrl: "views/surveys.html"
|
|
})
|
|
.when("/surveys/:surveyId", {
|
|
controller: "SurveyController",
|
|
templateUrl: "views/survey.html"
|
|
})
|
|
.when("/users", {
|
|
controller: "UsersController",
|
|
templateUrl: "views/users.html"
|
|
})
|
|
.when("/users/:userId", {
|
|
controller: "UserController",
|
|
templateUrl: "views/user.html"
|
|
})
|
|
.when("/", {
|
|
templateUrl: "views/home.html"
|
|
});
|
|
$locationProvider.html5Mode(true);
|
|
});
|
|
|
|
angular.module("AtsebaytApp")
|
|
.factory("Survey", function($resource) {
|
|
return $resource("/api/surveys/:surveyId", { surveyId: '@id' }, {
|
|
'update': {method: 'PUT'},
|
|
})
|
|
})
|
|
.factory("SurveyQuest", function($resource) {
|
|
return $resource("/api/surveys/:surveyId/questions/:questId", { surveyId: '@id', questId: '@id' }, {
|
|
'update': {method: 'PUT'},
|
|
})
|
|
})
|
|
.factory("User", function($resource) {
|
|
return $resource("/api/users/:userId", { userId: '@id' }, {
|
|
'update': {method: 'PUT'},
|
|
})
|
|
});
|
|
|
|
angular.module("AtsebaytApp")
|
|
.run(function($rootScope, $interval, $http) {
|
|
$rootScope.checkLoginState = function() {
|
|
$http({
|
|
method: 'GET',
|
|
url: "api/auth",
|
|
}).then(function(response) {
|
|
$rootScope.isLogged = response.data;
|
|
$rootScope.user = response.data;
|
|
}, function(response) {
|
|
$rootScope.isLogged = false;
|
|
});
|
|
};
|
|
$rootScope.checkLoginState();
|
|
$interval($rootScope.checkLoginState, 20000);
|
|
|
|
$rootScope.switchAdminMode = function() {
|
|
var tmp = $rootScope.user.is_admin
|
|
$rootScope.user.is_admin = $rootScope.user.was_admin || false;
|
|
$rootScope.user.was_admin = tmp;
|
|
}
|
|
|
|
$rootScope.disconnectCurrentUser = function() {
|
|
$http({
|
|
method: 'POST',
|
|
url: "api/auth/logout"
|
|
}).then(function(response) {
|
|
$rootScope.isLogged = false;
|
|
$rootScope.user = null;
|
|
});
|
|
}
|
|
})
|
|
|
|
.controller("AuthController", function($scope, $rootScope, $http, $location) {
|
|
$scope.auth = {
|
|
"username": "",
|
|
"password": "",
|
|
};
|
|
|
|
$scope.logmein = function() {
|
|
$scope.pleaseWait = true;
|
|
$http({
|
|
method: 'POST',
|
|
url: "api/auth",
|
|
data: $scope.auth
|
|
}).then(function(response) {
|
|
$scope.pleaseWait = false;
|
|
$rootScope.checkLoginState();
|
|
$location.url("/");
|
|
}, function(response) {
|
|
$scope.pleaseWait = false;
|
|
if (response.data && response.data.errmsg)
|
|
alert(response.data.errmsg);
|
|
});
|
|
}
|
|
})
|
|
|
|
.controller("SurveysController", function($scope, Survey, $location) {
|
|
$scope.now = Date.now();
|
|
$scope.surveys = Survey.query();
|
|
$scope.surveys.$promise.then(function(data) {
|
|
data.forEach(function(d,k) {
|
|
data[k].start_availability = Date.parse(data[k].start_availability)
|
|
data[k].end_availability = Date.parse(data[k].end_availability)
|
|
})
|
|
})
|
|
|
|
$scope.show = function(id) {
|
|
$location.url("surveys/" + id);
|
|
};
|
|
})
|
|
|
|
.controller("SurveyController", function($scope, Survey, $routeParams, $location) {
|
|
$scope.now = Date.now();
|
|
$scope.survey = Survey.get({ surveyId: $routeParams.surveyId });
|
|
$scope.survey.$promise.then(function(survey) {
|
|
survey.start_availability = Date.parse(survey.start_availability)
|
|
survey.end_availability = Date.parse(survey.end_availability)
|
|
})
|
|
|
|
$scope.saveSurvey = function() {
|
|
if (this.survey.id) {
|
|
this.survey.$update(function(v) {
|
|
$scope.survey = Survey.get({ surveyId: $routeParams.surveyId });
|
|
});
|
|
} else {
|
|
this.survey.$save(function() {
|
|
$location.url("surveys/" + $scope.survey.id);
|
|
});
|
|
}
|
|
}
|
|
|
|
$scope.editSurvey = function() {
|
|
this.survey.edit = true;
|
|
}
|
|
|
|
$scope.deleteSurvey = function() {
|
|
this.survey.$remove(function() { $location.url("/surveys/");});
|
|
}
|
|
})
|
|
|
|
.controller("QuestionsController", function($scope, SurveyQuest, $http, $location) {
|
|
$scope.questions = SurveyQuest.query({ surveyId: $scope.survey.id });
|
|
|
|
$scope.submitAnswers = function() {
|
|
$scope.submitInProgress = true;
|
|
var res = [];
|
|
$scope.questions.forEach(function(q) {
|
|
res.push({"id_question": q.id, "value": q.value})
|
|
});
|
|
console.log(res)
|
|
$http({
|
|
url: "/api/surveys/" + $scope.survey.id,
|
|
data: res,
|
|
method: "POST"
|
|
}).then(function(response) {
|
|
$scope.submitInProgress = false;
|
|
}, function(response) {
|
|
$scope.submitInProgress = false;
|
|
alert("Une erreur s'est produite durant l'envoie de vos réponses : " + response.data.errmsg + "\nVeuillez réessayer dans quelques instants.");
|
|
});
|
|
}
|
|
|
|
$scope.saveQuestion = function() {
|
|
this.question.edit = false;
|
|
if (this.question.id) {
|
|
this.question.$update({ surveyId: $scope.survey.id });
|
|
} else {
|
|
this.question.$save({ surveyId: $scope.survey.id });
|
|
}
|
|
}
|
|
|
|
$scope.addQuestion = function() {
|
|
$scope.questions.push(new SurveyQuest({edit:true, kind: 'text'}))
|
|
}
|
|
|
|
$scope.editQuestion = function() {
|
|
this.question.edit = true;
|
|
}
|
|
|
|
$scope.deleteQuestion = function() {
|
|
if (this.question.id) {
|
|
this.question.$remove(function() {
|
|
$scope.questions = SurveyQuest.query({ surveyId: $scope.survey.id });
|
|
});
|
|
} else {
|
|
$scope.questions.splice($scope.questions.indexOf(this.question), 1);
|
|
}
|
|
}
|
|
})
|