-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
271 lines (271 loc) · 11.8 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
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
// DATA CONTROLLER - Model
// tslint:disable-next-line: only-arrow-functions
var dataController = (function () {
var myLibrary = [];
// Book object constructor
function Book(title, author, pages, readStatus, id) {
this.title = title;
this.author = author;
this.pages = pages;
this.readStatus = readStatus;
this.id = id;
}
// Return object items that are needed for sharing between objects or accessing in browser console window
return {
addDataItem: function (title, author, pages, readStatus) {
var id;
if (myLibrary.length > 0) {
id = myLibrary[myLibrary.length - 1].id + 1;
}
else {
id = 0;
}
var newbook = new Book(title, author, pages, readStatus, id);
myLibrary.push(newbook);
},
getID: function () {
return myLibrary[myLibrary.length - 1].id;
},
deleteDataItem: function (id) {
var ids;
var index;
// tslint:disable-next-line: only-arrow-functions
ids = myLibrary.map(function (current) {
return current.id;
});
// tslint:disable-next-line: radix
index = ids.indexOf(parseInt(id));
if (index !== -1) {
myLibrary.splice(index, 1);
}
},
changeReadStatus: function (evt) {
var ItemID;
ItemID = evt.target.parentNode.parentNode.id;
var ids;
var index;
// tslint:disable-next-line: only-arrow-functions
ids = myLibrary.map(function (current) {
return current.id;
});
// tslint:disable-next-line: radix
index = ids.indexOf(parseInt(ItemID));
if (index !== -1) {
myLibrary[index].readStatus === "read" ? myLibrary[index].readStatus = "not read" :
myLibrary[index].readStatus = "read";
}
this.saveToLocalStorage();
},
saveToLocalStorage: function () {
if (storageAvailable("localStorage")) {
var localStorageArr_1 = [];
if (myLibrary.length >= 1) {
myLibrary.forEach(function (book) {
localStorageArr_1.push(book);
localStorage.setItem("myLibrary", JSON.stringify(localStorageArr_1));
});
}
else {
// tslint:disable-next-line: no-shadowed-variable
var localStorageArr_2 = [];
localStorage.setItem("myLibrary", JSON.stringify(localStorageArr_2));
}
}
},
testing: function () {
// tslint:disable-next-line: no-console
console.log(myLibrary);
}
};
})();
// UI CONTROLLER - View
// tslint:disable-next-line: only-arrow-functions
var UIController = (function () {
var DOMstrings = {
addBtn: "#addBtn",
authorTextElement: ".author",
checkbox: "#checkbox",
form: ".form",
formSubmit: "#submitBtn",
grid: ".grid",
pagesTextElement: ".pages",
readStatusTextElement: ".readStatus",
table: ".table",
tableBody: ".table-body",
titleTextElement: ".title",
toggleIcon: ".toggleIcon",
trashBtn: "#trashBtn"
};
// Return object items that are needed for sharing between objects or accessing in browser console window
return {
getInput: function () {
return {
author: document.querySelector(DOMstrings.authorTextElement).value,
pages: document.querySelector(DOMstrings.pagesTextElement).value,
readStatus: document.querySelector(DOMstrings.readStatusTextElement).value,
title: document.querySelector(DOMstrings.titleTextElement).value
};
},
addViewItem: function (title, author, pages, readStatus, id) {
var checked;
readStatus === "read" ? checked = "checked" : checked = "";
document.querySelector(DOMstrings.tableBody).insertAdjacentHTML("afterbegin", "\n <tr id=\"" + id + "\">\n <td data-Title\">" + title + "</td>\n <td data-Author>" + author + "</td>\n <td data-Pages>" + pages + "</td>\n <td>\n <input type=\"checkbox\" class=\"ui checkbox\" id=\"checkbox\" " + checked + "></input>\n </td>\n <td>\n <div class=\"ui icon inverted red button\" id=\"trashBtn\">\n <i class=\"trash icon\"></i>\n </div>\n </td>\n </tr>\n ");
},
deleteViewItem: function (id) {
var el = document.getElementById(id);
el.parentNode.removeChild(el);
},
toggleDisplay: function () {
var form = document.querySelector(DOMstrings.form);
var table = document.querySelector(DOMstrings.table);
var toggleIcon = document.querySelector(DOMstrings.toggleIcon);
var addBtn = document.querySelector(DOMstrings.addBtn);
form.className.split(" ")[2] === "hide" ? form.className = "ui form show" : form.className = "ui form hide";
form.className.split(" ")[2] === "hide" ? table.className = "ui celled table" : table.className = "ui celled table hide";
// tslint:disable-next-line: max-line-length
form.className.split(" ")[2] === "hide" ? toggleIcon.className = "add icon toggleIcon" : toggleIcon.className = "close icon toggleIcon";
// tslint:disable-next-line: max-line-length
form.className.split(" ")[2] === "hide" ? addBtn.className = "ui icon inverted green button" : addBtn.className = "ui icon inverted red button";
},
getDOMstrings: function () {
return DOMstrings;
},
testing: function () {
// tslint:disable-next-line: no-console
return console.log(document.querySelector(DOMstrings.readStatusTextElement).value);
}
};
})();
// GLOBAL APP CONTROLLER - Controller
// tslint:disable-next-line: only-arrow-functions
var controller = (function (dataCtrl, UICtrl) {
// 1. Access DOM Strings
var DOM = UICtrl.getDOMstrings();
// 2. Setup toggle and form submit global event listeners
function setupEventListeners() {
document.querySelector(DOM.addBtn).addEventListener("click", function () {
UICtrl.toggleDisplay();
});
document.querySelector(DOM.formSubmit).addEventListener("click", function (e) {
ctrlAddItem(e);
});
}
function render() {
// 1. Check for local storage - feature detection
if (storageAvailable("localStorage")) {
// 2. Check if local storage object already exists
if (localStorage.getItem("myLibrary")) {
// 3. Parse JSON values from local storage object
var libraryData = JSON.parse(localStorage.getItem("myLibrary"));
// 4. Get DOM strings
// tslint:disable-next-line: no-shadowed-variable
var DOM_1 = UICtrl.getDOMstrings();
// 5. For each book in local storage object, add to local array variable
libraryData.forEach(function (book) {
dataCtrl.addDataItem(book.title, book.author, book.pages, book.readStatus);
});
// 6. For each book in local storage object, render view item in table
libraryData.forEach(function (book) {
UICtrl.addViewItem(book.title, book.author, book.pages, book.readStatus, book.id);
});
// 7. For each rendered local storage object, add delete function event listeners
document.querySelectorAll(DOM_1.trashBtn).forEach(function (book) {
book.addEventListener("click", function (e) {
ctrlDeleteItem(e);
});
});
// 8. For each rendered local storage object, add changeReadStatus function event listeners
document.querySelectorAll(DOM_1.checkbox).forEach(function (book) {
book.addEventListener("change", function (e) {
dataCtrl.changeReadStatus(e);
});
});
}
}
}
function ctrlAddItem(evt) {
// 1. Access DOM Strings
// tslint:disable-next-line: no-shadowed-variable
var DOM = UICtrl.getDOMstrings();
// 2.. Access DOM Strings values for form inputs
var input = UICtrl.getInput();
// 3. Validate Form Input and Transform Data
if (input.title === "" || input.author === "" || input.pages === "") {
return;
}
else {
evt.preventDefault();
// tslint:disable-next-line: max-line-length
document.querySelector(DOM.readStatusTextElement).checked === true ? input.readStatus = "read" : input.readStatus = "not read";
// 4. Add item to Data
dataCtrl.addDataItem(input.title, input.author, input.pages, input.readStatus);
// 5. Add item to View
UICtrl.addViewItem(input.title, input.author, input.pages, input.readStatus, dataCtrl.getID());
// 6. Save change to LocalStorage
dataCtrl.saveToLocalStorage();
// 7. Close Form Window
UICtrl.toggleDisplay();
// 8. Add Event Listener to Item Delete Button
document.querySelector(DOM.trashBtn).addEventListener("click", function (e) {
ctrlDeleteItem(e);
});
// 9. Add Event Listener to Item Checkbox Button
document.querySelector(DOM.checkbox).addEventListener("change", function (e) {
dataCtrl.changeReadStatus(e);
});
}
}
function ctrlDeleteItem(evt) {
var ItemID;
// 1. Assign Item ID to table row item id
ItemID = evt.target.parentNode.parentNode.parentNode.id;
if (ItemID) {
// 2. Remove item from data
dataCtrl.deleteDataItem(ItemID);
// 3. Remove item from view
UICtrl.deleteViewItem(ItemID);
// 4. Save changes to local storage
dataCtrl.saveToLocalStorage();
}
}
// Return object items that are needed for sharing between objects or accessing in browser console window
return {
init: function () {
// tslint:disable-next-line: no-console
console.log("Application has started.");
setupEventListeners();
render();
},
testing: function () {
var DOMvalues = UICtrl.getInput();
// tslint:disable-next-line: no-console
console.log(DOMvalues);
}
};
})(dataController, UIController);
controller.init();
// Global variable - feature detection for local storage
function storageAvailable(type) {
var storage;
try {
storage = window[type];
var x = "__storage_test__";
storage.setItem(x, x);
storage.removeItem(x);
return true;
}
catch (e) {
return e instanceof DOMException && (
// everything except Firefox
e.code === 22 ||
// Firefox
e.code === 1014 ||
// test name field too, because code might not be present
// everything except Firefox
e.name === "QuotaExceededError" ||
// Firefox
e.name === "NS_ERROR_DOM_QUOTA_REACHED") &&
// acknowledge QuotaExceededError only if there's something already stored
(storage && storage.length !== 0);
}
}