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

9 years ago
8 years ago
9 years ago
9 years ago
  1. /*!
  2. * # Semantic UI 2.2.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";t="undefined"!=typeof t&&t.Math==Math?t:"undefined"!=typeof self&&self.Math==Math?self:Function("return this")(),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(h){var g,p,b,w,x,y,C,S,A=e.isPlainObject(a)?e.extend(!0,{},e.fn.dropdown.settings,a):e.extend({},e.fn.dropdown.settings),T=A.className,k=A.message,L=A.fields,D=A.keys,I=A.metadata,q=A.namespace,R=A.regExp,V=A.selector,E=A.error,O=A.templates,F="."+q,M="module-"+q,P=e(this),H=e(A.context),j=P.find(V.text),z=P.find(V.search),U=P.find(V.input),N=P.find(V.icon),W=P.prev().find(V.text).length>0?P.prev().find(V.text):P.prev(),B=P.children(V.menu),K=B.find(V.item),Q=!1,$=!1,Y=!1,G=this,J=P.data(M);S={initialize:function(){S.debug("Initializing dropdown",A),S.is.alreadySetup()?S.setup.reference():(S.setup.layout(),S.refreshData(),S.save.defaults(),S.restore.selected(),S.create.id(),S.bind.events(),S.observeChanges(),S.instantiate())},instantiate:function(){S.verbose("Storing instance of dropdown",S),J=S,P.data(M,S)},destroy:function(){S.verbose("Destroying previous dropdown",P),S.remove.tabbable(),P.off(F).removeData(M),B.off(F),r.off(w),y&&y.disconnect(),C&&C.disconnect()},observeChanges:function(){"MutationObserver"in t&&(y=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()&&y.observe(U[0],{childList:!0,subtree:!0}),S.has.menu()&&C.observe(B[0],{childList:!0,subtree:!0}),S.debug("Setting up mutation observer",y,C))},create:{id:function(){x=(Math.random().toString(16)+"000000000").substr(2,8),w="."+x,S.verbose("Creating unique id for element",x)},userChoice:function(t){var n,a,o;return(t=t||S.get.userValues())?(t=e.isArray(t)?t:[t],e.each(t,function(t,s){S.get.item(s)===!1&&(o=A.templates.addition(S.add.variables(k.addResult,s)),a=e("<div />").html(o).attr("data-"+I.value,s).attr("data-"+I.text,s).addClass(T.addition).addClass(T.item),A.hideAdditions&&a.addClass(T.hidden),n=n===i?a:n.add(a),S.verbose("Creating user choices for value",s,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)}))},menu:function(){B=e("<div />").addClass(T.menu).appendTo(P)}},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(),K.not(V.unselectable).not(V.addition+V.hidden).eq(0).addClass(T.selected)},nextAvailable:function(e){e=e.eq(0);var t=e.nextAll(V.item).not(V.unselectable).eq(0),n=e.prevAll(V.item).not(V.unselectable).eq(0),i=t.length>0;i?(S.verbose("Moving selection to",t),t.addClass(T.selected)):(S.verbose("Moving selection to",n),n.addClass(T.selected))}},setup:{api:function(){var e={debug:A.debug,urlData:{value:S.get.value(),query:S.get.query()},on:!1};S.verbose("First request, initializing API"),P.api(e)},layout:function(){P.is("select")&&(S.setup.select(),S.setup.returnedObject()),S.has.menu()||S.create.menu(),S.is.search()&&!S.has.search()&&(S.verbose("Adding search input"),z=e("<input />").addClass(T.search).prop("autocomplete","off").insertBefore(j)),A.allowTab&&S.set.tabbable()},select:function(){var t=S.get.selectValues();S.debug("Dropdown initialized on a select",t),P.is("select")&&(U=P),U.parent(V.dropdown).length>0?(S.debug("UI dropdown already exists. Creating dropdown menu only"),P=U.closest(V.dropdown),S.has.menu()||S.create.menu(),B=P.children(V.menu),S.setup.menu(t)):(S.debug("Creating entire dropdown from select"),P=e("<div />").attr("class",U.attr("class")).addClass(T.selection).addClass(T.dropdown).html(O.dropdown(t)).insertBefore(U),U.hasClass(T.multiple)&&U.prop("multiple")===!1&&(S.error(E.missingMultiple),U.prop("multiple",!0)),U.is("[multi
  12. P.removeClass(T.empty)},loading:function(){P.removeClass(T.loading)},initialLoad:function(){g=!1},upward:function(e){var t=e||P;t.removeClass(T.upward)},visible:function(){P.removeClass(T.visible)},activeItem:function(){K.removeClass(T.active)},filteredItem:function(){A.useLabels&&S.has.maxSelections()||(A.useLabels&&S.is.multiple()?K.not("."+T.active).removeClass(T.filtered):K.removeClass(T.filtered),S.remove.empty())},optionValue:function(e){var t=S.escape.value(e),n=U.find('option[value="'+t+'"]'),i=n.length>0;i&&n.hasClass(T.addition)&&(y&&(y.disconnect(),S.verbose("Temporarily disconnecting mutation observer")),n.remove(),S.verbose("Removing user addition as an <option>",t),y&&y.observe(U[0],{childList:!0,subtree:!0}))},message:function(){B.children(V.message).remove()},searchWidth:function(){z.css("width","")},searchTerm:function(){S.verbose("Cleared search term"),z.val(""),S.set.filtered()},userAddition:function(){K.filter(V.addition).remove()},selected:function(t,n){return(n=A.allowAdditions?n||S.get.itemWithAdditions(t):n||S.get.item(t))?void n.each(function(){var t=e(this),n=S.get.choiceText(t),i=S.get.choiceValue(t,n);S.is.multiple()?A.useLabels?(S.remove.value(i,n,t),S.remove.label(i)):(S.remove.value(i,n,t),0===S.get.selectionCount()?S.set.placeholderText():S.set.text(S.add.variables(k.count))):S.remove.value(i,n,t),t.removeClass(T.filtered).removeClass(T.active),A.useLabels&&t.removeClass(T.selected)}):!1},selectedItem:function(){K.removeClass(T.selected)},value:function(e,t,n){var i,a=S.get.values();S.has.selectInput()?(S.verbose("Input is <select> removing selected option",e),i=S.remove.arrayValue(e,a),S.remove.optionValue(e)):(S.verbose("Removing from delimited values",e),i=S.remove.arrayValue(e,a),i=i.join(A.delimiter)),A.fireOnInit===!1&&S.is.initialLoad()?S.verbose("No callback on initial load",A.onRemove):A.onRemove.call(G,e,t,n),S.set.value(i,t,n),S.check.maxSelections()},arrayValue:function(t,n){return e.isArray(n)||(n=[n]),n=e.grep(n,function(e){return t!=e}),S.verbose("Removed value from delimited string",t,n),n},label:function(e,t){var n=P.find(V.label),i=n.filter('[data-value="'+e+'"]');S.verbose("Removing label",i),i.remove()},activeLabels:function(e){e=e||P.find(V.label).filter("."+T.active),S.verbose("Removing active label selections",e),S.remove.labels(e)},labels:function(t){t=t||P.find(V.label),S.verbose("Removing labels",t),t.each(function(){var t=e(this),n=t.data(I.value),a=n!==i?String(n):n,o=S.is.userValue(a);return A.onLabelRemove.call(t,n)===!1?void S.debug("Label remove callback cancelled removal"):(S.remove.message(),void(o?(S.remove.value(a),S.remove.label(a)):S.remove.selected(a)))})},tabbable:function(){S.has.search()?(S.debug("Searchable dropdown initialized"),z.removeAttr("tabindex"),B.removeAttr("tabindex")):(S.debug("Simple selection dropdown initialized"),P.removeAttr("tabindex"),B.removeAttr("tabindex"))}},has:{search:function(){return z.length>0},selectInput:function(){return U.is("select")},firstLetter:function(e,t){var n,i;return e&&0!==e.length&&"string"==typeof t?(n=S.get.choiceText(e,!1),t=t.toLowerCase(),i=String(n).charAt(0).toLowerCase(),t==i):!1},input:function(){return U.length>0},items:function(){return K.length>0},menu:function(){return B.length>0},message:function(){return 0!==B.children(V.message).length},label:function(e){var t=S.escape.value(e),n=P.find(V.label);return n.filter('[data-value="'+t+'"]').length>0},maxSelections:function(){return A.maxSelections&&S.get.selectionCount()>=A.maxSelections},allResultsFiltered:function(){var e=K.not(V.addition);return e.filter(V.unselectable).length===e.length},query:function(){return""!==S.get.query()},value:function(t){var n=S.get.values(),i=e.isArray(n)?n&&-1!==e.inArray(t,n):n==t;return i?!0:!1}},is:{active:function(){return P.hasClass(T.active)},alreadySetup:function(){return P.is("select")&&P.parent(V.dropdown).length>0&&0===P.prev().length},animating:function(e){return e?e.transition&&e.transition("is animating"):B.transition&&B.transition("is animating")},disabled:function(){return P.hasClass(T.disabled)},