This repository has been archived by the owner on Apr 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
todoListCtrl.js
257 lines (220 loc) · 8.56 KB
/
todoListCtrl.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
"use strict";
(function () {
// The HTML for this View
var viewHTML;
var scope = {
scopes: [window.msalConfig.auth.clientId]
};
// Calls the TodoList Web API with an HTTP Bearer access request, and update data
function getTodoList(accessToken, dataContainer, loading) {
// Get TodoList Data
$.ajax({
type: "GET",
url: "/api/TodoList",
headers: {
'Authorization': 'Bearer ' + accessToken.accessToken
},
}).done(function (data) {
var $html = $(viewHTML);
var $template = $html.find(".data-container");
// For Each Todo Item Returned, Append a Table Row
var output = data.reduce(function (rows, todoItem, index, todos) {
var $entry = $template;
var $description = $entry.find(".view-data-description").html(todoItem.Description);
$entry.find(".data-template").attr('data-todo-id', todoItem.ID);
return rows + $entry.html();
}, '');
// Update the UI
loading.hide();
dataContainer.html(output);
}).fail(function (jqXHR, textStatus) {
printErrorMessage('Error getting todo list data')
}).always(function () {
// Register Handlers for Buttons in Data Table
registerDataClickHandlers();
});
}
// Calls the TodoList Web API with an HTTP Bearer access request, and deletes a todo item
function deleteTodoItem(accessToken, todoId) {
// Delete the Todo
$.ajax({
type: "DELETE",
url: "/api/TodoList/" + todoId,
headers: {
'Authorization': 'Bearer ' + accessToken.accessToken
},
}).done(function () {
console.log('DELETE success.');
}).fail(function () {
console.log('Fail on new Todo DELETE');
printErrorMessage('Error deleting todo item.')
}).always(function () {
refreshViewData();
});
}
// Calls the TodoList Web API with an HTTP Bearer acess request, and saves a todo item
function saveTodoItem(accessToken, todoId, description) {
// Update Todo Item
$.ajax({
type: "PUT",
url: "/api/TodoList",
headers: {
'Authorization': 'Bearer ' + accessToken.accessToken
},
data: {
Description: description.val(),
ID: todoId,
},
}).done(function () {
console.log('PUT success.');
}).fail(function () {
console.log('Fail on todo PUT');
printErrorMessage('Error saving todo item.')
}).always(function () {
refreshViewData();
description.val('');
});
}
function refreshViewData() {
// Empty Old View Contents
var $dataContainer = $(".data-container");
$dataContainer.empty();
var $loading = $(".view-loading");
// Get the access token for the backend, and calls the Web API to refresh the todo list
clientApplication.acquireTokenSilent(scope)
.then(function (token) {
getTodoList(token, $dataContainer, $loading);
}, function (error) {
clientApplication.acquireTokenPopup(scope).then(function (token) {
getTodoList(token, $dataContainer, $loading);
}, function (error) {
printErrorMessage(error);
});
})
}
function registerDataClickHandlers() {
// Delete Button(s)
$(".view-data-delete").click(function (event) {
clearErrorMessage();
var todoId = $(event.target).parents(".data-template").attr("data-todo-id");
// Get the access token for the backend, and calls the Web API to delete the current item
clientApplication.acquireTokenSilent(scope)
.then(function (token) {
deleteTodoItem(token, todoId);
}, function (error) {
clientApplication.acquireTokenPopup(scope).then(function (token) {
deleteTodoItem(token, todoId);
}, function (error) {
printErrorMessage(error);
});
})
});
// Edit Button(s)
$(".view-data-edit").click(function (event) {
clearErrorMessage();
var $entry = $(event.target).parents(".data-template");
var $entryDescription = $entry.find(".view-data-description").hide();
var $editInput = $entry.find(".view-data-edit-input");
$editInput.val($entryDescription.text());
$editInput.show();
$entry.find(".view-data-mode-delete").hide();
$entry.find(".view-data-mode-edit").show();
});
// Cancel Button(s)
$(".view-data-cancel-edit").click(function (event) {
clearErrorMessage();
var $entry = $(event.target).parents(".data-template");
$entry.find(".view-data-description").show();
var $editInput = $entry.find(".view-data-edit-input").hide();
$editInput.val('');
$entry.find(".view-data-mode-delete").show();
$entry.find(".view-data-mode-edit").hide();
});
// Save Button(s)
$(".view-data-save").click(function (event) {
clearErrorMessage();
var $entry = $(event.target).parents(".data-template");
var todoId = $entry.attr("data-todo-id");
// Validate Todo Description
var $description = $entry.find(".view-data-edit-input");
if ($description.val().length <= 0) {
printErrorMessage('Please enter a valid Todo description');
return;
}
// Get the access token for the backend, and calls the Web API to save the current item
clientApplication.acquireTokenSilent(scope)
.then(function (token) {
saveTodoItem(token, todoId, $description);
}, function (error) {
clientApplication.acquireTokenPopup(scope).then(function (token) {
deleteTodoItem(token, todoId, $description);
}, function (error) {
printErrorMessage(error);
});
})
});
};
function postNewTodo(accesstoken, description) {
// POST a New Todo
$.ajax({
type: "POST",
url: "/api/TodoList",
headers: {
'Authorization': 'Bearer ' + accesstoken,
},
data: {
Description: description.val(),
},
}).done(function () {
console.log('POST success.');
}).fail(function () {
console.log('Fail on new Todo POST');
printErrorMessage('Error adding new todo item.');
}).always(function () {
// Refresh TodoList
description.val('');
refreshViewData();
});
}
function registerViewClickHandlers() {
// Add Button
$(".view-addTodo").click(function () {
clearErrorMessage();
// Validate Todo Description
var $description = $("#view-todoDescription");
if ($description.val().length <= 0) {
printErrorMessage('Please enter a valid Todo description');
return;
}
clientApplication.acquireTokenSilent(scope)
.then(function (token) {
postNewTodo(token, $description);
}, function (error) {
clientApplication.acquireTokenPopup(scope).then(function (token) {
deleteTodoItem(token, $description);
}, function (error) {
printErrorMessage(error);
});
})
});
};
function clearErrorMessage() {
var $errorMessage = $(".app-error");
$errorMessage.empty();
};
function printErrorMessage(mes) {
var $errorMessage = $(".app-error");
$errorMessage.html(mes);
}
// Module
window.todoListCtrl = {
requireADLogin: true,
preProcess: function (html) {
},
postProcess: function (html) {
viewHTML = html;
registerViewClickHandlers();
refreshViewData();
},
};
}());