forked from imankulov/asuggest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.asuggest.js
292 lines (268 loc) · 9.72 KB
/
jquery.asuggest.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
/*
* jQuery textarea suggest plugin
*
* Copyright (c) 2009-2010 Roman Imankulov
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* Requires:
* - jQuery (tested with 1.3.x and 1.4.x)
* - jquery.a-tools >= 1.4.1 (http://plugins.jquery.com/project/a-tools)
*/
/*globals jQuery,document */
(function ($) {
// workaround for Opera browser
if ($.browser.opera) {
$(document).keypress(function (e) {
if ($.asuggestFocused) {
$.asuggestFocused.focus();
$.asuggestFocused = null;
e.preventDefault();
e.stopPropagation();
}
});
}
$.asuggestKeys = {
UNKNOWN: 0,
SHIFT: 16,
CTRL: 17,
ALT: 18,
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
DEL: 46,
TAB: 9,
RETURN: 13,
ESC: 27,
COMMA: 188,
PAGEUP: 33,
PAGEDOWN: 34,
BACKSPACE: 8//,
//SPACE: 32
};
$.asuggestFocused = null;
$.disabled = false;
$.fn.disable = function () {
$.disabled = true;
}
$.fn.enable = function () {
$.disabled = false;
}
$.fn.asuggest = function (suggests, options) {
return this.each(function () {
$.makeSuggest(this, suggests, options);
});
};
$.fn.asuggest.defaults = {
'delimiters': '\n ',
'minChunkSize': 1,
'cycleOnTab': true,
'autoComplete': true,
'endingSymbols': ' ',
'stopSuggestionKeys': [$.asuggestKeys.RETURN, $.asuggestKeys.SPACE],
'ignoreCase': false,
'serviceUrl': '',
'params': {}
};
/* Make suggest:
*
* create and return jQuery object on the top of DOM object
* and store suggests as part of this object
*
* @param area: HTML DOM element to add suggests to
* @param suggests: The array of suggest strings
* @param options: The options object
*/
$.makeSuggest = function (area, suggests, options) {
options = $.extend({}, $.fn.asuggest.defaults, options);
var KEY = $.asuggestKeys,
$area = $(area);
$area.suggests = suggests;
$area.options = options;
/* Internal method: get the chunk of text before the cursor */
$area.getChunk = function () {
var delimiters = this.options.delimiters.split(""), // array of chars
textBeforeCursor = this.val().substr(0, this.getSelection().start),
indexOfDelimiter = -1,
i,
d,
idx;
for (i = 0; i < delimiters.length; i++) {
d = delimiters[i];
idx = textBeforeCursor.lastIndexOf(d);
if (idx > indexOfDelimiter) {
indexOfDelimiter = idx;
}
}
if (indexOfDelimiter < 0) {
return textBeforeCursor;
} else {
return textBeforeCursor.substr(indexOfDelimiter + 1);
}
};
this.option2 = {
params: {}
};
$area.getSuggestions = function (q) {
//$.get('autocomplete/autocomplete.ashx', optio.params, function (txt) { $area.processResponse(txt); }, 'text');
if ($area.options.serviceUrl != '') {
var cr, me;
me = this;
$area.options.params.query = q;
// var optio = {
// params: {}
// };
// optio.params.query = q;
$.ajax({
type: 'GET',
url: $area.options.serviceUrl,
data: $area.options.params,
async: false
}).done(function (txt) {
$area.processResponse(txt);
});
}
else {
this.suggestions = $area.suggests;
}
};
$area.processResponse = function (text) {
var response;
try {
response = eval('(' + text + ')');
} catch (err) { return; }
if (!$.isArray(response.data)) { response.data = []; }
this.suggestions = response.suggestions;
var data = response.data;
};
/* Internal method: get completion.
* If performCycle is true then analyze getChunk() and and getSelection()
*/
$area.getCompletion = function (performCycle) {
var text = this.getChunk(),
selectionText = this.getSelection().text,
suggests = this.suggests,
foundAlreadySelectedValue = false,
firstMatchedValue = null,
i,
suggest;
$area.getSuggestions(text);
// search the variant
for (i = 0; i < this.suggestions.length; i++) {
suggest = this.suggestions[i];
if ($area.options.ignoreCase) {
suggest = suggest.toLowerCase();
text = text.toLowerCase();
}
// some variant is found
if (suggest.indexOf(text) === 0) {
if (performCycle) {
if (text + selectionText === suggest) {
foundAlreadySelectedValue = true;
} else if (foundAlreadySelectedValue) {
return suggest.substr(text.length);
} else if (firstMatchedValue === null) {
firstMatchedValue = suggest;
}
} else {
return suggest.substr(text.length);
}
}
}
if (performCycle && firstMatchedValue) {
return firstMatchedValue.substr(text.length);
} else {
return null;
}
};
$area.updateSelection = function (completion) {
if (completion) {
var _selectionStart = $area.getSelection().start,
_selectionEnd = _selectionStart + completion.length;
if ($area.getSelection().text === "") {
if ($area.val().length === _selectionStart) { // Weird IE workaround, I really have no idea why it works
$area.setCaretPos(_selectionStart + 10000);
}
$area.insertAtCaretPos(completion);
} else {
$area.replaceSelection(completion);
}
$area.setSelection(_selectionStart, _selectionEnd);
}
};
$area.keydown(function (e) {
if ($.disabled == true)
return;
if (e.keyCode === KEY.TAB) {
if ($area.options.cycleOnTab) {
var chunk = $area.getChunk();
if (chunk.length >= $area.options.minChunkSize) {
$area.updateSelection($area.getCompletion(true));
}
e.preventDefault();
e.stopPropagation();
$area.focus();
$.asuggestFocused = this;
return false;
}
}
// Check for conditions to stop suggestion
if ($area.getSelection().length &&
$.inArray(e.keyCode, $area.options.stopSuggestionKeys) !== -1) {
// apply suggestion. Clean up selection and insert a space
var _selectionEnd = $area.getSelection().end +
$area.options.endingSymbols.length;
var _text = $area.getSelection().text +
$area.options.endingSymbols;
$area.replaceSelection(_text);
$area.setSelection(_selectionEnd, _selectionEnd);
e.preventDefault();
e.stopPropagation();
this.focus();
$.asuggestFocused = this;
return false;
}
});
$area.keyup(function (e) {
if ($.disabled == true)
return;
var hasSpecialKeys = e.altKey || e.metaKey || e.ctrlKey,
hasSpecialKeysOrShift = hasSpecialKeys || e.shiftKey;
switch (e.keyCode) {
case KEY.UNKNOWN: // Special key released
case KEY.SHIFT:
case KEY.CTRL:
case KEY.ALT:
case KEY.RETURN: // we don't want to suggest when RETURN key has pressed (another IE workaround)
break;
case KEY.TAB:
if (!hasSpecialKeysOrShift && $area.options.cycleOnTab) {
break;
}
case KEY.ESC:
case KEY.BACKSPACE:
case KEY.DEL:
case KEY.UP:
case KEY.DOWN:
case KEY.LEFT:
case KEY.RIGHT:
if (!hasSpecialKeysOrShift && $area.options.autoComplete) {
$area.replaceSelection("");
}
break;
default:
if (!hasSpecialKeys && $area.options.autoComplete) {
var chunk = $area.getChunk();
if (chunk.length >= $area.options.minChunkSize) {
$area.updateSelection($area.getCompletion(false));
}
}
break;
}
});
return $area;
};
} (jQuery));