-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
463 lines (385 loc) · 14.5 KB
/
Copy pathscript.js
File metadata and controls
463 lines (385 loc) · 14.5 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
(function (global) {
// Set up a namespace for our utility
var ajaxUtils = {};
// Returns an HTTP request object
function getRequestObject() {
if (window.XMLHttpRequest) {
return (new XMLHttpRequest());
}
else if (window.ActiveXObject) {
// For very old IE browsers (optional)
return (new ActiveXObject("Microsoft.XMLHTTP"));
}
else {
global.alert("Ajax is not supported!");
return(null);
}
}
// Makes an Ajax GET request to 'requestUrl'
ajaxUtils.sendGetRequest =
function(requestUrl, responseHandler, isJsonResponse) {
var request = getRequestObject();
request.onreadystatechange =
function() {
handleResponse(request,
responseHandler,
isJsonResponse);
};
request.open("GET", requestUrl, true);
request.send(null); // for POST only
};
// Only calls user provided 'responseHandler'
// function if response is ready
// and not an error
function handleResponse(request,
responseHandler,
isJsonResponse) {
if ((request.readyState == 4) &&
(request.status == 200)) {
// Default to isJsonResponse = true
if (isJsonResponse == undefined) {
isJsonResponse = true;
}
if (isJsonResponse) {
responseHandler(JSON.parse(request.responseText));
}
else {
responseHandler(request.responseText);
}
}
}
// Expose utility to the global object
global.$ajaxUtils = ajaxUtils;
})(window);
$(function () { // Same as document.addEventListener("DOMContentLoaded"...
// Same as document.querySelector("#navbarToggle").addEventListener("blur",...
$("#navbarToggle").blur(function (event) {
var screenWidth = window.innerWidth;
if (screenWidth < 768) {
$("#collapsable-nav").collapse('hide');
}
});
});
(function (global) {
var dc = {};
var homeHtmlUrl = "snippets/home-snippet.html";
var allCategoriesUrl =
"https://coursera-jhu-default-rtdb.firebaseio.com/categories.json";
var categoriesTitleHtml = "snippets/categories-title-snippet.html";
var categoryHtml = "snippets/category-snippet.html";
var menuItemsUrl =
"https://coursera-jhu-default-rtdb.firebaseio.com/menu_items/";
var menuItemsTitleHtml = "snippets/menu-items-title.html";
var menuItemHtml = "snippets/menu-item.html";
// Convenience function for inserting innerHTML for 'select'
var insertHtml = function (selector, html) {
var targetElem = document.querySelector(selector);
targetElem.innerHTML = html;
};
// Show loading icon inside element identified by 'selector'.
var showLoading = function (selector) {
var html = "<div class='text-center'>";
html += "<img src='images/ajax-loader.gif'></div>";
insertHtml(selector, html);
};
// Return substitute of '{{propName}}'
// with propValue in given 'string'
var insertProperty = function (string, propName, propValue) {
var propToReplace = "{{" + propName + "}}";
string = string
.replace(new RegExp(propToReplace, "g"), propValue);
return string;
};
// Remove the class 'active' from home and switch to Menu button
var switchMenuToActive = function () {
// Remove 'active' from home button
var classes = document.querySelector("#navHomeButton").className;
classes = classes.replace(new RegExp("active", "g"), "");
document.querySelector("#navHomeButton").className = classes;
// Add 'active' to menu button if not already there
classes = document.querySelector("#navMenuButton").className;
if (classes.indexOf("active") === -1) {
classes += " active";
document.querySelector("#navMenuButton").className = classes;
}
};
// // On page load (before images or CSS)
// document.addEventListener("DOMContentLoaded", function (event) {
// // TODO: STEP 0: Look over the code from
// // *** start ***
// // to
// // *** finish ***
// // below.
// // We changed this code to retrieve all categories from the server instead of
// // simply requesting home HTML snippet. We now also have another function
// // called buildAndShowHomeHTML that will receive all the categories from the server
// // and process them: choose random category, retrieve home HTML snippet, insert that
// // random category into the home HTML snippet, and then insert that snippet into our
// // main page (index.html).
// //
// // TODO: STEP 1: Substitute [...] below with the *value* of the function buildAndShowHomeHTML,
// // so it can be called when server responds with the categories data.
// // *** start ***
// // On first load, show home view
// showLoading("#main-content");
// $ajaxUtils.sendGetRequest(
// allCategoriesUrl,
// [...], // ***** <---- TODO: STEP 1: Substitute [...] ******
// true); // Explicitly setting the flag to get JSON from server processed into an object literal
// });
// // *** finish **
// On page load (before images or CSS)
document.addEventListener("DOMContentLoaded", function (event) {
// TODO: STEP 0: Look over the code from
// *** start ***
// to
// *** finish ***
// below.
// We changed this code to retrieve all categories from the server instead of
// simply requesting home HTML snippet. We now also have another function
// called buildAndShowHomeHTML that will receive all the categories from the server
// and process them: choose random category, retrieve home HTML snippet, insert that
// random category into the home HTML snippet, and then insert that snippet into our
// main page (index.html).
//
// TODO: STEP 1: Substitute [...] below with the *value* of the function buildAndShowHomeHTML,
// so it can be called when server responds with the categories data.
// *** start ***
// On first load, show home view
showLoading("#main-content");
$ajaxUtils.sendGetRequest(
allCategoriesUrl,
buildAndShowHomeHTML, // ***** <---- STEP 1: Substitute with function name ******
true); // Explicitly setting the flag to get JSON from server processed into an object literal
});
// *** finish **
// // Builds HTML for the home page based on categories array
// // returned from the server.
// function buildAndShowHomeHTML (categories) {
// // Load home snippet page
// $ajaxUtils.sendGetRequest(
// homeHtmlUrl,
// function (homeHtml) {
// // TODO: STEP 2: Here, call chooseRandomCategory, passing it retrieved 'categories'
// // Pay attention to what type of data that function returns vs what the chosenCategoryShortName
// // variable's name implies it expects.
// var chosenCategoryShortName =
// // TODO: STEP 3: Substitute {{randomCategoryShortName}} in the home html snippet with the
// // chosen category from STEP 2. Use existing insertProperty function for that purpose.
// // Look through this code for an example of how to do use the insertProperty function.
// // WARNING! You are inserting something that will have to result in a valid Javascript
// // syntax because the substitution of {{randomCategoryShortName}} becomes an argument
// // being passed into the $dc.loadMenuItems function. Think about what that argument needs
// // to look like. For example, a valid call would look something like this:
// // $dc.loadMenuItems('L')
// // Hint: you need to surround the chosen category short name with something before inserting
// // it into the home html snippet.
// //
// // var homeHtmlToInsertIntoMainPage = ....
// // TODO: STEP 4: Insert the produced HTML in STEP 3 into the main page
// // Use the existing insertHtml function for that purpose. Look through this code for an example
// // of how to do that.
// // ....
// },
// false); // False here because we are getting just regular HTML from the server, so no need to process JSON.
// }
// Builds HTML for the home page based on categories array
// returned from the server.
function buildAndShowHomeHTML (categories) {
// Load home snippet page
$ajaxUtils.sendGetRequest(
homeHtmlUrl,
function (homeHtml) {
// STEP 2: Choose a random category and get its short_name
var chosenCategory = chooseRandomCategory(categories);
var chosenCategoryShortName = "'" + chosenCategory.short_name + "'";
// STEP 3: Substitute {{randomCategoryShortName}} in the home html snippet
var homeHtmlToInsertIntoMainPage = insertProperty(
homeHtml,
"randomCategoryShortName",
chosenCategoryShortName
);
// STEP 4: Insert the produced HTML into the main page
insertHtml("#main-content", homeHtmlToInsertIntoMainPage);
},
false); // False here because we are getting just regular HTML from the server, so no need to process JSON.
}
// Given array of category objects, returns a random category object.
function chooseRandomCategory (categories) {
// Choose a random index into the array (from 0 inclusively until array length (exclusively))
var randomArrayIndex = Math.floor(Math.random() * categories.length);
// return category object with that randomArrayIndex
return categories[randomArrayIndex];
}
// Load the menu categories view
dc.loadMenuCategories = function () {
showLoading("#main-content");
$ajaxUtils.sendGetRequest(
allCategoriesUrl,
buildAndShowCategoriesHTML);
};
// Load the menu items view
// 'categoryShort' is a short_name for a category
dc.loadMenuItems = function (categoryShort) {
showLoading("#main-content");
$ajaxUtils.sendGetRequest(
menuItemsUrl + categoryShort + ".json",
buildAndShowMenuItemsHTML);
};
// Builds HTML for the categories page based on the data
// from the server
function buildAndShowCategoriesHTML (categories) {
// Load title snippet of categories page
$ajaxUtils.sendGetRequest(
categoriesTitleHtml,
function (categoriesTitleHtml) {
// Retrieve single category snippet
$ajaxUtils.sendGetRequest(
categoryHtml,
function (categoryHtml) {
// Switch CSS class active to menu button
switchMenuToActive();
var categoriesViewHtml =
buildCategoriesViewHtml(categories,
categoriesTitleHtml,
categoryHtml);
insertHtml("#main-content", categoriesViewHtml);
},
false);
},
false);
}
// Using categories data and snippets html
// build categories view HTML to be inserted into page
function buildCategoriesViewHtml(categories,
categoriesTitleHtml,
categoryHtml) {
var finalHtml = categoriesTitleHtml;
finalHtml += "<section class='row'>";
// Loop over categories
for (var i = 0; i < categories.length; i++) {
// Insert category values
var html = categoryHtml;
var name = "" + categories[i].name;
var short_name = categories[i].short_name;
html =
insertProperty(html, "name", name);
html =
insertProperty(html,
"short_name",
short_name);
finalHtml += html;
}
finalHtml += "</section>";
return finalHtml;
}
// Builds HTML for the single category page based on the data
// from the server
function buildAndShowMenuItemsHTML (categoryMenuItems) {
// Load title snippet of menu items page
$ajaxUtils.sendGetRequest(
menuItemsTitleHtml,
function (menuItemsTitleHtml) {
// Retrieve single menu item snippet
$ajaxUtils.sendGetRequest(
menuItemHtml,
function (menuItemHtml) {
// Switch CSS class active to menu button
switchMenuToActive();
var menuItemsViewHtml =
buildMenuItemsViewHtml(categoryMenuItems,
menuItemsTitleHtml,
menuItemHtml);
insertHtml("#main-content", menuItemsViewHtml);
},
false);
},
false);
}
// Using category and menu items data and snippets html
// build menu items view HTML to be inserted into page
function buildMenuItemsViewHtml(categoryMenuItems,
menuItemsTitleHtml,
menuItemHtml) {
menuItemsTitleHtml =
insertProperty(menuItemsTitleHtml,
"name",
categoryMenuItems.category.name);
menuItemsTitleHtml =
insertProperty(menuItemsTitleHtml,
"special_instructions",
categoryMenuItems.category.special_instructions);
var finalHtml = menuItemsTitleHtml;
finalHtml += "<section class='row'>";
// Loop over menu items
var menuItems = categoryMenuItems.menu_items;
var catShortName = categoryMenuItems.category.short_name;
for (var i = 0; i < menuItems.length; i++) {
// Insert menu item values
var html = menuItemHtml;
html =
insertProperty(html, "short_name", menuItems[i].short_name);
html =
insertProperty(html,
"catShortName",
catShortName);
html =
insertItemPrice(html,
"price_small",
menuItems[i].price_small);
html =
insertItemPortionName(html,
"small_portion_name",
menuItems[i].small_portion_name);
html =
insertItemPrice(html,
"price_large",
menuItems[i].price_large);
html =
insertItemPortionName(html,
"large_portion_name",
menuItems[i].large_portion_name);
html =
insertProperty(html,
"name",
menuItems[i].name);
html =
insertProperty(html,
"description",
menuItems[i].description);
// Add clearfix after every second menu item
if (i % 2 !== 0) {
html +=
"<div class='clearfix visible-lg-block visible-md-block'></div>";
}
finalHtml += html;
}
finalHtml += "</section>";
return finalHtml;
}
// Appends price with '$' if price exists
function insertItemPrice(html,
pricePropName,
priceValue) {
// If not specified, replace with empty string
if (!priceValue) {
return insertProperty(html, pricePropName, "");
}
priceValue = "$" + priceValue.toFixed(2);
html = insertProperty(html, pricePropName, priceValue);
return html;
}
// Appends portion name in parens if it exists
function insertItemPortionName(html,
portionPropName,
portionValue) {
// If not specified, return original string
if (!portionValue) {
return insertProperty(html, portionPropName, "");
}
portionValue = "(" + portionValue + ")";
html = insertProperty(html, portionPropName, portionValue);
return html;
}
global.$dc = dc;
})(window);