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.

12 lines
40 KiB

  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,r=e(this),s=e(n),l=r.selector||"",c="ontouchstart"in n.documentElement,u=(new Date).getTime(),d=[],v=arguments[0],m="string"==typeof v,f=[].slice.call(arguments,1);return r.each(function(h){var g,b,p,w,x,C,S,y=e.isPlainObject(a)?e.extend(!0,{},e.fn.dropdown.settings,a):e.extend({},e.fn.dropdown.settings),A=y.className,T=y.message,k=y.metadata,L=y.namespace,D=y.regExp,I=y.selector,R=y.error,q=y.templates,O="."+L,V="module-"+L,E=e(this),F=e(y.context),P=E.find(I.text),M=E.find(I.search),z=E.find(I.input),H=E.find(I.icon),j=E.prev().find(I.text).length>0?E.prev().find(I.text):E.prev(),U=E.children(I.menu),N=U.find(I.item),K=!1,W=!1,B=this,$=E.data(V);S={initialize:function(){S.debug("Initializing dropdown",y),S.is.alreadySetup()?S.setup.reference():(S.setup.layout(),S.refreshData(),S.save.defaults(),S.restore.selected(),S.create.id(),c&&S.bind.touchEvents(),S.bind.mouseEvents(),S.bind.keyboardEvents(),S.observeChanges(),S.instantiate())},instantiate:function(){S.verbose("Storing instance of dropdown",S),$=S,E.data(V,S)},destroy:function(){S.verbose("Destroying previous dropdown",E),S.remove.tabbable(),E.off(O).removeData(V),U.off(O),s.off(p),x&&x.disconnect(),C&&C.disconnect()},observeChanges:function(){"MutationObserver"in t&&(x=new MutationObserver(function(e){S.debug("<select> modified, recreating menu"),S.setup.select()}),C=new MutationObserver(function(e){S.debug("Menu modified, updating selector cache"),S.refresh()}),S.has.input()&&x.observe(z[0],{childList:!0,subtree:!0}),S.has.menu()&&C.observe(U[0],{childList:!0,subtree:!0}),S.debug("Setting up mutation observer",x,C))},create:{id:function(){w=(Math.random().toString(16)+"000000000").substr(2,8),p="."+w,S.verbose("Creating unique id for element",w)},userChoice:function(t){var n,a,o;return(t=t||S.get.userValues())?(t=e.isArray(t)?t:[t],e.each(t,function(t,r){S.get.item(r)===!1&&(o=y.templates.addition(r),a=e("<div />").html(o).data(k.value,r).addClass(A.addition).addClass(A.item),n=n===i?a:n.add(a),S.verbose("Creating user choices for value",r,a))}),n):!1},userLabels:function(t){var n=S.get.userValues();n&&(S.debug("Adding user labels",n),e.each(n,function(e,t){S.verbose("Adding custom user value"),S.add.label(t,t)}))}},search:function(e){e=e!==i?e:S.get.query(),S.verbose("Searching for query",e),S.filter(e)},select:{firstUnfiltered:function(){S.verbose("Selecting first non-filtered element"),S.remove.selectedItem(),N.not(I.unselectable).eq(0).addClass(A.selected)},nextAvailable:function(e){e=e.eq(0);var t=e.nextAll(I.item).not(I.unselectable).eq(0),n=e.prevAll(I.item).not(I.unselectable).eq(0),i=t.length>0;i?(S.verbose("Moving selection to",t),t.addClass(A.selected)):(S.verbose("Moving selection to",n),n.addClass(A.selected))}},setup:{api:function(){var e={debug:y.debug,on:!1};S.verbose("First request, initializing API"),E.api(e)},layout:function(){E.is("select")&&(S.setup.select(),S.setup.returnedObject(),console.log(E)),S.is.search()&&!S.has.search()&&(S.verbose("Adding search input"),M=e("<input />").addClass(A.search).insertBefore(P)),y.allowTab&&S.set.tabbable(),0===U.length&&(U=e("<div />").addClass(A.menu).appendTo(E))},select:function(){var t=S.get.selectValues();S.debug("Dropdown initialized on a select",t),E.is("select")&&(z=E),z.parent(I.dropdown).length>0?(S.debug("UI dropdown already exists. Creating dropdown menu only"),E=z.closest(I.dropdown),U=E.children(I.menu),S.setup.menu(t)):(S.debug("Creating entire dropdown from select"),E=e("<div />").attr("class",z.attr("class")).addClass(A.selection).addClass(A.dropdown).html(q.dropdown(t)).insertBefore(z),z.removeAttr("class").detach().prependTo(E),console.log(E)),z.is("[multiple]")&&S.set.multiple(),S.refresh()},menu:function(e){U.html(q.menu(e)),N=U.find(I.item)},reference:function(){S.debug("Dropdown behavior was called on select, replacing with closest dropdown"),E=E.parent(I.dropdown),S.refresh(),S.setup.returnedObject(),m&&($=S,S.invoke(v))},returnedObject:function(){var e=r.slice(0,h),t=r.slice(h+1);r=e.add(E).add(t)}},refresh:function
  12. },hidden:function(e){return!S.is.visible(e)},initialLoad:function(){return g},onScreen:function(e){var t,n=e||U,i=!0,a={};return n.addClass(A.loading),t={context:{scrollTop:F.scrollTop(),height:F.outerHeight()},menu:{offset:n.offset(),height:n.outerHeight()}},a={above:t.context.scrollTop<=t.menu.offset.top-t.menu.height,below:t.context.scrollTop+t.context.height>=t.menu.offset.top+t.menu.height},a.below?(S.verbose("Dropdown can fit in context downward",a),i=!0):a.below||a.above?(S.verbose("Dropdown cannot fit below, opening upward",a),i=!1):(S.verbose("Dropdown cannot fit in either direction, favoring downward",a),i=!0),n.removeClass(A.loading),i},inObject:function(t,n){var i=!1;return e.each(n,function(e,n){return n==t?(i=!0,!0):void 0}),i},multiple:function(){return E.hasClass(A.multiple)},single:function(){return!S.is.multiple()},selectMutation:function(t){var n=!1;return e.each(t,function(t,i){return i.target&&e(i.target).is("select")?(n=!0,!0):void 0}),n},search:function(){return E.hasClass(A.search)},searchSelection:function(){return S.has.search()&&0===M.closest(I.menu).length},selection:function(){return E.hasClass(A.selection)},userValue:function(t){return-1!==e.inArray(t,S.get.userValues())},upward:function(e){var t=e||E;return t.hasClass(A.upward)},visible:function(e){return e?e.hasClass(A.visible):U.hasClass(A.visible)}},can:{click:function(){return c||"click"==y.on},show:function(){return!E.hasClass(A.disabled)&&N.length>0},useAPI:function(){return e.fn.api!==i}},animate:{show:function(t,n){var a,o=n||U,r=n?function(){}:function(){S.hideSubMenus(),S.hideOthers(),S.set.active()};t=e.isFunction(t)?t:function(){},S.verbose("Doing menu show animation",o),S.set.direction(n),a=S.get.transition(n),S.is.selection()&&S.set.scrollPosition(S.get.selectedItem(),!0),(S.is.hidden(o)||S.is.animating(o))&&("none"==a?(r(),o.transition("show"),t.call(B)):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:r,onComplete:function(){t.call(B)}}):S.error(R.noTransition,a))},hide:function(t,n){var a=n||U,o=(n?.9*y.duration:y.duration,n?function(){}:function(){S.can.click()&&S.unbind.intent(),S.remove.active()}),r=S.get.transition(n);t=e.isFunction(t)?t:function(){},(S.is.visible(a)||S.is.animating(a))&&(S.verbose("Doing menu hide animation",a),"none"==r?(o(),a.transition("hide"),t.call(B)):e.fn.transition!==i&&E.transition("is supported")?a.transition({animation:r+" out",duration:y.duration,debug:y.debug,verbose:y.verbose,queue:!0,onStart:o,onComplete:function(){"auto"==y.direction&&S.remove.upward(n),t.call(B)}}):S.error(R.transition))}},hideAndClear:function(){S.has.search()?(S.remove.searchTerm(),S.hide(function(){S.remove.filteredItem()})):S.hide()},delay:{show:function(){S.verbose("Delaying show event to ensure user intent"),clearTimeout(S.timer),S.timer=setTimeout(S.show,y.delay.show)},hide:function(){S.verbose("Delaying hide event to ensure user intent"),clearTimeout(S.timer),S.timer=setTimeout(S.hide,y.delay.hide)}},escape:{regExp:function(e){return e=String(e),e.replace(D.escape,"\\$&")}},setting:function(t,n){if(S.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,S,t);else{if(n===i)return S[t];S[t]=n}},debug:function(){y.debug&&(y.performance?S.performance.log(arguments):(S.debug=Function.prototype.bind.call(console.info,console,y.name+":"),S.debug.apply(console,arguments)))},verbose:function(){y.verbose&&y.debug&&(y.performance?S.performance.log(arguments):(S.verbose=Function.prototype.bind.call(console.info,console,y.name+":"),S.verbose.apply(console,arguments)))},error:function(){S.error=Function.prototype.bind.call(console.error,console,y.name+":"),S.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:B,"Execution Time":n})),clearTimeout(S.performance.timer),S.performance.timer=setT