-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
405 lines (325 loc) · 11.7 KB
/
main.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// SCSCSC MAIN.JS
// Check to see if there's nothing in localStorage, if nothing then ass empty array
var check = localStorage.getItem("cards");
if (!check) {
localStorage.setItem("cards", JSON.stringify([]));
}
// Random number as key for image
var key = Math.floor(Math.random() * 10000);
// PWA fundamentals -- Making a service worker..
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker
.register('/cards/sw.js')
.then(reg => console.log('Service Worker: Registered'))
.catch(err => console.log('Service Worker: Error' + err))
})
}
// Flag to acknowledge file change
var fileChanged = false;
// Angular Dynamic Binding
var CardsApp = angular.module('CardsApp', []);
// Angular file directive
CardsApp.directive('fileModel', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function () {
scope.$apply(function () {
modelSetter(scope, element[0].files[0]);
});
});
}
};
}]);
CardsApp.service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function (file) {
var fd = new FormData();
fd.append('file', file);
$http.post('/pics/add', fd, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
}).then(function mySuccess(response) {
$http({
method: "POST",
url: "/api/class/",
data: activeGrp
}).then((result) => {
// What do i do with the result ?
console.log("Successfully transacted the message ....");
}, (result) => {
alert("Some error geting messages");
})
}, function myError(response) {
console.log(response.data);
});
}
}]);
CardsApp.controller('CardsController', ($scope, $interval, $http) => {
console.log("Controller loaded !");
// Place for scope experimentation
$scope.nums = [
0,
1,
2,
3
]
// Card list
var cardList = JSON.parse(localStorage.getItem("cards"));
// Assigning 0 as default for unchecked box
for (let index = 0; index < cardList.length; index++) {
cardList[index].qopts =
[
false,
false,
false,
false
];
}
// Assigning to scope
$scope.cards = cardList;
// Initializing responses
$scope.qopts = [];
// Initializing submit button
$scope.submit = [];
// Assigning values for the responses as default false
for (let index = 0; index < cardList.length; index++) {
$scope.qopts[index] = [
false,
false,
false,
false
]
$scope.submit[index] = "Check"
}
// Assigning values for new card responses as default false
$scope.opts = [
{
"val": "",
"bool": false
},
{
"val": "",
"bool": false
}, {
"val": "",
"bool": false
}, {
"val": "",
"bool": false
}
]
// Declaring a flag to prevent click when already click is acknowledged
var disableBtn = false;
// Verifying answers
$scope.log = function (index) {
if (!disableBtn) {
disableBtn = true;
console.log('Index is ' + index);
console.log($scope.cards);
console.log($scope.cards[index].ans)
console.log($scope.cards[index].qopts)
for (let ind = 0; ind < 4; ind++) {
if ($scope.cards[index].ans[ind].bool && $scope.cards[index].qopts[ind]) {
$scope.resCol[ind] = "green";
} else if ((!$scope.cards[index].ans[ind].bool && $scope.cards[index].qopts[ind]) || ($scope.cards[index].ans[ind].bool && !$scope.cards[index].qopts[ind])) {
$scope.resCol[ind] = "red";
}
}
var tick = 0;
$interval(function resetCol() {
tick = tick + 1;
if (tick == 3) {
for (let count = 0; count < 4; count++) {
$scope.resCol[count] = "";
$scope.cards[index].qopts[count] = false;
$scope.submit[index] = "Submit";
disableBtn = false;
}
} else {
$scope.submit[index] = "Resetting in " + (3 - tick);
console.log(tick);
}
}, 1000, 3);
}
}
// Link to the new card page
$scope.addcard = function () {
console.log('Clicked !');
location.replace('/cards/add_card.html')
}
// Navigating to share
$scope.nav_share = function (index) {
localStorage.setItem('share', index);
location.replace('/cards/share.html');;
}
// Try to adjust scope for name of card
try {
$scope.share_head = JSON.parse(localStorage.getItem('cards'))[localStorage.getItem('share')].title
} catch (err) { }
// Share
$scope.share = function () {
tempCard = JSON.parse(localStorage.getItem('cards'))[localStorage.getItem('share')]
$http.post('http://192.168.0.109:1000/api/add/priv_cards',
{
"title":tempCard.title,
"pic":"/card",
"ans":tempCard.ans,
"crId":"Chin89"
}
).then((res=>{
console.log(res.data)
},(res)=>{
console.log(res.data)
}))
}
// Navigating edit card page, running the script to get the card object
$scope.nav_edit = function (index) {
localStorage.setItem('edit', index)
location.replace('/cards/edit_card.html');
}
// Setting edit item
$scope.edit_card = cardList[localStorage.getItem('edit')]
// Creating the new card as a json obj, appending to array and replacing that in the localStorage
// Image processing is frankly a pain...
// Here's the plan....
// Get file from input => using tha crazy peice of code i saw.. Store it just like that and see what happens
$scope.createCard = function () {
console.log("Creating Card...");
if (!document.getElementById('files').files[0]) {
localStorage.setItem(key, "./res/img/plce.jpg");
console.log("No image uploaded")
}
// New card object
new_card = {
"title": $scope.head,
"pic": localStorage.getItem(key),
"ans": $scope.opts
}
// Appending to the cardList array
cardList.push(new_card);
// Delete temporary image Data
localStorage.removeItem(key)
// Updating the localStorage
localStorage.setItem("cards", JSON.stringify(cardList));
// Logging error
console.log("Card added to list");
// Refresh the previous array
rerender();
}
// Rerendering the scope to accept an changes
function rerender() {
var cardList = JSON.parse(localStorage.getItem("cards"));
$scope.cards = cardList;
location.replace('./cards!.html')
}
// Creating Color for showing answer correctness
$scope.resCol = [];
// Editing card and creating new card
$scope.editCard = function () {
var edited = {};
edited.title = $scope.edit_card.title;
edited.ans = $scope.edit_card.ans;
console.log(fileChanged)
if (fileChanged) {
console.log('Change acknowledged')
$scope.edit_card.pic = localStorage.getItem(key);
}
edited.pic = $scope.edit_card.pic
fileChanged = false;
localStorage.removeItem(key);
console.log(edited);
var ind = localStorage.getItem('edit');
var tempList = JSON.parse(localStorage.getItem('cards'))
tempList[ind] = edited;
localStorage.setItem('cards', JSON.stringify(tempList));
localStorage.removeItem('edit');
rerender();
}
// Delete card functionality
$scope.deleteCard = function (index) {
console.log('Initiaiting delete for element ' + index);
var tempList = []
tempList = JSON.parse(localStorage.getItem('cards'))
tempList.splice(index, 1);
localStorage.setItem('cards', JSON.stringify(tempList));
rerender();
}
// This is the end !!
});
try {
document.getElementById('files').addEventListener('change', handleFileSelect, false);
} catch (error) {
console.log("Not in add card page.. ")
}
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
fileChanged = true;
console.log('File changed')
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function (theFile) {
return function (e) {
// Render thumbnail.
var e;
try {
// Chech and use indexeddb
// window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB ||
// window.msIndexedDB;
// window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction ||
// window.msIDBTransaction;
// window.IDBKeyRange = window.IDBKeyRange ||
// window.webkitIDBKeyRange || window.msIDBKeyRange
// if (!window.indexedDB) {
// window.alert("Your browser doesn't support a stable version of IndexedDB.")
// }
// var db;
// var request = window.indexedDB.open('test', 3)
// request.onsuccess = (e) => {
// db = request.result;
// var obje = {
// 'id':key,
// 'pic':e.target.result
// }
// var objectStore = db.createObjectStore("image", { keyPath: "id" });
// objectStore.add(obje);
// console.log('Successfully Setup db ' + JSON.stringify(db));
// }
// request.onerror = (e) => {
// console.log('Error seting up db ' + e)
// }
localStorage.setItem(key, e.target.result)
} catch (error) {
alert("Picture size exceeds storage space available... upload with lower resolution");
}
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
}
// Notification
// if ("Notification" in window) {
// let ask = Notification.requestPermission();
// ask.then(permisision => {
// if (permisision === "granted") {
// let msg = new Notification("SCSCSC",
// {
// body: "HI there",
// icon: "./res/img/pic.jpg"
// }
// );
// msg.addEventListener("click", e => {
// console.log('Click recieved from SC')
// });
// }
// });
// }