You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 lines
16 KiB
1 lines
16 KiB
!function(a,b,c,d){"use strict";a.fn.dropdown=function(b){var e,f=a(this),g=a(c),h=f.selector||"",i="ontouchstart"in c.documentElement,j=(new Date).getTime(),k=[],l=arguments[0],m="string"==typeof l,n=[].slice.call(arguments,1);return f.each(function(){var c,o,p=a.isPlainObject(b)?a.extend(!0,{},a.fn.dropdown.settings,b):a.extend({},a.fn.dropdown.settings),q=p.className,r=p.metadata,s=p.namespace,t=p.selector,u=p.error,v="."+s,w="module-"+s,x=a(this),y=x.find(t.text),z=x.find(t.search),A=x.find(t.input),B=x.prev().find(t.text).size()>0?x.prev().find(t.text):x.prev(),C=x.children(t.menu),D=C.find(t.item),E=!1,F=this,G=x.data(w);o={initialize:function(){o.debug("Initializing dropdown",p),o.setup.layout(),o.save.defaults(),o.set.selected(),i&&o.bind.touchEvents(),o.observeChanges(),o.bind.mouseEvents(),o.bind.keyboardEvents(),o.instantiate()},instantiate:function(){o.verbose("Storing instance of dropdown",o),G=o,x.data(w,o)},destroy:function(){o.verbose("Destroying previous dropdown for",x),x.off(v).removeData(w)},observeChanges:function(){MutationObserver!==d&&(c=new MutationObserver(function(){o.debug("DOM tree modified, updating selector cache"),o.refresh()}),c.observe(F,{childList:!0,subtree:!0}),o.debug("Setting up mutation observer",c))},setup:{layout:function(){x.is("select")&&o.setup.select(),o.is.search()&&!o.is.searchable()&&(z=a("<input />").addClass(q.search).insertBefore(y)),p.allowTab&&(o.is.searchable()?(o.debug("Searchable dropdown initialized"),z.val("").attr("tabindex",0),C.attr("tabindex","-1")):(o.debug("Simple selection dropdown initialized"),x.attr("tabindex")||(x.attr("tabindex",0),C.attr("tabindex","-1"))))},select:function(){var b=o.get.selectValues();o.debug("Dropdown initialized on a select",b),A=x,A.parents(t.dropdown).size()>0?(o.debug("Creating dropdown menu only from template"),x=A.closest(t.dropdown),0===x.find("."+q.dropdown).size()&&a("<div />").addClass(q.menu).html(p.templates.menu(b)).appendTo(x)):(o.debug("Creating entire dropdown from template"),x=a("<div />").attr("class",A.attr("class")).addClass(q.selection).addClass(q.dropdown).html(p.templates.dropdown(b)).insertBefore(A),A.removeAttr("class").prependTo(x)),o.refresh()}},refresh:function(){y=x.find(t.text),z=x.find(t.search),A=x.find(t.input),C=x.children(t.menu),D=C.find(t.item)},bind:{keyboardEvents:function(){o.debug("Binding keyboard events"),x.on("keydown"+v,o.event.keydown),o.is.searchable()&&x.on(o.get.inputEvent(),t.search,o.event.input),o.is.searchSelection()?x.on("focus"+v,t.search,o.event.searchFocus).on("blur"+v,t.search,o.event.blur):x.on("mousedown",o.event.mousedown).on("mouseup",o.event.mouseup).on("focus"+v,o.event.focus).on("blur"+v,o.event.blur)},touchEvents:function(){o.debug("Touch device detected binding touch events"),o.is.searchSelection()||x.on("touchstart"+v,o.event.test.toggle),x.on("touchstart"+v,t.item,o.event.item.mouseenter).on("touchstart"+v,t.item,o.event.item.click)},mouseEvents:function(){o.verbose("Mouse detected binding mouse events"),o.is.searchSelection()||("click"==p.on?x.on("click"+v,o.event.test.toggle):"hover"==p.on?x.on("mouseenter"+v,o.delay.show).on("mouseleave"+v,o.delay.hide):x.on(p.on+v,o.toggle)),x.on("mouseenter"+v,t.item,o.event.item.mouseenter).on("mouseleave"+v,t.item,o.event.item.mouseleave).on("click"+v,t.item,o.event.item.click)},intent:function(){o.verbose("Binding hide intent event to document"),i&&g.on("touchstart"+v,o.event.test.touch).on("touchmove"+v,o.event.test.touch),g.on("click"+v,o.event.test.hide)}},unbind:{intent:function(){o.verbose("Removing hide intent event from document"),i&&g.off("touchstart"+v).off("touchmove"+v),g.off("click"+v)}},filter:function(b){var c,e=a(),f=new RegExp("(?:s|^)"+b,"i"),g=new RegExp(b,"i");D.each(function(){var b=a(this),c=b.data(r.text)!==d?b.data(r.text):p.preserveHTML?b.html():b.text(),h=b.data(r.value)!==d?b.data(r.value):"string"==typeof c?c.toLowerCase():c;f.test(c)||f.test(h)?e=e.add(b):p.fullTextSearch&&(g.test(c)||g.test(h))&&(e=e.add(b))}),c=D.not(e),D.removeClass(q.filtered).removeClass(q.selected),c.addClass(q.filtered),D.not("."+q.filtered).eq(0).addClass(q.selected)},event:{mousedown:function(){E=!0},mouseup:function(){E=!1},focus:function(){E||o.show()},searchFocus:function(){E=!0,o.show()},blur:function(){E||o.hide()},input:function(){var a=z.val();y.addClass(q.filtered),o.filter(a)},keydown:function(b){var c,d=D.not(q.filtered).filter("."+q.selected).eq(0),e=D.not("."+q.filtered),f=b.which,g={enter:13,escape:27,upArrow:38,downArrow:40},h=q.selected,i=e.index(d),j=d.size()>0;if(j||(d=D.filter("."+q.active).eq(0),j=d.size()>0),f==g.escape&&(o.verbose("Escape key pressed, closing dropdown"),z.blur(),o.hide()),o.is.visible()){if(f==g.enter&&j)return o.verbose("Enter key pressed, choosing selected item"),o.is.searchable()&&(o.verbose("Removing focus from search input"),z.blur()),a.proxy(o.event.item.click,d)(b),b.preventDefault(),!1;f==g.upArrow?(c=j?d.prevAll(t.item+":not(."+q.filtered+")").eq(0):e.eq(0),0!==i&&(o.verbose("Up key pressed, changing active item"),D.removeClass(h),c.addClass(h),o.set.scrollPosition(c)),b.preventDefault()):f==g.downArrow&&(c=j?d.nextAll(t.item+":not(."+q.filtered+")").eq(0):e.eq(0),i+1<e.size()&&(o.verbose("Down key pressed, changing active item"),D.removeClass(h),c.addClass(h),o.set.scrollPosition(c)),b.preventDefault())}else f==g.enter&&o.show()},test:{toggle:function(a){o.determine.eventInMenu(a,o.toggle)&&a.preventDefault()},touch:function(a){o.determine.eventInMenu(a,function(){"touchstart"==a.type?o.timer=setTimeout(o.hide,p.delay.touch):"touchmove"==a.type&&clearTimeout(o.timer)}),a.stopPropagation()},hide:function(a){o.determine.eventInModule(a,o.hide)}},item:{mouseenter:function(b){var c=a(this).find(t.menu),d=a(this).siblings(t.item).children(t.menu);c.size()>0&&(clearTimeout(o.itemTimer),o.itemTimer=setTimeout(function(){o.animate.hide(!1,d),o.verbose("Showing sub-menu",c),o.animate.show(!1,c)},2*p.delay.show),b.preventDefault())},mouseleave:function(){var b=a(this).find(t.menu);b.size()>0&&(clearTimeout(o.itemTimer),o.itemTimer=setTimeout(function(){o.verbose("Hiding sub-menu",b),o.animate.hide(!1,b)},p.delay.hide))},click:function(b){var c=a(this),e=c.data(r.text)!==d?c.data(r.text):p.preserveHTML?c.html():c.text(),f=c.data(r.value)!==d?c.data(r.value):"string"==typeof e?e.toLowerCase():e,g=function(){z.val(""),o.determine.selectAction(e,f),a.proxy(p.onChange,F)(f,e,c)},h=c.find(t.menu).size()>0;h||("touchstart"==b.type?c.one("click",g):g())}},resetStyle:function(){a(this).removeAttr("style")}},determine:{selectAction:function(b,c){o.verbose("Determining action",p.action),a.isFunction(o.action[p.action])?(o.verbose("Triggering preset action",p.action,b,c),o.action[p.action](b,c)):a.isFunction(p.action)?(o.verbose("Triggering user action",p.action,b,c),p.action(b,c)):o.error(u.action,p.action)},eventInModule:function(b,c){return c=c||function(){},0===a(b.target).closest(x).size()?(o.verbose("Triggering event",c),c(),!0):(o.verbose("Event occurred in dropdown, canceling callback"),!1)},eventInMenu:function(b,c){return c=c||function(){},0===a(b.target).closest(C).size()?(o.verbose("Triggering event",c),c(),!0):(o.verbose("Event occurred in dropdown menu, canceling callback"),!1)}},action:{nothing:function(){},hide:function(){o.hide()},select:function(a,b){b=b!==d?b:a,o.set.selected(b),o.set.value(b),o.hide()},activate:function(a,b){b=b!==d?b:a,o.set.selected(b),o.set.value(b),o.hide()},combo:function(a,b){b=b!==d?b:a,o.set.selected(b),o.set.value(b),o.hide()}},get:{text:function(){return y.text()},value:function(){return A.size()>0?A.val():x.data(r.value)},inputEvent:function(){var a=z[0];return a?a.oninput!==d?"input":a.onpropertychange!==d?"propertychange":"keyup":!1},selectValues:function(){var b={values:{}};return x.find("option").each(function(){var c=a(this).html(),e=a(this).attr("value")!==d?a(this).attr("value"):c;""===e?b.placeholder=c:b.values[e]=c}),o.debug("Retrieved values from select",b),b},item:function(b,c){var e=!1;return b=b!==d?b:o.get.value()!==d?o.get.value():o.get.text(),c=""===b||0===b?!0:c||!1,b!==d?D.each(function(){var f=a(this),g=f.data(r.text)!==d?f.data(r.text):p.preserveHTML?f.html():f.text(),h=f.data(r.value)!==d?f.data(r.value):"string"==typeof g?g.toLowerCase():g;c?(o.debug("Ambiguous dropdown value using strict type check",b),h===b?e=a(this):e||g!==b||(e=a(this))):h==b?(o.verbose("Found select item by value",h,b),e=a(this)):e||g!=b||(o.verbose("Found select item by text",g,b),e=a(this))}):b=o.get.text(),e||!1}},restore:{defaults:function(){o.restore.defaultText(),o.restore.defaultValue()},defaultText:function(){var a=x.data(r.defaultText);o.debug("Restoring default text",a),o.set.text(a)},defaultValue:function(){var a=x.data(r.defaultValue);a!==d&&(o.debug("Restoring default value",a),o.set.selected(a),o.set.value(a))}},save:{defaults:function(){o.save.defaultText(),o.save.defaultValue()},defaultValue:function(){x.data(r.defaultValue,o.get.value())},defaultText:function(){x.data(r.defaultText,y.text())}},set:{scrollPosition:function(a){var b,c,d,e,f,g,h,i,a=a||o.get.item(),j=a&&a.size()>0,k=5;a&&j&&(g=C.height(),c=a.height(),f=C.scrollTop(),e=C.offset().top,d=a.offset().top,b=f-e+d,i=b+k>f+g,h=f>b-k,(h||i)&&(o.debug("Scrolling to active item"),C.scrollTop(b)))},text:function(a){"combo"==p.action?(o.debug("Changing combo button text",a,B),p.preserveHTML?B.html(a):B.text(a)):"select"!==p.action&&(o.debug("Changing text",a,y),y.removeClass(q.filtered).removeClass(q.placeholder),p.preserveHTML?y.html(a):y.text(a))},value:function(a){o.debug("Adding selected value to hidden input",a,A),A.size()>0?A.val(a).trigger("change"):x.data(r.value,a)},active:function(){x.addClass(q.active)},visible:function(){x.addClass(q.visible)},selected:function(a){var b,c=o.get.item(a);c&&(o.debug("Setting selected menu item to",c),b=c.data(r.text)!==d?c.data(r.text):p.preserveHTML?c.html():c.text(),D.removeClass(q.active).removeClass(q.selected),c.addClass(q.active).addClass(q.selected),o.set.text(b))}},remove:{active:function(){x.removeClass(q.active)},visible:function(){x.removeClass(q.visible)}},is:{search:function(){return x.hasClass(q.search)},searchable:function(){return z.size()>0},searchSelection:function(){return o.is.searchable()&&z.parent().is(x)},selection:function(){return x.hasClass(q.selection)},animated:function(a){return a?a.is(":animated")||a.transition&&a.transition("is animating"):C.is(":animated")||C.transition&&C.transition("is animating")},visible:function(a){return a?a.is(":visible"):C.is(":visible")},hidden:function(a){return a?a.is(":hidden"):C.is(":hidden")}},can:{click:function(){return i||"click"==p.on},show:function(){return!x.hasClass(q.disabled)}},animate:{show:function(b,c){var e=c||C;b=b||function(){},o.is.hidden(e)&&(o.verbose("Doing menu show animation",e),"none"==p.transition?a.proxy(b,F)():a.fn.transition!==d&&x.transition("is supported")?e.transition({animation:p.transition+" in",duration:p.duration,queue:!0,complete:function(){a.proxy(b,F)()}}):"slide down"==p.transition?e.hide().clearQueue().children().clearQueue().css("opacity",0).delay(50).animate({opacity:1},p.duration,"easeOutQuad",o.event.resetStyle).end().slideDown(100,"easeOutQuad",function(){a.proxy(o.event.resetStyle,this)(),a.proxy(b,F)()}):"fade"==p.transition?e.hide().clearQueue().fadeIn(p.duration,function(){a.proxy(o.event.resetStyle,this)(),a.proxy(b,F)()}):o.error(u.transition,p.transition))},hide:function(b,c){var e=c||C;b=b||function(){},o.is.visible(e)&&(o.verbose("Doing menu hide animation",e),"none"==p.transition?a.proxy(b,F)():a.fn.transition!==d&&x.transition("is supported")?e.transition({animation:p.transition+" out",duration:p.duration,queue:!0,complete:function(){a.proxy(b,F)()}}):"slide down"==p.transition?e.show().clearQueue().children().clearQueue().css("opacity",1).animate({opacity:0},100,"easeOutQuad",o.event.resetStyle).end().delay(50).slideUp(100,"easeOutQuad",function(){a.proxy(o.event.resetStyle,this)(),a.proxy(b,F)()}):"fade"==p.transition?e.show().clearQueue().fadeOut(150,function(){a.proxy(o.event.resetStyle,this)(),a.proxy(b,F)()}):o.error(u.transition))}},show:function(){o.debug("Checking if dropdown can show"),o.is.hidden()&&(o.hideOthers(),o.set.active(),o.set.scrollPosition(),o.animate.show(function(){o.can.click()&&o.bind.intent(),o.set.visible()}),a.proxy(p.onShow,F)())},hide:function(){!o.is.animated()&&o.is.visible()&&(o.debug("Hiding dropdown"),o.can.click()&&o.unbind.intent(),o.remove.active(),o.animate.hide(function(){D.removeClass(q.filtered),o.remove.visible()}),a.proxy(p.onHide,F)())},delay:{show:function(){o.verbose("Delaying show event to ensure user intent"),clearTimeout(o.timer),o.timer=setTimeout(o.show,p.delay.show)},hide:function(){o.verbose("Delaying hide event to ensure user intent"),clearTimeout(o.timer),o.timer=setTimeout(o.hide,p.delay.hide)}},hideOthers:function(){o.verbose("Finding other dropdowns to hide"),f.not(x).has(t.menu+":visible").dropdown("hide")},toggle:function(){o.verbose("Toggling menu visibility"),o.is.hidden()?o.show():o.hide()},setting:function(b,c){if(o.debug("Changing setting",b,c),a.isPlainObject(b))a.extend(!0,p,b);else{if(c===d)return p[b];p[b]=c}},internal:function(b,c){if(a.isPlainObject(b))a.extend(!0,o,b);else{if(c===d)return o[b];o[b]=c}},debug:function(){p.debug&&(p.performance?o.performance.log(arguments):(o.debug=Function.prototype.bind.call(console.info,console,p.name+":"),o.debug.apply(console,arguments)))},verbose:function(){p.verbose&&p.debug&&(p.performance?o.performance.log(arguments):(o.verbose=Function.prototype.bind.call(console.info,console,p.name+":"),o.verbose.apply(console,arguments)))},error:function(){o.error=Function.prototype.bind.call(console.error,console,p.name+":"),o.error.apply(console,arguments)},performance:{log:function(a){var b,c,d;p.performance&&(b=(new Date).getTime(),d=j||b,c=b-d,j=b,k.push({Name:a[0],Arguments:[].slice.call(a,1)||"",Element:F,"Execution Time":c})),clearTimeout(o.performance.timer),o.performance.timer=setTimeout(o.performance.display,100)},display:function(){var b=p.name+":",c=0;j=!1,clearTimeout(o.performance.timer),a.each(k,function(a,b){c+=b["Execution Time"]}),b+=" "+c+"ms",h&&(b+=" '"+h+"'"),(console.group!==d||console.table!==d)&&k.length>0&&(console.groupCollapsed(b),console.table?console.table(k):a.each(k,function(a,b){console.log(b.Name+": "+b["Execution Time"]+"ms")}),console.groupEnd()),k=[]}},invoke:function(b,c,f){var g,h,i,j=G;return c=c||n,f=F||f,"string"==typeof b&&j!==d&&(b=b.split(/[\. ]/),g=b.length-1,a.each(b,function(c,e){var f=c!=g?e+b[c+1].charAt(0).toUpperCase()+b[c+1].slice(1):b;if(a.isPlainObject(j[f])&&c!=g)j=j[f];else{if(j[f]!==d)return h=j[f],!1;if(!a.isPlainObject(j[e])||c==g)return j[e]!==d?(h=j[e],!1):(o.error(u.method,b),!1);j=j[e]}})),a.isFunction(h)?i=h.apply(f,c):h!==d&&(i=h),a.isArray(e)?e.push(i):e!==d?e=[e,i]:i!==d&&(e=i),h}},m?(G===d&&o.initialize(),o.invoke(l)):(G!==d&&o.destroy(),o.initialize())}),e!==d?e:this},a.fn.dropdown.settings={name:"Dropdown",namespace:"dropdown",debug:!1,verbose:!0,performance:!0,type:!1,on:"click",action:"activate",allowTab:!0,fullTextSearch:!0,preserveHTML:!0,delay:{show:200,hide:300,touch:50},transition:"slide down",duration:250,onChange:function(){},onShow:function(){},onHide:function(){},error:{action:"You called a dropdown action that was not defined",method:"The method you called is not defined.",transition:"The requested transition was not found"},metadata:{defaultText:"defaultText",defaultValue:"defaultValue",text:"text",value:"value"},selector:{dropdown:".ui.dropdown",text:"> .text:not(.icon)",input:'> input[type="hidden"], > select',search:"> .search, .menu > .search > input, .menu > input.search",menu:".menu",item:".item"},className:{active:"active",disabled:"disabled",dropdown:"ui dropdown",filtered:"filtered",menu:"menu",placeholder:"default",search:"search",selected:"selected",selection:"selection",visible:"visible"}},a.fn.dropdown.settings.templates={menu:function(b){var c=(b.placeholder||!1,b.values||{},"");return a.each(b.values,function(a,b){c+=a===b?'<div class="item">'+b+"</div>":'<div class="item" data-value="'+a+'">'+b+"</div>"}),c},dropdown:function(b){var c=b.placeholder||!1,d=(b.values||{},"");return d+='<i class="dropdown icon"></i>',d+=b.placeholder?'<div class="default text">'+c+"</div>":'<div class="text"></div>',d+='<div class="menu">',a.each(b.values,function(a,b){d+=a===b?'<div class="item">'+b+"</div>":'<div class="item" data-value="'+a+'">'+b+"</div>"}),d+="</div>"}},a.extend(a.easing,{easeOutQuad:function(a,b,c,d,e){return-d*(b/=e)*(b-2)+c}})}(jQuery,window,document);
|