-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathable-feedbacks.js
More file actions
164 lines (156 loc) · 4.02 KB
/
able-feedbacks.js
File metadata and controls
164 lines (156 loc) · 4.02 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
(function() {
'use strict';
angular.module('able').directive('elementFeedbacks', feedbacksDirective)
function feedbacksDirective() {
var directive = {
restrict: 'A',
controller: feedbacksController,
controllerAs: 'feedbacks',
bindToController: true
}
return directive
}
function feedbacksController(auth, $http, config, $q) {
var vm = this
vm.loading = true
vm.touched = false
vm.allsent = false
vm.list = []
vm.send = send
vm.clean = clean
vm.touch = touch
vm.untouch = untouch
function touch(path) {
for (var k in vm.list) {
if (vm.list[k].path == path) {
vm.list[k].object.touched = true
vm.touched = true
}
}
}
function untouch(path) {
var touches = 0
for (var k in vm.list) {
if (vm.list[k].path == path) {
vm.list[k].object.touched = false
vm.list[k].object.float = 0
}
if (vm.list[k].object.touched) {
touches++
}
}
if (touches > 0) {
vm.touched = true
} else {
vm.touched = false
}
}
function get() {
vm.loading = true
var req_config = {
headers: {
'Authorization': auth.token
}
}
$http.get(config.api + '/users/' + auth.id + '/feedbacks', req_config).then(function successCallback(response) {
vm.list = response.data.resources
vm.loading = false
}, function errorCallback(response) {
vm.loading = false
if (response.status == 401) {
if (navigator && navigator.notification) {
navigator.notification.alert('Você precisa se logar novamente.', false, 'Able', 'Ok')
} else {
window.alert('Você precisa se logar novamente.')
}
auth.signout()
return
}
if (response.status == 403) {
if (navigator && navigator.notification) {
navigator.notification.alert(response.data.errors[0].error, false, 'Able', 'Ok')
} else {
window.alert(response.data.errors[0].error)
}
return
}
if (navigator && navigator.notification) {
navigator.notification.alert('Verifique sua conexão.', get, 'Able', 'Ok')
return
} else {
window.alert('Verifique sua conexão.')
get()
return
}
}); //end then
}
get()
function send() {
vm.touched = false;
vm.list.slice().reverse().forEach(function(item, index, object) {
if (item.object.touched == true) {
vm.sent = true;
item.done = true;
item.sending = true;
put(item).then(function(resolve) {
item.done = true;
item.sending = true;
vm.clean();
}, function(reject) {
item.done = false;
item.sending = false;
vm.touched = true;
if (response.status == 401) {
if (navigator && navigator.notification) {
navigator.notification.alert('Você precisa se logar novamente.', false, 'Able', 'Ok')
} else {
window.alert('Você precisa se logar novamente.')
}
auth.signout()
return
}
if (response.status == 403) {
if (navigator && navigator.notification) {
navigator.notification.alert(response.data.errors[0].error, false, 'Able', 'Ok')
} else {
window.alert(response.data.errors[0].error)
}
return
}
if (navigator && navigator.notification) {
navigator.notification.alert('Verifique sua conexão.', false, 'Able', 'Ok')
} else {
window.alert('Verifique sua conexão.')
}
});
} //end if
}); //end foreach
}
function clean() {
vm.list.slice().reverse().forEach(function(item, index, object) {
if (item.done == true) {
vm.list.splice(object.length - 1 - index, 1);
}
})
}
function put(feedback) {
var req_config = {
headers: {
'Authorization': auth.token
}
};
var path = feedback.path;
var payload = {
float: feedback.object.float,
open: "false"
};
return $q(function(resolve, reject) {
$http.patch(config.api + path, payload, req_config).then(function successCallback(response) {
resolve(response.data);
}, function errorCallback(response) {
reject();
});
});
}
}
})();