From a065ae07855a531eac5e774c61545904b70f1b36 Mon Sep 17 00:00:00 2001 From: earthchie Date: Sun, 16 Apr 2017 23:04:19 +0700 Subject: [PATCH] 1.3.6 - improve $search performance --- jquery.Thailand.js/dist/jquery.Thailand.min.js | 5 +++-- jquery.Thailand.js/src/jquery.Thailand.js | 8 ++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/jquery.Thailand.js/dist/jquery.Thailand.min.js b/jquery.Thailand.js/dist/jquery.Thailand.min.js index 04cdf92..a409494 100644 --- a/jquery.Thailand.js/dist/jquery.Thailand.min.js +++ b/jquery.Thailand.js/dist/jquery.Thailand.min.js @@ -1,6 +1,6 @@ /** * @name jquery.Thailand.js - * @version 1.3.5 + * @version 1.3.6 * @update Apr 16, 2017 * @website https://github.com/earthchie/jquery.Thailand.js * @license WTFPL v.2 - http://www.wtfpl.net/ @@ -9,4 +9,5 @@ * typeahead.js * JQL.js **/ -$.Thailand=function(a){"use strict";a=$.extend({database:"./db.json",autocomplete_size:20,onLoad:function(){},onDataFill:function(){},$district:!1,$amphoe:!1,$province:!1,$zipcode:!1,$search:!1},a);var b=function(a){function e(a){function e(a){var b=a.charCodeAt(0);return c[b<97?b-65:26+b-97]}return d?("number"==typeof a&&(a=b[a]),a.replace(/[A-Z]/gi,e)):a}var b=[],c=[],d=!1;if(a.lookup&&a.words&&(d=!0,b=a.lookup.split("|"),c=a.words.split("|"),a=a.data),!a[0].length)return a;var f=[];return a.forEach(function(a){var b=a[0];a[1].forEach(function(a){var c=a[0];a[1].forEach(function(a){var d=a[0];(a[1]instanceof Array?a[1]:[a[1]]).forEach(function(a){f.push({d:e(d),a:e(c),p:e(b),z:a})})})})}),f},c=function(a,b,d){a+="",b+="";var j,k,l,m,e=0,f=0,g=0,h=a.length,i=b.length;for(j=0;jg&&(g=l,e=j,f=k)}return m=g,m&&(e&&f&&(m+=c(a.substr(0,f),b.substr(0,f),!1)),e+gi?Math.floor(m/h*100):Math.floor(m/i*100)};$.getJSON(a.database,function(d){var e=new JQL(b(d)),f={hint:!0,highlight:!0,minLength:1},g={empty:" ",suggestion:function(a){return"
"+a.d+" » "+a.a+" » "+a.p+" » "+(a.z||"ไม่มีรหัสไปรษณีย์")+"
"}},h=function(b,c){a.$district&&a.$district.val(c.d).trigger("change"),a.$amphoe&&a.$amphoe.val(c.a).trigger("change"),a.$province&&a.$province.val(c.p).trigger("change"),a.$zipcode&&a.$zipcode.val(c.z).trigger("change"),"function"==typeof a.onDataFill&&a.onDataFill({district:c.d,amphoe:c.a,province:c.p,zipcode:c.z})};a.$district&&a.$district.typeahead(f,{limit:a.autocomplete_size,templates:g,source:function(a,b){var c=[];try{c=e.select("*").where("d").match("^"+a).orderBy("d").fetch()}catch(a){}b(c)},display:function(a){return a.d}}),a.$amphoe&&a.$amphoe.typeahead(f,{limit:a.autocomplete_size,templates:g,source:function(a,b){var c=[];try{c=e.select("*").where("a").match("^"+a).orderBy("a").fetch()}catch(a){}b(c)},display:function(a){return a.a}}),a.$province&&a.$province.typeahead(f,{limit:a.autocomplete_size,templates:g,source:function(a,b){var c=[];try{c=e.select("*").where("p").match("^"+a).orderBy("p").fetch()}catch(a){}b(c)},display:function(a){return a.p}}),a.$zipcode&&a.$zipcode.typeahead(f,{limit:a.autocomplete_size,templates:g,source:function(a,b){var c=[];try{c=e.select("*").where("z").match("^"+a).orderBy("z").fetch()}catch(a){}b(c)},display:function(a){return a.z}}),a.$search&&a.$search.typeahead(f,{limit:a.autocomplete_size,templates:g,source:function(a,b){var f,d=[];try{d=new JQL(d.concat(e.select("*").where("d").match(a).fetch()).concat(e.select("*").where("a").match(a).fetch()).concat(e.select("*").where("p").match(a).fetch()).concat(e.select("*").where("z").match(a).fetch()).filter(function(a,b,c){var d=!0;for(f=0;fg&&(g=l,e=j,f=k)}return m=g,m&&(e&&f&&(m+=c(a.substr(0,f),b.substr(0,f),!1)),e+gi?Math.floor(m/h*100):Math.floor(m/i*100)};$.getJSON(a.database,function(d){var e=new JQL(b(d)),f={hint:!0,highlight:!0,minLength:1},g={empty:" ",suggestion:function(a){return"
"+a.d+" » "+a.a+" » "+a.p+" » "+(a.z||"ไม่มีรหัสไปรษณีย์")+"
"}},h=function(b,c){a.$district&&a.$district.val(c.d).trigger("change"),a.$amphoe&&a.$amphoe.val(c.a).trigger("change"),a.$province&&a.$province.val(c.p).trigger("change"),a.$zipcode&&a.$zipcode.val(c.z).trigger("change"),"function"==typeof a.onDataFill&&a.onDataFill({district:c.d,amphoe:c.a,province:c.p,zipcode:c.z})};a.$district&&a.$district.typeahead(f,{limit:a.autocomplete_size,templates:g,source:function(a,b){var c=[];try{c=e.select("*").where("d").match("^"+a).orderBy("d").fetch()}catch(a){}b(c)},display:function(a){return a.d}}),a.$amphoe&&a.$amphoe.typeahead(f,{limit:a.autocomplete_size,templates:g,source:function(a,b){var c=[];try{c=e.select("*").where("a").match("^"+a).orderBy("a").fetch()}catch(a){}b(c)},display:function(a){return a.a}}),a.$province&&a.$province.typeahead(f,{limit:a.autocomplete_size,templates:g,source:function(a,b){var c=[];try{c=e.select("*").where("p").match("^"+a).orderBy("p").fetch()}catch(a){}b(c)},display:function(a){return a.p}}),a.$zipcode&&a.$zipcode.typeahead(f,{limit:a.autocomplete_size,templates:g,source:function(a,b){var c=[];try{c=e.select("*").where("z").match("^"+a).orderBy("z").fetch()}catch(a){}b(c)},display:function(a){return a.z}}),a.$search&&a.$search.typeahead({hint:!0,highlight:!0,minLength:2},{limit:a.autocomplete_size,templates:g,source:function(a,b){var f,d=[];try{d=new JQL(d.concat(e.select("*").where("d").match(a).fetch()).concat(e.select("*").where("a").match(a).fetch()).concat(e.select("*").where("p").match(a).fetch()).concat(e.select("*").where("z").match(a).fetch()).filter(function(a,b,c){var d=!0;for(f=0;f