forked from zahirchanda/easydropdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.easydropdown.min.js
54 lines (54 loc) · 7.52 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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* EASYDROPDOWN - A Drop-down Builder for Styleable Inputs and Menus
* Version: 2.1.5
* 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(e){function t(){this.isField=!0,this.down=!1,this.inFocus=!1,this.disabled=!1,this.cutOff=!1,this.hasLabel=!1,this.keyboardMode=!1,this.nativeTouch=!0,this.wrapperClass="dropdown",this.onChange=null}t.prototype={constructor:t,instances:{},init:function(t,o){var n=this
e.extend(n,o),n.$select=e(t),n.id=t.id,n.options=[],n.$options=n.$select.find("option"),n.isTouch="ontouchend"in document,n.$select.removeClass(n.wrapperClass+" dropdown"),n.$select.is(":disabled")&&(n.disabled=!0),n.$options.length&&(n.$options.each(function(t){var o=e(this)
o.is(":selected")&&(n.selected={index:t,title:o.text()},n.focusIndex=t),o.hasClass("label")&&0==t?(n.hasLabel=!0,n.label=o.text(),o.attr("value","")):n.options.push({domNode:o[0],title:o.text(),value:o.val(),selected:o.is(":selected")})}),n.selected||(n.selected={index:0,title:n.$options.eq(0).text()},n.focusIndex=0),n.render())},render:function(){var t=this,o=t.isTouch&&t.nativeTouch?" touch":"",n=t.disabled?" disabled":""
t.$container=t.$select.wrap('<div class="'+t.wrapperClass+o+n+'"><span class="old"/></div>').parent().parent(),t.$active=e('<span class="selected">'+t.selected.title+"</span>").appendTo(t.$container),t.$carat=e('<span class="carat"/>').appendTo(t.$container),t.$scrollWrapper=e("<div><ul/></div>").appendTo(t.$container),t.$dropDown=t.$scrollWrapper.find("ul"),t.$form=t.$container.closest("form"),e.each(t.options,function(){var e=this,o=e.selected?' class="active"':""
t.$dropDown.append("<li"+o+">"+e.title+"</li>")}),t.$items=t.$dropDown.find("li"),t.cutOff&&t.$items.length>t.cutOff&&t.$container.addClass("scrollable"),t.getMaxHeight(),t.isTouch&&t.nativeTouch?t.bindTouchHandlers():t.bindHandlers()},getMaxHeight:function(){var e=this
for(e.maxHeight=0,i=0;i<e.$items.length;i++){var t=e.$items.eq(i)
if(e.maxHeight+=t.outerHeight(),e.cutOff==i+1)break}},bindTouchHandlers:function(){var t=this
t.$container.on("click.easyDropDown",function(){t.$select.focus()}),t.$select.on({change:function(){var o=e(this).find("option:selected"),n=o.text(),s=o.val()
t.$active.text(n),"function"==typeof t.onChange&&t.onChange.call(t.$select[0],{title:n,value:s})},focus:function(){t.$container.addClass("focus")},blur:function(){t.$container.removeClass("focus")}})},bindHandlers:function(){var t=this
t.query="",t.$container.on({"click.easyDropDown":function(){t.down||t.disabled?t.close():t.open()},"mousemove.easyDropDown":function(){t.keyboardMode&&(t.keyboardMode=!1)}}),e("body").on("click.easyDropDown."+t.id,function(o){var n=e(o.target),s=t.wrapperClass.split(" ").join(".")
!n.closest("."+s).length&&t.down&&t.close()}),t.$items.on({"click.easyDropDown":function(){var o=e(this).index()
t.select(o)},"mouseover.easyDropDown":function(){if(!t.keyboardMode){var o=e(this)
o.addClass("focus").siblings().removeClass("focus"),t.focusIndex=o.index()}},"mouseout.easyDropDown":function(){t.keyboardMode||e(this).removeClass("focus")}}),t.$select.on({"focus.easyDropDown":function(){t.$container.addClass("focus"),t.inFocus=!0},"blur.easyDropDown":function(){t.$container.removeClass("focus"),t.inFocus=!1},"keydown.easyDropDown":function(e){if(t.inFocus){t.keyboardMode=!0
var o=e.keyCode
if((38==o||40==o||32==o)&&(e.preventDefault(),38==o?(t.focusIndex--,t.focusIndex=t.focusIndex<0?t.$items.length-1:t.focusIndex):40==o&&(t.focusIndex++,t.focusIndex=t.focusIndex>t.$items.length-1?0:t.focusIndex),t.down||t.open(),t.$items.removeClass("focus").eq(t.focusIndex).addClass("focus"),t.cutOff&&t.scrollToView(),t.query=""),t.down)if(9==o||27==o)t.close()
else{if(13==o)return e.preventDefault(),t.select(t.focusIndex),t.close(),!1
if(8==o)return e.preventDefault(),t.query=t.query.slice(0,-1),t.search(),clearTimeout(t.resetQuery),!1
if(38!=o&&40!=o){var n=String.fromCharCode(o)
t.query+=n,t.search(),clearTimeout(t.resetQuery)}}}},"keyup.easyDropDown":function(){t.resetQuery=setTimeout(function(){t.query=""},1200)}}),t.$dropDown.on("scroll.easyDropDown",function(e){t.$dropDown[0].scrollTop>=t.$dropDown[0].scrollHeight-t.maxHeight?t.$container.addClass("bottom"):t.$container.removeClass("bottom")}),t.$form.length&&t.$form.on("reset.easyDropDown",function(){var e=t.hasLabel?t.label:t.options[0].title
t.$active.text(e)})},unbindHandlers:function(){var t=this
t.$container.add(t.$select).add(t.$items).add(t.$form).add(t.$dropDown).off(".easyDropDown"),e("body").off("."+t.id)},open:function(){var e=this,t=window.scrollY||document.documentElement.scrollTop,o=window.scrollX||document.documentElement.scrollLeft,n=e.notInViewport(t)
e.closeAll(),e.getMaxHeight(),e.$select.focus(),window.scrollTo(o,t+n),e.$container.addClass("open"),e.$scrollWrapper.css("height",e.maxHeight+"px"),e.down=!0},close:function(){var e=this
e.$container.removeClass("open"),e.$scrollWrapper.css("height","0px"),e.focusIndex=e.selected.index,e.query="",e.down=!1},closeAll:function(){var e=this,t=Object.getPrototypeOf(e).instances
for(var o in t){var n=t[o]
n.close()}},select:function(e){var t=this
"string"==typeof e&&(e=t.$select.find("option[value="+e+"]").index()-1)
var o=t.options[e],n=t.hasLabel?e+1:e
t.$items.removeClass("active").eq(e).addClass("active"),t.$active.text(o.title),t.$select.find("option").removeAttr("selected").eq(n).prop("selected",!0).parent().trigger("change"),t.selected={index:e,title:o.title},t.focusIndex=i,"function"==typeof t.onChange&&t.onChange.call(t.$select[0],{title:o.title,value:o.value})},search:function(){var e=this,t=function(t){e.focusIndex=t,e.$items.removeClass("focus").eq(e.focusIndex).addClass("focus"),e.scrollToView()},o=function(t){return e.options[t].title.toUpperCase()}
for(i=0;i<e.options.length;i++){var n=o(i)
if(0==n.indexOf(e.query))return void t(i)}for(i=0;i<e.options.length;i++){var n=o(i)
if(n.indexOf(e.query)>-1){t(i)
break}}},scrollToView:function(){var e=this
if(e.focusIndex>=e.cutOff){var t=e.$items.eq(e.focusIndex),o=t.outerHeight()*(e.focusIndex+1)-e.maxHeight
e.$dropDown.scrollTop(o)}},notInViewport:function(e){var t=this,o={min:e,max:e+(window.innerHeight||document.documentElement.clientHeight)},n=t.$dropDown.offset().top+t.maxHeight
return n>=o.min&&n<=o.max?0:n-o.max+5},destroy:function(){var e=this
e.unbindHandlers(),e.$select.unwrap().siblings().remove(),e.$select.unwrap(),delete Object.getPrototypeOf(e).instances[e.$select[0].id]},disable:function(){var e=this
e.disabled=!0,e.$container.addClass("disabled"),e.$select.attr("disabled",!0),e.down||e.close()},enable:function(){var e=this
e.disabled=!1,e.$container.removeClass("disabled"),e.$select.attr("disabled",!1)}}
var o=function(e,o){e.id=e.id?e.id:"EasyDropDown"+n()
var s=new t
s.instances[e.id]||(s.instances[e.id]=s,s.init(e,o))},n=function(){return("00000"+(16777216*Math.random()<<0).toString(16)).substr(-6).toUpperCase()}
e.fn.easyDropDown=function(){var e,n=arguments,s=[]
return e=this.each(function(){if(n&&"string"==typeof n[0]){var e=t.prototype.instances[this.id][n[0]](n[1],n[2])
e&&s.push(e)}else o(this,n[0])}),s.length?s.length>1?s:s[0]:e},e(function(){"function"!=typeof Object.getPrototypeOf&&(Object.getPrototypeOf="object"==typeof"test".__proto__?function(e){return e.__proto__}:function(e){return e.constructor.prototype}),e("select.dropdown").each(function(){var t=e(this).attr("data-settings")
settings=t?e.parseJSON(t):{},o(this,settings)})})}(jQuery)