11'use strict' ;
22
33var executeApiFetch = require ( './apifetch' ) ;
4- var settings = require ( './settings' ) ;
4+ var Settings = require ( './settings' ) ;
5+ var util = require ( './util' ) ;
56
67var client = function ( sitekey ) {
78 this . sitekey = sitekey ;
9+ this . settings = new Settings ( ) ;
810
911 /**
1012 * Fetch search results
1113 *
12- * @param keyword
14+ * @param a1 Argument 1: Keyword. If no Argument 2, then this is the callback function and search is executed with
15+ * the previous keyword. If there is no Argument 2 and no previous keywords, then search is executed
16+ * without keyword (i.e. match all query)
17+ * @param a2 Callback function to call with search results
1318 */
14- this . search = function ( keyword , cb ) {
15- executeApiFetch ( this . sitekey , 'search' , keyword , settings . getSettings ( ) , cb ) ;
19+ this . search = function ( a1 , a2 ) {
20+
21+ var keyword = a1 ;
22+ var callback = a2 ;
23+
24+ // If function is called with callback only, use previous keyword from settings object
25+ if ( ! a2 && util . isFunction ( a1 ) ) {
26+ keyword = this . settings . getSettings ( ) . keyword ;
27+ callback = a1 ;
28+ }
29+
30+ this . settings . setKeyword ( keyword ) ;
31+ executeApiFetch ( this . sitekey , 'search' , this . settings . getSettings ( ) , callback ) ;
1632 } ;
1733
1834
@@ -21,15 +37,20 @@ var client = function(sitekey) {
2137 *
2238 * @param keyword
2339 */
24- this . suggest = function ( keyword , cb ) {
25- executeApiFetch ( this . sitekey , 'suggest' , keyword , settings . getSettings ( ) , cb ) ;
40+ this . suggest = function ( keyword , callback ) {
41+ executeApiFetch ( this . sitekey , 'suggest' , this . settings . getSettings ( ) , callback ) ;
2642 } ;
2743
2844
2945 /**
3046 * Public functions
3147 */
32- this . setLanguage = settings . setLanguage ;
48+ this . getSettings = function ( ) { return this . settings . getSettings ( ) ; }
49+ this . setLanguage = function ( lang ) { this . settings . setLanguage ( lang ) ; } ;
50+ this . setPaging = function ( page , pageSize , sortBy , sortOder ) { this . settings . setPaging ( page , pageSize , sortBy , sortOder ) ; } ;
51+ this . nextPage = function ( ) { this . settings . nextPage ( ) ; } ;
52+ this . previousPage = function ( ) { this . settings . previousPage ( ) ; } ;
53+
3354}
3455
3556module . exports = client ;
0 commit comments