Repository navigation
Expand file tree
/
Copy pathplugin.js
More file actions
163 lines (131 loc) · 3.81 KB
/
Copy pathplugin.js
File metadata and controls
163 lines (131 loc) · 3.81 KB
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
'use strict';
// setup DOM element
var DOM = require('../common/dom.js');
var $ = require('jquery');
DOM.element = $;
// setup utils functions
var _ = require('../common/utils.js');
_.isArray = $.isArray;
_.isFunction = $.isFunction;
_.isObject = $.isPlainObject;
_.bind = $.proxy;
_.each = function(collection, cb) {
// stupid argument order for jQuery.each
$.each(collection, reverseArgs);
function reverseArgs(index, value) {
return cb(value, index);
}
};
_.map = $.map;
_.mixin = $.extend;
_.Event = $.Event;
var Typeahead = require('../autocomplete/typeahead.js');
var EventBus = require('../autocomplete/event_bus.js');
var old;
var typeaheadKey;
var methods;
old = $.fn.autocomplete;
typeaheadKey = 'aaAutocomplete';
methods = {
// supported signatures:
// function(o, dataset, dataset, ...)
// function(o, [dataset, dataset, ...])
initialize: function initialize(o, datasets) {
datasets = _.isArray(datasets) ? datasets : [].slice.call(arguments, 1);
o = o || {};
return this.each(attach);
function attach() {
var $input = $(this);
var eventBus = new EventBus({el: $input});
var typeahead;
typeahead = new Typeahead({
input: $input,
eventBus: eventBus,
dropdownMenuContainer: o.dropdownMenuContainer,
hint: o.hint === undefined ? true : !!o.hint,
minLength: o.minLength,
autoselect: o.autoselect,
autoselectOnBlur: o.autoselectOnBlur,
tabAutocomplete: o.tabAutocomplete,
openOnFocus: o.openOnFocus,
templates: o.templates,
debug: o.debug,
clearOnSelected: o.clearOnSelected,
cssClasses: o.cssClasses,
datasets: datasets,
keyboardShortcuts: o.keyboardShortcuts,
appendTo: o.appendTo,
autoWidth: o.autoWidth
});
$input.data(typeaheadKey, typeahead);
}
},
open: function open() {
return this.each(openTypeahead);
function openTypeahead() {
var $input = $(this);
var typeahead;
if (typeahead = $input.data(typeaheadKey)) {
typeahead.open();
}
}
},
close: function close() {
return this.each(closeTypeahead);
function closeTypeahead() {
var $input = $(this);
var typeahead;
if (typeahead = $input.data(typeaheadKey)) {
typeahead.close();
}
}
},
val: function val(newVal) {
// mirror jQuery#val functionality: read operate on first match,
// write operates on all matches
return !arguments.length ? getVal(this.first()) : this.each(setVal);
function setVal() {
var $input = $(this);
var typeahead;
if (typeahead = $input.data(typeaheadKey)) {
typeahead.setVal(newVal);
}
}
function getVal($input) {
var typeahead;
var query;
if (typeahead = $input.data(typeaheadKey)) {
query = typeahead.getVal();
}
return query;
}
},
destroy: function destroy() {
return this.each(unattach);
function unattach() {
var $input = $(this);
var typeahead;
if (typeahead = $input.data(typeaheadKey)) {
typeahead.destroy();
$input.removeData(typeaheadKey);
}
}
}
};
$.fn.autocomplete = function(method) {
var tts;
// methods that should only act on intialized typeaheads
if (methods[method] && method !== 'initialize') {
// filter out non-typeahead inputs
tts = this.filter(function() { return !!$(this).data(typeaheadKey); });
return methods[method].apply(tts, [].slice.call(arguments, 1));
}
return methods.initialize.apply(this, arguments);
};
$.fn.autocomplete.noConflict = function noConflict() {
$.fn.autocomplete = old;
return this;
};
$.fn.autocomplete.sources = Typeahead.sources;
$.fn.autocomplete.escapeHighlightedString = _.escapeHighlightedString;
module.exports = $.fn.autocomplete;