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
36 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,p,b,w,C=e.isPlainObject(a)?e.extend(!0,{},e.fn.dropdown.settings,a):e.extend({},e.fn.dropdown.settings),x=C.className,S=C.message,y=C.metadata,T=C.namespace,A=C.regExp,k=C.selector,D=C.error,L=C.templates,I="."+T,q="module-"+T,E=e(this),V=E.find(k.text),R=E.find(k.search),O=E.find(k.input),F=E.find(k.icon),P=E.prev().find(k.text).length>0?E.prev().find(k.text):E.prev(),M=E.children(k.menu),z=M.find(k.item),H=!1,j=!1,U=this,K=E.data(q);w={initialize:function(){w.debug("Initializing dropdown",C),w.is.alreadySetup()?w.setup.reference():(w.setup.layout(),w.save.defaults(),C.apiSettings?C.saveRemoteData?w.restore.remoteValues():w.clearValue():w.set.selected(),w.is.multiple()&&(C.allowAdditions&&(C.useLabels||(w.error(D.labels),C.useLabels=!0),w.create.userLabels()),w.check.maxSelections()),w.create.id(),c&&w.bind.touchEvents(),w.bind.mouseEvents(),w.bind.keyboardEvents(),w.observeChanges(),w.instantiate())},instantiate:function(){w.verbose("Storing instance of dropdown",w),K=w,E.data(q,w)},destroy:function(){w.verbose("Destroying previous dropdown for",E),w.remove.tabbable(),E.off(I).removeData(q),M.off(I),r.off(h),p&&p.disconnect(),b&&b.disconnect()},observeChanges:function(){"MutationObserver"in t&&(p=new MutationObserver(function(e){w.debug("<select> modified, recreating menu"),w.setup.select()}),b=new MutationObserver(function(e){w.debug("Menu modified, updating selector cache"),w.refresh()}),w.has.input()&&p.observe(O[0],{childList:!0,subtree:!0}),w.has.menu()&&b.observe(M[0],{childList:!0,subtree:!0}),w.debug("Setting up mutation observer",p,b))},create:{id:function(){g=(Math.random().toString(16)+"000000000").substr(2,8),h="."+g,w.verbose("Creating unique id for element",g)},userLabels:function(){var t=w.get.userValues();t&&(w.debug("Adding user labels",t),e.each(t,function(e,t){w.verbose("Adding custom user value"),w.add.label(t,t)}))}},search:function(e){e=e!==i?e:w.get.query(),w.verbose("Searching for query",e),w.filter(e)},select:{firstUnfiltered:function(){w.verbose("Selecting first non-filtered element"),w.remove.selectedItem(),z.not(k.unselectable).eq(0).addClass(x.selected)},nextAvailable:function(e){e=e.eq(0);var t=e.nextAll(k.item).not(k.unselectable).eq(0),n=e.prevAll(k.item).not(k.unselectable).eq(0),i=t.length>0;i?(w.verbose("Moving selection to",t),t.addClass(x.selected)):(w.verbose("Moving selection to",n),n.addClass(x.selected))}},setup:{api:function(){var e={debug:C.debug,cache:"local",on:!1};w.verbose("First request, initializing API"),E.api(e)},layout:function(){E.is("select")&&w.setup.select(),w.is.search()&&!w.has.search()&&(w.verbose("Adding search input"),R=e("<input />").addClass(x.search).insertBefore(V)),C.allowTab&&w.set.tabbable(),0===M.length&&(M=e("<div />").addClass(x.menu).appendTo(E))},select:function(){var t=w.get.selectValues();w.debug("Dropdown initialized on a select",t),E.is("select")&&(O=E),O.parent(k.dropdown).length>0?(w.debug("UI dropdown already exists. Creating dropdown menu only"),E=O.closest(k.dropdown),M=E.children(k.menu),w.setup.menu(t)):(w.debug("Creating entire dropdown from select"),E=e("<div />").attr("class",O.attr("class")).addClass(x.selection).addClass(x.dropdown).html(L.dropdown(t)).insertBefore(O),O.removeAttr("class").detach().prependTo(E)),O.is("[multiple]")&&w.set.multiple(),w.refresh()},menu:function(e){M.html(L.menu(e)),z=M.find(k.item)},reference:function(){var e,t,n=s.index(E);w.debug("Dropdown behavior was called on select, replacing with closest dropdown"),E=E.parent(k.dropdown),w.refresh(),e=s.slice(0,n),t=s.slice(n+1),s=e.add(E).add(t),m&&(K=w,w.invoke(v))}},refresh:function(){w.verbose("Refreshing selector cache"),V=E.find(k.text),R=E.find(k.search),O=E.find(k.input),F=E.find(k.icon),P=E.prev().find(k.text).length>0?E.prev().find(k.text):E.prev(),M=E.children(k.menu),z=M.find(k.item)},toggle:
  12. clearTimeout(w.timer),w.timer=setTimeout(w.show,C.delay.show)},hide:function(){w.verbose("Delaying hide event to ensure user intent"),clearTimeout(w.timer),w.timer=setTimeout(w.hide,C.delay.hide)}},escape:{regExp:function(e){return e=String(e),e.replace(A.escape,"\\$&")}},setting:function(t,n){if(w.debug("Changing setting",t,n),e.isPlainObject(t))e.extend(!0,C,t);else{if(n===i)return C[t];C[t]=n}},internal:function(t,n){if(e.isPlainObject(t))e.extend(!0,w,t);else{if(n===i)return w[t];w[t]=n}},debug:function(){C.debug&&(C.performance?w.performance.log(arguments):(w.debug=Function.prototype.bind.call(console.info,console,C.name+":"),w.debug.apply(console,arguments)))},verbose:function(){C.verbose&&C.debug&&(C.performance?w.performance.log(arguments):(w.verbose=Function.prototype.bind.call(console.info,console,C.name+":"),w.verbose.apply(console,arguments)))},error:function(){w.error=Function.prototype.bind.call(console.error,console,C.name+":"),w.error.apply(console,arguments)},performance:{log:function(e){var t,n,i;C.performance&&(t=(new Date).getTime(),i=u||t,n=t-i,u=t,d.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:U,"Execution Time":n})),clearTimeout(w.performance.timer),w.performance.timer=setTimeout(w.performance.display,500)},display:function(){var t=C.name+":",n=0;u=!1,clearTimeout(w.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=K;return n=n||f,a=U||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):(w.error(D.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?(K===i&&w.initialize(),w.invoke(v)):(K!==i&&K.invoke("destroy"),w.initialize())}),o!==i?o:s},e.fn.dropdown.settings={debug:!1,verbose:!1,performance:!0,on:"click",action:"activate",apiSettings:!1,saveRemoteData:!1,keepOnScreen:!0,match:"both",fullTextSearch:!1,placeholder:"auto",preserveHTML:!0,sortSelect:!1,forceSelection:!0,allowAdditions:!1,maxSelections:!1,useLabels:!0,delimiter:",",showOnFocus:!0,allowTab:!0,allowCategorySelection:!1,transition:"auto",duration:200,glyphWidth:1.0714,label:{transition:"scale",duration:200,variation:!1},delay:{hide:300,show:200,search:20,touch:50},onChange:function(e,t,n){},onLabelSelect:function(e){},onLabelCreate:function(t,n){return e(this)},onNoResults:function(e){return!0},onShow:function(){},onHide:function(){},name:"Dropdown",namespace:"dropdown",message:{addResult:"Add <b>{term}</b>",count:"{count} selected",maxSelections:"Max {maxCount} selections",noResults:"No results found.",serverError:"There was an error contacting the server"},error:{action:"You called a dropdown action that was not defined",alreadySetup:"Once a select has been initialized behaviors must be called on the created ui dropdown",labels:"Allowing user additions currently requires the use of labels.",method:"The method you called is not defined.",noAPI:"The API module is required to load resources remotely",noStorage:"Saving remote data requires local storage",noTransition:"This module requires ui transitions <https://github.com/Semantic-Org/UI-Transition>"},regExp:{escape:/[-[\]{}()*+?.,\\^$|#\s]/g},metadata:{defaultText:"defaultText",defaultValue:"defaultValue",placeholderText:"placeholderText",text:"text",value:"value"},selector:{addition:".addition",dropdown:".ui.dropdown",icon:"> .dropdown.icon",input:'> input[type="hidden"], > select',item:".item",label:"> .label",remove:"> .label > .delete.icon",siblingLabel:".label",menu:".menu",message:".message",menuIcon:".dropdown.icon",search:"input.search, .menu > .search > input",text:"> .text:not(