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.

11 lines
38 KiB

9 years ago
9 years ago
9 years ago
9 years ago
  1. /*!
  2. * # Semantic UI 2.0.0 - Dropdown
  3. * http://github.com/semantic-org/semantic-ui/
  4. *
  5. *
  6. * Copyright 2015 Contributors
  7. * Released under the MIT license
  8. * http://opensource.org/licenses/MIT
  9. *
  10. */
  11. !function(e,t,n,i){"use strict";e.fn.dropdown=function(a){var o,s=e(this),r=e(n),l=s.selector||"",c="ontouchstart"in n.documentElement,u=(new Date).getTime(),d=[],v=arguments[0],m="string"==typeof v,f=[].slice.call(arguments,1);return s.each(function(){var h,g,b,p,w,C,x,y=e.isPlainObject(a)?e.extend(!0,{},e.fn.dropdown.settings,a):e.extend({},e.fn.dropdown.settings),S=y.className,A=y.message,T=y.metadata,k=y.namespace,L=y.regExp,D=y.selector,I=y.error,R=y.templates,q="."+k,V="module-"+k,E=e(this),O=E.find(D.text),F=E.find(D.search),P=E.find(D.input),M=E.find(D.icon),z=E.prev().find(D.text).length>0?E.prev().find(D.text):E.prev(),H=E.children(D.menu),j=H.find(D.item),U=!1,N=!1,K=this,W=E.data(V);x={initialize:function(){x.debug("Initializing dropdown",y),x.is.alreadySetup()?x.setup.reference():(x.setup.layout(),x.save.defaults(),x.restore.selected(),x.create.id(),c&&x.bind.touchEvents(),x.bind.mouseEvents(),x.bind.keyboardEvents(),x.observeChanges(),x.instantiate())},instantiate:function(){x.verbose("Storing instance of dropdown",x),W=x,E.data(V,x)},destroy:function(){x.verbose("Destroying previous dropdown for",E),x.remove.tabbable(),E.off(q).removeData(V),H.off(q),r.off(b),w&&w.disconnect(),C&&C.disconnect()},observeChanges:function(){"MutationObserver"in t&&(w=new MutationObserver(function(e){x.debug("<select> modified, recreating menu"),x.setup.select()}),C=new MutationObserver(function(e){x.debug("Menu modified, updating selector cache"),x.refresh()}),x.has.input()&&w.observe(P[0],{childList:!0,subtree:!0}),x.has.menu()&&C.observe(H[0],{childList:!0,subtree:!0}),x.debug("Setting up mutation observer",w,C))},create:{id:function(){p=(Math.random().toString(16)+"000000000").substr(2,8),b="."+p,x.verbose("Creating unique id for element",p)},userChoice:function(t){var n,a,o;return(t=t||x.get.userValues())?(t=e.isArray(t)?t:[t],e.each(t,function(t,s){x.get.item(s)===!1&&(o=y.templates.addition(s),a=e("<div />").html(o).data(T.value,s).addClass(S.addition).addClass(S.item),n=n===i?a:n.add(a),x.verbose("Creating user choices for value",s,a))}),n):!1},userLabels:function(t){var n=x.get.userValues();n&&(x.debug("Adding user labels",n),e.each(n,function(e,t){x.verbose("Adding custom user value"),x.add.label(t,t)}))}},search:function(e){e=e!==i?e:x.get.query(),x.verbose("Searching for query",e),x.filter(e)},select:{firstUnfiltered:function(){x.verbose("Selecting first non-filtered element"),x.remove.selectedItem(),j.not(D.unselectable).eq(0).addClass(S.selected)},nextAvailable:function(e){e=e.eq(0);var t=e.nextAll(D.item).not(D.unselectable).eq(0),n=e.prevAll(D.item).not(D.unselectable).eq(0),i=t.length>0;i?(x.verbose("Moving selection to",t),t.addClass(S.selected)):(x.verbose("Moving selection to",n),n.addClass(S.selected))}},setup:{api:function(){var e={debug:y.debug,on:!1};x.verbose("First request, initializing API"),E.api(e)},layout:function(){E.is("select")&&x.setup.select(),x.is.search()&&!x.has.search()&&(x.verbose("Adding search input"),F=e("<input />").addClass(S.search).insertBefore(O)),y.allowTab&&x.set.tabbable(),0===H.length&&(H=e("<div />").addClass(S.menu).appendTo(E))},select:function(){var t=x.get.selectValues();x.debug("Dropdown initialized on a select",t),E.is("select")&&(P=E),P.parent(D.dropdown).length>0?(x.debug("UI dropdown already exists. Creating dropdown menu only"),E=P.closest(D.dropdown),H=E.children(D.menu),x.setup.menu(t)):(x.debug("Creating entire dropdown from select"),E=e("<div />").attr("class",P.attr("class")).addClass(S.selection).addClass(S.dropdown).html(R.dropdown(t)).insertBefore(P),P.removeAttr("class").detach().prependTo(E)),P.is("[multiple]")&&x.set.multiple(),x.refresh()},menu:function(e){H.html(R.menu(e)),j=H.find(D.item)},reference:function(){var e,t,n=s.index(E);x.debug("Dropdown behavior was called on select, replacing with closest dropdown"),E=E.parent(D.dropdown),x.refresh(),e=s.slice(0,n),t=s.slice(n+1),s=e.add(E).add(t),m&&(W=x,x.invoke(v))}},refresh:function(){x.verbose("Refreshing selector cache"),O=E.find(D.text),F=E.find(D.search),P=E.find(D.input),M=E.find(D.icon),z=E.pre
  12. return E.hasClass(S.search)},searchSelection:function(){return x.has.search()&&0===F.closest(D.menu).length},selection:function(){return E.hasClass(S.selection)},userValue:function(t){return-1!==e.inArray(t,x.get.userValues())},upward:function(e){var t=e||E;return t.hasClass(S.upward)},visible:function(e){return e?e.hasClass(S.visible):H.hasClass(S.visible)}},can:{click:function(){return c||"click"==y.on},show:function(){return!E.hasClass(S.disabled)&&j.length>0},useAPI:function(){return e.fn.api!==i}},animate:{show:function(t,n){var a,o=n||H,s=n?function(){}:function(){x.hideSubMenus(),x.hideOthers(),x.set.active()};t=e.isFunction(t)?t:function(){},x.verbose("Doing menu show animation",o),x.set.direction(n),a=x.get.transition(n),x.is.selection()&&x.set.scrollPosition(x.get.selectedItem(),!0),(x.is.hidden(o)||x.is.animating(o))&&("none"==a?(s(),o.transition("show"),t.call(K)):e.fn.transition!==i&&E.transition("is supported")?o.transition({animation:a+" in",debug:y.debug,verbose:y.verbose,duration:y.duration,queue:!0,onStart:s,onComplete:function(){t.call(K)}}):x.error(I.noTransition,a))},hide:function(t,n){var a=n||H,o=(n?.9*y.duration:y.duration,n?function(){}:function(){x.can.click()&&x.unbind.intent(),x.remove.active()}),s=x.get.transition(n);t=e.isFunction(t)?t:function(){},(x.is.visible(a)||x.is.animating(a))&&(x.verbose("Doing menu hide animation",a),"none"==s?(o(),a.transition("hide"),t.call(K)):e.fn.transition!==i&&E.transition("is supported")?a.transition({animation:s+" out",duration:y.duration,debug:y.debug,verbose:y.verbose,queue:!0,onStart:o,onComplete:function(){"auto"==y.direction&&x.remove.upward(n),t.call(K)}}):x.error(I.transition))}},hideAndClear:function(){x.has.search()?(x.remove.searchTerm(),x.hide(function(){x.remove.filteredItem()})):x.hide()},delay:{show:function(){x.verbose("Delaying show event to ensure user intent"),clearTimeout(x.timer),x.timer=setTimeout(x.show,y.delay.show)},hide:function(){x.verbose("Delaying hide event to ensure user intent"),clearTimeout(x.timer),x.timer=setTimeout(x.hide,y.delay.hide)}},escape:{regExp:function(e){return e=String(e),e.replace(L.escape,"\\$&")}},setting:function(t,n){if(x.debug("Changing setting",t,n),e.isPlainObject(t))e.extend(!0,y,t);else{if(n===i)return y[t];y[t]=n}},internal:function(t,n){if(e.isPlainObject(t))e.extend(!0,x,t);else{if(n===i)return x[t];x[t]=n}},debug:function(){y.debug&&(y.performance?x.performance.log(arguments):(x.debug=Function.prototype.bind.call(console.info,console,y.name+":"),x.debug.apply(console,arguments)))},verbose:function(){y.verbose&&y.debug&&(y.performance?x.performance.log(arguments):(x.verbose=Function.prototype.bind.call(console.info,console,y.name+":"),x.verbose.apply(console,arguments)))},error:function(){x.error=Function.prototype.bind.call(console.error,console,y.name+":"),x.error.apply(console,arguments)},performance:{log:function(e){var t,n,i;y.performance&&(t=(new Date).getTime(),i=u||t,n=t-i,u=t,d.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:K,"Execution Time":n})),clearTimeout(x.performance.timer),x.performance.timer=setTimeout(x.performance.display,500)},display:function(){var t=y.name+":",n=0;u=!1,clearTimeout(x.performance.timer),e.each(d,function(e,t){n+=t["Execution Time"]}),t+=" "+n+"ms",l&&(t+=" '"+l+"'"),(console.group!==i||console.table!==i)&&d.length>0&&(console.groupCollapsed(t),console.table?console.table(d):e.each(d,function(e,t){console.log(t.Name+": "+t["Execution Time"]+"ms")}),console.groupEnd()),d=[]}},invoke:function(t,n,a){var s,r,l,c=W;return n=n||f,a=K||a,"string"==typeof t&&c!==i&&(t=t.split(/[\. ]/),s=t.length-1,e.each(t,function(n,a){var o=n!=s?a+t[n+1].charAt(0).toUpperCase()+t[n+1].slice(1):t;if(e.isPlainObject(c[o])&&n!=s)c=c[o];else{if(c[o]!==i)return r=c[o],!1;if(!e.isPlainObject(c[a])||n==s)return c[a]!==i?(r=c[a],!1):(x.error(I.method,t),!1);c=c[a]}})),e.isFunction(r)?l=r.apply(a,n):r!==i&&(l=r),e.isArray(o)?o.push(l):o!==i?o=[o,l]:l!==i&&(o=l),r}},m?(W===i&&x.initialize(),x.invoke(v)):(W!==i&&W.invoke("destroy"),x.initialize())}),o!==i?o:s},e.fn.dropdown.settings