/
app-analyzer.js
295 lines (249 loc) · 11.6 KB
/
app-analyzer.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
/* **************************************************************
Copyright 2011 Zoovy, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************** */
var analyzer = function() {
var r = {
vars : {
"templates" : ['profileTemplate','catInfoTemplate','prodlistProdTemplate']
},
calls: {
appResource : {
init : function(filename,tagObj,Q) {
// app.u.dump("BEGIN analyzer.calls.appResource.init");
tagObj = $.isEmptyObject(tagObj) ? {} : tagObj;
tagObj.datapointer = 'appResource|'+filename;
this.dispatch(filename,tagObj,Q);
return 1;
},
dispatch : function(filename,tagObj,Q) {
var obj = {};
obj.filename = filename
obj["_cmd"] = "appResource";
obj["_tag"] = tagObj;
app.model.addDispatchToQ(obj,Q);
}
} //appResource
}, //calls
callbacks : {
//run when controller loads this extension. Should contain any validation that needs to be done. return false if validation fails.
init : {
onSuccess : function() {
var r = true; //return false if extension won't load for some reason (account config, dependencies, etc).
return r;
},
onError : function() {
//errors will get reported for this callback as part of the extensions loading. This is here for extra error handling purposes.
//you may or may not need it.
app.u.dump('BEGIN app.ext.analyzer.callbacks.init.onError');
}
},
//this is the callback defined to run after extension loads.
startMyProgram : {
onSuccess : function() {
$('#profileSummary').anycontent({'templateID':'profileTemplate','dataAttribs':{id:"profileSummaryList"}});
$('#tabs-1').append(app.ext.analyzer.u.objExplore(zGlobals));
$('#tabs-4').append(app.ext.analyzer.u.buildTagsList({'id':'tagList'}));
app.ext.analyzer.calls.appResource.init('product_attribs_all.json',{'callback':'handleFlexedit','extension':'analyzer'});
//request profile data (company name, logo, policies, etc)
app.calls.appProfileInfo.init({'profile':zGlobals.appSettings.profile},{'callback':'handleProfile','parentID':'profileSummaryList','extension':'analyzer'});
if(zGlobals && zGlobals.appSettings && zGlobals.appSettings.rootcat) {
app.ext.store_navcats.calls.appCategoryList.init(zGlobals.appSettings.rootcat,{"callback":"showRootCategories","extension":"analyzer"});
app.model.dispatchThis();
}
else {
$("#globalMessaging").anyMessage({'message':'Could not load category tree because zGlobals.appSettings.rootcat was not present/accessable.'});
}
},
onError : function(responseData,uuid) {
//error handling is a case where the response is delivered (unlike success where datapointers are used for recycling purposes)
app.u.handleErrors(responseData,uuid); //a default error handling function that will try to put error message in correct spot or into a globalMessaging element, if set. Failing that, goes to modal.
}
}, //startMyProgram
showRootCategories : {
onSuccess : function(tagObj) {
app.ext.store_navcats.u.getChildDataOf('.',{'parentID':'categoryTree','callback':'addCatToDom','templateID':'catInfoTemplate','extension':'store_navcats'},'more'); //generate left nav.
app.model.dispatchThis();
},
onError : function(responseData,uuid) {
//throw some messaging at the user. since the categories should have appeared in the left col, that's where we'll add the messaging.
app.u.handleErrors(responseData,uuid);
}
}, //showRootCategories
prodDebug : {
onSuccess : function(tagObj) {
// app.u.dump("BEGIN analyzer.callbacks.prodDebug");
$('#attribsDebugData').append(app.ext.analyzer.u.objExplore(app.data[tagObj.datapointer]['%attribs']));
$('#variationsDebugData').append(app.ext.analyzer.u.objExplore(app.data[tagObj.datapointer]['@variations']));
$('#inventoryDebugData').append(app.ext.analyzer.u.objExplore(app.data[tagObj.datapointer]['@inventory']));
$('#prodDebugThumbs').append(app.ext.analyzer.u.prodDebugImageList(tagObj.datapointer));
$('#tabs-5 li:odd').addClass('odd');
},
onError : function(responseData,uuid) {
//throw some messaging at the user. since the categories should have appeared in the left col, that's where we'll add the messaging.
app.u.handleErrors(responseData,uuid);
}
}, //prodDebug
handleProfile : {
onSuccess : function(tagObj) {
var dataSrc;
//not all data is at the root level.
app.renderFunctions.translateTemplate(app.data[tagObj.datapointer],tagObj.parentID);
$('#profileData').html(app.ext.analyzer.u.objExplore(app.data[tagObj.datapointer]));
},
onError : function(responseData,uuid) {
//throw some messaging at the user. since the categories should have appeared in the left col, that's where we'll add the messaging.
app.u.dump("BEGIN analyzer.callbacks.handleFlexedit.onError");
app.u.handleErrors(responseData,uuid);
}
}, //handleProfile
handleFlexedit : {
onSuccess : function(tagObj) {
// do something
},
onError : function(responseData,uuid) {
//throw some messaging at the user. since the categories should have appeared in the left col, that's where we'll add the messaging.
app.u.dump("BEGIN analyzer.callbacks.handleFlexedit.onError");
app.u.handleErrors(responseData,uuid);
}
}, //handleFlexedit
handleElasticResults : {
onSuccess : function(tagObj) {
// app.u.dump("BEGIN analyzer.callbacks.handleElasticResults.onSuccess.");
var L = app.data[tagObj.datapointer]['_count'];
// app.u.dump(" -> Number Results: "+L);
$parent = $('#'+tagObj.parentID).empty().removeClass('loadingBG');
if(L == 0) {
$parent.append("Your query returned zero results.");
}
else {
var pid;//recycled shortcut to product id.
for(var i = 0; i < L; i += 1) {
pid = app.data[tagObj.datapointer].hits.hits[i]['_source']['pid'];
$parent.append(app.renderFunctions.transmogrify({'id':pid,'pid':pid},'prodlistProdTemplate',app.data[tagObj.datapointer].hits.hits[i]['_source']));
}
}
},
onError : function(responseData,uuid) {
app.u.handleErrors(responseData,uuid)
}
} //handleElasticResults
}, //callbacks
a : {
showSubcats : function(path) {
// app.u.dump("BEGIN analyzer.a.showSubcats ["+path+"]");
var parentID = 'categoryTreeSubs_'+app.u.makeSafeHTMLId(path);
// app.u.dump(" -> size() = "+$('#'+parentID+' li').size());
//once the parentID has children, the subcats have already been loaded. don't load them twice.
if($('#'+parentID+' li').size() == 0) {
app.ext.store_navcats.u.getChildDataOf(path,{'parentID':parentID,'callback':'addCatToDom','templateID':'catInfoTemplate','extension':'store_navcats'},'more');
app.model.dispatchThis();
}
}, //showSubcats
exploreProduct : function(pid) {
$('#attribsDebugData, #variationsDebugData ,#inventoryDebugData, #prodDebugThumbs').empty();
app.ext.store_product.calls.appProductGet.init(pid,{'callback':'prodDebug','extension':'analyzer'});
app.model.dispatchThis();
},
showItemsTaggedAs : function(tag) {
$('#tagList li').removeClass('ui-state-active'); //remove any previously active states from list item choiced.
$('#'+tag).addClass('ui-state-active'); //add active state to list item now in focus.
$('#tagProdlist').empty().addClass('loadingBG'); //empty results container so new list isn't appended to previous list, if present.
app.ext.store_search.calls.appPublicProductSearch.init({'size':250,'mode':'elastic-search','filter':{'term':{'tags':tag}}},{'callback':'handleElasticResults','extension':'analyzer','datapointer':'appPublicSearch|'+tag,'parentID':'tagProdlist'});
app.model.dispatchThis();
}, //showItemsTaggedAs
changeDomains : function() {
localStorage.clear(); //make sure local storage is empty so a new cart is automatically obtained.
location.reload(true); //refresh page to restart experience.
},
showPageGetDetailsInModal : function(catSafeID) {
if(catSafeID) {
var $div = $("<div \/>",{'title':'Detail for: '+catSafeID});
$div.appendTo('body')
$div.dialog({'modal':true,'width':'90%','height':500});
$div.showLoading({'message':'fetching category data'});
app.ext.store_navcats.calls.appPageGet.init({'PATH':catSafeID,'@get':[],'all':1},{'callback':function(rd){
$div.hideLoading();
$div.append(app.ext.analyzer.u.objExplore(app.data[rd.datapointer]['%page']));
}},'mutable');
app.model.dispatchThis('mutable');
}
else {
//error
app.u.dump("No catsafeid passed into showPageGetDetailsinModal");
}
}
}, //actions
u : {
buildTagsList : function(P) {
var $ul = $("<ul>").attr(P); //what is returned.
var tags = new Array('IS_FRESH','IS_NEEDREVIEW','IS_HASERRORS','IS_CONFIGABLE','IS_COLORFUL','IS_SIZEABLE','IS_OPENBOX','IS_PREORDER','IS_DISCONTINUED','IS_SPECIALORDER','IS_BESTSELLER','IS_SALE','IS_SHIPFREE','IS_NEWARRIVAL','IS_CLEARANCE','IS_REFURB','IS_USER1','IS_USER2','IS_USER3','IS_USER4','IS_USER5','IS_USER6','IS_USER7','IS_USER8','IS_USER9');
var $li;
var L = tags.length;
for(var i = 0; i < L; i += 1) {
$li = $("<li>").attr('id',tags[i]).addClass('ui-state-default').text(tags[i]).click(function(){
app.ext.analyzer.a.showItemsTaggedAs(this.id);
});
$li.appendTo($ul);
}
return $ul;
},
prodDebugImageList : function(datapointer) {
var data = app.data[datapointer]['%attribs']
var $div = $("<div>").attr('id','debugImageContainer');
var filename;
for(var i = 1; i < 10; i += 1) {
filename = data['zoovy:prod_image'+i];
if(filename) {
$div.append("<figure>"+app.u.makeImage({"name":filename,"w":150,"b":"FFFFFF","tag":1})+"<figcaption>image"+i+": "+filename+"</figcaption>");
}
}
return $div;
},
handleElasticFilterOrQuery : function() {
var quilter = app.ext.store_search.u.buildElasticRaw($.parseJSON($('#advsrch_filterQuery').val())); //query/filter object
if(quilter) {
app.ext.store_search.calls.appPublicProductSearch.init(quilter,{'callback':'handleElasticResults','extension':'analyzer','parentID':'elasticResults','datapointer':'elasticsearch|Test'});
app.model.dispatchThis();
}
else {
alert('invalid json');
}
},
objExplore : function(obj) {
// app.u.dump("BEGIN analyzer.u.objExplore");
var keys = new Array();
for (var n in obj) {
keys.push(n);
}
keys.sort();
var L = keys.length;
var $ul = $('<ul \/>');
var $prompt,$value,$li;
var r = '<ul>'; //what is returned. an html string of key/value pairs for obj passed in.
for(var i = 0; i < L; i += 1) {
$li = $('<li>');
$prompt = $('<span>').addClass('prompt').text(keys[i]).appendTo($li);
if(typeof obj[keys[i]] == 'object') {
$value = app.ext.analyzer.u.objExplore(obj[keys[i]]);
}
else {
$value = $('<span>').addClass('value').text(obj[keys[i]]);
}
$value.appendTo($li);
$li.appendTo($ul);
}
return $ul;
}
} //util
} //r object.
return r;
}