forked from aidarbek/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
138 lines (135 loc) · 3.56 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
Comment reference:
BE - BackEnd
*/
var App = angular.module('App', ['ui.select']);
function AddUser($scope)
{
var url = ""; // we send POST request to
// register new users to this URL
// response should be in JSON
// and also contain a user ID in addition
// to general user information(except password)
if($scope.userCreation.login && $scope.userCreation.full && $scope.userCreation.mail
&& $scope.userCreation.password && $scope.userCreation.passwordConfirm && $scope.userCreation.role
|| $scope.isAdmin == true)
{
// If fields are not empty
if($scope.userCreation.password == $scope.userCreation.passwordConfirm)
{
//If password is confirmed
$scope.err = false;//There is no error
tmp = angular.copy($scope.userCreation);
tmp.editing = false;
//Next line should be commented, till the BE is available
/*
$http.post(url, tmp).success(function(data){
$scope.Users.push(tmp);
$scope.userCreation = {};
$('#userField').modal('hide');
}).error(function(data, status){
err = status + " error";
});
*/
//Delete next 4 lines, when BE available
tmp.user_id = 1;
$scope.Users.push(tmp);
$scope.userCreation = {role: []};
}
else
{
$scope.err = "Password's are not equal!";
}
}
else
{
$scope.err = "You should fill all fields!";
}
}
App.factory('Users', function () {
return [{login: "aidarbek", full: "aidarbek", mail: "[email protected]", role:['admin', 'mentor'],editing: false}];// Binds Users list between Controllers
});
App.controller('User', ['$scope','Users' , '$http', function ($scope, Users, $http) {
$scope.Users = Users;
$scope.userCreation = {role: []};
$scope.err = false;
$scope.isAdmin = true;
$scope.registring = false;
$scope.roles = ['student', 'mentor', 'admin'];
$scope.Role = function(role, index)
{
//console.log(role);
if(typeof index === 'undefined')
{
if($scope.userCreation.role.indexOf(role) > -1)
{
$scope.userCreation.role.splice(role, 1);
}
else
{
$scope.userCreation.role.push(role);
}
//console.log($scope.userCreation.role);
}
else
{
if($scope.Users[index].role.indexOf(role) > -1)
{
$scope.Users[index].role.splice($scope.Users[index].role.indexOf(role), 1);
}
else
{
$scope.Users[index].role.push(role);
}
}
}
$scope.Checked = function(role, index)
{
if($scope.Users[index].role.indexOf(role) > -1)
return "Checked";
else
return "";
}
$scope.Add = function()
{
$scope.registring = true;
}
$scope.AddToArray = function()
{
AddUser($scope);
$scope.registring = false;
}
$scope.Delete = function(index)
{
var url = "";//We send a GET request on this URL, in which we say that
//we want to delete a user with specific ID
// Should be commented till BE is not available
/*
$http.get(url).success(function(data){
$("#id-"+index).fadeOut('slow', function(){
$scope.Users.splice(index, 1);
});
});
*/
// Delete next lines when BE is available
$("#id-"+index).fadeOut('slow', function(){
$scope.Users.splice(index, 1);
});
}
$scope.Edit = function(index)
{
$scope.Users[index].editing = true;
}
$scope.Save = function(index)
{
var url = "";//We send a POST request here, which contain new information about
// user with specific ID, which is described in $scopeUsers[index]
//Should be commented till BE is not available
/*
$http.post(url, $scope.Users[index]).success(function(data){
$scope.Users[index].editing = false;
});
*/
$scope.Users[index].editing = false;
}
}]);