Skip to content

Commit e5b5809

Browse files
committed
Implemented view types
1 parent eab9b18 commit e5b5809

File tree

2 files changed

+153
-1
lines changed

2 files changed

+153
-1
lines changed

README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,96 @@ Consider a normal search page with basic layout as shown in the figure below and
213213
]
214214
}
215215
```
216+
It can also contain multiple templates to include different view types(grid view/list view) like below:
217+
218+
```
219+
,searchResultSetTemp:
220+
{
221+
"grid":
222+
'{{#products}}'+
223+
'{{{every_three}}}'+
224+
'<div class="product-container {{{nth_item}}}">'+
225+
'<div class="product-item item-template-0 alternative">'+
226+
'<div class="img">'+
227+
'<a href="{{productUrl}}" >'+
228+
'<img src="{{imageUrl}}" alt="" onerror="this.style.display=\'none\';">'+
229+
'</a>'+
230+
'</div>'+
231+
'<div class="name">'+
232+
'<a href="{{productUrl}}" > {{title}}'+
233+
'</a>'+
234+
'</div>'+
235+
'<div class="stars">'+
236+
'</div>'+
237+
'<div class="price">'+
238+
'{{#if_on_sale this}}'+
239+
'<div class="on-sale">Sale</div>'+
240+
'<del class="price2">${{format_price retail_price}}&nbsp;</del>'+
241+
'{{/if_on_sale}}'+
242+
'<span class="hidden">&nbsp;</span> ${{format_price price}}'+
243+
'</div>'+
244+
'</div>'+
245+
'</div>'+
246+
'{{/products}}',
247+
"list":
248+
'<ul class="nxt-product-list nxt-view-list clearfix">'
249+
+'{{#products}}'
250+
+'<li class="nxt-product-item product-item">'
251+
+'<div class="nxt-product-item-wrap">'
252+
+'<div class="nxt-image-wrapper">'
253+
+'<a href="{{productUrl}}" >'
254+
+'<img class="nxt-product-image" width="150" height="150" src="{{imageUrl}}" alt="" onerror="this.style.display=\'none\';">'
255+
+'{{#if_on_sale this}}<div class="on-sale">Sale</div>{{/if_on_sale}}'
256+
+'</a>'
257+
+'</div>'
258+
+'<div class="nxt-product-details">'
259+
+'<div class="nxt-product-col1">'
260+
+'<div class="nxt-product-name">'
261+
+'<a href="{{productUrl}}" >{{title}}</a>'
262+
+'</div>'
263+
+'<div class="nxt-product-description">After more than 14,000 live fittings with lady golfers, Bridgestone Golf has developed a ladies specific model engineered for moderate swing speeds. The new Lady Precept is designed to produce higher launch ...</div>'
264+
+'</div>'
265+
+'<div class="nxt-product-col2">'
266+
+'{{#if_on_sale this}}<del class="price2">${{format_price retail_price}}&nbsp;</del>{{/if_on_sale}}'
267+
+'<span class="hidden">&nbsp;</span> ${{format_price price}} '
268+
+'<div class="nxt-product-buttons">'
269+
+'<input type="button" class="nxt-btn-primary" onclick="top.location.href=\'{{productUrl}}\'" value="More Info">'
270+
+'</div>'
271+
+'</div>'
272+
+'</div>'
273+
+'</div>'
274+
+'</li>'
275+
+'{{/products}}'
276+
+'</ul>'
277+
}
278+
```
279+
280+
In this case, we need to also mention the different view types under 'viewTypes':
281+
282+
```
283+
,viewTypes: ['grid', 'list']
284+
```
285+
286+
- **viewTypeContainerSelector** : The selector for the container of view types
287+
288+
```
289+
,viewTypeContainerSelector: '.view_type_select'
290+
```
291+
292+
- **viewTypeContainerTemp** : The template which paints the view type to the view type container selector
293+
294+
```
295+
,viewTypeContainerTemp:
296+
'{{#options}}'
297+
+'<li class="nxt-{{#if selected}}current{{/if}}">'
298+
+'<a title="{{value}} View" class="nxt-{{value}}view-button" unbxdviewtype="{{value}}">'
299+
+'<span class="icon-{{value}}view">'
300+
+'</span>'
301+
+'</a>'
302+
+'</li>'
303+
+'{{/options}}'
304+
```
305+
216306
- **searchResultContainer** : The jQuery selector of DOM element to append the HTML generated from **searchResultSetTemp** (*#results_container* in the first image).
217307
- **isClickNScroll** : Set it to **true** if you want users to click an element to fetch the next page.
218308
- **clickNScrollSelector** : The jQuery selector of the DOM element that can be clicked for displaying the next page.(PS. in this case the new results will be appended to the **searchResultContainer**.)

unbxdSearch.js

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,16 @@ var unbxdSearchInit = function(jQuery, Handlebars){
605605
,setEvents : function(){
606606
var self = this;
607607

608+
var changeViewType = function(e) {
609+
e.preventDefault();
610+
var $t = jQuery(this);
611+
var selected = $t.attr("unbxdviewtype");
612+
613+
self.setViewType(selected);
614+
615+
self.callResults(self.paintResultSet, true);
616+
};
617+
608618
var changeSort = function(e){
609619
e.preventDefault();
610620
var $t = jQuery(this),
@@ -901,6 +911,10 @@ var unbxdSearchInit = function(jQuery, Handlebars){
901911
}
902912
}, 3000);
903913
}
914+
915+
if (this.options.searchResultSetTemp !== null && typeof this.options.searchResultSetTemp === 'object') {
916+
jQuery(this.options.viewTypeContainerSelector).on("click", '[unbxdviewtype]',changeViewType);
917+
}
904918
}
905919
,addSort : function(field, dir){
906920
this.params.sort[field] = dir || "desc";
@@ -980,6 +994,15 @@ var unbxdSearchInit = function(jQuery, Handlebars){
980994
,getPageSize : function(){
981995
return this.params.extra.rows;
982996
}
997+
998+
,setViewType: function(viewType) {
999+
this.params.extra.view = viewType;
1000+
return this;
1001+
}
1002+
,getViewType: function() {
1003+
return this.params.extra.view;
1004+
}
1005+
9831006
,addQueryParam : function(key, value, dontOverried){
9841007
if(!(key in this.params.extra) || !dontOverried){
9851008
this.params.extra[key] = value;
@@ -1086,7 +1109,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
10861109
}
10871110
} else if(key === 'wt' || key === 'format') {
10881111
nonhistoryPath += '&' + key + '=' + encodeURIComponent(value);
1089-
} else if(this.isUsingPagination() && key === 'rows'){
1112+
} else if((this.isUsingPagination() && key === 'rows') || key === "view"){
10901113
url += '&' + key + '=' + encodeURIComponent(value);
10911114
} else if(this.defaultParams.hasOwnProperty('extra') && this.defaultParams.extra.hasOwnProperty(key)){
10921115
nonhistoryPath += '&' + key + '=' + encodeURIComponent(value);
@@ -1182,6 +1205,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
11821205
format : "json"
11831206
,page : 1
11841207
,rows : 12
1208+
,view : this.options.viewTypes[0]
11851209
}
11861210
};
11871211

@@ -1275,6 +1299,12 @@ var unbxdSearchInit = function(jQuery, Handlebars){
12751299
if("start" in obj)
12761300
params.extra.page = (parseInt(obj.start) / parseInt(params.extra.rows)) + 1;
12771301

1302+
if(!("view" in obj)) {
1303+
params.extra.view = this.options.viewTypes[0];
1304+
} else {
1305+
params.extra.view = obj["view"];
1306+
}
1307+
12781308
return params;
12791309
}
12801310
,paintResultSet: function(obj){
@@ -1384,6 +1414,16 @@ var unbxdSearchInit = function(jQuery, Handlebars){
13841414

13851415
if(this.getClass(this.options.searchResultSetTemp) == 'Function'){
13861416
this.options.searchResultSetTemp.call(this,obj);
1417+
} else if (this.options.searchResultSetTemp !== null && typeof this.options.searchResultSetTemp === 'object') {
1418+
this.paintViewTypes(obj);
1419+
var currentViewType = this.getViewType();
1420+
if (!this.compiledResultTemp) {
1421+
this.compiledResultTemp = {};
1422+
this.options.viewTypes.forEach(function(val) {
1423+
this.compiledResultTemp[val] = Handlebars.compile(this.options.searchResultSetTemp[val]);
1424+
}.bind(this));
1425+
}
1426+
jQuery(this.options.searchResultContainer).html(this.compiledResultTemp[currentViewType](obj.response));
13871427
}else{
13881428
if(!this.compiledResultTemp)
13891429
this.compiledResultTemp = Handlebars.compile(this.options.searchResultSetTemp);
@@ -1452,6 +1492,28 @@ var unbxdSearchInit = function(jQuery, Handlebars){
14521492
options: pageSizeOptions
14531493
}));
14541494
}
1495+
1496+
,paintViewTypes: function(obj) {
1497+
if ("error" in obj || this.options.viewTypeContainerSelector.length <= 0) {
1498+
return;
1499+
}
1500+
if (!this.compiledViewTypesContainerTemp) {
1501+
this.compiledViewTypesContainerTemp = Handlebars.compile(this.options.viewTypeContainerTemp);
1502+
}
1503+
var viewTypeOptions = this.options.viewTypes.map(function(opt) {
1504+
1505+
var values = {};
1506+
values["value"] = opt;
1507+
values["selected"] = this.getViewType() == opt ? true : false;
1508+
return values;
1509+
1510+
}.bind(this));
1511+
jQuery(this.options.viewTypeContainerSelector).html(this.compiledViewTypesContainerTemp({
1512+
options: viewTypeOptions
1513+
}));
1514+
return this.getViewType();
1515+
}
1516+
14551517
,paintPagination: function(obj) {
14561518
if("error" in obj)
14571519
return;

0 commit comments

Comments
 (0)