-
Notifications
You must be signed in to change notification settings - Fork 0
/
squeego-activities-demo.js
334 lines (292 loc) · 10.6 KB
/
squeego-activities-demo.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
$(function(){
// The name of the html element where all the activities will go
var activities_jq = $('#squeego-activities');
// Your squeego url
var url = 'http://squeego.com/get-activities-for?id=' + activities_jq.data('squeegoId');
/***
* Request data from server using AJAX. Use XDR for IE and standard jQuery AJAX for other browsers.
***/
if (window.XDomainRequest) {
var xdr = new XDomainRequest();
if (xdr) {
xdr.onerror = error_loading_activities;
xdr.onload = function() { load_data_into_dom(xdr.responseText) };
xdr.open("get", url);
xdr.send();
} else {
// Error using xdr
activities_jq.html('<p>Error loading activities</p>');
}
// No XDomainRequest - use jQuery ajax
} else {
$.ajax({
url: url,
dataType: 'json'
})
.done(function(data) { load_data_into_dom(data) })
.fail(function() {
error_loading_activities()
});
}
/***
* Interpet the AJAX response from SqueeGo.
*
* Response will either be straight text (e.g. 'id not found'), or JSON data of activities.
*
***/
function load_data_into_dom( data ) {
if (data == 'id not found') {
activities_jq.html('<p>Error loading activities</p>');
} else if (data == 'activities not found') {
/* This will be executed if there were no activities in the SqueeGo database for your id. You can easily add some at squeego.com.
* SqueeGo has a review process to ensure the quality of content on squeego.com but unreviewed activities are added to the SqueeGo database immediately. This means both unreviewed and reviewed activities are returned with your activity request - so you can get started straight away! */
activities_jq.html('<p>Fun and interesting activities will appear here soon!</p>');
} else {
// If data is a string it will need to be converted to JavaScript. A response from IE (which uses XDR) will be a plain string. jQuery automatically parses the JSON string into a JavaScript object and so for non-IE (non-xdr) browsers, data is already a JavaScript object.
if (typeof data === 'string') {
data = $.parseJSON(data);
}
add_activities_to_dom(data);
}
}
/***
* Executed if there was an AJAX error when contacting the SqueeGo server.
***/
function error_loading_activities() {
/* This message will be displayed if there was an error retreiving the details from SqueeGo. This shouldn't actually occur */
activities_jq.html('<p>Our activities would normally appear here but there was an error retreiving them from SqueeGo.</p>');
}
/***
* Example code which interprets the activities data in a similar way to the SqueeGo website. You can edit this code to interpret the data in any way you like. You could also use it to bring in data from other parts of your website. SqueeGo sends you all your activity data so you can do with it as you please.
*
* This function does the main work of adding the activities data to the DOM (your web page).
***/
function add_activities_to_dom(activities) {
// Showdown converts the details text into MarkDown HTML
var converter = new Showdown.converter();
for (i in activities) {
var activity = activities[i];
//activities_jq.append($('<div class="title-row">' + activity.name + '</div>\
//<div>something else</div>'));
activities_jq.append($('<div class="activity clearfix">\
<div class="title-row">' + activity.name + '</div>\
<div class="activity-info">\
<div class="icons-column">\
<div class="time clearfix">\
<div class="sprite time-icon"></div>' +
(function(){
var s = '';
if (activity.date_ranges.length > 0) {
s = s + '<table class="date-ranges" summary="Periods (date ranges) activity is avilable">\
<tbody>' +
(function(){
for (var j=0; j<activity.date_ranges.length;j++) {
s = s + '<tr>\
<th>Available from</th>\
<td>18/03/2013 <br>to 20/03/2013</td>\
</tr>';
}
return s;
})()
s = s + '</tbody>\
</table>';
}
return s;
})() +
'<table class="times">\
<tbody>\
<tr>\
<th>Monday</th>\
<td>' + activity.monday_times + '</td>\
</tr>\
<tr>\
<th>Tuesday</th>\
<td>' + activity.tuesday_times + '</td>\
</tr>\
<tr>\
<th>Wednesday</th>\
<td>' + activity.wednesday_times + '</td>\
</tr>\
<tr>\
<th>Thursday</th>\
<td>' + activity.thursday_times + '</td>\
</tr>\
<tr>\
<th>Friday</th>\
<td>' + activity.friday_times + '</td>\
</tr>\
<tr>\
<th>Saturday</th>\
<td>' + activity.saturday_times + '</td>\
</tr>\
<tr>\
<th class="sunday">Sunday</th>\
<td class="sunday">' + activity.sunday_times + '</td>\
</tr>' +
(function(){
var s='';
for (var j=0; j<activity.single_datetimes.length; j++) {
s = s +
'<tr>\
<th>' + dateify(activity.single_datetimes[j].single_date) + '</th>\
<td>' + activity.single_datetimes[j].times + '</td>\
</tr>';
}
return s;
})() +
'</tbody>\
</table>\
</div><!--times-row-->\
<div class="cost clearfix">\
<div class="sprite cost-icon"></div>\
<table class="cost-table">\
<tbody>' +
(function(){
var s = '';
// Loop over prices and add rows as appropriate
for (var j=0; j<activity.prices.length;j++) {
s = s +
'<tr>\
<th>' + activity.prices[j].price_type + '</th>\
<td>' + dollarify(activity.prices[j].price) + '</td>\
<tr>';
}
return s;
})() +
'</tbody>\
</table>\
</div><!-- costs row -->\
<div class="other-icons">' +
(function(){
var s = '';
if (activity.cf_wheelchair == "checked") {
s = s + '<img class="sprite wheelchair-icon" src="empty.png" alt="Wheelchair friendly" title="Wheelchair friendly">';
}
if (activity.cf_alcohol_permitted == "checked") {
s = s + '<img class="sprite alcohol-icon" src="empty.png" alt="Alcohol served at venue" title="Alcohol served at venue">';
}
if (activity.cf_18plus == "checked") {
s = s + '<img class="sprite eighteenplus-icon" src="empty.png" alt="Restricted to persons aged 18 and over" title="Restricted to persons aged 18 and over">';
}
if (activity.cf_visa == "checked") {
s = s + '<img class="sprite visa-icon" src="empty.png" alt="Visa accepted" title="Visa accepted">';
}
if (activity.cf_mastercard == "checked") {
s = s + '<img class="sprite mastercard-icon" src="empty.png" alt="Mastercard accepted" title="Mastercard accepted">';
}
if (activity.cf_eftpos == "checked") {
s = s + '<img class="sprite eftpos-icon" src="empty.png" alt="EFTPOS accepted" title="EFTPOS accepted">';
}
return s;
})() +
'</div>\
</div>\
<div class="details">' +
converter.makeHtml(activity.details) +
'<div class="details-icons">' +
(function(){
var s = '';
for (var j=0; j<activity.locations.length;j++) {
// Loop over locations and add rows as appropriate
s = s +
'<div class="details-icon-address location">\
<img class="sprite address-icon" src="empty.png">' +
activity.locations[j] +
'</div>';
}
return s;
})() +
'</div>' +
/* Activities can optionally have contacts */
(function(){
var s = '';
// Contacts are optionally so make sure there are some before adding rows for them
if (activity.contacts.length > 0) {
for (var j=0; j<activity.contacts.length;j++) {
s = s +
'<div class="details-icons">\
<div class="details-icon-heading">';
if (activity.contacts[j].contact_type.toLowerCase() == 'phone') {
s = s +
'<div class="icon">\
<img class="sprite phone-icon" src="empty.png">\
</div>\
Phone';
} else if (activity.contacts[j].contact_type.toLowerCase() == 'twitter') {
s = s +
'<div class="icon">\
<img class="sprite twitter-icon" src="empty.png">\
</div>\
Twitter';
} else if (activity.contacts[j].contact_type.toLowerCase() == 'e-mail') {
s = s +
'<div class="icon">\
<img class="sprite email-icon" src="empty.png">\
</div>\
E-mail';
} else {
s = s +
'<div class="icon">\
<img class="sprite no-icon" src="empty.png">\
</div>' +
activity.contacts[j].contact_type;
}
s = s +
'</div>\
<div class="details-icon-info">' + activity.contacts[j].contact + '</div>\
</div>';
}
}
//s = s + '</div>';
return s;
})() +
'<div class="comments">\
<span class="sprite comments-icon action-link-button"></span>\
<a href="' + activity.squeego_url + '">Discuss this activity on SqueeGo!</a>\
</div>\
</div><!-- end of details -->\
</div>\
<hr />\
</div><!-- end of activities -->'));
} // End of for loop
}
});
/***
* Convenience function to convert the price (stored as cents by SqueeGo) into dollars and cents
***/
function dollarify(amount) {
// Instead of having a 0 value - say 'Free' instead.
if (amount == 0)
return 'Free';
// Add a dollar sign and decimal point to the number
amount = amount.toString();
return '$' + amount.slice(0,-2) + '.' + amount.slice(-2);
}
/***
* Convenience function to convert a SqueeGo stored date (yyyymmdd) into a normal date dd mmm.
***/
function dateify(d) {
d = new Date(d.slice(0,4), d.slice(4,6)-1, d.slice(6,8))
var curr_date = d.getDate();
var sup = "";
if (curr_date == 1 || curr_date == 21 || curr_date ==31)
{
sup = "st";
}
else if (curr_date == 2 || curr_date == 22)
{
sup = "nd";
}
else if (curr_date == 3 || curr_date == 23)
{
sup = "rd";
}
else
{
sup = "th";
}
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
return curr_date + "<SUP>" + sup + "</SUP> " + m_names[curr_month] + " " + curr_year;
}
var m_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];