-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyscripts.js
302 lines (243 loc) · 10.8 KB
/
myscripts.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
initLocalSorage(); //initialize local storage with local json data file
showUserslist();
var my_data = JSON.parse(localStorage.getItem("myData"));
showUserById(my_data[0].id); // display 1st user
initDragAndDrop();
function initLocalSorage(){
if (typeof(Storage) !== "undefined") { //checks if local storage is supported
if(localStorage.getItem("isIniti")==null){ // if first use -> copy data.json to local storage under "my_data"
//set initialize to yes
localStorage.setItem("isIniti", "yes");
//init all values from file to local storage
localStorage.setItem("myData", JSON.stringify(data));
}
}
else {
alert("Sorry, your browser does not support Web Storage...");
}
}
function showUserslist() { //displays all users in nav -> checkList -> ul
var out = '';
var my_data = JSON.parse(localStorage.getItem("myData")); //extractimg most updated data
for(var i = 0; i<my_data.length; i++)//building styled div for each user data
out += '<li class="listItem" draggable="true"><div class = "listDiv" onclick="showUserById(' + my_data[i].id + ')"><img class = "userImg" src="' + my_data[i].profileImage + '"><div class = "nameAndBday"><h2 id="name"> ' + my_data[i].firstName + ' ' + my_data[i].lastName + '</h2><h3> ' + formatDate(new Date(my_data[i].birthDate)) + '</h3></div><img class = "deleteImg" src="deleteIcon.png" onclick="deleteUserById(' + my_data[i].id + ')"> </div><br></li>';
document.getElementById("checklist").innerHTML = out;
initDragAndDrop();
}
function formatDate(date) { // transform a numeric raw date to a styled format
var monthNames = [
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"
];
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
return monthNames[monthIndex] + ' ' + day + ', ' + year;
}
function showUserById(id){
//input: User Id
//displays user details in <article>
resetDetailsView(); //restes view in case the view is set for addUser
var my_data = JSON.parse(localStorage.getItem("myData"));
for(var i = 0; i<my_data.length; i++) {
if(my_data[i].id==id){
document.getElementById("userImgBig").src = my_data[i].profileImage;
document.getElementById("id").innerHTML = my_data[i].id;
document.getElementById("fName").innerHTML = my_data[i].firstName;
document.getElementById("lName").innerHTML = my_data[i].lastName;
document.getElementById("bDay").innerHTML = formatDate(new Date(my_data[i].birthDate));
return;
}
}
}
function deleteUserById(id){
var my_data = JSON.parse(localStorage.getItem("myData"));
for(var i = 0; i<my_data.length; i++){
if(my_data[i].id==id){
temp = my_data[0]; // putting found elemnt at first position in order to use shift() to delete element
my_data[0] = my_data[i];
my_data[i] = temp;
my_data.shift();
break;
}
}
localStorage.setItem("myData", JSON.stringify(my_data)); //updating local storage
showUserslist();
}
function showAddUserForm(){
//changing inner html of existing tags to create an add user form -- all of the tags return to their state when resetDetailsView() is called
document.getElementById("detailsHeadr").innerHTML = 'New User';
document.getElementById("idTxt").innerHTML = '<input type="file" id="selectedImg" class="custom-file-input" accept="image/*" onchange="onFileSelected(event)"/>';
document.getElementById("userImgBig").src = "plusImg.png";
document.getElementById("id").innerHTML = '';
document.getElementById("fName").innerHTML = '<input type="text" id="fNameBox">';
document.getElementById("lName").innerHTML = '<input type="text" id="lNameBox">';
document.getElementById("bDay").innerHTML = '<input type="date" id="bDayBox">';
document.getElementById("submit").innerHTML = '<button class = "btn" onclick="addUser()">send</button>';
document.getElementById("cancel").innerHTML = '<button class = "btn" onclick="cancel()">cancel</button>';
}
function cancel(){
//return to default view (before the add user form)
resetDetailsView();
showUserById(data[0].id);
}
function resetDetailsView(){//resets deltails view to its original state
document.getElementById("submit").innerHTML = '';
document.getElementById("cancel").innerHTML = '';
document.getElementById("detailsHeadr").innerHTML = 'User Details';
document.getElementById("idTxt").innerHTML = 'ID';
}
function addUser(){
if(validateForm()){ //checks if all input were filled
var newUser = { //creates a new user object styled json... extracting values from input boxes
id: uniqueId(),
firstName: document.getElementById("fNameBox").value,
lastName: document.getElementById("lNameBox").value,
birthDate: document.getElementById("bDayBox").value,
profileImage: document.getElementById("selectedImg").files[0].name
};
var my_data = JSON.parse(localStorage.getItem("myData"));
my_data.push(newUser);
localStorage.setItem("myData", JSON.stringify(my_data)); // updating local storage with the new user added
resetDetailsView();
showUserById(data[0].id);
showUserslist();
}
}
function validateForm(){
//check if all input fiels were filled, including an upload image
if(document.getElementById("fNameBox").value=='' || document.getElementById("lNameBox").value==0|| document.getElementById("bDayBox").value==0 || document.getElementById("selectedImg").files.length == 0 ){
alert("Please Fill ALL Details");
return false;
}
else
return true;
}
function uniqueId() {
//generates a uniqe number for id, using time...
return new Date().getUTCMilliseconds();;
}
function sortUsers(){ //sorts the user jason data structue via a custom compare function
var my_data = JSON.parse(localStorage.getItem("myData"));
my_data.sort(compare);
localStorage.setItem("myData", JSON.stringify(my_data));
showUserslist();
}
function compare(a,b) {
if (a.firstName < b.firstName)
return -1;
if (a.firstName > b.firstName)
return 1;
return 0;
}
function onFileSelected(event) { //displaing a preview of the uploaded user image
var selectedFile = event.target.files[0];
var reader = new FileReader();
var imgtag = document.getElementById("userImgBig");
imgtag.title = selectedFile.name;
reader.onload = function(event) {
imgtag.src = event.target.result;
};
reader.readAsDataURL(selectedFile);
}
/* --------------------------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------------------------- */
/* -------------------------------- Drag&Drop Functions ------------------------- */
function initDragAndDrop(){
var listItems = document.querySelectorAll('.listItem');
var dragSrcEl = null;
for (i = 0; i < listItems.length; i++) {
listItem = listItems[i];
listItem.setAttribute("order-id", i);
listItem.addEventListener('dragstart', handleDragStart, false)
listItem.addEventListener('dragenter', handleDragEnter, false)
listItem.addEventListener('dragover', handleDragOver, false)
listItem.addEventListener('dragleave', handleDragLeave, false)
listItem.addEventListener('drop', handleDrop, false)
listItem.addEventListener('dragend', handleDragEnd, false)
}
}
function handleDragStart(e) {
this.className += " dragStartClass";
dragSrcEl = this;
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/html', this.innerHTML);
e.dataTransfer.setDragClass("dataTransferClass");
}
function handleDragOver(e) {
// if (e.preventDefault) { not needed according to my question and anwers on : http://stackoverflow.com/questions/36920665/why-if-statement-with-e-preventdefault-drag-and-drop-javascript
e.preventDefault();
// }
e.dataTransfer.dropEffect = 'move'; // sets cursor
return false;
}
function handleDragEnter(e) {
// this / e.target is the current hover target.
this.classList.add('over');
}
function handleDragLeave(e) {
this.classList.remove('over'); // this / e.target is previous target element.
}
function handleDrop(e) {
var listItems = document.querySelectorAll('.listItem');
e.stopPropagation(); // stops the browser from redirecting.
dragSrcOrderId = parseInt(dragSrcEl.getAttribute("order-id"));
dragTargetOrderId = parseInt(this.getAttribute("order-id"));
var tempThis = this;
// Don't do anything if dropping the same column we're dragging.
// and
// check if only one difference and then do not execute
// && ((Math.abs(dragSrcOrderId - dragTargetOrderId)) != 1)
if (dragSrcEl != this) {
// Set the source column's HTML to the HTML of the column we dropped on.
var tempThis = this;
function makeNewOrderIds(tempThis) {
// check if up or down movement
dragSrcEl.setAttribute("order-id", dragTargetOrderId);
tempThis.setAttribute("order-id", dragTargetOrderId);
// find divs between old and new location and set new ids - different in up or down movement (if else)
if (dragSrcOrderId < dragTargetOrderId) {
for (i = dragSrcOrderId + 1; i < dragTargetOrderId; i++) {
listItems[i].setAttribute("order-id", i - 1);
// set new id src
dragSrcEl.setAttribute("order-id", dragTargetOrderId - 1);
}
} else {
for (i = dragTargetOrderId; i < dragSrcOrderId; i++) {
listItems[i].setAttribute("order-id", i + 1);
// set new id src
dragSrcEl.setAttribute("order-id", dragTargetOrderId);
}
}
};
makeNewOrderIds(tempThis);
dragSrcEl.classList.remove("dragStartClass");
reOrder(listItems);
} else {
dragSrcEl.classList.remove("dragStartClass");
return false;
}
};
function handleDragEnd(e) {
for (i = 0; i < listItems.length; i++) {
listItem = listItems[i];
listItem.classList.remove('over');
}
dragSrcEl.classList.remove("dragStartClass");
}
function reOrder(listItems) {
var tempListItems = listItems;
tempListItems = Array.prototype.slice.call(tempListItems, 0);
tempListItems.sort(function(a, b) {
return a.getAttribute("order-id") - b.getAttribute("order-id");
});
var parent = document.getElementById('checklist');
parent.innerHTML = "";
for (var i = 0, l = tempListItems.length; i < l; i++) {
parent.appendChild(tempListItems[i]);
}
};