This repository has been archived by the owner on May 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.js
119 lines (95 loc) · 3.81 KB
/
application.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
/*global jQuery:false, THA:false */
/**
* This file contains all of the logic that handles the user-interaction of the
* sample application. This file is assumed to be added to the page LAST, so
* that the DOM is guaranteed to be ready when this file is loaded.
*
* The variable naming scheme used in this code is an adaption of Systems
* Hungarian which is explained at http://blog.pother.ca/VariableNamingConvention/
*/
(function(window, $, THA){
'use strict';
var
sFilterTabTemplate,
sRestaurantItemTemplate,
sSortItemTemplate,
$Loading,
$RestaurantList,
$SearchFilter,
$ShowFavoritesButton
;
function fetchRestaurantList(p_$ActiveItem) {
var sActiveItem;
sActiveItem = p_$ActiveItem.data('sort-option');
return THA.restaurantList.fetchList(sActiveItem, function () {
$Loading.show();
/*/ Clear out current list and search value /*/
$RestaurantList.html('');
$SearchFilter.val('');
}).then(
function (p_oData) {
return THA.list.populate(
sRestaurantItemTemplate,
p_oData.restaurants,
THA.dataMaps
);
},
THA.view.displayAjaxError
).then(function(p_$List) {
return THA.view.decorateListItem(p_$List, sActiveItem);
}).then(function(p_$List) {
$RestaurantList.append(p_$List);
/*/ Move favorites to the top of the list /*/
$RestaurantList.prepend(p_$List.has('[data-favorite="true"]'));
/*/ Add search/filter functionality /*/
$SearchFilter.filterFor('.restaurant-list__item',{
sortItemSelector: '.card-header-title'
});
}).always(function () {
$Loading.hide();
});
}
/*/ Add error handling /*/
window.addEventListener('error', function (p_oError) {
THA.modal.showError(p_oError, 'Javascript Error');
});
/*/ Retrieve HTML templates /*/
sRestaurantItemTemplate = $('#restaurant-item-template').html();
sSortItemTemplate = $('#sortingOptions-item-template').html();
sFilterTabTemplate = $('#filter-tab-template').html();
$Loading = $('.loading-spinner');
$RestaurantList = $('.restaurant-list');
$SearchFilter = $('.js-search-filter');
$ShowFavoritesButton = $('.js-show-favorites');
$ShowFavoritesButton.on('click', function () {
$ShowFavoritesButton.toggleClass('show-favorites--is-active');
THA.view.filterRestaurantList($('.restaurant-list__item'));
});
Promise.all([
THA.filterTabs.populate(sFilterTabTemplate, THA.dataMaps.state),
THA.sortOptions.populate(sSortItemTemplate, THA.dataMaps.sort)
]).then(function(p_aPromiseValues) {
var $TabFilters = p_aPromiseValues.shift();
var $SortOptions = p_aPromiseValues.shift();
THA.view.setup($ShowFavoritesButton, $SortOptions, $TabFilters);
THA.view.attachFilterTabs();
THA.view.attachtSortOptions(
THA.dataMaps.sort,
$('.restaurant-filters .panel-heading')
);
$SortOptions.on('click', function (p_oEvent) {
var $ActiveItem;
$ActiveItem = THA.view.getTarget(p_oEvent, '[data-sort-option]');
$SortOptions.removeClass('is-active');
$ActiveItem.addClass('is-active');
fetchRestaurantList($ActiveItem).then(function (){
THA.view.filterRestaurantList($('.restaurant-list__item'));
});
});
/*/ Set up initial state /*/
THA.view.updateShowFavoriteButton(THA.favorites.length());
$SortOptions.first().trigger('click');
$TabFilters.first().trigger('click');
});
}(window, jQuery, THA));
/*EOF*/