forked from jamieholst/selectToAutocomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.select-to-autocomplete.min.js
2 lines (2 loc) · 4.7 KB
/
jquery.select-to-autocomplete.min.js
1
2
/* V 1.0.5 http://baymard.com/labs/country-selector#documentation (C) 2011 by Jamie Appleseed, Baymard Institute (baymard.com) http://tellibus.com fork - 16 June 2012 */
(function(e){var t={sort:false,"sort-attr":"data-priority","sort-desc":false,autoselect:true,"alternative-spellings":true,"alternative-spellings-attr":"data-alternative-spellings","remove-valueless-options":true,"copy-attributes-to-text-field":true,"autocomplete-plugin":"jquery_ui","relevancy-sorting":true,"relevancy-sorting-partial-match-value":1,"relevancy-sorting-strict-match-value":5,"relevancy-sorting-booster-attr":"data-relevancy-booster",position:{my:"left top",at:"left bottom"},handle_invalid_input:function(e){e.$text_field.val(e.$select_field.find("option:selected:first").text())},handle_select_field:function(e){return e.hide()},insert_text_field:function(n){var r=e("<input></input>");if(t["copy-attributes-to-text-field"]){var i={};var s=n.$select_field[0].attributes;for(var o=0;o<s.length;o++){var u=s[o].nodeName;var a=s[o].nodeValue;if(u!=="name"&&u!=="id"&&typeof n.$select_field.attr(u)!=="undefined"){i[u]=a}}r.attr(i)}r.blur(function(){var i=n.$select_field.find("option").map(function(t,n){return e(n).text()});if(!(r.val()in i)&&typeof t["handle_invalid_input"]==="function"){t["handle_invalid_input"](n)}});if(n.settings["autoselect"]){r.click(function(){this.select()})}return r.val(n.$select_field.find("option:selected:first").text()).insertAfter(n.$select_field)},extract_options:function(n){var r=[];var i=n.find("option");var s=i.length;i.each(function(){var n=e(this);var i={"real-value":n.attr("value"),label:n.text()};if(t["remove-valueless-options"]&&i["real-value"]===""){}else{i["matches"]=i["label"];var o=n.attr(t["alternative-spellings-attr"]);if(o){i["matches"]+=" "+o}if(t["sort"]){var u=parseInt(n.attr(t["sort-attr"]),10);if(u){i["weight"]=u}else{i["weight"]=s}}if(t["relevancy-sorting"]){i["relevancy-score"]=0;i["relevancy-score-booster"]=1;var a=parseFloat(n.attr(t["relevancy-sorting-booster-attr"]));if(a){i["relevancy-score-booster"]=a}}r.push(i)}});if(t["sort"]){if(t["sort-desc"]){r.sort(function(e,t){return t["weight"]-e["weight"]})}else{r.sort(function(e,t){return e["weight"]-t["weight"]})}}return r}};var n={init:function(n){if(e.browser.msie&&parseInt(e.browser.version,10)<=6){return this}else{t=e.extend(t,n);return this.each(function(){var n=e(this);var i={$select_field:n,options:t["extract_options"](n),settings:t};i["$text_field"]=t["insert_text_field"](i);t["handle_select_field"](n);if(typeof t["autocomplete-plugin"]==="string"){r[t["autocomplete-plugin"]](i)}else{t["autocomplete-plugin"](i)}})}}};var r={jquery_ui:function(t){var n=function(n){var r=n.split(" ");var i=[];for(var s=0;s<r.length;s++){if(r[s].length>0){var o={};o["partial"]=new RegExp(e.ui.autocomplete.escapeRegex(r[s]),"i");if(t.settings["relevancy-sorting"]){o["strict"]=new RegExp("^"+e.ui.autocomplete.escapeRegex(r[s]),"i")}i.push(o)}}return e.grep(t.options,function(e){var r=0;if(t.settings["relevancy-sorting"]){var s=false;var o=e.matches.split(" ")}for(var u=0;u<i.length;u++){if(i[u]["partial"].test(e.matches)){r++}if(t.settings["relevancy-sorting"]){for(var a=0;a<o.length;a++){if(i[u]["strict"].test(o[a])){s=true;break}}}}if(t.settings["relevancy-sorting"]){var f=0;f+=r*t.settings["relevancy-sorting-partial-match-value"];if(s){f+=t.settings["relevancy-sorting-strict-match-value"]}f=f*e["relevancy-score-booster"];e["relevancy-score"]=f}return!n||i.length===r})};var r=function(e){if(e){if(t.$select_field.val()!==e["real-value"]){t.$select_field.val(e["real-value"]);t.$select_field.change()}}else{var n=t.$text_field.val().toLowerCase();var r={"real-value":false};for(var i=0;i<t.options.length;i++){if(n===t.options[i]["label"].toLowerCase()){r=t.options[i];break}}if(t.$select_field.val()!==r["real-value"]){t.$select_field.val(r["real-value"]||"");t.$select_field.change()}if(r["real-value"]){t.$text_field.val(r["label"])}if(typeof t.settings["handle_invalid_input"]==="function"&&t.$select_field.val()===""){t.settings["handle_invalid_input"](t)}}};t.$text_field.autocomplete({minLength:0,delay:0,autoFocus:true,position:t.settings["position"],source:function(e,r){var i=n(e.term);if(t.settings["relevancy-sorting"]){i=i.sort(function(e,t){return t["relevancy-score"]-e["relevancy-score"]})}r(i)},open:function(){e(this).autocomplete("widget").css("z-index",100);return false},select:function(e,t){r(t.item)},change:function(e,t){r(t.item)}});t.$text_field.parents("form:first").submit(function(){r()});r()}};e.fn.selectToAutocomplete=function(t){if(n[t]){return n[t].apply(this,Array.prototype.slice.call(arguments,1))}else if(typeof t==="object"||!t){return n.init.apply(this,arguments)}else{e.error("Method "+t+" does not exist on jQuery.fn.selectToAutocomplete")}}})(jQuery)