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
39 KiB
12 lines
39 KiB
/*!
|
|
* # Semantic UI 2.0.0 - Dropdown
|
|
* http://github.com/semantic-org/semantic-ui/
|
|
*
|
|
*
|
|
* Copyright 2015 Contributors
|
|
* Released under the MIT license
|
|
* http://opensource.org/licenses/MIT
|
|
*
|
|
*/
|
|
!function(e,t,n,i){"use strict";e.fn.dropdown=function(a){var o,s=e(this),r=e(n),l=e(t),c=s.selector||"",u="ontouchstart"in n.documentElement,d=(new Date).getTime(),v=[],m=arguments[0],f="string"==typeof m,h=[].slice.call(arguments,1);return s.each(function(){var g,b,p,w,C,x,y,S=e.isPlainObject(a)?e.extend(!0,{},e.fn.dropdown.settings,a):e.extend({},e.fn.dropdown.settings),A=S.className,T=S.message,k=S.metadata,L=S.namespace,D=S.regExp,I=S.selector,R=S.error,q=S.templates,V="."+L,E="module-"+L,O=e(this),F=O.find(I.text),P=O.find(I.search),M=O.find(I.input),z=O.find(I.icon),H=O.prev().find(I.text).length>0?O.prev().find(I.text):O.prev(),j=O.children(I.menu),U=j.find(I.item),N=!1,K=!1,W=this,B=O.data(E);y={initialize:function(){y.debug("Initializing dropdown",S),y.is.alreadySetup()?y.setup.reference():(y.setup.layout(),y.refreshData(),y.save.defaults(),y.restore.selected(),y.create.id(),u&&y.bind.touchEvents(),y.bind.mouseEvents(),y.bind.keyboardEvents(),y.observeChanges(),y.instantiate())},instantiate:function(){y.verbose("Storing instance of dropdown",y),B=y,O.data(E,y)},destroy:function(){y.verbose("Destroying previous dropdown for",O),y.remove.tabbable(),O.off(V).removeData(E),j.off(V),r.off(p),C&&C.disconnect(),x&&x.disconnect()},observeChanges:function(){"MutationObserver"in t&&(C=new MutationObserver(function(){y.debug("<select> modified, recreating menu"),y.setup.select()}),x=new MutationObserver(function(){y.debug("Menu modified, updating selector cache"),y.refresh()}),y.has.input()&&C.observe(M[0],{childList:!0,subtree:!0}),y.has.menu()&&x.observe(j[0],{childList:!0,subtree:!0}),y.debug("Setting up mutation observer",C,x))},create:{id:function(){w=(Math.random().toString(16)+"000000000").substr(2,8),p="."+w,y.verbose("Creating unique id for element",w)},userChoice:function(t){var n,a,o;return(t=t||y.get.userValues())?(t=e.isArray(t)?t:[t],e.each(t,function(t,s){y.get.item(s)===!1&&(o=S.templates.addition(s),a=e("<div />").html(o).data(k.value,s).addClass(A.addition).addClass(A.item),n=n===i?a:n.add(a),y.verbose("Creating user choices for value",s,a))}),n):!1},userLabels:function(){var t=y.get.userValues();t&&(y.debug("Adding user labels",t),e.each(t,function(e,t){y.verbose("Adding custom user value"),y.add.label(t,t)}))}},search:function(e){e=e!==i?e:y.get.query(),y.verbose("Searching for query",e),y.filter(e)},select:{firstUnfiltered:function(){y.verbose("Selecting first non-filtered element"),y.remove.selectedItem(),U.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?(y.verbose("Moving selection to",t),t.addClass(A.selected)):(y.verbose("Moving selection to",n),n.addClass(A.selected))}},setup:{api:function(){var e={debug:S.debug,on:!1};y.verbose("First request, initializing API"),O.api(e)},layout:function(){O.is("select")&&y.setup.select(),y.is.search()&&!y.has.search()&&(y.verbose("Adding search input"),P=e("<input />").addClass(A.search).insertBefore(F)),S.allowTab&&y.set.tabbable(),0===j.length&&(j=e("<div />").addClass(A.menu).appendTo(O))},select:function(){var t=y.get.selectValues();y.debug("Dropdown initialized on a select",t),O.is("select")&&(M=O),M.parent(I.dropdown).length>0?(y.debug("UI dropdown already exists. Creating dropdown menu only"),O=M.closest(I.dropdown),j=O.children(I.menu),y.setup.menu(t)):(y.debug("Creating entire dropdown from select"),O=e("<div />").attr("class",M.attr("class")).addClass(A.selection).addClass(A.dropdown).html(q.dropdown(t)).insertBefore(M),M.removeAttr("class").detach().prependTo(O)),M.is("[multiple]")&&y.set.multiple(),y.refresh()},menu:function(e){j.html(q.menu(e)),U=j.find(I.item)},reference:function(){var e,t,n=s.index(O);y.debug("Dropdown behavior was called on select, replacing with closest dropdown"),O=O.parent(I.dropdown),y.refresh(),e=s.slice(0,n),t=s.slice(n+1),s=e.add(O).add(t),f&&(B=y,y.invoke(m))}},refresh:function(){y.refreshSelectors(),y.refreshData()},refreshSelectors:function(){y.verbose("Refreshing selector cache"),F=O.find(I.text),P=O.find(I.search),M=O.find(I.input),z=O.find(I.icon),H=O.prev().find(I.text).length>0?O.prev().find(I.text):O.prev(),j=O.children(I.menu),U=j.find(I.item)},refreshData:function(){y.verbose("Refreshing cached metadata"),U.removeData(k.text).removeData(k.value),O.removeData(k.defaultText).removeData(k.defaultValue).removeData(k.placeholderText)},toggle:function(){y.verbose("Toggling menu visibility"),y.is.active()?y.hide():y.show()},show:function(t){if(t=e.isFunction(t)?t:function(){},y.can.show()&&!y.is.active()){if(y.debug("Showing dropdown"),y.is.multiple()&&!y.has.search()&&y.is.allFiltered())return!0;y.animate.show(function(){y.can.click()&&y.bind.intent(),y.set.visible(),t.call(W)}),S.onShow.call(W)}},hide:function(t){t=e.isFunction(t)?t:function(){},y.is.active()&&(y.debug("Hiding dropdown"),y.animate.hide(function(){y.remove.visible(),t.call(W)}),S.onHide.call(W))},hideOthers:function(){y.verbose("Finding other dropdowns to hide"),s.not(O).has(I.menu+"."+A.visible).dropdown("hide")},hideMenu:function(){y.verbose("Hiding menu instantaneously"),y.remove.active(),y.remove.visible(),j.transition("hide")},hideSubMenus:function(){var e=j.children(I.item).find(I.menu);y.verbose("Hiding sub menus",e),e.transition("hide")},bind:{keyboardEvents:function(){y.debug("Binding keyboard events"),O.on("keydown"+V,y.event.keydown),y.has.search()&&O.on(y.get.inputEvent()+V,I.search,y.event.input),y.is.multiple()&&r.on("keydown"+p,y.event.document.keydown)},touchEvents:function(){y.debug("Touch device detected binding additional touch events"),y.is.searchSelection()||O.on("touchstart"+V,y.event.test.toggle),j.on("touchstart"+V,I.item,y.event.item.mouseenter)},mouseEvents:function(){y.debug("Mouse detected binding mouse events"),y.is.multiple()&&O.on("click"+V,I.label,y.event.label.click).on("click"+V,I.remove,y.event.remove.click),y.is.searchSelection()?(O.on("mousedown"+V,I.menu,y.event.menu.mousedown).on("mouseup"+V,I.menu,y.event.menu.mouseup).on("click"+V,I.search,y.show).on("focus"+V,I.search,y.event.search.focus).on("blur"+V,I.search,y.event.search.blur).on("click"+V,I.text,y.event.text.focus),y.is.multiple()&&O.on("click"+V,y.event.click)):("click"==S.on?O.on("click"+V,y.event.test.toggle):"hover"==S.on?O.on("mouseenter"+V,y.delay.show).on("mouseleave"+V,y.delay.hide):O.on(S.on+V,y.toggle),O.on("mousedown"+V,y.event.mousedown).on("mouseup"+V,y.event.mouseup).on("focus"+V,y.event.focus).on("blur"+V,y.event.blur)),j.on("mouseenter"+V,I.item,y.event.item.mouseenter).on("mouseleave"+V,I.item,y.event.item.mouseleave).on("click"+V,I.item,y.event.item.click)},intent:function(){y.verbose("Binding hide intent event to document"),u&&r.on("touchstart"+p,y.event.test.touch).on("touchmove"+p,y.event.test.touch),r.on("click"+p,y.event.test.hide)}},unbind:{intent:function(){y.verbose("Removing hide intent event from document"),u&&r.off("touchstart"+p).off("touchmove"+p),r.off("click"+p)}},filter:function(e){var t=e!==i?e:y.get.query(),n=function(){y.is.multiple()&&y.filterActive(),y.select.firstUnfiltered(),y.has.allResultsFiltered()?S.onNoResults.call(W,t)?S.allowAdditions||(y.verbose("All items filtered, showing message",t),y.add.message(T.noResults)):(y.verbose("All items filtered, hiding dropdown",t),y.hideMenu()):y.remove.message(),S.allowAdditions&&y.add.userSuggestion(e),y.is.searchSelection()&&y.can.show()&&y.is.focusedOnSearch()&&y.show()};y.has.maxSelections()||(S.apiSettings?y.can.useAPI()?y.queryRemote(t,function(){n()}):y.error(R.noAPI):(y.filterItems(t),n()))},queryRemote:function(t,n){var i={errorDuration:!1,throttle:S.throttle,cache:"local",urlData:{query:t},onError:function(){y.add.message(T.serverError),n()},onFailure:function(){y.add.message(T.serverError),n()},onSuccess:function(e){y.remove.message(),y.setup.menu({values:e.results}),n()}};O.api("get request")||y.setup.api(),i=e.extend(!0,{},i,S.apiSettings),O.api("setting",i).api("query")},filterItems:function(t){var n=t!==i?t:y.get.query(),a=e(),o=y.escape.regExp(n),s=new RegExp("^"+o,"igm");""===n?a=U:(y.verbose("Searching for matching values",n),U.each(function(){var t,i,o=e(this);if("both"==S.match||"text"==S.match){if(t=String(y.get.choiceText(o,!1)),-1!==t.search(s))return a=a.add(o),!0;if(S.fullTextSearch&&y.fuzzySearch(n,t))return a=a.add(o),!0}if("both"==S.match||"value"==S.match){if(i=String(y.get.choiceValue(o,t)),-1!==i.search(s))return a=a.add(o),!0;if(S.fullTextSearch&&y.fuzzySearch(n,i))return a=a.add(o),!0}})),y.debug("Showing only matched items",n),y.remove.filteredItem(),U.not(a).addClass(A.filtered)},fuzzySearch:function(e,t){var n=t.length,i=e.length;if(e=e.toLowerCase(),t=t.toLowerCase(),i>n)return!1;if(i===n)return e===t;e:for(var a=0,o=0;i>a;a++){for(var s=e.charCodeAt(a);n>o;)if(t.charCodeAt(o++)===s)continue e;return!1}return!0},filterActive:function(){S.useLabels&&U.filter("."+A.active).addClass(A.filtered)},focusSearch:function(){y.is.search()&&!y.is.focusedOnSearch()&&P[0].focus()},forceSelection:function(){var e=U.not(A.filtered).filter("."+A.selected).eq(0),t=U.not(A.filtered).filter("."+A.active).eq(0),n=e.length>0?e:t,i=n.size()>0;i?(y.debug("Forcing partial selection to selected item",n),y.event.item.click.call(n)):y.hide()},event:{focus:function(){S.showOnFocus&&!N&&y.is.hidden()&&!b&&y.show()},click:function(t){var n=e(t.target);!n.is(O)&&!n.is(z)||y.is.focusedOnSearch()||y.focusSearch()},blur:function(){b=n.activeElement===this,N||b||(y.remove.activeLabel(),y.hide())},mousedown:function(){N=!0},mouseup:function(){N=!1},search:{focus:function(){N=!0,y.is.multiple()&&y.remove.activeLabel(),S.showOnFocus&&y.show()},blur:function(){b=n.activeElement===this,K||b||(y.is.multiple()?(y.remove.activeLabel(),y.hide()):S.forceSelection?y.forceSelection():y.hide())}},text:{focus:function(){N=!0,y.focusSearch()}},input:function(){(y.is.multiple()||y.is.searchSelection())&&y.set.filtered(),clearTimeout(y.timer),y.timer=setTimeout(y.search,S.delay.search)},label:{click:function(t){var n=e(this),i=O.find(I.label),a=i.filter("."+A.active),o=n.nextAll("."+A.active),s=n.prevAll("."+A.active),r=o.length>0?n.nextUntil(o).add(a).add(n):n.prevUntil(s).add(a).add(n);t.shiftKey?(a.removeClass(A.active),r.addClass(A.active)):t.ctrlKey?n.toggleClass(A.active):(a.removeClass(A.active),n.addClass(A.active)),S.onLabelSelect.apply(this,i.filter("."+A.active))}},remove:{click:function(){var t=e(this).parent();t.hasClass(A.active)?y.remove.activeLabels():y.remove.activeLabels(t)}},test:{toggle:function(e){var t=y.is.multiple()?y.show:y.toggle;y.determine.eventOnElement(e,t)&&e.preventDefault()},touch:function(e){y.determine.eventOnElement(e,function(){"touchstart"==e.type?y.timer=setTimeout(y.hide,S.delay.touch):"touchmove"==e.type&&clearTimeout(y.timer)}),e.stopPropagation()},hide:function(e){y.determine.eventInModule(e,y.hide)}},menu:{mousedown:function(){K=!0},mouseup:function(){K=!1}},item:{mouseenter:function(t){var n=e(this).children(I.menu),i=e(this).siblings(I.item).children(I.menu);n.length>0&&(clearTimeout(y.itemTimer),y.itemTimer=setTimeout(function(){y.verbose("Showing sub-menu",n),e.each(i,function(){y.animate.hide(!1,e(this))}),y.animate.show(!1,n)},S.delay.show),t.preventDefault())},mouseleave:function(){var t=e(this).children(I.menu);t.length>0&&(clearTimeout(y.itemTimer),y.itemTimer=setTimeout(function(){y.verbose("Hiding sub-menu",t),y.animate.hide(!1,t)},S.delay.hide))},click:function(t){var n=e(this),i=e(t?t.target:""),a=n.find(I.menu),o=y.get.choiceText(n),s=y.get.choiceValue(n,o),r=a.length>0,l=a.find(i).length>0;l||r&&!S.allowCategorySelection||(S.useLabels||y.remove.searchTerm(),y.determine.selectAction.call(this,o,s))}},document:{keydown:function(e){var t=e.which,n=y.get.shortcutKeys(),i=y.is.inObject(t,n);if(i){var a=O.find(I.label),o=a.filter("."+A.active),s=(o.data("value"),a.index(o)),r=a.length,l=o.length>0,c=o.length>1,u=0===s,d=s+1==r,v=y.is.searchSelection(),m=y.is.focusedOnSearch(),f=y.is.focused(),h=m&&0===y.get.caretPosition();if(v&&!l&&!m)return;t==n.leftArrow?!f&&!h||l?l&&(e.shiftKey?y.verbose("Adding previous label to selection"):(y.verbose("Selecting previous label"),a.removeClass(A.active)),u&&!c?o.addClass(A.active):o.prev(I.siblingLabel).addClass(A.active).end(),e.preventDefault()):(y.verbose("Selecting previous label"),a.last().addClass(A.active)):t==n.rightArrow?(f&&!l&&a.first().addClass(A.active),l&&(e.shiftKey?y.verbose("Adding next label to selection"):(y.verbose("Selecting next label"),a.removeClass(A.active)),d?v?m?a.removeClass(A.active):y.focusSearch():c?o.next(I.siblingLabel).addClass(A.active):o.addClass(A.active):o.next(I.siblingLabel).addClass(A.active),e.preventDefault())):t==n.deleteKey||t==n.backspace?l?(y.verbose("Removing active labels"),d&&v&&!m&&y.focusSearch(),o.last().next(I.siblingLabel).addClass(A.active),y.remove.activeLabels(o),e.preventDefault()):h&&!l&&t==n.backspace&&(y.verbose("Removing last label on input backspace"),o=a.last().addClass(A.active),y.remove.activeLabels(o)):o.removeClass(A.active)}}},keydown:function(e){var t=e.which,n=y.get.shortcutKeys(),i=y.is.inObject(t,n);if(i){var a,o,s=U.not(I.unselectable).filter("."+A.selected).eq(0),r=j.children("."+A.active).eq(0),l=s.length>0?s:r,c=l.length>0?l.siblings(":not(."+A.filtered+")").andSelf():j.children(":not(."+A.filtered+")"),u=l.children(I.menu),d=l.closest(I.menu),v=d.hasClass(A.visible)||d.hasClass(A.animating)||d.parent(I.menu).length>0,m=u.length>0,f=l.length>0,h=l.not(I.unselectable).length>0;if(y.is.visible()){if((t==n.enter||t==n.delimiter)&&(t==n.enter&&f&&m&&!S.allowCategorySelection?(y.verbose("Pressed enter on unselectable category, opening sub menu"),t=n.rightArrow):h&&(y.verbose("Selecting item from keyboard shortcut",l),y.event.item.click.call(l,e),S.useLabels&&y.is.searchSelection()?y.hideAndClear():y.remove.searchTerm()),e.preventDefault()),t==n.leftArrow&&(o=d[0]!==j[0],o&&(y.verbose("Left key pressed, closing sub-menu"),y.animate.hide(!1,d),l.removeClass(A.selected),d.closest(I.item).addClass(A.selected),e.preventDefault())),t==n.rightArrow&&m&&(y.verbose("Right key pressed, opening sub-menu"),y.animate.show(!1,u),l.removeClass(A.selected),u.find(I.item).eq(0).addClass(A.selected),e.preventDefault()),t==n.upArrow){if(a=f&&v?l.prevAll(I.item+":not("+I.unselectable+")").eq(0):U.eq(0),c.index(a)<0)return y.verbose("Up key pressed but reached top of current menu"),void e.preventDefault();y.verbose("Up key pressed, changing active item"),l.removeClass(A.selected),a.addClass(A.selected),y.set.scrollPosition(a),e.preventDefault()}if(t==n.downArrow){if(a=f&&v?a=l.nextAll(I.item+":not("+I.unselectable+")").eq(0):U.eq(0),0===a.length)return y.verbose("Down key pressed but reached bottom of current menu"),void e.preventDefault();y.verbose("Down key pressed, changing active item"),U.removeClass(A.selected),a.addClass(A.selected),y.set.scrollPosition(a),e.preventDefault()}t==n.pageUp&&(y.scrollPage("up"),e.preventDefault()),t==n.pageDown&&(y.scrollPage("down"),e.preventDefault()),t==n.escape&&(y.verbose("Escape key pressed, closing dropdown"),y.hide())}else t==n.delimiter&&e.preventDefault(),t==n.downArrow&&(y.verbose("Down key pressed, showing dropdown"),y.show(),e.preventDefault())}else y.is.selection()&&!y.is.search()&&y.set.selectedLetter(String.fromCharCode(t))}},determine:{selectAction:function(t,n){y.verbose("Determining action",S.action),e.isFunction(y.action[S.action])?(y.verbose("Triggering preset action",S.action,t,n),y.action[S.action].call(this,t,n)):e.isFunction(S.action)?(y.verbose("Triggering user action",S.action,t,n),S.action.call(this,t,n)):y.error(R.action,S.action)},eventInModule:function(t,n){return n=e.isFunction(n)?n:function(){},0===e(t.target).closest(O).length?(y.verbose("Triggering event",n),n(),!0):(y.verbose("Event occurred in dropdown, canceling callback"),!1)},eventOnElement:function(t,n){var i=e(t.target);return n=e.isFunction(n)?n:function(){},0===i.closest(j).length?(y.verbose("Triggering event",n),n(),!0):(y.verbose("Event occurred in dropdown menu, canceling callback"),!1)}},action:{nothing:function(){},activate:function(t,n){n=n!==i?n:t,y.set.selected(n,e(this)),(!y.is.multiple()||y.is.allFiltered())&&y.hideAndClear()},select:function(){y.action.activate.call(this)},combo:function(t,n){n=n!==i?n:t,y.set.selected(n,e(this)),y.hideAndClear()},hide:function(){y.hideAndClear()}},get:{id:function(){return w},text:function(){return F.text()},query:function(){return e.trim(P.val())},searchWidth:function(e){return e*S.glyphWidth+"em"},selectionCount:function(){var t=y.get.values();return y.is.multiple()?e.isArray(t)?t.length:0:""!==y.get.value()?1:0},transition:function(e){return"auto"==S.transition?y.is.upward(e)?"slide up":"slide down":S.transition},userValues:function(){var t=y.get.values();return t?(t=e.isArray(t)?t:[t],e.grep(t,function(e){return y.get.item(e)===!1})):!1},uniqueArray:function(t){return e.grep(t,function(n,i){return e.inArray(n,t)===i})},caretPosition:function(){var e,t,i=P.get(0);return"selectionStart"in i?i.selectionStart:n.selection?(i.focus(),e=n.selection.createRange(),t=e.text.length,e.moveStart("character",-i.value.length),e.text.length-t):void 0},shortcutKeys:function(){return{backspace:8,delimiter:188,deleteKey:46,enter:13,escape:27,pageUp:33,pageDown:34,leftArrow:37,upArrow:38,rightArrow:39,downArrow:40}},value:function(){return M.length>0?M.val():O.data(k.value)},values:function(){var e=y.get.value();return""===e?"":!M.is("select")&&y.is.multiple()?"string"==typeof e?e.split(S.delimiter):"":e},remoteValues:function(){var t=y.get.values(),n=!1;return t&&("string"==typeof t&&(t=[t]),n={},e.each(t,function(e,t){var i=y.read.remoteData(t);y.verbose("Restoring value from session data",i,t),n[t]=i?i:t})),n},choiceText:function(e,t){return t=t!==i?t:S.preserveHTML,e?(e.find(I.menu).length>0&&(y.verbose("Retreiving text of element with sub-menu"),e=e.clone(),e.find(I.menu).remove(),e.find(I.menuIcon).remove()),e.data(k.text)!==i?e.data(k.text):t?e.html().trim():e.text().trim()):void 0},choiceValue:function(e,t){return t=t||y.get.choiceText(e),e?e.data(k.value)!==i?e.data(k.value):"string"==typeof t?t.toLowerCase().trim():t:!1},inputEvent:function(){var e=P[0];return e?e.oninput!==i?"input":e.onpropertychange!==i?"propertychange":"keyup":!1},selectValues:function(){var t={};return t.values=[],O.find("option").each(function(){var n=e(this),a=n.html(),o=n.attr("disabled"),s=n.attr("value")!==i?n.attr("value"):a;"auto"===S.placeholder&&""===s?t.placeholder=a:t.values.push({name:a,value:s,disabled:o})}),S.placeholder&&"auto"!==S.placeholder&&(y.debug("Setting placeholder value to",S.placeholder),t.placeholder=S.placeholder),S.sortSelect?(t.values.sort(function(e,t){return e.name>t.name?1:-1}),y.debug("Retrieved and sorted values from select",t)):y.debug("Retreived values from select",t),t},activeItem:function(){return U.filter("."+A.active)},selectedItem:function(){var e=U.not(I.unselectable).filter("."+A.selected);return e.length>0?e:U.eq(0)},itemWithAdditions:function(e){var t=y.get.item(e),n=y.create.userChoice(e),i=n&&n.length>0;return i&&(t=t.length>0?t.add(n):n),t},item:function(t,n){var a,o,s=!1;return t=t!==i?t:y.get.values()!==i?y.get.values():y.get.text(),a=o?t.length>0:t!==i&&""!==t&&null!==t,o=y.is.multiple()&&e.isArray(t),n=""===t||0===t?!0:n||!1,a&&U.each(function(){var a=e(this),r=y.get.choiceText(a),l=y.get.choiceValue(a,r);if(null!==l&&l!==i)if(o)(-1!==e.inArray(l.toString(),t)||-1!==e.inArray(r,t))&&(s=s?s.add(a):a);else if(n){if(y.verbose("Ambiguous dropdown value using strict type check",a,t),l===t||r===t)return s=a,!0}else if(l.toString()==t.toString()||r==t)return y.verbose("Found select item by value",l,t),s=a,!0}),s}},check:{maxSelections:function(e){return S.maxSelections?(e=e!==i?e:y.get.selectionCount(),e>=S.maxSelections?(y.debug("Maximum selection count reached"),U.addClass(A.filtered),y.add.message(T.maxSelections),!0):(y.verbose("No longer at maximum selection count"),y.remove.message(),y.remove.filteredItem(),y.is.searchSelection()&&y.filterItems(),!1)):!0}},restore:{defaults:function(){y.restore.defaultText(),y.restore.defaultValue()},defaultText:function(){var e=O.data(k.defaultText);y.debug("Restoring default text",e),y.set.text(e),F.addClass(A.placeholder)},defaultValue:function(){var e=O.data(k.defaultValue);e!==i&&(y.debug("Restoring default value",e),""!==e?(y.set.value(e),y.set.selected()):(y.remove.activeItem(),y.remove.selectedItem()))},labels:function(){S.allowAdditions&&(S.useLabels||(y.error(R.labels),S.useLabels=!0),y.debug("Restoring selected values"),y.create.userLabels()),y.check.maxSelections()},selected:function(){y.restore.values(),y.is.multiple()?(y.debug("Restoring previously selected values and labels"),y.restore.labels()):y.debug("Restoring previously selected values")},values:function(){y.set.initialLoad(),S.apiSettings?S.saveRemoteData?y.restore.remoteValues():y.clearValue():y.set.selected(),y.remove.initialLoad()},remoteValues:function(){var t=y.get.remoteValues();y.debug("Recreating selected from session data",t),t&&(y.is.single()?e.each(t,function(e,t){y.set.text(t)}):e.each(t,function(e,t){y.add.label(e,t)}))}},read:{remoteData:function(e){var n;return t.Storage===i?void y.error(R.noStorage):(n=sessionStorage.getItem(e),n!==i?n:!1)}},save:{defaults:function(){y.save.defaultText(),y.save.placeholderText(),y.save.defaultValue()},defaultValue:function(){O.data(k.defaultValue,y.get.value())},defaultText:function(){O.data(k.defaultText,F.text())},placeholderText:function(){F.hasClass(A.placeholder)&&O.data(k.placeholderText,F.text())},remoteData:function(e,n){return t.Storage===i?void y.error(R.noStorage):void sessionStorage.setItem(n,e)}},clear:function(){y.is.multiple()?y.remove.labels():(y.remove.activeItem(),y.remove.selectedItem()),y.set.placeholderText(),y.clearValue()},clearValue:function(){y.set.value("")},scrollPage:function(e,t){var n,i,a,t=t||y.get.selectedItem(),o=t.closest(I.menu),s=o.outerHeight(),r=o.scrollTop(),l=U.eq(0).outerHeight(),c=Math.floor(s/l),u=(o.prop("scrollHeight"),"up"==e?r-l*c:r+l*c),d=U.not(I.unselectable);a="up"==e?d.index(t)-c:d.index(t)+c,n="up"==e?a>=0:a<d.length,i=n?d.eq(a):"up"==e?d.first():d.last(),i.length>0&&(y.debug("Scrolling page",e,i),t.removeClass(A.selected),i.addClass(A.selected),o.scrollTop(u))},set:{filtered:function(){var e=y.is.multiple(),t=y.is.searchSelection(),n=e&&t,i=t?y.get.query():"",a="string"==typeof i&&i.length>0,o=y.get.searchWidth(i.length),s=""!==i;e&&a&&(y.verbose("Adjusting input width",o,S.glyphWidth),P.css("width",o)),a||n&&s?(y.verbose("Hiding placeholder text"),F.addClass(A.filtered)):(!e||n&&!s)&&(y.verbose("Showing placeholder text"),F.removeClass(A.filtered))},loading:function(){O.addClass(A.loading)},placeholderText:function(e){y.debug("Restoring placeholder text"),e=e||O.data(k.placeholderText),y.set.text(e),F.addClass(A.placeholder)},tabbable:function(){y.has.search()?(y.debug("Added tabindex to searchable dropdown"),P.val("").attr("tabindex",0),j.attr("tabindex",-1)):(y.debug("Added tabindex to dropdown"),O.attr("tabindex")||(O.attr("tabindex",0),j.attr("tabindex",-1)))},initialLoad:function(){y.verbose("Setting initial load"),g=!0},scrollPosition:function(e,t){var n,a,o,s,r,l,c,u,d,v=5;e=e||y.get.selectedItem(),n=e.closest(I.menu),a=e&&e.length>0,t=t!==i?t:!1,e&&n.length>0&&a&&(s=e.position().top,n.addClass(A.loading),l=n.scrollTop(),r=n.offset().top,s=e.offset().top,o=l-r+s,t||(c=n.height(),d=o+v>l+c,u=l>o-v),y.debug("Scrolling to active item",o),(t||u||d)&&n.scrollTop(o),n.removeClass(A.loading))},text:function(e){"select"!==S.action&&("combo"==S.action?(y.debug("Changing combo button text",e,H),S.preserveHTML?H.html(e):H.text(e)):(y.debug("Changing text",e,F),F.removeClass(A.filtered).removeClass(A.placeholder),S.preserveHTML?F.html(e):F.text(e)))},selectedLetter:function(t){var n=U.filter("."+A.selected),i=!1;U.each(function(){var n=e(this),a=y.get.choiceText(n,!1),o=String(a).charAt(0).toLowerCase(),s=t.toLowerCase();return o==s?(i=n,!1):void 0}),i&&(y.verbose("Scrolling to next value with letter",t),y.set.scrollPosition(i),n.removeClass(A.selected),i.addClass(A.selected))},direction:function(e){"auto"==S.direction?y.is.onScreen(e)?y.remove.upward(e):y.set.upward(e):"upward"==S.direction&&y.set.upward(e)},upward:function(e){var t=e||O;t.addClass(A.upward)},value:function(e,t,n){var i=M.length>0,a=(!y.has.value(e),y.get.values());if(i){if(e==a&&(y.verbose("Skipping value update already same value",e,a),!y.is.initialLoad()))return;y.debug("Updating input value",e,a),M.val(e).trigger("change")}else y.verbose("Storing value in metadata",e,M),e!==a&&O.data(k.value,e);S.fireOnInit===!1&&y.is.initialLoad()?y.verbose("No callback on initial load",S.onChange):S.onChange.call(W,e,t,n)},active:function(){O.addClass(A.active)},multiple:function(){O.addClass(A.multiple)},visible:function(){O.addClass(A.visible)},selected:function(t,n){var i=y.is.multiple();return(n=S.allowAdditions?n||y.get.itemWithAdditions(t):n||y.get.item(t))?(y.debug("Setting selected menu item to",n),y.is.single()?(y.remove.activeItem(),y.remove.selectedItem()):S.useLabels&&y.remove.selectedItem(),void n.each(function(){var t=e(this),a=y.get.choiceText(t),o=y.get.choiceValue(t,a),s=t.hasClass(A.filtered),r=t.hasClass(A.active),l=t.hasClass(A.addition),c=i&&1==n.length;i?!r||l?(S.apiSettings&&S.saveRemoteData&&y.save.remoteData(a,o),S.useLabels?(y.add.value(o,a,t),y.add.label(o,a,c),t.addClass(A.active),y.filterActive(),y.select.nextAvailable(n)):(y.add.value(o,a,t),y.set.text(y.add.variables(T.count)),t.addClass(A.active))):s||(y.debug("Selected active value, removing label"),y.remove.selected(o)):(S.apiSettings&&S.saveRemoteData&&y.save.remoteData(a,o),y.set.value(o,a,t),y.set.text(a),t.addClass(A.active).addClass(A.selected))})):!1}},add:{label:function(t,n,i){var a,o=y.is.searchSelection()?P:F;return a=e("<a />").addClass(A.label).attr("data-value",t).html(q.label(t,n)),a=S.onLabelCreate.call(a,t,n),y.has.label(t)?void y.debug("Label already exists, skipping",t):(S.label.variation&&a.addClass(S.label.variation),void(i===!0?(y.debug("Animating in label",a),a.addClass(A.hidden).insertBefore(o).transition(S.label.transition,S.label.duration)):(y.debug("Adding selection label",a),a.insertBefore(o))))},message:function(t){var n=j.children(I.message),i=S.templates.message(y.add.variables(t));n.length>0?n.html(i):n=e("<div/>").html(i).addClass(A.message).appendTo(j)},optionValue:function(t){var n=M.find('option[value="'+t+'"]'),i=n.length>0;i||(C&&(C.disconnect(),y.verbose("Temporarily disconnecting mutation observer",t)),e("<option/>").prop("value",t).html(t).appendTo(M),y.verbose("Adding user addition as an <option>",t),C&&C.observe(M[0],{childList:!0,subtree:!0}))},userSuggestion:function(e){var t,n=j.children(I.addition),i=y.get.item(e),a=n.length>0;if(!y.has.maxSelections()){if(""===e||i)return void n.remove();U.removeClass(A.selected),a?(t=S.templates.addition(e),n.html(t).data(k.value,e).removeClass(A.filtered).addClass(A.selected),y.verbose("Replacing user suggestion with new value",n)):(n=y.create.userChoice(e),n.prependTo(j).addClass(A.selected),y.verbose("Adding item choice to menu corresponding with user choice addition",n))}},variables:function(e){var t,n,i=-1!==e.search("{count}"),a=-1!==e.search("{maxCount}"),o=-1!==e.search("{term}");return y.verbose("Adding templated variables to message",e),i&&(t=y.get.selectionCount(),e=e.replace("{count}",t)),a&&(t=y.get.selectionCount(),e=e.replace("{maxCount}",S.maxSelections)),o&&(n=y.get.query(),e=e.replace("{term}",n)),e},value:function(t,n,i){var a,o=y.get.values();return""===t?void y.debug("Cannot select blank values from multiselect"):(e.isArray(o)?(a=o.concat([t]),a=y.get.uniqueArray(a)):a=[t],M.is("select")?S.allowAdditions&&(y.add.optionValue(t),y.debug("Adding value to select",t,a,M)):(a=a.join(S.delimiter),y.debug("Setting hidden input to delimited value",a,M)),S.fireOnInit===!1&&y.is.initialLoad()?y.verbose("No callback on initial load",S.onAdd):S.onAdd.call(W,t,n,i),y.set.value(a,t,n,i),void y.check.maxSelections())}},remove:{active:function(){O.removeClass(A.active)},activeLabel:function(){O.find(I.label).removeClass(A.active)},loading:function(){O.removeClass(A.loading)},initialLoad:function(){g=!1},upward:function(e){var t=e||O;t.removeClass(A.upward)},visible:function(){O.removeClass(A.visible)},activeItem:function(){U.removeClass(A.active)},filteredItem:function(){y.has.maxSelections()||(S.useLabels?U.not("."+A.active).removeClass(A.filtered):U.removeClass(A.filtered))},message:function(){j.children(I.message).remove()},searchTerm:function(){y.verbose("Cleared search term"),P.val(""),y.set.filtered()},selected:function(t,n){return(n=S.allowAdditions?n||y.get.itemWithAdditions(t):n||y.get.item(t))?void n.each(function(){var t=e(this),n=y.get.choiceText(t),i=y.get.choiceValue(t,n);y.is.multiple()?S.useLabels?(y.remove.value(i,n,t),y.remove.label(i)):(y.remove.value(i,n,t),y.set.text(y.add.variables(T.count))):y.remove.value(i,n,t),t.removeClass(A.filtered).removeClass(A.active),S.useLabels&&t.removeClass(A.selected)}):!1},selectedItem:function(){U.removeClass(A.selected)},value:function(e,t,n){var i,a=M.val();M.is("select")?(y.verbose("Input is <select> removing selected option",e),i=y.remove.arrayValue(e,a)):(y.verbose("Removing from delimited values",e),a=a.split(S.delimiter),i=y.remove.arrayValue(e,a),i=i.join(S.delimiter)),S.fireOnInit===!1&&y.is.initialLoad()?y.verbose("No callback on initial load",S.onRemove):S.onRemove.call(W,e,t,n),y.set.value(i,t,n),y.check.maxSelections()},arrayValue:function(t,n){return n=e.grep(n,function(e){return t!=e}),y.verbose("Removed value from delimited string",t,n),n},label:function(e){var t=O.find(I.label),n=t.filter('[data-value="'+e+'"]'),i=t.length,a=t.index(n)+1==i,o=(!y.is.searchSelection()||!y.is.focusedOnSearch())&&a;o?(y.verbose("Animating and removing label",n),n.transition(S.label.transition,S.label.duration,function(){n.remove()})):(y.verbose("Removing label",n),n.remove())},activeLabels:function(e){e=e||O.find(I.label).filter("."+A.active),y.verbose("Removing active label selections",e),y.remove.labels(e)},labels:function(t){t=t||O.find(I.label),y.verbose("Removing labels",t),t.each(function(){var t=e(this).data("value"),n=y.is.userValue(t);n?(y.remove.value(t),y.remove.label(t)):y.remove.selected(t)})},tabbable:function(){y.has.search()?(y.debug("Searchable dropdown initialized"),P.attr("tabindex","-1"),j.attr("tabindex","-1")):(y.debug("Simple selection dropdown initialized"),O.attr("tabindex","-1"),j.attr("tabindex","-1"))}},has:{search:function(){return P.length>0},input:function(){return M.length>0},menu:function(){return j.length>0},message:function(){return 0!==j.children(I.message).length},label:function(e){var t=O.find(I.label);return t.filter('[data-value="'+e+'"]').length>0},maxSelections:function(){return S.maxSelections&&y.get.selectionCount()>=S.maxSelections},allResultsFiltered:function(){return U.filter(I.unselectable).length===U.length},value:function(t){var n=y.get.values(),i=e.isArray(n)?n&&-1!==e.inArray(t,n):n==t;return i?!0:!1}},is:{active:function(){return O.hasClass(A.active)},alreadySetup:function(){return O.is("select")&&O.parent(I.dropdown).length>0&&0===O.prev().length},animating:function(e){return e?e.transition&&e.transition("is animating"):j.transition&&j.transition("is animating")},focused:function(){return n.activeElement===O[0]},focusedOnSearch:function(){return n.activeElement===P[0]},allFiltered:function(){return(y.is.multiple()||y.has.search())&&!y.has.message()&&y.has.allResultsFiltered()},hidden:function(e){return!y.is.visible(e)},initialLoad:function(){return g},onScreen:function(t){var n,a=t||j;return a.addClass(A.loading),n=e.fn.visibility!==i?a.visibility("bottom visible"):l.scrollTop()+l.height()>=a.offset().top+a.height(),y.debug("Checking if menu can fit on screen",n,j),a.removeClass(A.loading),n
|
|
},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 O.hasClass(A.multiple)},single:function(){return!y.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 O.hasClass(A.search)},searchSelection:function(){return y.has.search()&&0===P.closest(I.menu).length},selection:function(){return O.hasClass(A.selection)},userValue:function(t){return-1!==e.inArray(t,y.get.userValues())},upward:function(e){var t=e||O;return t.hasClass(A.upward)},visible:function(e){return e?e.hasClass(A.visible):j.hasClass(A.visible)}},can:{click:function(){return u||"click"==S.on},show:function(){return!O.hasClass(A.disabled)&&U.length>0},useAPI:function(){return e.fn.api!==i}},animate:{show:function(t,n){var a,o=n||j,s=n?function(){}:function(){y.hideSubMenus(),y.hideOthers(),y.set.active()};t=e.isFunction(t)?t:function(){},y.verbose("Doing menu show animation",o),y.set.direction(n),a=y.get.transition(n),y.is.selection()&&y.set.scrollPosition(y.get.selectedItem(),!0),(y.is.hidden(o)||y.is.animating(o))&&("none"==a?(s(),o.transition("show"),t.call(W)):e.fn.transition!==i&&O.transition("is supported")?o.transition({animation:a+" in",debug:S.debug,verbose:S.verbose,duration:S.duration,queue:!0,onStart:s,onComplete:function(){t.call(W)}}):y.error(R.noTransition,a))},hide:function(t,n){var a=n||j,o=(n?.9*S.duration:S.duration,n?function(){}:function(){y.can.click()&&y.unbind.intent(),y.remove.active()}),s=y.get.transition(n);t=e.isFunction(t)?t:function(){},(y.is.visible(a)||y.is.animating(a))&&(y.verbose("Doing menu hide animation",a),"none"==s?(o(),a.transition("hide"),t.call(W)):e.fn.transition!==i&&O.transition("is supported")?a.transition({animation:s+" out",duration:S.duration,debug:S.debug,verbose:S.verbose,queue:!0,onStart:o,onComplete:function(){"auto"==S.direction&&y.remove.upward(n),t.call(W)}}):y.error(R.transition))}},hideAndClear:function(){y.has.search()?(y.remove.searchTerm(),y.hide(function(){y.remove.filteredItem()})):y.hide()},delay:{show:function(){y.verbose("Delaying show event to ensure user intent"),clearTimeout(y.timer),y.timer=setTimeout(y.show,S.delay.show)},hide:function(){y.verbose("Delaying hide event to ensure user intent"),clearTimeout(y.timer),y.timer=setTimeout(y.hide,S.delay.hide)}},escape:{regExp:function(e){return e=String(e),e.replace(D.escape,"\\$&")}},setting:function(t,n){if(y.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,y,t);else{if(n===i)return y[t];y[t]=n}},debug:function(){S.debug&&(S.performance?y.performance.log(arguments):(y.debug=Function.prototype.bind.call(console.info,console,S.name+":"),y.debug.apply(console,arguments)))},verbose:function(){S.verbose&&S.debug&&(S.performance?y.performance.log(arguments):(y.verbose=Function.prototype.bind.call(console.info,console,S.name+":"),y.verbose.apply(console,arguments)))},error:function(){y.error=Function.prototype.bind.call(console.error,console,S.name+":"),y.error.apply(console,arguments)},performance:{log:function(e){var t,n,i;S.performance&&(t=(new Date).getTime(),i=d||t,n=t-i,d=t,v.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:W,"Execution Time":n})),clearTimeout(y.performance.timer),y.performance.timer=setTimeout(y.performance.display,500)},display:function(){var t=S.name+":",n=0;d=!1,clearTimeout(y.performance.timer),e.each(v,function(e,t){n+=t["Execution Time"]}),t+=" "+n+"ms",c&&(t+=" '"+c+"'"),(console.group!==i||console.table!==i)&&v.length>0&&(console.groupCollapsed(t),console.table?console.table(v):e.each(v,function(e,t){console.log(t.Name+": "+t["Execution Time"]+"ms")}),console.groupEnd()),v=[]}},invoke:function(t,n,a){var s,r,l,c=B;return n=n||h,a=W||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):(y.error(R.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}},f?(B===i&&y.initialize(),y.invoke(m)):(B!==i&&B.invoke("destroy"),y.initialize())}),o!==i?o:s},e.fn.dropdown.settings={debug:!1,verbose:!1,performance:!0,on:"click",action:"activate",apiSettings:!1,saveRemoteData:!0,throttle:200,direction:"auto",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,fireOnInit:!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(){},onAdd:function(){},onRemove:function(){},onLabelSelect:function(){},onLabelCreate:function(){return e(this)},onNoResults:function(){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 session storage",noTransition:"This module requires ui transitions <https://github.com/Semantic-Org/UI-Transition>"},regExp:{escape:/[-[\]{}()*+?.,\\^$|#\s]/g},metadata:{defaultText:"defaultText",defaultValue:"defaultValue",placeholderText:"placeholder",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(.icon)",unselectable:".disabled, .filtered"},className:{active:"active",addition:"addition",animating:"animating",disabled:"disabled",dropdown:"ui dropdown",filtered:"filtered",hidden:"hidden transition",item:"item",label:"ui label",loading:"loading",menu:"menu",message:"message",multiple:"multiple",placeholder:"default",search:"search",selected:"selected",selection:"selection",upward:"upward",visible:"visible"}},e.fn.dropdown.settings.templates={dropdown:function(t){var n=t.placeholder||!1,i=(t.values||{},"");return i+='<i class="dropdown icon"></i>',i+=t.placeholder?'<div class="default text">'+n+"</div>":'<div class="text"></div>',i+='<div class="menu">',e.each(t.values,function(e,t){i+=t.disabled?'<div class="disabled item" data-value="'+t.value+'">'+t.name+"</div>":'<div class="item" data-value="'+t.value+'">'+t.name+"</div>"}),i+="</div>"},menu:function(t){var n=(t.values||{},"");return e.each(t.values,function(e,t){n+='<div class="item" data-value="'+t.value+'">'+t.name+"</div>"}),n},label:function(e,t){return t+'<i class="delete icon"></i>'},message:function(e){return e},addition:function(e){return e}}}(jQuery,window,document);
|