forked from FormstoneClassic/Selecter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.fs.selecter.min.js
10 lines (9 loc) · 6.2 KB
/
jquery.fs.selecter.min.js
1
2
3
4
5
6
7
8
9
10
/*
* Selecter Plugin [Formstone Library]
* @author Ben Plum
* @version 1.9.8
*
* Copyright © 2013 Ben Plum <[email protected]>
* Released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
*/
if(jQuery)(function(g){function u(a){a=a||{};a=g.extend({},v,a);for(var b=g(this),d=0,e=b.length;d<e;d++){var f=b.eq(d),h=a;if(!f.data("selecter")){h.externalLinks&&(h.links=!0);var k=f.find("option, optgroup"),j=k.filter("option"),s=j.filter(":selected"),p=h.defaultLabel?-1:j.index(s),q=k.length-1,r=h.links?"nav":"div",w=h.links?"a":"span";h.multiple=f.prop("multiple");h.disabled=f.is(":disabled");var c="<"+r+' class="selecter '+h.customClass;m?c+=" mobile":h.cover&&(c+=" cover");c=h.multiple?c+" multiple": c+" closed";h.disabled&&(c+=" disabled");c+='">';h.multiple||(c+='<span class="selecter-selected">',c+=x(h.trimOptions,!1!=h.defaultLabel?h.defaultLabel:s.text()),c+="</span>");for(var c=c+'<div class="selecter-options">',s=0,l=null,n=0,u=k.length;n<u;n++)l=g(k[n]),"OPTGROUP"==l[0].tagName?c+='<span class="selecter-group">'+l.attr("label")+"</span>":(c+="<"+w+' class="selecter-item',l.is(":selected")&&!h.defaultLabel&&(c+=" selected"),0==n&&(c+=" first"),n==q&&(c+=" last"),c+='" ',c=h.links?c+('href="'+ l.val()+'"'):c+('data-value="'+l.val()+'"'),c+=">"+x(h.trimOptions,l.text())+"</"+w+">",s++);c+="</div>";c+="</"+r+">";f.addClass("selecter-element").after(c);k=f.next(".selecter");j=g.extend({$selectEl:f,$optionEls:j,$selecter:k,$selected:k.find(".selecter-selected"),$itemsWrapper:k.find(".selecter-options"),$items:k.find(".selecter-item"),index:p,guid:y},h);void 0!=g.fn.scroller&&j.$itemsWrapper.scroller();k.on("click.selecter",".selecter-selected",j,z).on("click.selecter",".selecter-item",j,A).on("selecter-close", j,t).data("selecter",j);if(!h.links&&!m||m){if(f.on("change",j,B).on("blur.selecter",j,C),!m)f.on("focus.selecter",j,D)}else f.hide();y++}}return b}function z(a){a.preventDefault();a.stopPropagation();var b=a.data;if(!b.$selectEl.is(":disabled"))if(g(".selecter").not(b.$selecter).trigger("selecter-close",[b]),m)a=b.$selectEl[0],document.createEvent?(b=document.createEvent("MouseEvents"),b.initMouseEvent("mousedown",!0,!0,window,0,0,0,0,0,!1,!1,!1,!1,0,null),a.dispatchEvent(b)):element.fireEvent&& a.fireEvent("onmousedown");else if(b.$selecter.hasClass("closed")){if(a.preventDefault(),a.stopPropagation(),a=a.data,!a.$selecter.hasClass("open")){var b=a.$selecter.offset(),d=g("body").outerHeight(),e=a.$itemsWrapper.outerHeight(!0);b.top+e>d&&m?a.$selecter.addClass("bottom"):a.$selecter.removeClass("bottom");a.$itemsWrapper.show();a.$selecter.removeClass("closed").addClass("open");g("body").on("click.selecter-"+a.guid,":not(.selecter-options)",a,E);void 0!=g.fn.scroller&&a.$itemsWrapper.scroller("reset")}}else b.$selecter.hasClass("open")&& t(a)}function t(a){a.preventDefault();a.stopPropagation();a=a.data;a.$selecter.hasClass("open")&&(a.$itemsWrapper.hide(),a.$selecter.removeClass("open").addClass("closed"),g("body").off(".selecter-"+a.guid))}function E(a){a.preventDefault();a.stopPropagation();0==g(a.currentTarget).parents(".selecter").length&&t(a)}function A(a){a.preventDefault();a.stopPropagation();var b=g(this),d=a.data;d.$selectEl.is(":disabled")||(d.links?(b=b.attr("href"),d.externalLinks?window.open(b):window.location.href= b):d.$itemsWrapper.is(":visible")&&(b=d.$items.index(b),p(b,d,!1)),d.multiple||t(a))}function B(a,b){if(!b){var d=g(this),e=a.data;e.links?(d=d.attr("href"),e.externalLinks?window.open(d):window.location.href=d):(d=e.$optionEls.index(e.$optionEls.filter("[value="+d.val()+"]")),p(d,e,!1))}}function D(a){a.preventDefault();a.stopPropagation();a=a.data;!a.$selectEl.is(":disabled")&&!a.multiple&&(a.$selecter.addClass("focus"),g(".selecter").not(a.$selecter).trigger("selecter-close",[a]),g("body").on("keydown.selecter-"+ a.guid,a,F))}function C(a){a.preventDefault();a.stopPropagation();a=a.data;a.$selecter.removeClass("focus");g(".selecter").not(a.$selecter).trigger("selecter-close",[a]);g("body").off(".selecter-"+a.guid)}function F(a){if(9!=a.keyCode&&!a.metaKey&&!a.altKey&&!a.ctrlKey&&!a.shiftKey){a.preventDefault();a.stopPropagation();var b=a.data,d=b.$items.length-1,e=-1;if(-1<g.inArray(a.keyCode,q?[38,40,37,39]:[38,40]))e=b.index+(38==a.keyCode||q&&37==a.keyCode?-1:1),0>e&&(e=0),e>d&&(e=d);else{a=String.fromCharCode(a.keyCode).toUpperCase(); for(i=b.index+1;i<=d;i++){var f=b.$optionEls.eq(i).text().charAt(0).toUpperCase();if(f==a){e=i;break}}if(0>e)for(i=0;i<=d;i++)if(f=b.$optionEls.eq(i).text().charAt(0).toUpperCase(),f==a){e=i;break}}0<=e&&p(e,b,!0);return!1}}function p(a,b,d){var e=b.$items.eq(a);if(e.hasClass("selected"))b.multiple&&(b.$optionEls.eq(a).prop("selected",null),e.removeClass("selected"));else{var f=e.html();e.data("value");if(b.multiple)b.$optionEls.eq(a).prop("selected",!0);else if(b.$selected.html(f),b.$items.filter(".selected").removeClass("selected"), !d||d&&!q)b.$selectEl[0].selectedIndex=a;b.$selectEl.trigger("change",[!0]);e.addClass("selected");b.callback.call(b.$selecter,b.$selectEl.val());b.index=a}}function x(a,b){return!1===a?b:b.length>a?b.substring(0,a)+"...":b}var q=-1<navigator.userAgent.toLowerCase().indexOf("firefox"),m=/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent||navigator.vendor||window.opera),v={callback:function(){},cover:!1,customClass:"",defaultLabel:!1,externalLinks:!1,links:!1,trimOptions:!1},y=0, r={defaults:function(a){v=g.extend(v,a||{});return g(this)},disable:function(){for(var a=g(this),b=0,d=a.length;b<d;b++){var e=a.eq(b),f=e.next(".selecter");f.hasClass("open")&&f.find(".selecter-selected").trigger("click");e.prop("disabled",!0);f.addClass("disabled")}return a},enable:function(){for(var a=g(this),b=0,d=a.length;b<d;b++){var e=a.eq(b),f=e.next(".selecter");e.prop("disabled",null);f.removeClass("disabled")}return a},destroy:function(){for(var a=g(this),b=0,d=a.length;b<d;b++){var e= a.eq(b),f=e.next(".selecter");f.hasClass("open")&&f.find(".selecter-selected").trigger("click");void 0!=g.fn.scroller&&f.find(".selecter-options").scroller("destroy");e.off(".selecter").removeClass("selecter-element").show();f.off(".selecter").remove()}return a}};g.fn.selecter=function(a){return r[a]?r[a].apply(this,Array.prototype.slice.call(arguments,1)):"object"===typeof a||!a?u.apply(this,arguments):this}})(jQuery);