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