forked from eldelentes/comoayudarmx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
224 lines (185 loc) · 5.95 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
var handleFilterChange = function(e){
var type = $("#donation_type").val();
var location = $("#location").val();
var state = {type: type, location: location};
filterCards(state);
history.replaceState(state, "", "?" + $.param(state));
}
var getUrlParameter = function(param) {
var pageUrl = decodeURIComponent(window.location.search.substring(1)),
urlVariables = pageUrl.split('&'),
paramName,
i;
for (i = 0; i < urlVariables.length; i++) {
paramName = urlVariables[i].split('=');
if (paramName[0] === param) {
return paramName[1] === undefined ? true : paramName[1];
}
}
};
var filterCardFromQueryParams = function() {
var state = {type: getUrlParameter("type"), location: getUrlParameter("location")};
$("#donation_type").val(state.type).trigger("chosen:updated");;
$("#location").val(state.location).trigger("chosen:updated");
filterCards(state);
}
var filterCards = function(state) {
var $noResults = $("#no_results");
var cardMatchFilter = function($card, filter) {
return (!state[filter] || $card.find(".card__" + filter).text().includes(state[filter]) || state[filter] == "all")
};
var cardMatchFilters = function($card) {
return cardMatchFilter($card, "type") && cardMatchFilter($card, "location");
}
var hasResults = function() {
return $(".card").is(":visible");
}
$noResults.hide();
$(".card").each(function() {
var $card = $(this);
if (cardMatchFilters($card)) {
$card.show();
} else {
$card.hide();
}
})
if(!hasResults()) {
$noResults.show();
}
}
var populateFilters = function(e) {
var populateFilter = function(selectorInCard, filterSelector) {
$(selectorInCard).each(function() {
var option = $(this).text();
var $select = $(filterSelector);
var $option = $("<option>" + option + "</option>");
if (!$select.text().includes(option)) {
$select.append($option);
}
});
}
populateFilter(".card__type h3", "#donation_type");
populateFilter(".card__location h3", "#location");
$('select#donation_type').chosen()
$('select#location').chosen()
}
var renderCards = function(cardsFromService) {
Cards = Cards.concat(cardsFromService);
var template = $("#card_template").html();
var monetaryType = "Monetaria";
var isWorldPage = function() {
return location.pathname.includes("world.html");
};
var isMonetaryCard = function(card) {
return getCardTypes(card).indexOf(monetaryType) !== -1;
};
var getCardTypes = function(card) {
if (Array.isArray(card.type)) {
return card.type;
}
return [card.type];
}
var translateMonetaryType = function(type) {
if (isWorldPage() && (type == monetaryType)) {
return "Monetary";
} else {
return type;
}
}
var renderIconType= function(type){
var code={
"Víveres":'cutlery',
"Especie":'cutlery',
"Monetario":'money',
"Monetaria":'money',
"Albergue":'bed',
"Albergues":'bed',
"Trabajo Voluntario":'users',
"Equipo de auxilio médico":'medkit',
"Medicamentos":'medkit',
"Artículos de limpieza":'paint-brush',
"Artículos de aseo personal":'paint-brush',
"Limpieza":'paint-brush',
"Equipo de rescate":'life-ring',
"Asesoría":'user-circle',
"Asesoría profesional":'user-circle',
"Herramientas":'wrench',
"Sangre":'tint',
"Veterinario":'paw',
"Transporte":'truck',
"Ropa":'shopping-bag'
};
return '<i class="fa fa-'+code[type]+'"></i> ';
};
var renderCardTypes = function($card, types) {
var template = $card.find(".card__type h3").clone();
template.find('i').remove();
$card.find(".card__type h3").remove();
types.forEach(function(type) {
$card.find(".card__type").append(template.clone().append(renderIconType(type)+"<span>" + translateMonetaryType(type) + "</span>"));
});
}
var renderBadges = function($card, card) {
if(card.verified) {
$card.find(".card__badges").append('<span class="badge-verified" ><i class="fa fa-check"></i> Fuente Oficial</span>');
}
}
var renderCard = function(card) {
var $card = $(template);
var $location = $("<span>" + card.location + "</span>");
$card.find(".card__title").text(card.title);
renderBadges($card, card);
$card.find(".card__desc").text(card.description);
renderCardTypes($card, getCardTypes(card));
$card.find(".card__location h3").append($location);
$card.find(".card__button").attr("href", card.link);
$("#cards_container").append($card);
};
if (isWorldPage()) {
Cards.filter(isMonetaryCard).forEach(renderCard);
} else {
Cards.forEach(renderCard);
}
}
var start = function(cards) {
renderCards(cards);
populateFilters();
filterCardFromQueryParams();
}
var getCards = function() {
var getEntryProperty = function(entry, propName) {
return entry['gsx$' + propName] && entry['gsx$' + propName]['$t']
}
var formatType = function(type) {
var types = type.split(',');
return types.map(function (type) {
return type.trim();
});
}
var isApprovedCard = function (card) {
return card.approved === 'TRUE';
}
var buildCard = function(entry) {
return {
timespamp: getEntryProperty(entry, 'timestamp'),
title: getEntryProperty(entry, 'formadeayuda'),
description: getEntryProperty(entry, 'informaciónadicionaldeayuda'),
type: formatType(getEntryProperty(entry, 'tipodedonación')),
location: getEntryProperty(entry, 'puedesayudardesde'),
link: getEntryProperty(entry, 'fuentedeinformaciónlink'),
adicional: getEntryProperty(entry, 'informaciónadicional'),
approved: getEntryProperty(entry, 'approved')
}
}
$.get(
'https://spreadsheets.google.com/feeds/list/1zAFK1sSjIaHurnKzLx-e3GJZNmZ9QWfFSlIZLyYk8IE/1/public/values?alt=json',
function (data) {
start(
data.feed.entry.map(buildCard).filter(isApprovedCard)
);
},
);
}
$(document).on("change", "#donation_type", handleFilterChange);
$(document).on("change", "#location", handleFilterChange);
$(document).ready(getCards);