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
6.6 KiB
1 lines
6.6 KiB
(function(e,n,t,o){e.fn.dropdown=function(n){var i,a=e(this),r=e(t),s=e.isPlainObject(n)?e.extend(!0,{},e.fn.dropdown.settings,n):e.fn.dropdown.settings,c="."+s.namespace,l="module-"+s.namespace,u=a.selector||"",d=(new Date).getTime(),m=[],f=arguments[0],g="string"==typeof f,p=[].slice.call(arguments,1);return a.each(function(){var n,h=e(this),v=e(this).find(s.selector.menu),b=e(this).find(s.selector.item),y=e(this).find(s.selector.text),x=e(this).find(s.selector.input),w="ontouchstart"in t.documentElement,C=(h.selector||"",this),T=h.data("module-"+s.namespace),k=s.className,N=s.metadata,z=s.namespace,E=s.animation,P=s.errors;n={initialize:function(){n.verbose("Initializing dropdown with bound events",h),w?h.on("touchstart"+c,n.event.test.toggle):"click"==s.on?h.on("click"+c,n.event.test.toggle):"hover"==s.on?h.on("mouseenter"+c,n.show).on("mouseleave"+c,n.delayedHide):h.on(s.on+c,n.toggle),"form"==s.action&&n.set.selected(),b.on(n.get.selectEvent()+c,n.event.item.click),h.data(l,n)},destroy:function(){n.verbose("Destroying previous module for",h),h.off(z)},event:{stopPropagation:function(e){e.stopPropagation()},test:{toggle:function(e){n.intent.test(e,n.toggle),e.stopPropagation()},hide:function(e){n.intent.test(e,n.hide),e.stopPropagation()}},item:{click:function(t){var o=e(this),i=o.data(N.text)||o.text(),a=o.data(N.value)||i;n.verbose("Adding active state to selected item"),b.removeClass(k.active),o.addClass(k.active),n.action.determine(i,a),e.proxy(s.onChange,h.get())(a,i),t.stopPropagation()}}},intent:{test:function(t,o){n.debug("Determining whether event occurred in dropdown",t.target),o=o||function(){},0===e(t.target).closest(v).size()?(n.verbose("Triggering event",o),o()):n.verbose("Event occurred in dropdown, canceling callback")},bind:function(){n.verbose("Binding hide intent event to document"),r.on(n.get.selectEvent(),n.event.test.hide)},unbind:function(){n.verbose("Removing hide intent event from document"),r.off(n.get.selectEvent())}},action:{determine:function(t,o){e.isFunction(n.action[s.action])?(n.verbose("Triggering preset action",s.action),n.action[s.action](t,o)):e.isFunction(s.action)?(n.verbose("Triggering user action",s.action),s.action(t,o)):n.error(P.action)},none:function(){},hide:function(){n.hide()},changeText:function(e){n.set.text(e),n.hide()},form:function(e,t){n.set.text(e),n.set.value(t),n.hide()}},get:{selectEvent:function(){return w?"touchstart":"click"},text:function(){return y.text()},value:function(){return x.val()},item:function(n){var t;return n=n||x.val(),b.each(function(){e(this).data(N.value)==n&&(t=e(this))}),t||!1}},set:{text:function(e){n.debug("Changing text",e,y),y.removeClass(k.placeholder),y.text(e)},value:function(e){n.debug("Adding selected value to hidden input",e,x),x.val(e)},selected:function(e){var t,o=(e||x.val(),n.get.item(e));o&&(n.debug("Setting selected menu item to",o),t=o.data(N.text)||o.text(),b.removeClass(k.active),o.addClass(k.active),n.set.text(t))}},is:{visible:function(){return v.is(":visible")},hidden:function(){return v.is(":not(:visible)")}},can:{click:function(){return w||"click"==s.on},show:function(){return!h.hasClass(k.disabled)}},animate:{show:function(){n.verbose("Doing menu showing animation"),"none"==E.show?v.show():"fade"==E.show?v.fadeIn(150):"slide"==E.show?v.clearQueue().children().clearQueue().css("opacity",0).delay(50).animate({opacity:1},200,"easeOutQuad").end().slideDown(100,"easeOutQuad"):n.error(P.animation)},hide:function(){n.verbose("Doing menu hiding animation"),"none"==E.hide?v.hide():"fade"==E.hide?v.fadeOut(150):"slide"==E.hide?v.clearQueue().children().clearQueue().css("opacity",1).animate({opacity:0},100,"easeOutQuad").end().delay(50).slideUp(100,"easeOutQuad"):n.error(P.animation)}},show:function(){n.debug("Checking if dropdown can show"),clearTimeout(n.graceTimer),n.is.visible()||(n.hideOthers(),h.addClass(k.visible),n.animate.show(),n.can.click()&&n.intent.bind(),e.proxy(s.onShow,h.get())())},hide:function(){n.is.hidden()||(n.debug("Hiding dropdown"),h.removeClass(k.visible),n.can.click()&&n.intent.unbind(),n.animate.hide(),e.proxy(s.onHide,h.get())())},delayedHide:function(){n.verbose("User moused away setting timer to hide dropdown"),n.graceTimer=setTimeout(n.hide,s.gracePeriod)},hideOthers:function(){n.verbose("Finding other dropdowns to hide"),a.not(h).has(s.selector.menu+":visible").dropdown("hide")},toggle:function(){n.verbose("Toggling menu visibility"),n.is.hidden()?n.show():n.hide()},setting:function(t,i){return n.debug("Changing setting",t,i),i===o?s[t]:(e.isPlainObject(t)?e.extend(!0,s,t):s[t]=i,o)},internal:function(t,i){return n.debug("Changing internal",t,i),i===o?n[t]:(e.isPlainObject(t)?e.extend(!0,n,t):n[t]=i,o)},verbose:function(){s.verbose&&s.debug&&(n.performance.log(arguments[0]),n.verbose=Function.prototype.bind.call(console.info,console,s.moduleName+":"))},debug:function(){s.debug&&(n.performance.log(arguments[0]),n.debug=Function.prototype.bind.call(console.info,console,s.moduleName+":"))},error:function(){console.log!==o&&(n.error=Function.prototype.bind.call(console.error,console,s.moduleName+":"))},performance:{log:function(e){var t,o,i;s.performance&&(t=(new Date).getTime(),i=d||t,o=t-i,d=t,m.push({Element:C,Name:e[0],Arguments:e[1]||"","Execution Time":o})),clearTimeout(n.performance.timer),n.performance.timer=setTimeout(n.performance.display,100)},display:function(){var n=s.moduleName+":",t=0;d=!1,e.each(m,function(e,n){t+=n["Execution Time"]}),n+=" "+t+"ms",u&&(n+=" '"+u+"'"),(console.group!==o||console.table!==o)&&m.length>0&&(console.groupCollapsed(n),console.table?console.table(m):e.each(m,function(e,n){console.log(n.Name+": "+n["Execution Time"]+"ms")}),console.groupEnd()),m=[]}},invoke:function(t,i,a){var r,s;return i=i||p,a=C||a,"string"==typeof t&&T!==o&&(t=t.split("."),r=t.length-1,e.each(t,function(t,i){return e.isPlainObject(T[i])&&t!=r?(T=T[i],!0):T[i]!==o?(s=T[i],!0):(n.error(P.method),!1)})),e.isFunction(s)?(T.verbose("Executing invoked function",s),s.apply(a,i)):s||!1}},g?(T===o&&n.initialize(),i=n.invoke(f)):(T!==o&&n.destroy(),n.initialize())}),i?i:this},e.fn.dropdown.settings={moduleName:"Dropdown",namespace:"dropdown",verbose:!0,debug:!0,performance:!0,on:"click",gracePeriod:300,action:"hide",animation:{show:"slide",hide:"slide"},onChange:function(){},onShow:function(){},onHide:function(){},errors:{action:"You called a dropdown action that was not defined",method:"The method you called is not defined.",animation:"The requested animation was not found"},metadata:{text:"text",value:"value"},selector:{menu:".menu",item:".menu > .item",text:"> .text",input:'> input[type="hidden"]'},className:{active:"active",placeholder:"default",disabled:"disabled",visible:"visible"}}})(jQuery,window,document);
|