/[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

Diff of /tag_complete/jquery-tac.js

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 54 by dpavlin, Sun Aug 20 03:35:32 2006 UTC revision 59 by dpavlin, Sun Aug 20 23:37:52 2006 UTC
# Line 16  function tac( Line 16  function tac(
16                  exists: new Array()                  exists: new Array()
17          };          };
18    
19            // all tags indexed by first letter
20          this.all_tags = new Array();          this.all_tags = new Array();
21          this.suggested = new Array();          this.suggested = new Array();
22    
# Line 33  function tac( Line 34  function tac(
34          this.suggest_selector = '#suggest';          this.suggest_selector = '#suggest';
35          this.tags_input = '#tags';          this.tags_input = '#tags';
36    
37          // tags from previous event          // all tags from previous event
38          this.last_tags = '';          this.last_tags = '';
39            // just last tag
40            this.last_tag = '';
41    
42          // parse all tags from html          // parse all tags from html
43          var obj = this;          var obj = this;
44          $( this.tags_selector ).each( function(i) {          $( this.tags_selector ).each( function(i) {
45                  var n = this.firstChild.nodeValue;                  var n = this.firstChild.nodeValue;
46                    var c = n.substr(0,1);
47    
48                  obj.t.obj[n] = this;                  obj.t.obj[n] = this;
49                  obj.t.exists[n] = true;                  obj.t.exists[n] = true;
50                  obj.all_tags.push( n );                  if (obj.all_tags[c] == null) {
51                            obj.all_tags[c] = new Array( n );
52                    } else {
53                            obj.all_tags[c].push( n );
54                    }
55    
56                  this.onclick = function() {                  this.onclick = function() {
57                          return obj.tag( n );                          return obj.tag( n );
58                  }                  }
59                  this.href = '#'+i;      // FIXME debug                  this.href = '#'+i;      // FIXME debug
60          });          });
61          $.log.info( 'found ' + this.all_tags.length + ' tags' );          $.log.info( 'found ' + this.t.obj.length + ' tags' );
62    
63          $.log.info( 'hook onchange to '+this.tags_form );          $.log.info( 'hook onchange to '+this.tags_form );
64          $( this.tags_form ).keyup( function(e) {          $( this.tags_form ).keyup( function(e) {
65    
66                  $.log.debug('keyup: '+e.keyCode);                  $.log.debug('keyup: '+e.keyCode);
67    
68                    var ret = true;
69    
70                  switch (e.keyCode) {                  switch (e.keyCode) {
71                          case 38:        // up                          case 38:        // up
72                                  e.preventDefault();                                  e.preventDefault();
# Line 67  function tac( Line 77  function tac(
77                                  obj.move_suggested( +1 );                                  obj.move_suggested( +1 );
78                                  return false;                                  return false;
79                          case 9:         // tab                          case 9:         // tab
80                                    obj.focus();
81                                    obj.take_suggested();
82                                    return false;
83                          case 13:        // return                          case 13:        // return
84                                  if (obj.current_selected != null) obj.focus();                                  if (obj.current_selected != null) obj.focus();
85                                  e.preventDefault();                                  return true;
                                 return false;  
86                          case 8:         // backspace                          case 8:         // backspace
87                          case 46:        // del                          case 46:        // del
88                                  obj.parse();                                  ret = false;
89                                  return false;                                  break;
90                            case 37:        // left
91                            case 39:        // right
92                                    return true;
93                  }                  }
94    
95    
96                  var t = obj.current_tag();                  var t = obj.current_tag();
97    
98                    if (t == '') return false;
99    
100                  $.log.debug('tag: ' + t + ' ['+t.length+']');                  $.log.debug('tag: ' + t + ' ['+t.length+']');
101    
102                  obj.clean_suggested();                  obj.clean_suggested();
   
                 if (t == '') return false;  
   
103                  obj.suggest(t);                  obj.suggest(t);
104    
105                  return true;                  return ret;
106    
107          }).submit( function() {          }).submit( function() {
108                  $.log.debug('submit');                  $.log.debug('submit');
109                  return obj.take_suggested();                  return obj.take_suggested();
# Line 110  tac.prototype.suggest = function( partia Line 125  tac.prototype.suggest = function( partia
125          var suggest = '';          var suggest = '';
126          this.suggested = new Array();          this.suggested = new Array();
127    
128          for(var i = 0; i < this.all_tags.length; i++) {          var c = partial.substr(0,1);
129                  var t = this.all_tags[i];  
130                  if ( t.substr(0, len) == partial ) {          for(var i = 0; i < this.all_tags[ c ].length; i++) {
131                          $.log.debug('suggested: ' + t );                  var t = this.all_tags[c][i];
132                    if ( t.substr(0, len) == partial && this.entered.tag[ t ] == null) {
133    
134                            //$.log.debug('suggested: ' + t );
135    
136                          jQuery.className.add( this.t.obj[t], 'suggested' );                          jQuery.className.add( this.t.obj[t], 'suggested' );
137    
138                          suggest += '<a href="#'+i+'"';                          suggest += '<a href="#"';
139                          if (this.suggested.length == 0) suggest += ' class="suggested" ';                          if (this.suggested.length == 0) suggest += ' class="suggested" ';
140                          suggest += ' onclick="javascript:return this.tag(\'' + t + '\')">' +                          suggest += '">' + t + '</a> ';
                                 t + '</a> ';  
141                          this.suggested.push(t);                          this.suggested.push(t);
142                  }                  }
143          }          }
144    
145          $(this.suggest_selector).html( suggest );          $(this.suggest_selector).html( suggest );
146            var obj = this;
147            $(this.suggest_selector+' a').click( function() {
148                    var t = this.firstChild.nodeValue;
149                    $.log.debug('click add: '+ t);
150                    obj.clean_suggested();
151                    obj.add_tag( t );
152            });
153          this.current_suggested = 0;          this.current_suggested = 0;
154          $.log.info('suggested ' + this.suggested.length + ' tags');          $.log.info('suggested ' + this.suggested.length + ' tags');
155    
# Line 161  tac.prototype.take_suggested = function( Line 185  tac.prototype.take_suggested = function(
185          }          }
186    
187          var i = this.suggested[c].i;          var i = this.suggested[c].i;
188          $.log.debug('take_suggested '+i+':'+s);          $.log.debug('take_suggested: '+s);
189          this.add_tag( s );          this.add_tag( s );
190          return false;          return false;
191  }  }
# Line 236  tac.prototype.remove_tag = function( t ) Line 260  tac.prototype.remove_tag = function( t )
260    
261          this.clean_suggested();          this.clean_suggested();
262    
263          var tags = this.entered.ordered.join(' ')+' ';          var tags = this.entered.ordered.join(' ');
264            if (tags.length) tags += ' ';
265          $.log.debug('removed '+o+' left: '+tags);          $.log.debug('removed '+o+' left: '+tags);
266          $(this.tags_input).val( tags );          $(this.tags_input).val( tags );
267          return false;          return false;
# Line 259  tac.prototype.parse = function() { Line 284  tac.prototype.parse = function() {
284    
285          var t = $(this.tags_input).val().replace(/^  */,'').replace(/  *$/,'').split(/ /);          var t = $(this.tags_input).val().replace(/^  */,'').replace(/  *$/,'').split(/ /);
286    
287            if (this.entered.ordered.length) $('.entered').removeClass('entered');
288            this.clean_suggested();
289    
290          this.entered = {          this.entered = {
291                  ordered: new Array(),                  ordered: new Array(),
292                  tag: new Array()                  tag: new Array()
293          };          };
294    
         if (this.entered.ordered.length) $('.entered').removeClass('entered');  
   
295          var debug = '';          var debug = '';
296          for (var i = 0; i < t.length; i++) {          for (var i = 0; i < t.length; i++) {
297                  var tag = t[i];                  var tag = t[i];
# Line 279  tac.prototype.parse = function() { Line 305  tac.prototype.parse = function() {
305                  }                  }
306          }          }
307          $.log.debug('parsed '+debug+' to '+this.entered.ordered.join(','));          $.log.debug('parsed '+debug+' to '+this.entered.ordered.join(','));
   
308  }  }
309    
310  tac.prototype.current_tag = function() {  tac.prototype.current_tag = function() {
# Line 288  tac.prototype.current_tag = function() { Line 313  tac.prototype.current_tag = function() {
313                  this.last_tags = tags;                  this.last_tags = tags;
314                  this.parse();                  this.parse();
315          }          }
316          return tags.replace(/^([^ ][^ ]* )*/, '');          var last_tag = tags.replace(/^([^ ][^ ]* )*/, '');
317            if (last_tag != this.last_tag) {
318                    this.last_tag = last_tag;
319                    return last_tag;
320            } else {
321                    this.parse();
322                    return '';
323            }
324  }  }
325    
326  tac.prototype.focus = function() {  tac.prototype.focus = function() {

Legend:
Removed from v.54  
changed lines
  Added in v.59

  ViewVC Help
Powered by ViewVC 1.1.26