Skip to content

Commit

Permalink
Changed all nil to null. No idea how they got in there. Added new met…
Browse files Browse the repository at this point in the history
…hod insertCurrent, which will add whatever is currently in the input box as an element. Set enter to add elements along with comma.
  • Loading branch information
nathanstitt committed Jul 16, 2009
1 parent 0c204d2 commit 67159aa
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 86 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -111,5 +111,5 @@ Copyright: InteRiders <http://interiders.com/> - Distributed under MIT - Keep th
- Renamed main class from FacebookList to MultiSelect. I don't like calling it Facebook becouse of trademark issues. Plus it's confusingly called that in the docs.
- Went through file with Emacs JS2 mode and fixed errors identified, mostly missing commas and semicolons.
- Ignore Emacs backup and OSX .DS_Store files

- added new method insertCurrent, which will add whatever is currently in the input box as an element. Set enter to add elements along with comma.

186 changes: 101 additions & 85 deletions js/proto_multi_select.js
Expand Up @@ -89,14 +89,14 @@ var TextboxList = Class.create({
document.observe(
'keyup', function(e) {
e.stop();
if(! this.current) return nil;
if(! this.current) return null;
switch(e.keyCode){
case Event.KEY_LEFT: return this.move('left');
case Event.KEY_RIGHT: return this.move('right');
case Event.KEY_DELETE:
case Event.KEY_BACKSPACE: return this.moveDispose();
}
return nil;
return null;
}.bind(this)).observe(
'click', function() { document.fire('blur'); }.bindAsEventListener(this)
);
Expand Down Expand Up @@ -222,7 +222,7 @@ var TextboxList = Class.create({
moveDispose: function() {
if(this.current.retrieveData('type') == 'box') return this.dispose(this.current);
if(this.checkInput() && this.bits.keys().length && this.current.previous()) return this.focus(this.current.previous());
return nil;
return null;
}
});

Expand Down Expand Up @@ -296,15 +296,18 @@ var ProtoMultiSelect = Class.create(TextboxList, {
var matches = new Array();
if (search) {
if (!this.options.get('caseSensitive')) {
search = search.toLowerCase();
}
var matches_found = 0;
for (var i=0,len=this.data_searchable.length; i<len; i++) {
if (this.data_searchable[i].indexOf(search) >= 0) {
var v=this.data[i];
matches[matches_found++] = v;
}
search = search.toLowerCase();
}
var matches_found = 0;
for (var i=0,len=this.data_searchable.length; i<len; i++) {
if (this.data_searchable[i].indexOf(search) >= 0) {
var v=this.data[i];
if ( v !== undefined ){
matches[matches_found++] = v !== v;
}

}
}
}
} else {
if (this.options.get('wordMatch')) {
Expand Down Expand Up @@ -366,14 +369,14 @@ var ProtoMultiSelect = Class.create(TextboxList, {
},

autoFocus: function(el) {
if(! el) return nil;
if(! el) return null;
if(this.autocurrent) this.autocurrent.removeClassName('auto-focus');
this.autocurrent = el.addClassName('auto-focus');
return this;
},

autoMove: function(direction) {
if(!this.resultsshown) return nil;
if(!this.resultsshown) return null;
this.autoFocus(this.autocurrent[(direction == 'up' ? 'previous' : 'next')]());
this.autoresults.scrollTop = this.autocurrent.positionedOffset()[1]-this.autocurrent.getHeight();
return this;
Expand All @@ -393,7 +396,7 @@ var ProtoMultiSelect = Class.create(TextboxList, {
this.add({caption: el.value, value: el.value, newValue: true});
var input = el;
} else if(!el || ! el.retrieveData('result')) {
return nil;
return null;
} else {
this.add(el.retrieveData('result'));
delete this.data[this.data.indexOf(Object.toJSON(el.retrieveData('result')))];
Expand All @@ -412,86 +415,99 @@ var ProtoMultiSelect = Class.create(TextboxList, {
this.newvalue = false;

switch(e.keyCode) {
case Event.KEY_UP: e.stop(); return this.autoMove('up');
case Event.KEY_DOWN: e.stop(); return this.autoMove('down');
case Event.KEY_DOWN: e.stop(); return this.autoMove('down');
case Event.KEY_RETURN:
// If the text input is blank and the user hits Enter call the
// onEmptyInput callback.
if (String('').valueOf() == String(this.current.retrieveData('input').getValue()).valueOf()) {
this.options.get("onEmptyInput")();
}
e.stop();
if(!this.autocurrent || !this.resultsshown) break;
this.current_input = "";
this.autoAdd(this.autocurrent);
this.autocurrent = false;
this.autoenter = true;
break;
case Event.KEY_ESC:
this.autoHide();
if(this.current && this.current.retrieveData('input'))
this.current.retrieveData('input').clear();
break;
default:
this.dosearch = true;
case Event.KEY_UP: e.stop(); return this.autoMove('up');
case Event.KEY_DOWN: e.stop(); return this.autoMove('down');
case Event.KEY_DOWN: e.stop(); return this.autoMove('down');
case Event.KEY_RETURN:
// If the text input is blank and the user hits Enter call the
// onEmptyInput callback.
if (String('').valueOf() == String(this.current.retrieveData('input').getValue()).valueOf()) {
this.options.get("onEmptyInput")();
}
e.stop();
if(!this.autocurrent || !this.resultsshown) {
this.insertCurrent();
break;
}
this.current_input = "";
this.autoAdd(this.autocurrent);
this.autocurrent = false;
this.autoenter = true;
break;
case Event.KEY_ESC:
this.autoHide();
if(this.current && this.current.retrieveData('input'))
this.current.retrieveData('input').clear();
break;
default:
this.dosearch = true;
}
return nil;
return null;
}.bind(this));
input.observe('keyup',function(e) {
switch(e.keyCode) {
case Event.KEY_COMMA:
if(this.options.get('newValues')) {
new_value_el = this.current.retrieveData('input');
if (!new_value_el.value.endsWith('<')) {
keep_input = "";
new_value_el.value = new_value_el.value.strip();
if (new_value_el.value.indexOf(",") < (new_value_el.value.length - 1)){
comma_pos = new_value_el.value.indexOf(",");
keep_input = new_value_el.value.substr(comma_pos + 1);
new_value_el.value = new_value_el.value.substr(0,comma_pos).escapeHTML().strip();
} else {
new_value_el.value = new_value_el.value.gsub(",","").escapeHTML().strip();
}
if(!this.options.get("spaceReplace").blank()) new_value_el.value.gsub(" ", this.options.get("spaceReplace"));
if(!new_value_el.value.blank()) {
e.stop();
this.newvalue = true;
this.current_input = keep_input.escapeHTML().strip();
this.autoAdd(new_value_el);
input.value = keep_input;
this.update();
}
}
}
break;
case Event.KEY_UP:
case Event.KEY_DOWN:
case Event.KEY_RETURN:
case Event.KEY_ESC:
break;
default:
// If the user doesn't add comma after, the value is discarded upon submit
this.current_input = input.value.strip().escapeHTML();
this.update();

// Removed Ajax.Request from here and moved to initialize,
// now doesn't create server queries every search but only
// refreshes the list on initialize (page load)
if(this.searchTimeout) clearTimeout(this.searchTimeout);
this.searchTimeout = setTimeout(function(){
var sanitizer = new RegExp("[({[^$*+?\\\]})]","g");
if(this.dosearch) this.autoShow(input.value.replace(sanitizer,"\\$1"));
}.bind(this), 250);
case Event.KEY_COMMA:
if ( this.insertCurrent() ){
e.stop();
}
break;
case Event.KEY_RETURN:
case Event.KEY_UP:
case Event.KEY_DOWN:
case Event.KEY_ESC:
break;
default:
// If the user doesn't add comma after, the value is discarded upon submit
this.current_input = input.value.strip().escapeHTML();
this.update();
// Removed Ajax.Request from here and moved to initialize,
// now doesn't create server queries every search but only
// refreshes the list on initialize (page load)
if(this.searchTimeout) clearTimeout(this.searchTimeout);
this.searchTimeout = setTimeout(function(){
var sanitizer = new RegExp("[({[^$*+?\\\]})]","g");
if(this.dosearch)
this.autoShow(input.value.replace(sanitizer,"\\$1"));
}.bind(this), 250);
}
}.bind(this));
input.observe(Prototype.Browser.IE ? 'keydown' : 'keypress', function(e) {
if ((e.keyCode == Event.KEY_RETURN) && this.autoenter) e.stop();
this.autoenter = false;
}.bind(this));
if ((e.keyCode == Event.KEY_RETURN) && this.autoenter) e.stop();
this.autoenter = false;
}.bind(this));
return li;
},

insertCurrent: function() {
if(this.options.get('newValues')) {
var new_value_el = this.current.retrieveData('input');
debugger
if (!new_value_el.value.endsWith('<')) {
keep_input = "";
new_value_el.value = new_value_el.value.strip();
if (new_value_el.value.indexOf(",") < (new_value_el.value.length - 1)){
var comma_pos = new_value_el.value.indexOf(",");
if ( comma_pos > 0 ){
keep_input = new_value_el.value.substr(comma_pos + 1);
new_value_el.value = new_value_el.value.substr(0,comma_pos).escapeHTML().strip();
} else {
keep_input = new_value_el.value;
}
} else {
new_value_el.value = new_value_el.value.gsub(",","").escapeHTML().strip();
}
if(!this.options.get("spaceReplace").blank()) new_value_el.value.gsub(" ", this.options.get("spaceReplace"));
if(!new_value_el.value.blank()) {
this.newvalue = true;
this.current_input = keep_input.escapeHTML().strip();
this.autoAdd(new_value_el);
input.value = keep_input;
this.update();
return true;
}
}
}
return false;
},
createBox: function($super,text, options) {
var li = $super(text, options);
li.observe('mouseover',function() {
Expand Down

0 comments on commit 67159aa

Please sign in to comment.