-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
options_parser.js
87 lines (67 loc) · 2.04 KB
/
options_parser.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
/*
* typeahead.js
* https://github.com/twitter/typeahead.js
* Copyright 2013 Twitter, Inc. and other contributors; Licensed MIT
*/
var oParser = (function() {
return {
local: getLocal,
prefetch: getPrefetch,
remote: getRemote
};
function getLocal(o) {
return o.local || null;
}
function getPrefetch(o) {
var prefetch, defaults;
defaults = {
url: null,
thumbprint: '',
ttl: 24 * 60 * 60 * 1000, // 1 day
filter: null,
ajax: {}
};
if (prefetch = o.prefetch || null) {
// support basic (url) and advanced configuration
prefetch = _.isString(prefetch) ? { url: prefetch } : prefetch;
prefetch = _.mixin(defaults, prefetch);
prefetch.thumbprint = VERSION + prefetch.thumbprint;
prefetch.ajax.method = prefetch.ajax.method || 'get';
prefetch.ajax.dataType = prefetch.ajax.dataType || 'json';
!prefetch.url && $.error('prefetch requires url to be set');
}
return prefetch;
}
function getRemote(o) {
var remote, defaults;
defaults = {
url: null,
wildcard: '%QUERY',
replace: null,
rateLimitBy: 'debounce',
rateLimitWait: 300,
send: null,
filter: null,
ajax: {}
};
if (remote = o.remote || null) {
// support basic (url) and advanced configuration
remote = _.isString(remote) ? { url: remote } : remote;
remote = _.mixin(defaults, remote);
remote.rateLimiter = /^throttle$/i.test(remote.rateLimitBy) ?
byThrottle(remote.rateLimitWait) : byDebounce(remote.rateLimitWait);
remote.ajax.method = remote.ajax.method || 'get';
remote.ajax.dataType = remote.ajax.dataType || 'json';
delete remote.rateLimitBy;
delete remote.rateLimitWait;
!remote.url && $.error('remote requires url to be set');
}
return remote;
function byDebounce(wait) {
return function(fn) { return _.debounce(fn, wait); };
}
function byThrottle(wait) {
return function(fn) { return _.throttle(fn, wait); };
}
}
})();