jquery-ui.autocompleteTrigger-deleted.js
6.29 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
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
/*
* autocompleteTrigger (based on jQuery-UI Autocomplete)
* https://github.com/experteer/autocompleteTrigger
*
* Copyright 2012, Experteer GmbH, Munich
*
* @version: 1.1
* @author <a href="mailto:daniel.mattes@experteer.com">Daniel Mattes</a>
*
* @requires jQuery 1.6> and jQuery-Ui (including Autocomplete) 1.8>
*
* @description
* autocompleteTrigger allows you to specify a trigger (e. g. @ like twitter or facebook) and bind it to a textarea or input text field.
* If a user writes the trigger-sign into the textbox, the autocomplete dialog will displayed and the text after the trigger-sign
* will be used as search-query for the autocomplete options. If one of the suggested items will be selected, the value and trigger
* will be added to the textfield.
*
* Thanks to https://github.com/kof/fieldSelection (getCursorPosition) and
* http://stackoverflow.com/questions/4564378/jquery-autocomplete-plugin-that-triggers-after-token
*
* Dual licensed under MIT or GPLv2 licenses
* http://en.wikipedia.org/wiki/MIT_License
* http://en.wikipedia.org/wiki/GNU_General_Public_License
*
* @example
* $('input').autocompleteTrigger({
* triggerStart : '@',
* triggerEnd: '',
* source: [
"Asp",
"BASIC",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
]
* });
*/
;
(function($, window, document, undefined) {
$.widget("ui.autocompleteTrigger", {
//Options to be used as defaults
options:{
triggerStart:"%{",
triggerEnd:"}"
},
_create:function() {
this.triggered = false;
this.element.autocomplete($.extend({
search:function() {
/**
* @description only make a request and suggest items if acTrigger.triggered is true
*/
var acTrigger = $(this).data("autocompleteTrigger");
return acTrigger.triggered;
},
select:function(event, ui) {
/**
* @description if a item is selected, insert the value between triggerStart and triggerEnd
*/
acTrigger = $(this).data("autocompleteTrigger");
var text = this.value;
var trigger = acTrigger.options.triggerStart;
var cursorPosition = acTrigger.getCursorPosition();
var lastTriggerPosition = text.substring(0, cursorPosition).lastIndexOf(trigger);
var firstTextPart = text.substring(0, lastTriggerPosition + trigger.length) +
ui.item.value +
acTrigger.options.triggerEnd;
this.value = firstTextPart + text.substring(cursorPosition, text.length);
acTrigger.triggered = false;
acTrigger.setCursorPosition(firstTextPart.length);
return false;
},
focus:function() {
/**
* @description prevent to replace the hole text, if a item is hovered
*/
return false;
},
minLength:0
}, this.options))
.bind("keyup", function(event) {
/**
* @description Bind to keyup-events to detect text changes.
* If the trigger is found before the cursor, autocomplete will be called
*/
var acTrigger = $(this).data("autocompleteTrigger");
if (event.keyCode != $.ui.keyCode.UP && event.keyCode != $.ui.keyCode.DOWN) {
var text = this.value;
var textLength = text.length;
var cursorPosition = acTrigger.getCursorPosition();
var lastString;
var query;
var lastTriggerPosition;
var trigger = acTrigger.options.triggerStart;
if (acTrigger.triggered) {
// call autocomplete with the string after the trigger
// Example: triggerStart = @, string is '@foo' -> query string is 'foo'
lastTriggerPosition = text.substring(0, cursorPosition).lastIndexOf(trigger);
query = text.substring(lastTriggerPosition + trigger.length, cursorPosition);
$(this).autocomplete("search", query);
}
else if (textLength >= trigger.length) {
// set trigged to true, if the string before the cursor is triggerStart
lastString = text.substring(cursorPosition - trigger.length, cursorPosition);
acTrigger.triggered = (lastString === trigger);
}
}
});
},
/**
* @description Destroy an instantiated plugin and clean up modifications the widget has made to the DOM
*/
destroy:function() {
// this.element.removeStuff();
// For UI 1.8, destroy must be invoked from the
// base widget
$.Widget.prototype.destroy.call(this);
// For UI 1.9, define _destroy instead and don't
// worry about
// calling the base widget
},
/**
* @description calculates the the current cursor position in the bound textfield, area,...
* @returns {int} the position of the cursor.
*/
getCursorPosition:function() {
var elem = this.element[0];
var position = 0;
// dom 3
if (elem.selectionStart >= 0) {
position = elem.selectionStart;
// IE
} else if (elem.ownerDocument.selection) {
var r = elem.ownerDocument.selection.createRange();
if (!r) return 0;
var tr = elem.createTextRange(), ctr = tr.duplicate();
tr.moveToBookmark(r.getBookmark());
ctr.setEndPoint('EndToStart', tr);
position = ctr.text.length;
}
return position;
},
/**
* @description set the position of the cursor in a textfield, area,...
*/
setCursorPosition:function (position) {
var elem = this.element[0];
if (elem.selectionStart) {
// firefox
elem.selectionStart = position;
elem.selectionEnd = position;
} else if (elem.ownerDocument.selection) {
// IE
elem.focus();
var r = elem.ownerDocument.selection.createRange();
// Move selection start and end to 0 position
r.moveStart('character', -elem.value.length);
r.moveEnd('character', -elem.value.length);
// Move selection start and end to desired position
r.moveStart('character', position);
r.moveEnd('character', 0);
r.select();
}
}
});
})(jQuery, window, document);