forked from sergeyt/meteor-typeahead
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
468 lines (407 loc) · 12 KB
/
index.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
var global = this || window;
function identity(x) { return x; }
// String.trim polyfill
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, '');
};
}
var typeaheadEvents = [
"active",
"idle",
"open",
"close",
"change",
"render",
"select",
"autocomplete",
"asyncrequest",
"asynccancel",
"asyncreceive",
];
/**
* Activates typeahead behavior for given element.
* @param element (required) The DOM element to infect.
* @param source (optional) The custom data source.
*/
Meteor.typeahead = function(element, source) {
var $e = $(element);
var datasets = resolve_datasets($e, source);
$e.typeahead('destroy');
var options = $e.data('options') || {};
if (typeof options != 'object') {
options = {};
}
function get_bool(name, defval) {
var val = $e.data(name);
return val === undefined ? defval : !!val;
}
// other known options passed via data attributes
var highlight = get_bool('highlight', false);
var hint = get_bool('hint', true);
var autoselect = get_bool('autoselect', false);
var minLength = get_min_length($e);
options = $.extend(options, {
highlight: highlight,
hint: hint,
minLength: minLength,
autoselect: autoselect
});
var instance;
if (Array.isArray(datasets)) {
instance = $e.typeahead.apply($e, [options].concat(datasets));
} else {
var dataset = datasets;
// TODO remove this when typeahead.js will support minLength = 0
if (minLength === 0 && dataset.local) {
// based on @snekse suggestion (see https://github.com/twitter/typeahead.js/pull/719)
var altSource = dataset.source;
dataset.source = function(query, sync, async) {
return query ? altSource(query, sync, async) : sync(dataset.local());
};
}
instance = $e.typeahead(options, dataset);
}
// bind event handlers
typeaheadEvents.forEach(function(name) {
var fn = resolve_template_function($e[0], $e.data(name));
if ($.isFunction(fn)) {
instance.on('typeahead:' + name, fn);
}
});
// fix to apply bootstrap form-control to tt-hint
// TODO support other classes if needed
if ($e.hasClass('form-control')) {
$e.parent('.twitter-typeahead').find('.tt-hint').addClass('form-control');
}
// TODO remove this when typeahead.js will support minLength = 0
if (minLength === 0) {
$e.on('focus', function() {
if ($e.val() === '') {
$e.data('ttTypeahead').input.trigger('queryChanged', '');
}
});
}
return instance;
};
/**
* Activates all typeahead elements.
* @param selector (optional) selector to find typeahead elements to be activated
*/
Meteor.typeahead.inject = function(selector) {
if (!selector) {
selector = '.typeahead';
}
// See if we have a template instance to reference
var template = Template.instance();
if (!template) {
// If we don't, just init on the entire DOM
$(selector).each(init_typeahead);
} else {
// Otherwise just init this template's typeaheads
template.$(selector).each(init_typeahead);
}
};
function init_typeahead(index, element) {
try {
if (!$(element).data('ttTypeahead')) {
Meteor.typeahead(element);
}
} catch (err) {
console.log(err);
return;
}
}
function resolve_datasets($e, source) {
var element = $e[0];
var datasets = $e.data('sources') || $e.data('sets');
if (datasets) {
if (typeof datasets == 'string') {
datasets = resolve_template_function(element, datasets);
}
if ($.isFunction(datasets)) {
datasets = datasets() || [];
}
return datasets.map(function(ds) {
return make_bloodhound(ds);
});
}
var name = normalize_dataset_name($e.attr('data-source-name') || $e.attr('name') || $e.attr('id') || 'dataset');
var limit = $e.data('limit');
var templateName = $e.data('template'); // specifies name of custom template
var templates = $e.data('templates'); // specifies custom templates
var valueKey = $e.data('value-key') || 'value';
var minLength = get_min_length($e);
if (!source) {
source = $e.data('source') || [];
}
var dataset = {
name: name,
valueKey: valueKey,
displayKey: valueKey,
minLength: minLength,
};
if (limit) {
dataset.limit = limit;
}
// support for custom templates
if (templateName) {
dataset.template = templateName;
}
// parse string with custom templates if it is specified
if (templates && typeof templates === 'string') {
set_templates(dataset, templates);
}
dataset.templates = make_templates(dataset);
if (typeof source === 'string') {
dataset.async = true;
if (source.indexOf('/') >= 0) { // support prefetch urls
isprefetch = true;
dataset.prefetch = {
url: source,
filter: function(list) {
return (list || []).map(value_wrapper(dataset));
}
};
return make_bloodhound(dataset);
}
source = resolve_data_source(element, source);
}
if ($.isArray(source) || ($.isFunction(source) && source.length === 0)) {
dataset.local = source;
return make_bloodhound(dataset);
}
dataset.source = source;
return dataset;
}
function get_min_length($e) {
var value = parseInt($e.data('min-length'));
return isNaN(value) || value < 0 ? 1 : value;
}
// typeahead.js throws error if dataset name does not meet /^[_a-zA-Z0-9-]+$/
function normalize_dataset_name(name) {
return name.replace(/[\]\[\.]/g, '_');
}
// Parses string with template names and set appropriate dataset properties.
function set_templates(dataset, templates) {
var templateKeys = {
header: 1,
footer: 1,
suggestion: 1,
template: 1, // suggestion alias
notFound: 1,
empty: 1, // notFound alias
pending: 1
};
var pairs = templates.split(/[;,]+/);
pairs.map(function(s) {
var p = s.split(/[:=]+/).map(function(it) { return it.trim(); });
switch (p.length) {
case 1: // set suggestion template when no key is specified
return ['template', p[0]];
case 2:
return (p[0] in templateKeys) ? [p[0], p[1]] : null;
default:
return null;
}
}).filter(identity).forEach(function(p) {
dataset[p[0]] = p[1];
});
}
// Resolves data source function.
function resolve_data_source(element, name) {
var fn = resolve_template_function(element, name);
if ($.isFunction(fn)) {
return fn;
}
// collection.name
var path = name.split('.');
if (path.length > 0) {
var collection = find_collection(path[0]);
if (collection) {
var property = path.length > 1 ? path[1] : "";
return function() {
return collection.find().fetch()
.map(function(it) {
var value = property ? it[property] : it.name || it.title;
// wrap to object to use object id in selected event handler
return value ? {value: value, id: it._id} : "";
})
.filter(identity);
};
}
}
console.log("Unable to resolve data source function '%s'.", name);
return [];
}
function find_collection(name) {
if (typeof Mongo != "undefined" && typeof Mongo.Collection != "undefined") {
// when use dburles:mongo-collection-instances
if ($.isFunction(Mongo.Collection.get)) {
return Mongo.Collection.get(name);
}
}
if (global) {
return global[name] || global[name.toLowerCase()] || null;
}
return null;
}
// Resolves function with specified name from context of given element.
function resolve_template_function(element, name) {
var fn = null;
var opts = {sync:false};
// traverse view hierarchy and find helper function
var view = Blaze.getView(element);
while (view) {
fn = resolve_helper(view, name, opts);
if ($.isFunction(fn)) {
break;
}
view = view.parentView;
}
if (!$.isFunction(fn)) {
return null;
}
// calls template helper function with Template.instance() context
function invoke(args) {
// use Blaze._withCurrentView internal function to set current view
// also since meteor 1.2 no need to use Template._withTemplateInstanceFunc
// since the function is already bound to template instance
return Blaze._withCurrentView(view, function() {
return fn.apply(null, args);
});
}
if (opts.sync) { // local dataset?
return function() {
return invoke(Array.prototype.slice.call(arguments));
};
}
// async data source
return function(a) {
return invoke(Array.prototype.slice.call(arguments));
};
}
function resolve_helper(view, name, opts) {
if (!view.template) {
return null;
}
// we need to known whether the helper function is async
// use view.template.__helpers to determine that
// since meteor 1.2 Blaze._getTemplateHelper wraps
// the helper function binding it to template context
var fn = view.template.__helpers.get(name) || view.template[name];
if (!$.isFunction(fn)) {
return null;
}
opts.sync = fn.length === 0;
return Blaze._getTemplateHelper(view.template, name, function() {
return view.templateInstance();
});
}
// Returns HTML template function that generates HTML string using data from suggestion item.
// This function is implemented using given meteor template specified by templateName argument.
function make_template_function(templateName) {
if (!templateName) {
throw new Error("templateName is not specified");
}
var tmpl = Template[templateName];
if (!tmpl) {
throw new Error("Template '" + templateName + "' is not defined");
}
return function(context) {
var div = $("<div/>");
if ($.isFunction(Blaze.renderWithData)) {
Blaze.renderWithData(tmpl, context, div[0]);
} else { // for meteor < v0.9
var range = UI.renderWithData(tmpl, context);
UI.insert(range, div[0]);
}
// return html wrapped into div to avoid visual issues
return div[0].outerHTML;
};
}
// Creates object with template functions (for header, footer, suggestion, empty templates).
function make_templates(dataset) {
function make(value) {
if (!value) {
return null;
}
if (typeof value === "string") {
if (value.indexOf('<') >= 0) { // detect HTML string
return value;
}
return make_template_function(value);
}
return $.isFunction(value) ? value : null;
}
var templates = {
header: make(dataset.header),
footer: make(dataset.footer),
suggestion: make(dataset.suggestion || dataset.template),
notFound: make(dataset.notFound || dataset.empty),
pending: make(dataset.pending)
};
return Object.keys(templates)
.filter(function(key) { return templates[key]; })
.reduce(function(result, key) {
result[key] = templates[key];
return result;
}, {});
}
// Returns function to map string value to plain JS object required by typeahead.
function value_wrapper(dataset) {
return function(value) {
if (typeof value === 'object') {
return value;
}
var item = {};
item[dataset.valueKey] = value;
return item;
};
}
// Creates Bloodhound suggestion engine based on given dataset.
function make_bloodhound(dataset) {
if (!dataset.template) {
if (Array.isArray(dataset.local)) {
dataset.local = dataset.local.map(value_wrapper(dataset));
} else if ($.isFunction(dataset.local) && dataset.local.length === 0) {
var localFn = dataset.local;
dataset.local = function() {
return (localFn() || []).map(value_wrapper(dataset));
};
}
}
var need_bloodhound = dataset.prefetch || Array.isArray(dataset.local) ||
$.isFunction(dataset.local) && dataset.local.length === 0;
var engine;
if (need_bloodhound) {
var options = $.extend({}, dataset, {
// TODO support custom tokenizers
datumTokenizer: Bloodhound.tokenizers.obj.whitespace(dataset.valueKey),
queryTokenizer: Bloodhound.tokenizers.whitespace
});
engine = new Bloodhound(options);
engine.initialize();
if ($.isFunction(dataset.local) && dataset.local.length === 0) {
// update data source on changing deps of local function
// TODO find better (functional) way to do that
var tracker = Template.instance() || Tracker;
tracker.autorun(function(comp) {
// TODO stop tracking if typeahead is explicitly destroyed (issue #70)
engine = new Bloodhound(options);
engine.initialize();
});
}
}
function bloodhound_source(query, cb) {
var fn = engine.ttAdapter();
return fn(query, cb);
}
var src = need_bloodhound || typeof dataset.local !== 'undefined' ?
{source: need_bloodhound ? bloodhound_source : dataset.local}
: {};
var templates = typeof dataset.templates === 'undefined' ?
{templates: make_templates(dataset)}
: {};
return $.extend({}, dataset, src, templates);
}