+ AbstractChosen.prototype.winnow_results = function() {
+ var escapedQuery, fix, i, len, option, prefix, query, ref, regex, results, results_group, search_match, startpos, suffix, text;
+ this.no_results_clear();
+ results = 0;
+ query = this.get_search_text();
+ escapedQuery = query.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
+ regex = this.get_search_regex(escapedQuery);
+ ref = this.results_data;
+ for (i = 0, len = ref.length; i < len; i++) {
+ option = ref[i];
+ option.search_match = false;
+ results_group = null;
+ search_match = null;
+ option.highlighted_html = '';
+ if (this.include_option_in_results(option)) {
+ if (option.group) {
+ option.group_match = false;
+ option.active_options = 0;
+ }
+ if ((option.group_array_index != null) && this.results_data[option.group_array_index]) {
+ results_group = this.results_data[option.group_array_index];
+ if (results_group.active_options === 0 && results_group.search_match) {
+ results += 1;
+ }
+ results_group.active_options += 1;
+ }
+ text = option.group ? option.label : option.text;
+ if (!(option.group && !this.group_search)) {
+ search_match = this.search_string_match(text, regex);
+ option.search_match = search_match != null;
+ if (option.search_match && !option.group) {
+ results += 1;
+ }
+ if (option.search_match) {
+ if (query.length) {
+ startpos = search_match.index;
+ prefix = text.slice(0, startpos);
+ fix = text.slice(startpos, startpos + query.length);
+ suffix = text.slice(startpos + query.length);
+ option.highlighted_html = (this.escape_html(prefix)) + "<em>" + (this.escape_html(fix)) + "</em>" + (this.escape_html(suffix));
+ }
+ if (results_group != null) {
+ results_group.group_match = true;
+ }
+ } else if ((option.group_array_index != null) && this.results_data[option.group_array_index].search_match) {
+ option.search_match = true;
+ }
+ }
+ }
+ }
+ this.result_clear_highlight();
+ if (results < 1 && query.length) {
+ this.update_results_content("");
+ return this.no_results(query);
+ } else {
+ this.update_results_content(this.results_option_build());
+ return this.winnow_results_set_highlight();
+ }
+ };
+
+ AbstractChosen.prototype.get_search_regex = function(escaped_search_string) {
+ var regex_flag, regex_string;
+ regex_string = this.search_contains ? escaped_search_string : "(^|\\s|\\b)" + escaped_search_string + "[^\\s]*";
+ if (!(this.enable_split_word_search || this.search_contains)) {
+ regex_string = "^" + regex_string;
+ }
+ regex_flag = this.case_sensitive_search ? "" : "i";
+ return new RegExp(regex_string, regex_flag);
+ };
+
+ AbstractChosen.prototype.search_string_match = function(search_string, regex) {
+ var match;
+ match = regex.exec(search_string);
+ if (!this.search_contains && (match != null ? match[1] : void 0)) {
+ match.index += 1;
+ }
+ return match;
+ };
+
+ AbstractChosen.prototype.choices_count = function() {
+ var i, len, option, ref;
+ if (this.selected_option_count != null) {
+ return this.selected_option_count;
+ }
+ this.selected_option_count = 0;
+ ref = this.form_field.options;
+ for (i = 0, len = ref.length; i < len; i++) {
+ option = ref[i];
+ if (option.selected) {
+ this.selected_option_count += 1;
+ }
+ }
+ return this.selected_option_count;
+ };
+