Skip to content

Commit

Permalink
Fixed loadFromInput being called to soon when used in conjunction wit…
Browse files Browse the repository at this point in the history
…h fetchFile. Added sortResults option to sort the results alphabetically by caption.
  • Loading branch information
dvandersluis committed Jul 28, 2009
1 parent 86f6c38 commit d40e548
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
3 changes: 2 additions & 1 deletion README.md
@@ -1,6 +1,6 @@
# Proto!MultiSelect

Prototype version required: 6.0
Prototype version required: 1.6.0 (1.6.1 for IE8)

Copyright: InteRiders <http://interiders.com/> - Distributed under MIT - Keep this message!

Expand Down Expand Up @@ -125,3 +125,4 @@ Copyright: InteRiders <http://interiders.com/> - Distributed under MIT - Keep th
- Added createAutohandler method to automatically create the needed div/ul elements so that they don't need to be created in the HTML.
- Added loadFromInput option (default: true) to load values given in the initial text input into the control.
- Added defaultMessage option (default: "") which is used for a default message div if the control creates the autohandler. The default message is no longer mandatory.
- Added sortResults option (default: false) which will sort autocomplete results by caption.
4 changes: 3 additions & 1 deletion index.html
Expand Up @@ -115,9 +115,11 @@ <h1>Proto!MultiSelect 0.9</h1>
newValues: true,
regexSearch: false,
feed: json_feed,
defaultMessage: "This is the default message"
defaultMessage: "This is the default message",
sortResults: true
});
});
</script>
</body>
</html>
<!-- vi: set noexpandtab -->
62 changes: 39 additions & 23 deletions js/multiselect.js
Expand Up @@ -128,7 +128,8 @@ var TextboxList = Class.create({
caseSensitive: false,
regexSearch: true,
loadFromInput: true,
defaultMessage: "" // Used to provide the default autocomplete message if built by the control
defaultMessage: "", // Used to provide the default autocomplete message if built by the control
sortResults: false
});

this.current_input = "";
Expand Down Expand Up @@ -421,6 +422,7 @@ var ProtoMultiSelect = Class.create(TextboxList, {
onSuccess: function(transport)
{
transport.responseText.evalJSON(true).each(function(t) { this.autoFeed(t); }.bind(this));
if (this.options.get('loadFromInput')) this.loadFromInput()
}.bind(this)
});
}
Expand All @@ -429,28 +431,12 @@ var ProtoMultiSelect = Class.create(TextboxList, {
this.options.get('feed').each(function(t) { this.autoFeed(t) }.bind(this));
}

if (this.options.get('loadFromInput'))
// We need to load from input as part of the AJAX request when using fetchFile
// or else the data won't have completed being fetched before the data in the
// input is loaded
if (Object.isUndefined(this.options.get('fetchFile')) && this.options.get('loadFromInput'))
{
var input_values = this.element.value.split(',').invoke('strip');

if (this.data.length)
{
this.data.select(function(el) { return input_values.include(el.evalJSON(true).value) }).each(function(el)
{
el = el.evalJSON(true);
this.add({ value: el.value, caption: el.caption});
delete this.data[this.data.indexOf(Object.toJSON(el))];
input_values = input_values.without(el.value);
}, this);
}

input_values.each(function(el)
{
if (!el.empty())
{
this.add({ value: el, caption: el });
}
}, this);
this.loadFromInput()
}
},

Expand Down Expand Up @@ -514,6 +500,11 @@ var ProtoMultiSelect = Class.create(TextboxList, {
);
}

if (this.options.get('sortResults'))
{
matches = matches.sortBy(function(el) { return el.evalJSON(true).caption })
}

var count = 0;
matches.each(
function(result, ti)
Expand Down Expand Up @@ -803,8 +794,33 @@ var ProtoMultiSelect = Class.create(TextboxList, {

this.element.insert({ after: div });
return div
}
},

loadFromInput: function()
{
var input_values = this.element.value.split(',').invoke('strip');

if (this.data.length)
{
this.data.select(function(el) { return input_values.include(el.evalJSON(true).value) }).each(function(el)
{
el = el.evalJSON(true);
this.add({ value: el.value, caption: el.caption});
delete this.data[this.data.indexOf(Object.toJSON(el))];
input_values = input_values.without(el.value);
}, this);
}

input_values.each(function(el)
{
if (!el.empty())
{
this.add({ value: el, caption: el });
}
}, this);
}
});


/* Copyright: InteRiders <http://interiders.com/> - Distributed under MIT - Keep this message! */
// vi: noexpandtab

0 comments on commit d40e548

Please sign in to comment.