-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathjquery.easydropdown.min.js
21 lines (20 loc) · 5.66 KB
/
jquery.easydropdown.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
* EASYDROPDOWN - A Drop-down Builder for Styleable Inputs and Menus
* Version: 1.9
* License: Creative Commons Attribution 3.0 Unported - CC BY 3.0
* http://creativecommons.org/licenses/by/3.0/
* This software may be used freely on commercial and non-commercial projects with attribution to the author/copyright holder.
* Author: Patrick Kunka
* Copyright 2013 Patrick Kunka, All Rights Reserved
*/
(function(d){function e(){this.isField=!0;this.keyboardMode=this.hasLabel=this.cutOff=this.inFocus=this.down=!1;this.nativeTouch=!0;this.wrapperClass="dropdown";this.onSelect=null}function f(a,b){var c=new e;c.init(a,b);c.instances.push(c)}e.prototype={constructor:e,instances:[],init:function(a,b){var c=this;d.extend(c,b);c.$select=d(a);c.options=[];c.isTouch="ontouchend"in document;c.$select.removeClass(c.wrapperClass+" dropdown").find("option").each(function(a){var b=d(this);b.is(":selected")&&
(c.selected={index:a,title:b.text()},c.focusIndex=a);b.hasClass("label")&&0==a?(c.hasLabel=!0,c.label=b.text(),b.attr("value","")):c.options.push({domNode:b[0],title:b.text(),value:b.val(),selected:b.is(":selected")})});c.render()},render:function(){var a=this;a.$container=a.$select.wrap('<div class="'+a.wrapperClass+(a.isTouch&&a.nativeTouch?" touch":"")+'"><span class="old"/></div>').parent().parent();a.$active=d('<span class="selected">'+a.selected.title+"</span>").appendTo(a.$container);a.$carat=
d('<span class="carat"/>').appendTo(a.$container);a.$scrollWrapper=d("<div><ul/></div>").appendTo(a.$container);a.$dropDown=a.$scrollWrapper.find("ul");a.$form=a.$container.closest("form");d.each(a.options,function(){a.$dropDown.append("<li"+(this.selected?' class="active"':"")+">"+this.title+"</li>")});a.$items=a.$dropDown.find("li");a.maxHeight=0;a.cutOff&&a.$items.length>a.cutOff&&a.$container.addClass("scrollable");for(i=0;i<a.$items.length;i++){var b=a.$items.eq(i);a.maxHeight+=b.outerHeight();
if(a.cutOff==i+1)break}a.isTouch&&a.nativeTouch?a.bindTouchHandlers():a.bindHandlers()},bindTouchHandlers:function(){var a=this;a.$container.on("click",function(){a.$select.focus()});a.$select.on({change:function(){var b=d(this).find("option:selected"),c=b.text(),b=b.val();a.$active.text(c);"function"==typeof a.onSelect&&a.onSelect.call(a,{title:c,value:b})},focus:function(){a.$container.addClass("focus")},blur:function(){a.$container.removeClass("focus")}})},bindHandlers:function(){var a=this;a.query=
"";a.$container.on({click:function(){a.down?a.close():a.open()},mousemove:function(){a.keyboardMode&&(a.keyboardMode=!1)}});d("body").on("click",function(b){b=d(b.target);var c=a.wrapperClass.split(" ").join(".");!b.closest("."+c).length&&a.down&&a.close()});a.$items.on({click:function(){var b=d(this).index();a.select(b)},mouseover:function(){if(!a.keyboardMode){var b=d(this);b.addClass("focus").siblings().removeClass("focus");a.focusIndex=b.index()}},mouseout:function(){a.keyboardMode||d(this).removeClass("focus")}});
a.$select.on({focus:function(){a.$container.addClass("focus");a.inFocus=!0},blur:function(){a.$container.removeClass("focus");a.inFocus=!1}});a.$dropDown.on("scroll",function(b){a.$dropDown[0].scrollTop==a.$dropDown[0].scrollHeight-a.maxHeight?a.$container.addClass("bottom"):a.$container.removeClass("bottom")});a.$select.on("keydown",function(b){if(a.inFocus){a.keyboardMode=!0;var c=b.keyCode;if(38==c||40==c||32==c){b.preventDefault();38==c?(a.focusIndex--,a.focusIndex=0>a.focusIndex?a.$items.length-
1:a.focusIndex):40==c&&(a.focusIndex++,a.focusIndex=a.focusIndex>a.$items.length-1?0:a.focusIndex);a.down||a.open();a.$items.removeClass("focus").eq(a.focusIndex).addClass("focus");if(a.cutOff){var d=a.$items.eq(a.focusIndex).outerHeight()*(a.focusIndex+1)-a.maxHeight;a.$dropDown.scrollTop(d)}a.query=""}a.down&&(9==c||27==c?a.close():13==c?(b.preventDefault(),a.select(a.focusIndex),a.close()):8==c?(b.preventDefault(),a.query=a.query.slice(0,-1),a.search()):38!=c&&40!=c&&(b=String.fromCharCode(c),
a.query+=b,a.search()))}});if(a.$form.length)a.$form.on("reset",function(){a.$active.text(a.hasLabel?a.label:"")})},open:function(){var a=window.scrollY||document.documentElement.scrollTop,b=window.scrollX||document.documentElement.scrollLeft,c=this.notInViewport(a);this.closeAll();this.$select.focus();window.scrollTo(b,a+c);this.$container.addClass("open");this.$scrollWrapper.css("height",this.maxHeight+"px");this.down=!0},close:function(){this.$container.removeClass("open");this.$scrollWrapper.css("height",
"0px");this.focusIndex=this.selected.index;this.query="";this.down=!1},closeAll:function(){var a=Object.getPrototypeOf(this).instances;for(i=0;i<a.length;i++)a[i].close()},select:function(a){var b=this.options[a],c=this.hasLabel?a+1:a;this.$items.removeClass("active").eq(a).addClass("active");this.$active.text(b.title);this.$select.find("option").prop("selected",!1).eq(c).prop("selected","selected");this.selected={index:a,title:b.title};this.focusIndex=i;"function"==typeof this.onSelect&&this.onSelect.call(this,
{title:b.title,value:b.value})},search:function(){for(i=0;i<this.options.length;i++)if(-1!=this.options[i].title.toUpperCase().indexOf(this.query)){this.focusIndex=i;this.$items.removeClass("focus").eq(this.focusIndex).addClass("focus");break}},notInViewport:function(a){var b=a+(window.innerHeight||document.documentElement.clientHeight),c=this.$dropDown.offset().top+this.maxHeight;return c>=a&&c<=b?0:c-b+5}};d.fn.easyDropDown=function(a){return this.each(function(){f(this,a)})};d(function(){"function"!==
typeof Object.getPrototypeOf&&(Object.getPrototypeOf="object"===typeof"test".__proto__?function(a){return a.__proto__}:function(a){return a.constructor.prototype});d(".dropdown").each(function(){var a=d(this).attr("data-settings");settings=a?d.parseJSON(a):{};f(this,settings)})})})(jQuery);