/[jquery]/tag_complete/jquery-tac.js
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /tag_complete/jquery-tac.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 54 - (show annotations)
Sun Aug 20 03:35:32 2006 UTC (17 years, 8 months ago) by dpavlin
File MIME type: application/javascript
File size: 6632 byte(s)
added new experimental implementation
1 /*
2 tag auto-completer written with jquery (prototype style)
3
4 2006-08-20 Dobrica Pavlinusic <dpavlin@rot13.org>
5 */
6
7 function tac(
8 tag_class, // tags are elements with this class
9 tags_form_id, // id of tags edit form
10 foo
11 ) {
12
13 // all indexed by tag name
14 this.t = {
15 obj: new Array(), // html objects
16 exists: new Array()
17 };
18
19 this.all_tags = new Array();
20 this.suggested = new Array();
21
22 this.entered = { // array of entered tags
23 ordered: new Array(), // tags entered in original order
24 tag: new Array() // mapping from tag name to order number
25 };
26
27 this.current_selected = null; // current selected suggestion
28
29 this.tags_selector = '.tag';
30 if (tag_class) this.tags_selector = '.'+tag_class;
31 this.tags_form = '#tags_form';
32 if (tags_form_id) this.tags_form = '#'+tags_form_id;
33 this.suggest_selector = '#suggest';
34 this.tags_input = '#tags';
35
36 // tags from previous event
37 this.last_tags = '';
38
39 // parse all tags from html
40 var obj = this;
41 $( this.tags_selector ).each( function(i) {
42 var n = this.firstChild.nodeValue;
43
44 obj.t.obj[n] = this;
45 obj.t.exists[n] = true;
46 obj.all_tags.push( n );
47
48 this.onclick = function() {
49 return obj.tag( n );
50 }
51 this.href = '#'+i; // FIXME debug
52 });
53 $.log.info( 'found ' + this.all_tags.length + ' tags' );
54
55 $.log.info( 'hook onchange to '+this.tags_form );
56 $( this.tags_form ).keyup( function(e) {
57
58 $.log.debug('keyup: '+e.keyCode);
59
60 switch (e.keyCode) {
61 case 38: // up
62 e.preventDefault();
63 obj.move_suggested( -1 );
64 return false;
65 case 40: // down
66 e.preventDefault();
67 obj.move_suggested( +1 );
68 return false;
69 case 9: // tab
70 case 13: // return
71 if (obj.current_selected != null) obj.focus();
72 e.preventDefault();
73 return false;
74 case 8: // backspace
75 case 46: // del
76 obj.parse();
77 return false;
78 }
79
80
81 var t = obj.current_tag();
82
83 $.log.debug('tag: ' + t + ' ['+t.length+']');
84
85 obj.clean_suggested();
86
87 if (t == '') return false;
88
89 obj.suggest(t);
90
91 return true;
92 }).submit( function() {
93 $.log.debug('submit');
94 return obj.take_suggested();
95 }).blur( function() {
96 $.log.debug('blur');
97 return obj.take_suggested();
98 });
99
100 $.log.toggle();
101
102 this.focus();
103 }
104
105 tac.prototype.suggest = function( partial ) {
106
107 $.log.info('suggest to '+partial);
108
109 var len = partial.length;
110 var suggest = '';
111 this.suggested = new Array();
112
113 for(var i = 0; i < this.all_tags.length; i++) {
114 var t = this.all_tags[i];
115 if ( t.substr(0, len) == partial ) {
116 $.log.debug('suggested: ' + t );
117
118 jQuery.className.add( this.t.obj[t], 'suggested' );
119
120 suggest += '<a href="#'+i+'"';
121 if (this.suggested.length == 0) suggest += ' class="suggested" ';
122 suggest += ' onclick="javascript:return this.tag(\'' + t + '\')">' +
123 t + '</a> ';
124 this.suggested.push(t);
125 }
126 }
127
128 $(this.suggest_selector).html( suggest );
129 this.current_suggested = 0;
130 $.log.info('suggested ' + this.suggested.length + ' tags');
131
132
133 }
134
135 tac.prototype.clean_suggested = function() {
136 for(var i = 0; i < this.suggested.length; i++) {
137 var t = this.suggested[i];
138 jQuery.className.remove( this.t.obj[t], 'suggested' );
139 }
140
141 var c = this.current_suggested;
142 if (c == null) return c;
143
144 $(this.suggest_selector).html('');
145 this.current_suggested = null;
146
147 return c;
148 }
149
150 tac.prototype.take_suggested = function() {
151 var c = this.current_suggested;
152 if (c == null) {
153 $.log.debug('no current_select, return true');
154 return true;
155 }
156
157 var s = $(this.suggest_selector+' a:nth('+c+')').html();
158 if (s == null) {
159 $.log.debug('no suggest, return true');
160 return true;
161 }
162
163 var i = this.suggested[c].i;
164 $.log.debug('take_suggested '+i+':'+s);
165 this.add_tag( s );
166 return false;
167 }
168
169 tac.prototype.move_suggested = function( where ) {
170 var c = this.current_suggested;
171 if (c == null) {
172 $.log.error('this.current_suggested is null');
173 return;
174 }
175 var to = c + where;
176 $.log.info('move_suggested('+where+') '+c+' -> '+to);
177 if (to < 0 || to >= this.suggested.length) {
178 $.log.error('move to invalid element '+to);
179 return;
180 }
181 var s = this.suggest_selector+' a:nth('+c+')';
182 $( s ).removeClass('suggested');
183 $.log.debug('remove suggested from '+s);
184 s = this.suggest_selector+' a:nth('+to+')';
185 $( s ).addClass('suggested');
186 $.log.debug('add suggested to '+s);
187 this.current_suggested = to;
188 }
189
190 /*
191 tag operations
192 */
193
194 tac.prototype.add_tag = function( t ) {
195 $.log.info('add '+t);
196 this.focus();
197
198 jQuery.className.add( this.t.obj[t], 'entered' );
199
200 var last = this.entered.ordered.length;
201 this.entered.ordered[ last ] = t;
202 this.entered.tag[t] = last;
203
204 this.clean_suggested();
205
206 // strip last tag and add new one
207 $(this.tags_input).val(
208 $(this.tags_input).val().replace(
209 /[^ ]*$/, t + ' '
210 )
211 );
212 return false;
213 }
214
215 tac.prototype.remove_tag = function( t ) {
216 $.log.info('remove '+t);
217
218 this.focus();
219
220 var o = this.entered.tag[t];
221 if (o == null) {
222 $.log.error('remove non-entered tag '+t);
223 return false;
224 }
225
226 jQuery.className.remove( this.t.obj[t], 'entered' );
227
228 // remove suggested tag and rebuild tags
229
230 this.entered.ordered.splice(o,1);
231 this.entered.tag[t] = null;
232
233 for (var i = o; i < this.entered.ordered.length; i++) {
234 this.entered.tag[ this.entered.ordered[i] ] = i;
235 }
236
237 this.clean_suggested();
238
239 var tags = this.entered.ordered.join(' ')+' ';
240 $.log.debug('removed '+o+' left: '+tags);
241 $(this.tags_input).val( tags );
242 return false;
243 }
244
245 tac.prototype.tag = function( t ) {
246 if (this.entered.tag[t] != null) {
247 this.remove_tag( t );
248 } else {
249 this.add_tag( t );
250 }
251 }
252
253 /*
254 parse current string into tags
255 */
256
257 tac.prototype.parse = function() {
258 $.log.info('re-parse tags');
259
260 var t = $(this.tags_input).val().replace(/^ */,'').replace(/ *$/,'').split(/ /);
261
262 this.entered = {
263 ordered: new Array(),
264 tag: new Array()
265 };
266
267 if (this.entered.ordered.length) $('.entered').removeClass('entered');
268
269 var debug = '';
270 for (var i = 0; i < t.length; i++) {
271 var tag = t[i];
272 if (this.t.exists[ tag ] != null) {
273 this.entered.tag[tag] = this.entered.ordered.length;
274 this.entered.ordered.push( tag );
275 jQuery.className.add( this.t.obj[ tag ], 'entered' );
276 debug += tag + ' '
277 } else {
278 debug += '{'+tag+'} ';
279 }
280 }
281 $.log.debug('parsed '+debug+' to '+this.entered.ordered.join(','));
282
283 }
284
285 tac.prototype.current_tag = function() {
286 var tags = $(this.tags_input).val();
287 if (tags != this.last_tags) {
288 this.last_tags = tags;
289 this.parse();
290 }
291 return tags.replace(/^([^ ][^ ]* )*/, '');
292 }
293
294 tac.prototype.focus = function() {
295 // $(this.tags_input).focus() doesn't work!
296 document.getElementById('tags').focus();
297 }
298
299 // example usage
300 var tags;
301 $(document).ready( function() {
302 tags = new tac();
303 });
304

  ViewVC Help
Powered by ViewVC 1.1.26