-
Notifications
You must be signed in to change notification settings - Fork 89
/
jquery.fs.selecter.min.js
9 lines (8 loc) · 9.12 KB
/
jquery.fs.selecter.min.js
1
2
3
4
5
6
7
8
9
/*
* Selecter v3.2.4 - 2015-04-04
* A jQuery plugin for replacing default select elements. Part of the Formstone Library.
* http://classic.formstone.it/selecter/
*
* Copyright 2015 Ben Plum; MIT Licensed
*/
!function(a,b){"use strict";function c(b){b=a.extend({},D,b||{}),null===C&&(C=a("body"));for(var c=a(this),e=0,f=c.length;f>e;e++)d(c.eq(e),b);return c}function d(b,c){if(!b.hasClass("selecter-element")){c=a.extend({},c,b.data("selecter-options")),c.multiple=b.prop("multiple"),c.disabled=b.is(":disabled"),c.external&&(c.links=!0);var d=b.find("[selected]").not(":disabled"),g=b.find("option").index(d);c.multiple||""===c.label?c.label="":(b.prepend('<option value="" class="selecter-placeholder" selected>'+c.label+"</option>"),g>-1&&g++);var h=b.find("option, optgroup"),j=h.filter("option");d.length||(d=j.eq(0));var k=g>-1?g:0,q=""!==c.label?c.label:d.text(),s="div";c.tabIndex=b[0].tabIndex,b[0].tabIndex=-1;var t="",u="";u+="<"+s+' class="selecter '+c.customClass,A?u+=" mobile":c.cover&&(u+=" cover"),u+=c.multiple?" multiple":" closed",c.disabled&&(u+=" disabled"),u+='" tabindex="'+c.tabIndex+'">',u+="</"+s+">",c.multiple||(t+='<span class="selecter-selected">',t+=a("<span></span>").text(v(q,c.trim)).html(),t+="</span>"),t+='<div class="selecter-options">',t+="</div>",b.addClass("selecter-element").wrap(u).after(t);var w=b.parent(".selecter"),y=a.extend({$select:b,$allOptions:h,$options:j,$selecter:w,$selected:w.find(".selecter-selected"),$itemsWrapper:w.find(".selecter-options"),index:-1,guid:x++},c);e(y),y.multiple||r(k,y),void 0!==a.fn.scroller&&y.$itemsWrapper.scroller(),y.$selecter.on("touchstart.selecter",".selecter-selected",y,f).on("click.selecter",".selecter-selected",y,i).on("click.selecter",".selecter-item",y,m).on("close.selecter",y,l).data("selecter",y),y.$select.on("change.selecter",y,n),A||(y.$selecter.on("focusin.selecter",y,o).on("blur.selecter",y,p),y.$select.on("focusin.selecter",y,function(a){a.data.$selecter.trigger("focus")}))}}function e(b){for(var c="",d=b.links?"a":"span",e=0,f=0,g=b.$allOptions.length;g>f;f++){var h=b.$allOptions.eq(f);if("OPTGROUP"===h[0].tagName)c+='<span class="selecter-group',h.is(":disabled")&&(c+=" disabled"),c+='">'+h.attr("label")+"</span>";else{var i=h.val();h.attr("value")||h.attr("value",i),c+="<"+d+' class="selecter-item',h.hasClass("selecter-placeholder")&&(c+=" placeholder"),h.is(":selected")&&(c+=" selected"),h.is(":disabled")&&(c+=" disabled"),c+='" ',c+=b.links?'href="'+i+'"':'data-value="'+i+'"',c+=">"+a("<span></span>").text(v(h.text(),b.trim)).html()+"</"+d+">",e++}}b.$itemsWrapper.html(c),b.$items=b.$selecter.find(".selecter-item")}function f(a){a.stopPropagation();var b=a.data;b.touchStartEvent=a.originalEvent,b.touchStartX=b.touchStartEvent.touches[0].clientX,b.touchStartY=b.touchStartEvent.touches[0].clientY,b.$selecter.on("touchmove.selecter",".selecter-selected",b,g).on("touchend.selecter",".selecter-selected",b,h)}function g(a){var b=a.data,c=a.originalEvent;(Math.abs(c.touches[0].clientX-b.touchStartX)>10||Math.abs(c.touches[0].clientY-b.touchStartY)>10)&&b.$selecter.off("touchmove.selecter touchend.selecter")}function h(a){var b=a.data;b.touchStartEvent.preventDefault(),b.$selecter.off("touchmove.selecter touchend.selecter"),i(a)}function i(c){c.preventDefault(),c.stopPropagation();var d=c.data;if(!d.$select.is(":disabled"))if(a(".selecter").not(d.$selecter).trigger("close.selecter",[d]),d.mobile||!A||B)d.$selecter.hasClass("closed")?j(c):d.$selecter.hasClass("open")&&l(c);else{var e=d.$select[0];if(b.document.createEvent){var f=b.document.createEvent("MouseEvents");f.initMouseEvent("mousedown",!1,!0,b,0,0,0,0,0,!1,!1,!1,!1,0,null),e.dispatchEvent(f)}else e.fireEvent&&e.fireEvent("onmousedown")}}function j(a){a.preventDefault(),a.stopPropagation();var b=a.data;if(!b.$selecter.hasClass("open")){{var c=b.$selecter.offset(),d=C.outerHeight(),e=b.$itemsWrapper.outerHeight(!0);b.index>=0?b.$items.eq(b.index).position():{left:0,top:0}}c.top+e>d&&b.$selecter.addClass("bottom"),b.$itemsWrapper.show(),b.$selecter.removeClass("closed").addClass("open"),C.on("click.selecter-"+b.guid,":not(.selecter-options)",b,k),s(b)}}function k(b){b.preventDefault(),b.stopPropagation(),0===a(b.currentTarget).parents(".selecter").length&&l(b)}function l(a){a.preventDefault(),a.stopPropagation();var b=a.data;b.$selecter.hasClass("open")&&(b.$itemsWrapper.hide(),b.$selecter.removeClass("open bottom").addClass("closed"),C.off(".selecter-"+b.guid))}function m(b){b.preventDefault(),b.stopPropagation();var c=a(this),d=b.data;if(!d.$select.is(":disabled")){if(d.$itemsWrapper.is(":visible")){var e=d.$items.index(c);e!==d.index&&(r(e,d),t(d))}d.multiple||l(b)}}function n(b,c){var d=a(this),e=b.data;if(!c&&!e.multiple){var f=e.$options.index(e.$options.filter("[value='"+w(d.val())+"']"));r(f,e),t(e)}}function o(b){b.preventDefault(),b.stopPropagation();var c=b.data;c.$select.is(":disabled")||c.multiple||(c.$selecter.addClass("focus").on("keydown.selecter-"+c.guid,c,q),a(".selecter").not(c.$selecter).trigger("close.selecter",[c]))}function p(b,c,d){b.preventDefault(),b.stopPropagation();var e=b.data;e.$selecter.removeClass("focus").off("keydown.selecter-"+e.guid),a(".selecter").not(e.$selecter).trigger("close.selecter",[e])}function q(b){var c=b.data;if(13===b.keyCode)c.$selecter.hasClass("open")&&(l(b),r(c.index,c)),t(c);else if(!(9===b.keyCode||b.metaKey||b.altKey||b.ctrlKey||b.shiftKey)){b.preventDefault(),b.stopPropagation();var d=c.$items.length-1,e=c.index<0?0:c.index;if(a.inArray(b.keyCode,z?[38,40,37,39]:[38,40])>-1)e+=38===b.keyCode||z&&37===b.keyCode?-1:1,0>e&&(e=0),e>d&&(e=d);else{var f,g,h=String.fromCharCode(b.keyCode).toUpperCase();for(g=c.index+1;d>=g;g++)if(f=c.$options.eq(g).text().charAt(0).toUpperCase(),f===h){e=g;break}if(0>e||e===c.index)for(g=0;d>=g;g++)if(f=c.$options.eq(g).text().charAt(0).toUpperCase(),f===h){e=g;break}}e>=0&&(r(e,c),s(c))}}function r(a,b){var c=b.$items.eq(a),d=c.hasClass("selected"),e=c.hasClass("disabled");if(!e)if(b.multiple)d?(b.$options.eq(a).prop("selected",null),c.removeClass("selected")):(b.$options.eq(a).prop("selected",!0),c.addClass("selected"));else if(a>-1&&a<b.$items.length){{var f=c.html();c.data("value")}b.$selected.html(f).removeClass("placeholder"),b.$items.filter(".selected").removeClass("selected"),b.$select[0].selectedIndex=a,c.addClass("selected"),b.index=a}else""!==b.label&&b.$selected.html(b.label)}function s(b){var c=b.$items.eq(b.index),d=b.index>=0&&!c.hasClass("placeholder")?c.position():{left:0,top:0};void 0!==a.fn.scroller?b.$itemsWrapper.scroller("scroll",b.$itemsWrapper.find(".scroller-content").scrollTop()+d.top,0).scroller("reset"):b.$itemsWrapper.scrollTop(b.$itemsWrapper.scrollTop()+d.top)}function t(a){a.links?u(a):(a.callback.call(a.$selecter,a.$select.val(),a.index),a.$select.trigger("change",[!0]))}function u(a){var c=a.$select.val();a.external?b.open(c):b.location.href=c}function v(a,b){return 0===b?a:a.length>b?a.substring(0,b)+"...":a}function w(a){return"string"==typeof a?a.replace(/([;&,\.\+\*\~':"\!\^#$%@\[\]\(\)=>\|])/g,"\\$1"):a}var x=0,y=b.navigator.userAgent||b.navigator.vendor||b.opera,z=/Firefox/i.test(y),A=/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(y),B=z&&A,C=null,D={callback:a.noop,cover:!1,customClass:"",label:"",external:!1,links:!1,mobile:!1,trim:0},E={defaults:function(b){return D=a.extend(D,b||{}),"object"==typeof this?a(this):!0},disable:function(b){return a(this).each(function(c,d){var e=a(d).parent(".selecter").data("selecter");if(e)if("undefined"!=typeof b){var f=e.$items.index(e.$items.filter("[data-value="+b+"]"));e.$items.eq(f).addClass("disabled"),e.$options.eq(f).prop("disabled",!0)}else e.$selecter.hasClass("open")&&e.$selecter.find(".selecter-selected").trigger("click.selecter"),e.$selecter.addClass("disabled"),e.$select.prop("disabled",!0)})},destroy:function(){return a(this).each(function(b,c){var d=a(c).parent(".selecter").data("selecter");d&&(d.$selecter.hasClass("open")&&d.$selecter.find(".selecter-selected").trigger("click.selecter"),void 0!==a.fn.scroller&&d.$selecter.find(".selecter-options").scroller("destroy"),d.$select[0].tabIndex=d.tabIndex,d.$select.find(".selecter-placeholder").remove(),d.$selected.remove(),d.$itemsWrapper.remove(),d.$selecter.off(".selecter"),d.$select.off(".selecter").removeClass("selecter-element").show().unwrap())})},enable:function(b){return a(this).each(function(c,d){var e=a(d).parent(".selecter").data("selecter");if(e)if("undefined"!=typeof b){var f=e.$items.index(e.$items.filter("[data-value="+b+"]"));e.$items.eq(f).removeClass("disabled"),e.$options.eq(f).prop("disabled",!1)}else e.$selecter.removeClass("disabled"),e.$select.prop("disabled",!1)})},refresh:function(){return E.update.apply(a(this))},update:function(){return a(this).each(function(b,c){var d=a(c).parent(".selecter").data("selecter");if(d){var f=d.index;d.$allOptions=d.$select.find("option, optgroup"),d.$options=d.$allOptions.filter("option"),d.index=-1,f=d.$options.index(d.$options.filter(":selected")),e(d),d.multiple||r(f,d)}})}};a.fn.selecter=function(a){return E[a]?E[a].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof a&&a?this:c.apply(this,arguments)},a.selecter=function(a){"defaults"===a&&E.defaults.apply(this,Array.prototype.slice.call(arguments,1))}}(jQuery,window);