-
Notifications
You must be signed in to change notification settings - Fork 0
/
dining-builder.js
218 lines (210 loc) · 8.56 KB
/
dining-builder.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
//domain = "http://www.valuecityfurniture.com";
domain = document.location.origin;
diningURL = "/search?portal_menu_level_1=1|Dining~257256";
environment = "http://search.blueport.com/sites/asi/?hawktype=search&output=json&portal_menu_level_1=1|Dining~257256";
newRequest = '';
function menuBuild(environment) {
$.ajax({
url: environment,
type: "GET",
crossDomain: true,
success: function(data) {
// Get the Facets object
var facets = data.Facets;
$.each(facets, function(i, item) {
// If the Facet is Style
if (item.Name == "Dining Height") {
var facetField = item.Field;
var facetName = item.Name;
var facetValues = item.Values;
styleMenu = "";
styleMenu += '<option>Height</option>';
// Loop through and build the menu
$.each(facetValues, function(i, item) {
var facetValue = item.Value;
var facetValueSpace = facetValue.replace(/\s+/g, '%20');
var facetValueClean = facetValueSpace.replace(/&+/g, '%26');
var facetValueLabel = item.Label;
/*if (facetValueLabel == "Casual Contemporary") {
facetValueLabel = "Casual";
}*/
var facetValueCount = item.Count;
if (facetValueCount > 0) {
styleMenu += '<option value="'+facetValueClean+'">'+facetValueLabel+'</option>';
}
});
$('#dr-select-1').html(styleMenu);
}
// If the Facet is Color
if (item.Name == "Color or Finish") {
var facetField = item.Field;
console.log(facetField);
var facetName = item.Name;
var facetNameClean = facetName.replace("Color or Finish", "Color");
var facetValues = item.Values;
colorsMenu = "";
colorsMenu += '<option>Color</option>';
// Loop through and build the menu
$.each(facetValues, function(i, item) {
var facetValue = item.Value;
var facetValueSpace = facetValue.replace(/\s+/g, '%20');
var facetValueClean = facetValueSpace.replace(/&+/g, '%26');
var facetValueLabel = item.Label;
var facetValueCount = item.Count;
if (facetValueCount > 0) {
colorsMenu += '<option value="'+facetValueClean+'">'+facetValueLabel+'</option>';
}
});
$('#dr-select-2').html(colorsMenu);
}
// If the Facet is Table Shape
if (item.Name == "Table Shape") {
//console.log(item.Name);
var facetField = item.Field;
var facetName = item.Name;
var facetNameClean = facetName.replace("Table Shape", "Shape");
var facetValues = item.Values;
shapeMenu = "";
shapeMenu += '<option>Shape</option>';
// Loop through and build the menu
$.each(facetValues, function(i, item) {
var facetValue = item.Value;
var facetValueSpace = facetValue.replace(/\s+/g, '%20');
var facetValueClean = facetValueSpace.replace(/&+/g, '%26');
var facetValueLabel = item.Label;
var facetValueCount = item.Count;
if (facetValueCount > 0) {
shapeMenu += '<option value="'+facetValueClean+'">'+facetValueLabel+'</option>';
//console.log(shapeMenu);
}
});
$('#dr-select-3').html(shapeMenu);
}
});
},
error: function(data) {
//console.log(data.responseText);
}
});
}
function buildRequest() {
// build a new search request
$("select").change(function(){
$('form select :selected').each(function(){
// seet query params for search
var heightSelectionValue = "&dining_height_1="+$("#dr-select-1").val();
var colorSelectionValue = "&color_facet_1="+$("#dr-select-2").val();
var shapeSelectionValue = "&table_shape_1="+$("#dr-select-3").val();
// if all 3 menus are not default
if ($("#dr-select-1 :selected").val() != "Height" &&
$("#dr-select-2 :selected").val() != "Color" &&
$("#dr-select-3 :selected").val() != "Shape") {
// build the search count request + link for button
newRequest = "";
newRequest = environment+heightSelectionValue+colorSelectionValue+shapeSelectionValue;
$("#drFinderSubmitBtn").attr('href', domain+diningURL+heightSelectionValue+colorSelectionValue+shapeSelectionValue);
return newRequest;
}
// if style, color menus are not default
else if ($("#dr-select-1 :selected").val() != "Height" &&
$("#dr-select-2 :selected").val() != "Color") {
// build the search count request + link for button
newRequest = "";
newRequest = environment+heightSelectionValue+colorSelectionValue;
$("#drFinderSubmitBtn").attr('href', domain+diningURL+heightSelectionValue+colorSelectionValue);
return newRequest;
}
// if color, shape menus are not default
else if ($("#dr-select-2 :selected").val() != "Color" &&
$("#dr-select-3 :selected").val() != "Shape") {
// build the search count request + link for button
newRequest = "";
newRequest = environment+colorSelectionValue+shapeSelectionValue;
$("#drFinderSubmitBtn").attr('href', domain+diningURL+colorSelectionValue+shapeSelectionValue);
return newRequest;
}
// if style, shape menus are not default
else if ($("#dr-select-1 :selected").val() != "Height" &&
$("#dr-select-3 :selected").val() != "Shape") {
// build the search count request + link for button
newRequest = "";
newRequest = environment+heightSelectionValue+shapeSelectionValue;
$("#drFinderSubmitBtn").attr('href', domain+diningURL+heightSelectionValue+shapeSelectionValue);
return newRequest;
}
// if style menu is not default
else if ($("#dr-select-1 :selected").val() != "Height") {
// build the search count request + link for button
newRequest = "";
newRequest = environment+heightSelectionValue;
$("#drFinderSubmitBtn").attr('href', domain+diningURL+heightSelectionValue);
return newRequest;
}
// if color menu is not default
else if ($("#dr-select-2 :selected").val() != "Color") {
// build the search count request + link for button
newRequest = "";
newRequest = environment+colorSelectionValue;
$("#drFinderSubmitBtn").attr('href', domain+diningURL+colorSelectionValue);
return newRequest;
}
// if shape menu is not default
else if ($("#dr-select-3 :selected").val() != "Shape") {
// build the search count request + link for button
newRequest = "";
newRequest = environment+shapeSelectionValue;
$("#drFinderSubmitBtn").attr('href', domain+diningURL+shapeSelectionValue);
return newRequest;
}
// if shape menu is not default
else if ($("#dr-select-1 :selected").val() === "Height" &&
$("#dr-select-2 :selected").val() === "Color" &&
$("#dr-select-3 :selected").val() === "Shape") {
// build the search count request + link for button
newRequest = "";
newRequest = environment;
$("#drFinderSubmitBtn").attr('href', domain+diningURL);
return newRequest;
}
});
});
}
function resultCount() {
$("select").change(function(){
// grab latest build request count
buildRequest();
nr = newRequest;
$.ajax({
url: nr,
type: "GET",
crossDomain: true,
success: function(data) {
// populate the results message
var numResults = data.Pagination.NofResults;
if (numResults > 0) {
$("div#numResults").html("<h3 style='color:#ffffff !important;'>"+numResults+" Found</h3>");
}
// no results, remove link, reset menus
else {
$("#drFinderSubmitBtn").removeAttr("href");
$("div#numResults").html("<h3 style='color:#ffffff !important;'>Nothing Found</h3>");
//menuBuild(environment);
}
},
error: function(data) {
}
});
});
}
function reset() {
// handler function to clear menus
$("#numResults").html("");
$("#numResults").text("");
menuBuild(environment);
}
// call the functions
menuBuild(environment);
buildRequest();
resultCount();
// reset button handler
$("a#reset").on("click", reset);