forked from wagnerfusca/eventos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
248 lines (226 loc) · 7.58 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
const app = function () {
const API_BASE = 'https://script.google.com/macros/s/AKfycbzfcFWq9-9MZmDeZKdFa0dEg9a7JEV0PJ-NR7xboZtRRtr9FUM/exec';
const API_KEY = 'eventosfusca';
const CATEGORIES = {
'Todos': {name: 'Todos', color: '#000'},
'Agilidade': {name: 'Agilidade', color: '#bfa719'},
'Blockchain': {name: 'Blockchain', color: '#18b1fc'},
'CIO/Executivos': {name: 'CIO/Executivos', color: '#3b987d'},
'Cloud': {name: 'Cloud', color: '#e34000'},
'Desenvolvimento': {name: 'Desenvolvimento', color: '#a14360'},
'Ecommerce': {name: 'Ecommerce', color: '#f49715'},
'Geral': {name: 'Geral', color: '#613176'},
'IA-Inteligencia_Cognitiva': {name: 'IA e Inteligência Cognitiva', color: '#0da58d'},
'Inovacao': {name: 'Inovação', color: '#d02a15'},
'IOT': {name: 'IOT', color: '#283ee0'},
'Lean': {name: 'Lean', color: '#8d73aa'},
'Lideranca': {name: 'Liderança', color: '#5a83de'},
'PMI-PMP': {name: 'PMI e PMP', color: '#6b14a4'},
'Produtos': {name: 'Produtos', color: '#02b820'},
'RH': {name: 'RH', color: '#e90f95'},
'Robotica': {name: 'Robótica', color: '#fc8d21'},
'Seguranca': {name: 'Segurança', color: '#7ec26b'},
'UX-CX-PX': {name: 'UX, CX e PX', color: '#5d7969'}
};
const MONTH_NAMES = ['JAN', 'FEV', 'MAR', 'ABR', 'MAI', 'JUN',
'JUL', 'AGO', 'SET', 'OUT', 'NOV', 'DEZ'
];
const state = {activeCategory: null, initialized: false};
const page = {};
function init () {
page.notice = document.getElementById('notice');
page.filter = document.getElementById('category-list');
page.container = document.getElementById('container');
page.alert = document.getElementById('alert');
_buildFilter();
_getJson();
}
function _setDataAtualizacao(cabecalho) {
page.alert.innerHTML = '<div> Atualizado em: <b>'+cabecalho[13]+'</b></div>';
}
function _getJson () {
$('.loading').toggle();
fetch(_buildApiUrl(state.activeCategory))
.then((response) => response.json())
.then((json) => {
if (json.status !== 'success') {
_setNotice(json.message);
}
_setDataAtualizacao(json.cabecalho)
_renderEvents(json.data);
if(!state.initialized){
_setQtyBadges(json.data);
state.initialized = true;
}
$('.loading').toggle();
})
.catch((error) => {
_setNotice('Unexpected error loading events');
})
}
function _setQtyBadges (events) {
var categories = [];
categories['Todos'] = events.length;
events.forEach(function(event){
const category = _getCategoryByName(event.categoria);
if(!categories[event.categoria])
categories[event.categoria] = 0;
if(
event.categoria === category.name ||
event.categoria === category.index
){
categories[event.categoria] += 1;
}
});
var filters = document.querySelectorAll('[data-category]');
filters.forEach(function(filterElement){
var cat = _getCategoryByName(filterElement.dataset.category);
filterElement.innerHTML += `
<span style="background-color: ${cat.color}" class="float-right badge round">
${categories[filterElement.dataset.category] || 0}
</span>
`;
});
}
function _buildFilter () {
for(var prop in CATEGORIES) {
page.filter.appendChild(_buildFilterLink(CATEGORIES[prop].name, prop, false));
}
}
function _buildFilterLink (element, key, isSelected) {
const categoryListElement = document.createElement('a');
categoryListElement.innerHTML = _capitalize(element);
categoryListElement.setAttribute('data-category', key);
categoryListElement.href = '#';
categoryListElement.classList = isSelected ? 'list-group-item active' : 'list-group-item';
categoryListElement.onclick = function (event) {
let category = key === 'Todos' ? null : key.toLowerCase();
_setActiveCategory(category);
_getJson();
};
return categoryListElement;
}
function _buildApiUrl (category) {
let url = API_BASE;
url += '?key=' + API_KEY;
url += category !== null ? '&category=' + category : '';
return url;
}
function _setNotice (label) {
page.notice.innerHTML = label;
}
function _renderEvents (events) {
let eventListElement = document.getElementById('event-list');
eventListElement.innerHTML = '';
events.forEach(function (event) {
let linha = document.createElement('li');
const category = _getCategoryByName(event.categoria);
linha.innerHTML = `
${_renderTime(event)}
<div class="info">
<div class="category" style="background-color: ${category.color}">${category.name}</div>
<div class="row">
<div class="col-sm-10">
<h2 class="title">${event.nome}</h2>
</div>
</div>
<div class="d-flex">
<div class="col-xs-12 col-sm-6">
<div class="row">
<div class="col-sm-3 font-weight-bold">Início:</div>
<div class="col-sm-9">${_formatDateDDMMYYYY(event.inicio)}</div>
</div>
<div class="row">
<div class="col-sm-3 font-weight-bold">Fim:</div>
<div class="col-sm-9">${_formatDateDDMMYYYY(event.fim)}</div>
</div>
</div>
<div class="col-xs-12 col-sm-6">
<div class="row">
<div class="col-sm-3 font-weight-bold">Local:</div>
<div class="col-sm-9">${event.local} - ${event.uf}</div>
</div>
<div class="row">
<div class="col-sm-3 font-weight-bold">Call4Papers:</div>
<div class="col-sm-9">${_formatString(event.call4papers)}</div>
</div>
</div>
</div>
<div class="links mt-3">
<a href="${event.site}" target="_blank">
<span>Site do Evento</span>
<i class="fa fa-arrow-circle-right"></i>
</a>
</div>
</div>`;
eventListElement.appendChild(linha);
});
}
function _getCategoryByName (category) {
const categoryIndex = Object.keys(CATEGORIES).filter(function(index){
return CATEGORIES[index].name === category
|| index === category;
});
CATEGORIES[categoryIndex].index = categoryIndex[0];
return CATEGORIES[categoryIndex];
}
function _formatString (string) {
if(string == ''){
return '';
}
return string;
}
function _formatLink (string) {
if(string == ''){
return '';
}
return 'Acesse o site';
}
function _renderTime (event) {
var date = new Date(event.inicio);
const category = _getCategoryByName(event.categoria);
if(event.inicio){
return `
<time datetime="${_formatDateDDMMYYYY(event.inicio)}" style="background-color: ${category.color}">
<span class="day">${date.getDate()}</span>
<span class="month">${ MONTH_NAMES[date.getMonth()] }</span>
<span class="year">${ date.getFullYear() }</span>
</time>
`;
}
var date = new Date(event.mesano);
return `
<time datetime="${_formatDateDDMMYYYY(event.mesano)}" style="background-color: ${category.color}"">
<span class="month">${ MONTH_NAMES[date.getMonth()] }</span>
<span class="year">${ date.getFullYear() }</span>
</time>
`;
}
function _formatDateMMYYYY (string) {
if(string == ''){
return '';
}
var options = { year: 'numeric', month: 'numeric' };
return new Date(string).toLocaleDateString('en-GB', options);
}
function _formatDateDDMMYYYY (string) {
if(string == ''){
return '';
}
return new Date(string).toLocaleDateString('en-GB');
}
function _capitalize (label) {
return label.slice(0, 1).toUpperCase() + label.slice(1).toLowerCase();
}
function _setActiveCategory (category) {
$('#collapseAside').removeClass('show');
state.activeCategory = category;
const label = category === null ? 'Todos' : category;
Array.from(page.filter.children).forEach(function (element) {
element.classList = label === element.innerHTML.toLowerCase() ? 'list-group-item active' : 'list-group-item';
});
}
return {
init: init
};
}();