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