-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
180 lines (141 loc) · 4.94 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
$(function () {
$('[data-toggle="tooltip"]').tooltip()
});
$(function () {
$('[data-toggle="dropdown"]').dropdown('toggle');
});
// ES6 Shoutout Messages
class Shoutout {
constructor(name, message, tour_session) {
this.name = name;
this.message = message;
this.tour_session = tour_session;
}
}
class UI {
addMessageToList(Shoutmessage) {
const list = document.getElementById('messageList');
// create li element
const li = document.createElement('li');
//Insert li into ul
li.innerHTML = `
<li><strong>Customer Name: </strong> <p class='lead'>${Shoutmessage.name}</p></li>
<li><strong>Message: </strong> <p class='lead'> ${Shoutmessage.message}</p></li>
<li><strong>Tour Session: </strong> <p class='lead'> ${Shoutmessage.tour_session} </p></li>
<li><a href="#" class="delete bttn-minimal bttn-sm" tour_session=${Shoutmessage.tour_session}>Remove Shoutout<a></li>
`;
list.appendChild(li);
}
showAlert(message, className) {
// create div
const div = document.createElement('div');
// Add classes
div.className = `alert ${className}`;
// Add text
div.appendChild(document.createTextNode(message));
// Get Parent Class
const container = document.querySelector('.shoutout-form-container');
// Get form Id
const form = document.querySelector('#shoutout-form');
// Insert alert
container.insertBefore(div, form);
// Timeout after 3 seconds
setTimeout(function () {
document.querySelector('.alert').remove();
}, 3000);
}
deleteShout(target) {
if (target.className.split(' ').indexOf('delete') > -1) {
target.parentElement.parentElement.remove();
}
}
clearFields() {
document.getElementById('name').value = '';
document.getElementById('message').value = '';
document.getElementById('tour_session').value = '';
}
}
// Local Storage Class Object
class Store {
static getShoutouts() {
let messages;
if (localStorage.getItem('messages') === null) {
messages = [];
} else {
messages = JSON.parse(localStorage.getItem('messages'));
}
return messages;
}
static displayShoutouts() {
const messages = Store.getShoutouts();
messages.reverse().forEach(function (message) {
const ui = new UI;
ui.addMessageToList(message);
});
}
static addMessage(message) {
const messages = Store.getShoutouts();
messages.push(message);
localStorage.setItem('messages', JSON.stringify(messages));
}
static removeMessage(tour_session) {
const messages = Store.getShoutouts();
messages.forEach(function (message, index) {
if (message.tour_session === tour_session) {
messages.splice(index, 1);
}
});
localStorage.setItem('messages', JSON.stringify(messages));
}
}
// DOM Load Event
document.addEventListener('DOMContentLoaded', Store.displayShoutouts);
// Event Listener for add message
document.getElementById('shoutout-form').addEventListener('submit', function (e) {
//Grab form values
const name = document.getElementById('name').value,
message = document.getElementById('message').value,
tour_session = document.getElementById('tour_session').value
// Instantiate message
const msg = new Shoutout(name, message, tour_session);
// Instantiate UI
const ui = new UI();
console.log(ui);
// Validate
if (!name || !message || !tour_session) {
//Error alert
ui.showAlert('Please fill in all fields', 'error');
} else {
// Add message to list
ui.addMessageToList(msg);
// Add to Local Storage
Store.addMessage(msg);
// Show success
ui.showAlert('Shoutout Message Added!', 'success');
// Clear fields
ui.clearFields();
}
e.preventDefault();
});
// Event Listener for delete
document.getElementById('messageList').addEventListener('click', function (e) {
// Instantiate UI
const ui = new UI();
// Delete message
ui.deleteShout(e.target);
// Remove from Local Storage
Store.removeMessage(e.target.getAttribute('tour_session'));
// Show message
ui.showAlert('Message Removed!', 'success');
e.preventDefault();
});
/* Bootstrap Active Link Enabler*/
$(document).ready(function () {
var liItems = $('.navbar ul li');
$.each(liItems, function(key, liItem){
var aEl = $(this).children(liItem)[0];
if(aEl == document.URL){
$(liItem).addClass('active');
}
})
})