forked from emposha/FCBKcomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.fcbkcomplete.min.js
More file actions
19 lines (19 loc) · 9.15 KB
/
jquery.fcbkcomplete.min.js
File metadata and controls
19 lines (19 loc) · 9.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(function(e){e.fn.fcbkcomplete=function(v){if("string"==typeof v){var n=e(this).data("fcbkcomplete");return n&&n[v]?n[v]():this}this.each(function(){var m=e(this),w=m.data("fcbkcomplete");if(!w){var o=function(a,c,b,g,j){if(!r())return!1;var g="bit-box"+(g?" locked":""),h;h="";for(var s=0;32>s;s++){var m=Math.floor(61*Math.random());h+="0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz".substring(m,m+1)}m=document.createTextNode(p(a));s=e('<a class="closebutton" href="#">×</a>');
g=e('<li class="'+g+'" rel="'+c+'" id="pt_'+h+'"></li>').prepend(m).append(s);k.append(g);s.click(function(){n(e(this).parent("li"));return!1});b||(e("#"+t+"_annoninput").remove(),H(j),a=e('<option value="'+p(c,1)+'" id="opt_'+h+'" class="selected" selected="selected">'+p(a)+"</option>"),f.append(a),d.onselect&&A(d.onselect,a),f.change());k.children("li.bit-box.deleted").removeClass("deleted");x(1);l.children(".default").fadeOut("fast");return h},n=function(a){if(!a.hasClass("locked")){a.fadeOut("fast");
var c=a.attr("id");if(d.onremove){var b=c?e("#o"+c+""):f.children("option[value="+a.attr("rel")+"]");A(d.onremove,b)}c?e("#o"+c+"").remove():f.children('option[value="'+a.attr("rel")+'"]').remove();a.remove();f.change();B=0}},H=function(a){var c=e('<li class="bit-input" id="'+t+'_annoninput">'),b=e('<input type="text" class="maininput" size="'+d.input_min_size+'" autocomplete="off">');0<d.input_tabindex&&b.attr("tabindex",d.input_tabindex);""!=d.input_name&&b.attr("name",d.input_name);k.append(c.append(b));
b.focus(function(){I=!0;r()&&l.fadeIn("fast")});k.click(function(){0>d.input_min_size&&g.length&&J(u(b.val(),1));b.focus();g.length&&b.val().length>d.input_min_size?g.show():(x(1),l.children(".default").show())});b.keypress(function(a){if(a.keyCode==h.enter)return!1;a=d.input_min_size>b.val().length?d.input_min_size:b.val().length+1;b.attr("size",a).width(parseInt(b.css("font-size"))*a)});b.keyup(function(a){var c=u(b.val(),1);if(a.keyCode==h.backspace&&0==c.length&&(x(1),!k.children("li.bit-box:last").hasClass("locked"))){if(0==
k.children("li.bit-box.deleted").length)return k.children("li.bit-box:last").addClass("deleted"),!1;if(B)return;B=1;k.children("li.bit-box.deleted").fadeOut("fast",function(){n(e(this));return!1})}a.keyCode!=h.downarrow&&(a.keyCode!=h.uparrow&&a.keyCode!=h.leftarrow&&a.keyCode!=h.rightarrow&&c.length>d.input_min_size)&&(J(c),l.children(".default").hide(),g.show())});d.oncreate&&A(d.oncreate,b);a&&setTimeout(function(){b.focus();l.children(".default").show()},1)},C=function(a,c){g.html("");!d.cache&&
null!=c&&q.clear();if(d.newel&&r()&&(g.children("li[fckb=1]").remove(),0!=a.length)){var b=e('<li rel="'+a+'" fckb="1">').html(p(a));g.prepend(b);y++}null!=c&&c.length&&e.each(c,function(a,c){q.set(u(c.key),u(c.value))});var h=d.maxshownitems<q.length()?d.maxshownitems:q.length(),k="";e.each(q.search(a),function(c,b){if(h&&(!d.filter_selected||!f.children('option[value="'+b.key+'"]').hasClass("selected"))){var e=k,g='<li rel="'+b.key+'">',j=b.value,l=d.filter_begin?"<em>$1</em>$2":"$1<em>$2</em>$3",
m=(d.filter_begin?"":"(.*)")+(d.filter_case?"("+a+")(.*)":"("+a.toLowerCase()+")(.*)");try{j=j.replace(RegExp(m,d.filter_case?"g":"gi"),l)}catch(n){}k=e+(g+p(j)+"</li>");y++;h--}});g.append(k);d.firstselected&&(j=g.children("li:visible:first"),j.addClass("auto-focus"));y>d.height?g.css({height:24*d.height+"px",overflow:"auto"}):g.css("height","auto");r()&&l.is(":hidden")&&l.show()},K=function(){g.children("li").mouseover(function(){g.children("li").removeClass("auto-focus");j=e(this);j.addClass("auto-focus")});
g.children("li").mouseout(function(){e(this).removeClass("auto-focus");j=null})},E=function(){var a=e("#"+t+"_annoninput").children(".maininput");K();g.children("li").unbind("mousedown").mousedown(function(){var a=e(this);o(a.text(),a.attr("rel"),0,0,1);x(1);l.hide()});a.unbind("keydown");a.keydown(function(a){a.keyCode!=h.backspace&&k.children("li.bit-box.deleted").removeClass("deleted");if((a.keyCode==h.enter||a.keyCode==h.tab||a.keyCode==h.comma)&&!(null==j||0==j.length)){var b=j;o(b.text(),b.attr("rel"),
0,0,1);return D(a)}if((a.keyCode==h.enter||a.keyCode==h.tab||a.keyCode==h.comma)&&(null==j||0==j.length)){if(d.newel)return b=u(e(this).val()),o(b,b,0,0,1),D(a);if((d.addontab||d.addoncomma)&&d.newel)return b=j=g.children("li:visible:first"),o(b.text(),b.attr("rel"),0,0,1),D(a)}a.keyCode==h.downarrow&&L("first");a.keyCode==h.uparrow&&L("last")})},L=function(a){g.unbind("mouseover").unbind("mouseout").mousemove(function(){K();g.unbind("mousemove")});if(null==j||0==j.length)j=g.children("li:visible:"+
a),g.get(0).scrollTop="first"==a?0:parseInt(j.get(0).scrollHeight,10)*(parseInt(g.children("li:visible").length,10)-Math.round(d.height/2));else{j.removeClass("auto-focus");j="first"==a?j.nextAll("li:visible:first"):j.prevAll("li:visible:first");var c=parseInt(j.prevAll("li:visible").length,10),b=parseInt(j.nextAll("li:visible").length,10);if((("first"==a?c:b)>Math.round(d.height/2)||("first"==a?c:b)<=Math.round(d.height/2))&&"undefined"!=typeof j.get(0))g.get(0).scrollTop=parseInt(j.get(0).scrollHeight,
10)*(c-Math.round(d.height/2))}g.children("li").removeClass("auto-focus");j.addClass("auto-focus")},D=function(a){l.hide();a.preventDefault();j=null;return!1},r=function(){return 0!=d.maxitems&&k.children("li.bit-box").length<d.maxitems},A=function(a,c){var b={};for(i=0;i<c.get(0).attributes.length;i++)null!=c.get(0).attributes[i].nodeValue&&(b["_"+c.get(0).attributes[i].nodeName]=c.get(0).attributes[i].nodeValue);return a.call(a,b)},u=function(a,c){if("undefined"!=typeof c){for(i=0;i<a.length;i++){var b=
a.charCodeAt(i);if(h.exclamation<=b&&b<=h.slash||h.colon<=b&&b<=h.at||h.squarebricket_left<=b&&b<=h.apostrof)a=a.replace(a[i],escape(a[i]))}a=a.replace(/(\{|\}|\*)/i,"\\$1")}return a.replace(/script(.*)/g,"")},p=function(a,c){a=a.toString();a=a.replace("\\","");return"undefined"!=typeof c?a:unescape(a)},x=function(a){g.children().remove();a&&g.hide()},J=function(a){y=0;if((d.json_url||d.getData)&&r())if(d.cache&&F.get(a))C(a),E();else{G++;var c=G;setTimeout(function(){function b(b){I&&(C(a,b),F.set(a,
1),E())}c==G&&("function"===typeof d.getData?d.getData(a,b):e.getJSON(d.json_url,{tag:p(a)},b))},d.delay)}else C(a),E()},d=e.extend({json_url:null,width:512,cache:!1,height:"10",newel:!1,addontab:!1,addoncomma:!1,firstselected:!1,filter_case:!1,filter_selected:!1,filter_begin:!1,complete_text:"Start to type...",select_all_text:null,maxshownitems:30,maxitems:10,oncreate:null,onselect:null,onremove:null,attachto:null,delay:350,input_tabindex:0,input_min_size:1,input_name:"",bricket:!0},v),k=null,g=
null,l=null,y=0,I=!1,j=null,B=0,f=m,t=f.attr("id"),G=0,F={set:function(a,c){var b=f.data("jsoncache");b[a]=c;f.data("jsoncache",b)},get:function(a){return"undefined"!=f.data("jsoncache")[a]?f.data("jsoncache")[a]:null},init:function(){f.data("jsoncache",{})}},h={enter:13,tab:9,comma:188,backspace:8,leftarrow:37,uparrow:38,rightarrow:39,downarrow:40,exclamation:33,slash:47,colon:58,at:64,squarebricket_left:91,apostrof:96},q={search:function(a){var c=[],b=RegExp((d.filter_begin?"^":"")+a,d.filter_case?
"g":"gi");e.each(f.data("cache"),function(a,d){"function"===typeof d.search&&-1!=d.search(b)&&c.push({key:a,value:d})});return c},set:function(a,c){var b=f.data("cache");b[a]=c;f.data("cache",b)},get:function(a){return"undefined"!=f.data("cache")[a]?f.data("cache")[a]:null},clear:function(){f.data("cache",{})},length:function(){if("object"==typeof f.data("cache")){var a=0;for(i in f.data("cache"))a++;return a}return f.data("cache").length},init:function(){"undefined"==f.data("cache")&&f.data("cache",
{})}},m=d.preserveClasses?f.get(0).className:"",k=e('<ul class="holder fcbkcomplete '+m+'"></ul>');d.width&&k.width(d.width);d.attachto?"object"==typeof d.attachto?d.attachto.append(k):e(d.attachto).append(k):f.after(k);m=k.outerWidth();l=e('<div class="facebook-auto">').width(m);""!=d.complete_text&&(l.append('<div class="default">'+d.complete_text+"</div>"),d.select_all_text&&l.children(".default").append(e('<a href="" class="select_all_items">'+d.select_all_text+"</a>").click(function(){e(f).trigger("selectAll");
return!1})));l.hover(function(){},function(){});g=e('<ul id="'+t+'_feed"></ul>').width(m);k.after(l.prepend(g));name=f.attr("name");d.bricket&&"undefined"!=typeof name&&-1==name.indexOf("[]")&&(name+="[]");var z=e('<select name="'+name+'" id="'+t+'" multiple="multiple" class="'+f.get(0).className+' hidden">').data("cache",{});e.each(f.children("option"),function(a,c){c=e(c);z.data("cache")[c.val()]=c.text();if(c.hasClass("selected")){var b=o(c.text(),c.val(),true,c.hasClass("locked"));z.append('<option value="'+
c.val()+'" selected="selected" id="opt_'+b+'"class="selected">'+c.text()+"</option>")}});f.after(z);f.remove();f=z;e(f).bind("addItem",function(a,c){o(c.title,c.value,0,0,0)});e(f).bind("removeItem",function(a,c){var b=k.children("li[rel="+c.value+"]");b.length&&n(b)});e(f).bind("destroy",function(){k.remove();l.remove();f.show()});e(f).bind("selectAll",function(){var a=e(f).val()||[];e.each(e(f).data("cache"),function(c,b){e.inArray(c,a)===-1&&o(b,c,0,0,0)});g.parent().hide()});H(0);F.init();q.init();
w={element:f,getItems:function(){var a=[];e("option",f).each(function(c,b){a.push(e(b).val())});return a}};w.element.data("fcbkcomplete",w)}});return this}})(jQuery);