Skip to content

Commit

Permalink
Allow location function to be passed in and not depend on window object
Browse files Browse the repository at this point in the history
  • Loading branch information
Siavash authored and Siavash committed Mar 27, 2017
1 parent 67ed35e commit 446b58e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/core/SearchkitManager.md
Expand Up @@ -30,6 +30,8 @@ const searchkit = new SearchkitManager(host, {

* **createHistory** - A function which returns an instance of a history obj, defaults to `createBrowserHistory from history@4.6.1`

* **getLocation** - A function which returns the current location which has to implement the LocationInterface {pathname:String,search:String}, defaults to `()=> window.location`

* **searchUrlPath** - A the search endpoint name, defaults to `/_search`. Used if your server proxy endpoint needs a different url convention.
## Default Queries
Sometimes we need to apply a default query which affects the entire search and is not serialized to the browser url.
Expand Down
10 changes: 6 additions & 4 deletions src/core/SearchkitManager.ts
Expand Up @@ -22,6 +22,7 @@ import {after} from "lodash"
export interface SearchkitOptions {
useHistory?:boolean,
createHistory?:Function,
getLocation?:Function,
searchOnLoad?:boolean,
httpHeaders?:Object,
basicAuth?:string,
Expand Down Expand Up @@ -67,7 +68,8 @@ export class SearchkitManager {
useHistory:true,
httpHeaders:{},
searchOnLoad:true,
createHistory:createHistoryInstance
createHistory:createHistoryInstance,
getLocation:()=> window.location
})
this.host = host

Expand Down Expand Up @@ -154,7 +156,7 @@ export class SearchkitManager {

runInitialSearch(){
if(this.options.searchOnLoad) {
this._searchWhenCompleted(window.location)
this._searchWhenCompleted(this.options.getLocation())
}
}

Expand All @@ -172,15 +174,15 @@ export class SearchkitManager {
const historyMethod = (replaceState) ?
this.history.replace : this.history.push

let url = window.location.pathname + "?" + encodeObjUrl(this.state)
let url = this.options.getLocation().pathname + "?" + encodeObjUrl(this.state)
historyMethod.call(this.history, url)
}
}

buildSearchUrl(extraParams = {}){
const params = defaults(extraParams, this.state || this.accessors.getState())
const queryString = qs.stringify(params, { encode: true })
return window.location.pathname + '?' + queryString
return this.options.getLocation().pathname + '?' + queryString
}

reloadSearch(){
Expand Down

0 comments on commit 446b58e

Please sign in to comment.