All pastes #634719 Raw Edit

Someone

public text v1 · immutable
#634719 ·published 2007-07-26 16:53 UTC
rendered paste body
cidItem = Class.create(); 
idItem.prototype = {
	initialize: function(name, id) {
		this.name = name;
		this.id = id;
		if (arguments.length == 3)
			this.formatted = arguments[2];
	}	
}

Autocompleter.idLocal = Class.create();
Autocompleter.idLocal.prototype = Object.extend(new Autocompleter.Base(), {
  initialize: function(element, hidden, update, array, options) {
	this.baseInitialize(element, update, options);
	this.hidden = $(hidden);
	this.options.array = array;
  },

  updateChoices: function(choices) {
	if (!this.changed && this.hasFocus) {
		this.choices = choices;
		for (var i = 0; i < choices.length; i++) 
			content += choices[i].formatted;					
		this.update.innerHTML = "<ul>"+content+"</ul>";
		
		if (this.update.firstChild && this.update.down().childNOdes) {
			this.entryCount = this.update.down().childNodes.length;
			for (var i = 0; i < this.entryCount; i++) {
				var entry = this.getEntry(i);
				entry.autocompleteIndex = i;
				this.addObservers(entry);
			} 
		} else {
			this.entryCount = 0;
		}
		
		this.stopIndicator();
		this.index = 0;
		if (this.entryCount == 1 && this.options.autoSelect) {
			this.selectEntry();
			this.hide();
		} else {
			this.render();
		}
	}
  },
  
  updateElement: function(selectedElement) {	
	this.element.value = this.choices[this.index].name;
	this.hidden.value = this.choices[this.index].id;
	this.elemnt.focus();	
  },
  
  getUpdatedChoices: function() {
    this.updateChoices(this.options.selector(this));
  },

  setOptions: function(options) {
    this.options = Object.extend({
      choices: 10,
      partialSearch: true,
      partialChars: 2,
      ignoreCase: true,
      fullSearch: false,
      selector: function(instance) {
        var ret       = []; // Beginning matches
        var partial   = []; // Inside matches
        var entry     = instance.getToken();
        var count     = 0;

        for (var i = 0; i < instance.options.array.length &&  
			ret.length < instance.options.choices ; i++) { 

          var elem = instance.options.array[i].name;
          var foundPos = instance.options.ignoreCase ? 
            elem.toLowerCase().indexOf(entry.toLowerCase()) : 
            elem.indexOf(entry);

          while (foundPos != -1) {
            if (foundPos == 0 && elem.length != entry.length) { 
              ret.push( new idItem( instance.options.array[i].name, instance.options.array[i].id, 
					"<li><strong>" + elem.substr(0, entry.length) + "</strong>" + 
						elem.substr(entry.length) + "</li>"));
              break;
            } else if (entry.length >= instance.options.partialChars && 
              instance.options.partialSearch && foundPos != -1) {
              if (instance.options.fullSearch || /\s/.test(elem.substr(foundPos-1,1))) {
                partial.push( new idItem( instance.options.array[i].name, instance.options.array[i].id,
					"<li>" + elem.substr(0, foundPos) + "<strong>" +
                  elem.substr(foundPos, entry.length) + "</strong>" + elem.substr(
                  foundPos + entry.length) + "</li>"));
                break;
              }
            }

            foundPos = instance.options.ignoreCase ? 
              elem.toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) : 
              elem.indexOf(entry, foundPos + 1);

          }
        }
        if (partial.length)
          ret = ret.concat(partial.slice(0, instance.options.choices - ret.length))
        return ret;
      }
    }, options || {});
  }
});