felixge / debuggable-scraps

MIT licensed code without warranty ; )

This URL has Read+Write access

debuggable-scraps / jquery / jac / jquery.jac.js
100644 169 lines (148 sloc) 4.432 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
/**
* jAc Browser Tab Completion
*
* A Jquery plugin that will allow you to tab-complete a set of strings found in your markup
*
* Copyright 2008, Debuggable, Ltd.
* Hibiskusweg 26c
* 13089 Berlin, Germany
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2008, Debuggable, Ltd.
* @link http://debuggable.com/open-source/jtab
* @version 0.1
* @author Tim Koschuetzki <tim@debuggable.com>, Felix Geisendörfer <felix@debuggable.com>
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
$.fn.jac = function(action, options) {
if (typeof action == 'string') {
switch (action) {
case 'suggest':
var settings = this.data('jac.options'), val = this.val(), caret = this.caret(), context = {
val: val,
caret: caret,
before: val.substr(0, caret.begin),
after: val.substr(caret.end)
};
context.word = (context.before.match(settings.word) || [false])[1];
 
if (!context.word) {
return [];
}
settings.context = context;
 
var suggest = settings.suggest || function(context, settings) {
var suggestions = [], word = (settings.ignoreCase)
? context.word.toLowerCase()
: context.word;
 
$.each(settings.items, function() {
var item = settings.ignoreCase
? this.toLowerCase()
: item;
 
if (item.indexOf(word) === 0) {
var suggestion = (settings.alter)
? settings.alter(String(this), context, settings)
: String(this);
suggestions.push(suggestion);
}
 
if (suggestions.length && !settings.findAll) {
return false;
}
});
 
return suggestions;
};
 
return suggest.call(this, context, settings);
case 'inject':
var settings = this.data('jac.options'), val = this.val(), caret = this.caret();
if (settings.inject) {
settings.inject.call(this, val, {caret: caret}, settings);
return true;
}
 
val = val.substr(0, caret.begin - settings.context.word.length)
+ options
+ val.substr(caret.end, val.length);
 
this.val(val);
this.caret(caret.begin + options.length);
 
return true;
case 'complete':
var suggestions = this.jac('suggest');
if (!suggestions.length) {
return;
}
this.jac('inject', suggestions[0]);
 
return false;
}
}
 
if (typeof action == 'object' && action.constructor === Array) {
options = {items: action};
} else if (typeof action == 'object') {
options = action;
}
 
action = 'init';
options = $.extend({
items: '.author',
ignoreCase: true,
findAll: false,
word: /([\w]+)$/i,
alter: function(suggestion, context, settings) {
var start = context.before == context.word, newLine = (context.before.substr(-(context.word.length+1), 1) == "\n");
if (start || newLine) {
return suggestion+': ';
}
return suggestion+' ';
}
}, options || {});
 
if (typeof options.items == 'string') {
var items = [];
$(options.items).each(function() {
items.push($(this).text().replace(/[\r\n\t]+/, ''));
})
options.items = items;
}
 
this.data('jac.options', options);
var keypress = ($.browser.safari)
? 'keydown'
: 'keypress';
 
return this.bind(keypress, function(e) {
if (e.keyCode != 9) {
return;
}
return $(this).jac('complete');
});
};
 
if (!$.fn.caret) {
// Copyright (c) 2007-2008 Josh Bush (digitalbush.com) (MIT licensed) -- Part of the masked input plugin
// refactored by Felix Geisendörfer
$.fn.caret = function(begin,end) {
if (this.length == 0) {
return;
}
 
if (typeof begin == 'number') {
end = (typeof end == 'number')
? end
: begin;
 
return this.each(function() {
if (this.setSelectionRange){
this.focus();
this.setSelectionRange(begin,end);
} else if (this.createTextRange) {
var range = this.createTextRange();
range.collapse(true);
range.moveEnd('character', end);
range.moveStart('character', begin);
range.select();
}
});
}
 
if (this[0].setSelectionRange) {
begin = this[0].selectionStart;
end = this[0].selectionEnd;
} else if (document.selection && document.selection.createRange) {
var range = document.selection.createRange();
begin = 0 - range.duplicate().moveStart('character', -100000);
end = begin + range.text.length;
}
 
return {begin:begin, end:end};
};
}