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
39 KiB

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