Skip to content

Commit ad75f4d

Browse files
authored
Merge pull request #46 from santosh1994/navigation
Navigation
2 parents ef6df4b + 635ed94 commit ad75f4d

File tree

4 files changed

+122
-22
lines changed

4 files changed

+122
-22
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ Unbxd JavaScript Search library
22
===============================
33
[![Build Status](https://travis-ci.org/unbxd/js-sdk.svg?branch=master)](https://travis-ci.org/unbxd/js-sdk.svg?branch=master)
44

5-
unbxdSearch.js library can be used to integrated UNBXD search or browse on client side. It supports History API, so users can share the URI.
5+
unbxdSearch.js library can be used to integrated UNBXD search and category page on client side. It supports History API, so users can share the URI.
66

77
Note : _*This library makes use of jQuery selectors and Handlebars templates.*_
88

99
_*Please find an example of implementation in demo folder.*_
1010

11-
##Usage
11+
## Usage
1212
Just include [unbxdSearch.js](//d21gpk1vhmjuf5.cloudfront.net/unbxdSearch.js) in HTML and include the configuration.
1313

14-
##configuration
14+
## configuration
1515
Consider a normal search page with basic layout as shown in the figure below and respective configuration below the image.
1616

1717
![Basic search layout](https://raw.githubusercontent.com/unbxd/js-sdk/master/images/search_layout.png "Basic search layout")
@@ -137,11 +137,11 @@ Consider a normal search page with basic layout as shown in the figure below and
137137

138138
- **siteName** : This value can be found in UNBXD dashboard. It is unique for every search site created in the dashboard.
139139
- **APIKey** : This is a unique for every user account. It can also be found in dashboard.
140-
- **type** : It has to be either _*search*_ or _*browse*_.
141-
- **getCategoryId** : This option has to be a function which return the category_id in case of *browse*. Please ignore incase of *search*.
142-
- **inputSelector** : The jQuery selector for search input. Please make sure that the form of this input has no action and method is GET. Please ignore incase of *browse*.
143-
- **searchButtonSelector** : The jQuery selector for search submit button. Please ignore incase of *browse*.
144-
- **spellCheck** : The jQuery selector for DOM element to display spell suggestion. Please ignore incase of *browse*.
140+
- **type** : It has to be either _*search*_, _*browse*_ or _*category*_.
141+
- **getCategoryId** : This option has to be a function which return the category_id in case of *browse* and *category*. Please ignore incase of *search*.
142+
- **inputSelector** : The jQuery selector for search input. Please make sure that the form of this input has no action and method is GET. Please ignore incase of *browse* and *category*.
143+
- **searchButtonSelector** : The jQuery selector for search submit button. Please ignore incase of *browse* and *category*.
144+
- **spellCheck** : The jQuery selector for DOM element to display spell suggestion. Please ignore incase of *browse* and *category*.
145145
- **spellCheckTemp** : Handlebars template for generating the spell suggestion template.
146146
```javascript
147147
...
@@ -154,7 +154,7 @@ Consider a normal search page with basic layout as shown in the figure below and
154154
suggestion : "something else"
155155
}
156156
```
157-
- **searchQueryDisplay** : The jQuery selector of DOM element to display the query (which use has searched for) and total number of results from search. Please ignore incase of *browse*.
157+
- **searchQueryDisplay** : The jQuery selector of DOM element to display the query (which use has searched for) and total number of results from search. Please ignore incase of *browse* and *category*.
158158
- **searchQueryDisplayTemp** : Handlebars template for displaying the search query and total number of results. Please ignore incase of *browse*.
159159
```javascript
160160
...

karma.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module.exports = function (config) {
3434
'./unbxdSearch.js',
3535
'./app.js',
3636
'mock/*.json',
37+
'test/navigation/*.spec.js',
3738
'test/search/*.spec.js',
3839
],
3940

test/navigation/init.spec.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
describe('Navigation - On Init', function () {
2+
3+
before(function(done){
4+
this.searchTest = fixture.load('mock/searchTestResponse.json');
5+
6+
//setup document to hold search results
7+
document.body.innerHTML = __html__['index.html'];
8+
9+
//initialize search
10+
var searchConfig = jQuery.extend({}, window.config);
11+
searchConfig.type = 'category';
12+
searchConfig.getCategoryId = function(){
13+
return '/clothing/women/jewelry-hair/cat/cat1910053';
14+
};
15+
this.searchobj = new window.Unbxd.setSearch(searchConfig);
16+
17+
//stub search ajax call with mock response
18+
this.stub = sinon.stub(jQuery, 'ajax').yieldsTo('success',this.searchTest);
19+
20+
//spy OnIntialResultLoad, OnFacetLoad, OnPageLoad and OnNoResult
21+
this.spyOnIntialResultLoad = sinon.spy(this.searchobj.options,
22+
'onIntialResultLoad');
23+
this.spyOnFacetLoad = sinon.spy(this.searchobj.options, 'onFacetLoad');
24+
this.spyOnPageLoad = sinon.spy(this.searchobj.options, 'onPageLoad');
25+
this.spyOnNoResult = sinon.spy(this.searchobj.options, 'onNoResult');
26+
27+
this.searchobj.callResults(this.searchobj.paintResultSet);
28+
done();
29+
});
30+
31+
after(function(){
32+
this.stub.restore();
33+
fixture.cleanup();
34+
});
35+
36+
it('should call navigation handler', function(){
37+
expect(this.searchobj.getHostNPath().indexOf('/category') > -1)
38+
.to.be.true;
39+
});
40+
41+
it('Should call onIntialResultLoad', function(){
42+
expect(this.spyOnIntialResultLoad.called).to.equal(true);
43+
});
44+
45+
it('Should call onFacetLoad', function(){
46+
expect(this.spyOnFacetLoad.called).to.equal(true);
47+
});
48+
49+
it('Should not call onPageLoad', function(){
50+
expect(this.spyOnPageLoad.called).to.equal(false);
51+
});
52+
53+
it('Should not call onNoResult', function(){
54+
expect(this.spyOnNoResult.called).to.equal(false);
55+
});
56+
});

unbxdSearch.js

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//uglifyjs unbxdSearch.js -o unbxdSearch.min.js && gzip -c unbxdSearch.min.js > unbxdSearch.min.js.gz && aws s3 cp unbxdSearch.min.js.gz s3://unbxd/unbxdSearch.js --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers --content-encoding gzip --cache-control max-age=3600
22
var unbxdSearchInit = function(jQuery, Handlebars){
33
window.Unbxd = window.Unbxd || {};
4-
Unbxd.jsSdkVersion = "1.0.10";
4+
Unbxd.jsSdkVersion = "2.0.0";
55

66
// Production steps of ECMA-262, Edition 5, 15.4.4.14
77
// Reference: http://es5.github.io/#x15.4.4.14
@@ -339,6 +339,30 @@ var unbxdSearchInit = function(jQuery, Handlebars){
339339
this.init();
340340
}
341341

342+
var isTypeSearch = function isTypeSearch(type) {
343+
return type === "search";
344+
};
345+
346+
var isTypeBrowse = function isTypeBrowse(type) {
347+
return type === "browse";
348+
};
349+
350+
var isTypeCategory = function isTypeCategory(type) {
351+
return type === "category";
352+
};
353+
354+
var isTypeBrowseOrCategory = function isTypeBrowseOrCategory(type) {
355+
return isTypeBrowse(type) || isTypeCategory(type);
356+
};
357+
358+
var isCategoryIdPresent = function isCategoryIdPresent(params) {
359+
return ("categoryId" in params && params.categoryId.trim().length > 0);
360+
};
361+
362+
var isQueryPresent = function isQueryPresent(params) {
363+
return ("query" in params && params.query.trim().length > 0);
364+
};
365+
342366
Handlebars.registerHelper('prepareFacetName', function(txt){
343367
txt = txt.replace("_fq","");
344368
return txt.replace("_"," ");
@@ -556,8 +580,10 @@ var unbxdSearchInit = function(jQuery, Handlebars){
556580

557581
this.params = finalParams;
558582

559-
this.params.categoryId = this.options.type == "browse" && typeof this.options.getCategoryId == "function" ?
560-
this.options.getCategoryId() : (this.params.categoryId ? this.params.categoryId : "");
583+
this.params.categoryId = isTypeBrowseOrCategory(this.options.type) &&
584+
typeof this.options.getCategoryId == 'function'
585+
? this.options.getCategoryId()
586+
: this.params.categoryId ? this.params.categoryId : '';
561587

562588
this.setPage("page" in finalParams.extra ? finalParams.extra.page : 1)
563589
.setPageSize("rows" in finalParams.extra ? finalParams.extra.rows : this.options.pageSize);
@@ -580,9 +606,11 @@ var unbxdSearchInit = function(jQuery, Handlebars){
580606
if(typeof this.options.setDefaultFilters == "function")
581607
this.setDefaultParams(this.params);
582608

583-
if((this.options.type == "search" && "query" in this.params && this.params["query"].trim().length > 0) ||
584-
(this.options.type == "browse" && "categoryId" in this.params && this.params["categoryId"].trim().length > 0))
585-
this.callResults(this.paintResultSet);
609+
if (
610+
(isTypeSearch(this.options.type) && isQueryPresent(this.params)) ||
611+
(isTypeBrowseOrCategory(this.options.type) && isCategoryIdPresent(this.params))
612+
)
613+
this.callResults(this.paintResultSet);
586614
}
587615
,getClass : function(object){
588616
return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1];
@@ -1001,7 +1029,11 @@ var unbxdSearchInit = function(jQuery, Handlebars){
10011029
return !this.options.isAutoScroll && this.options.isPagination;
10021030
}
10031031
,getHostNPath: function(){
1004-
return "//search.unbxdapi.com/"+ this.options.APIKey + "/" + this.options.siteName + "/" + (this.options.type == "browse" ? "browse" : "search" )
1032+
var handler = "search";
1033+
if(isTypeBrowseOrCategory(this.options.type)) {
1034+
handler = this.options.type;
1035+
}
1036+
return "//search.unbxdapi.com/"+ this.options.APIKey + "/" + this.options.siteName + "/" + handler;
10051037
}
10061038
,getUrlSubstring: function(){
10071039
return window.location.search.substring(1) || window.location.hash.substring(1);
@@ -1031,6 +1063,8 @@ var unbxdSearchInit = function(jQuery, Handlebars){
10311063
url += '&'+ this.options.searchQueryParam +'='+ encodeURIComponent(this.params.query);
10321064
}else if(this.options.type == "browse" && this.params['categoryId'] != undefined){
10331065
url += '&category-id=' + encodeURIComponent(this.params.categoryId);
1066+
} else if(isTypeCategory(this.options.type) && this.params.categoryId !== undefined) {
1067+
url += "&p=" + encodeURIComponent(this.params.categoryId);
10341068
}
10351069

10361070
for(var x in this.params.filters){
@@ -1310,6 +1344,10 @@ var unbxdSearchInit = function(jQuery, Handlebars){
13101344
if("category-id" in obj)
13111345
params.categoryId = obj["category-id"];
13121346

1347+
//lets get categoryId(navigation)
1348+
if("p" in obj)
1349+
params.categoryId = obj["p"];
1350+
13131351
//lets get boost
13141352
if("boost" in obj)
13151353
params.extra.boost = obj.boost;
@@ -1418,13 +1456,18 @@ var unbxdSearchInit = function(jQuery, Handlebars){
14181456
this.productEndIdx = (this.getPage() * this.getPageSize() <= obj.response.numberOfProducts) ?
14191457
this.getPage() * this.getPageSize() : obj.response.numberOfProducts;
14201458
this.totalPages = Math.ceil(obj.response.numberOfProducts/this.getPageSize());
1459+
var queryParams = {
1460+
numberOfProducts : obj.response.numberOfProducts
1461+
,start: this.productStartIdx
1462+
,end: this.productEndIdx
1463+
}
1464+
if (isTypeCategory(this.options.type)) {
1465+
queryParams.categoryId = obj.searchMetaData.queryParams.p
1466+
} else {
1467+
queryParams.query = obj.searchMetaData.queryParams.q
1468+
}
14211469

1422-
jQuery(this.options.searchQueryDisplay).html(this.compiledSearchQueryTemp({
1423-
query : obj.searchMetaData.queryParams.q
1424-
,numberOfProducts : obj.response.numberOfProducts
1425-
,start: this.productStartIdx
1426-
,end: this.productEndIdx
1427-
})).show();
1470+
jQuery(this.options.searchQueryDisplay).html(this.compiledSearchQueryTemp(queryParams)).show();
14281471

14291472
this.paintSort(obj);
14301473
this.paintPageSize(obj);

0 commit comments

Comments
 (0)