-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
232 lines (191 loc) · 7.04 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
class Card {
constructor(name) {
this.name = name;
this.links = [];
this.notCreated = true;
}
}
let model = {
cards: [],
addLinkCaller: null
};
let controller = {
init: function() {
model.cards = JSON.parse(localStorage.getItem('cards'));
if (model.cards === null) {
model.cards = [];
}
else {
model.cards.map(function(card) {
card.notCreated = true;
});
}
headerView.init();
contentView.init();
addCardView.init();
addLinkView.init();
shortcuts.init();
},
getCards: function() {
return model.cards
},
setAddLinkCaller: function(obj) {
model.addLinkCaller = obj;
},
getAddLinkCaller: function() {
return model.addLinkCaller;
},
getContentElem: function() {
return contentView.content_elem;
},
clearContentElem: function() {
contentView.content_elem.innerHTML = '';
},
getAddLinkModalElem: function() {
return addLinkView.addLinkModal_elem;
},
};
let headerView = {
init: function() {
this.time_elem = document.querySelector('.time');
this.date_elem = document.querySelector('.date');
this.render();
},
render: function() {
this.time_elem.textContent = new Date().toLocaleTimeString();
this.date_elem.textContent = new Date().toLocaleDateString();
setInterval((function(time_elem, date_elem) {
return function() {
time_elem.textContent = new Date().toLocaleTimeString();date_elem.textContent = new Date().toLocaleDateString();
}
})(this.time_elem, this.date_elem), 1000);
}
};
let cardView = {
init: function(obj) {
if (obj.notCreated) {
obj.notCreated = false;
let cardOuter_elem = document.createElement('div');
cardOuter_elem.className = 'card-outer';
obj.cardOuter_elem = cardOuter_elem;
let cardHeader_elem = document.createElement('div');
cardHeader_elem.className = 'card-header';
obj.cardHeader_elem = cardHeader_elem;
let cardName_elem = document.createElement('p');
cardName_elem.className = 'card-name'
cardName_elem.textContent = obj.name;
obj.cardName_elem = cardName_elem;
let addLink_elem = document.createElement('button');
addLink_elem.className = 'add-link-btn fas fa-plus';
addLink_elem.addEventListener('click', function() {
controller.getAddLinkModalElem().classList.toggle('show');
controller.setAddLinkCaller(obj);
if (addCardView.addCardModal_elem.classList.contains('show')) {
addCardView.addCardModal_elem.classList.toggle('show');
}
});
obj.addLink_elem = addLink_elem;
let cardInner_elem = document.createElement('div');
cardInner_elem.className = 'card-inner';
obj.cardInner_elem = cardInner_elem;
}
},
render: function(obj) {
obj.cardHeader_elem.appendChild(obj.cardName_elem);
obj.cardHeader_elem.appendChild(obj.addLink_elem);
obj.cardInner_elem.innerHTML = '';
for (let i = 0; i < obj.links.length; i++) {
obj.cardInner_elem.appendChild(this.createLink(obj.links[i]));
}
obj.cardOuter_elem.appendChild(obj.cardHeader_elem);
obj.cardOuter_elem.appendChild(obj.cardInner_elem);
controller.getContentElem().appendChild(obj.cardOuter_elem);
},
createLink: function(link) {
let link_elem = document.createElement('a');
link_elem.textContent = link.name;
link_elem.href = link.url;
return link_elem;
},
addLink: function(name, url, obj) {
obj.links.push({name: name, url: url});
}
};
let contentView = {
init: function() {
this.content_elem = document.querySelector('.content');
this.render();
},
render: function() {
for (let i = 0; i < controller.getCards().length; i++) {
cardView.init(controller.getCards()[i])
cardView.render(controller.getCards()[i])
}
}
};
let addCardView = {
init: function() {
this.addCardModal_elem = document.querySelector('.add-card-modal');
this.addCard_elem = document.querySelector('.add-card-btn');
this.nameIn_elem = document.querySelector('.card-name-in');
this.save_elem = document.querySelector('.card-save-btn');
this.render();
},
render: function() {
this.addCardModal_elem.classList.toggle('show');
this.addCard_elem.addEventListener('click', (function(modal) {
return function() {
modal.classList.toggle('show');
if (addLinkView.addLinkModal_elem.classList.contains('show')) {
addLinkView.addLinkModal_elem.classList.toggle('show')
}
}
})(this.addCardModal_elem));
this.save_elem.addEventListener('click', (function(nameIn_elem, modal) {
return function() {
if (nameIn_elem.value !== '') {
controller.getCards().push(new Card(nameIn_elem.value));
nameIn_elem.value = '';
contentView.render();
modal.classList.toggle('show');
localStorage.setItem('cards', JSON.stringify(controller.getCards()));
}
}
})(this.nameIn_elem, this.addCardModal_elem));
}
};
let addLinkView = {
init: function() {
this.addLinkModal_elem = document.querySelector('.add-link-modal');
this.nameIn_elem = document.querySelector('.link-name-in');
this.urlIn_elem = document.querySelector('.link-url-in');
this.save_elem = document.querySelector('.link-save-btn');
this.render();
},
render: function() {
this.addLinkModal_elem.classList.toggle('show');
this.save_elem.addEventListener('click', (function(nameIn_elem, urlIn_elem, modal) {
return function() {
if ((nameIn_elem.value && urlIn_elem) !== '') {
cardView.addLink(nameIn_elem.value, urlIn_elem.value, controller.getAddLinkCaller());
nameIn_elem.value = '';
urlIn_elem.value = '';
contentView.render();
modal.classList.toggle('show');
localStorage.setItem('cards', JSON.stringify(controller.getCards()));
}
}
})(this.nameIn_elem, this.urlIn_elem, this.addLinkModal_elem));
}
}
let shortcuts = {
init: function() {
document.onkeyup = function(event) {
if (event.key === 'Escape') {
addCardView.addCardModal_elem.classList.toggle('show', false);
addLinkView.addLinkModal_elem.classList.toggle('show', false);
}
}
}
};
controller.init();