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
36 KiB
12 lines
36 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=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:function(){w.verbose("Toggling menu visibility"),w.is.active()?w.hide():w.show()},show:function(t){if(t=e.isFunction(t)?t:function(){},w.can.show()&&!w.is.active()){if(w.debug("Showing dropdown"),w.is.multiple()&&!w.has.search()&&w.is.allFiltered())return!0;w.animate.show(function(){w.can.click()&&w.bind.intent(),w.set.visible(),t.call(U)}),C.onShow.call(U)}},hide:function(t){t=e.isFunction(t)?t:function(){},w.is.active()&&(w.debug("Hiding dropdown"),w.animate.hide(function(){w.remove.visible(),t.call(U)}),C.onHide.call(U))},hideOthers:function(){w.verbose("Finding other dropdowns to hide"),s.not(E).has(k.menu+"."+x.visible).dropdown("hide")},hideMenu:function(){w.verbose("Hiding menu instantaneously"),w.remove.active(),w.remove.visible(),M.transition("hide")},hideSubMenus:function(){var e=M.children(k.item).find(k.menu);w.verbose("Hiding sub menus",e),e.transition("hide")},bind:{keyboardEvents:function(){w.debug("Binding keyboard events"),E.on("keydown"+I,w.event.keydown),w.has.search()&&E.on(w.get.inputEvent()+I,k.search,w.event.input),w.is.multiple()&&r.on("keydown"+h,w.event.document.keydown)},touchEvents:function(){w.debug("Touch device detected binding additional touch events"),w.is.searchSelection()||E.on("touchstart"+I,w.event.test.toggle),M.on("touchstart"+I,k.item,w.event.item.mouseenter)},mouseEvents:function(){w.debug("Mouse detected binding mouse events"),w.is.multiple()&&E.on("click"+I,k.label,w.event.label.click).on("click"+I,k.remove,w.event.remove.click),w.is.searchSelection()?(E.on("mousedown"+I,k.menu,w.event.menu.mousedown).on("mouseup"+I,k.menu,w.event.menu.mouseup).on("click"+I,k.search,w.show).on("focus"+I,k.search,w.event.search.focus).on("blur"+I,k.search,w.event.search.blur).on("click"+I,k.text,w.event.text.focus),w.is.multiple()&&E.on("click"+I,w.event.click)):("click"==C.on?E.on("click"+I,w.event.test.toggle):"hover"==C.on?E.on("mouseenter"+I,w.delay.show).on("mouseleave"+I,w.delay.hide):E.on(C.on+I,w.toggle),E.on("mousedown"+I,w.event.mousedown).on("mouseup"+I,w.event.mouseup).on("focus"+I,w.event.focus).on("blur"+I,w.event.blur)),M.on("mouseenter"+I,k.item,w.event.item.mouseenter).on("mouseleave"+I,k.item,w.event.item.mouseleave).on("click"+I,k.item,w.event.item.click)},intent:function(){w.verbose("Binding hide intent event to document"),c&&r.on("touchstart"+h,w.event.test.touch).on("touchmove"+h,w.event.test.touch),r.on("click"+h,w.event.test.hide)}},unbind:{intent:function(){w.verbose("Removing hide intent event from document"),c&&r.off("touchstart"+h).off("touchmove"+h),r.off("click"+h)}},filter:function(e){var t=e!==i?e:w.get.query(),n=function(){w.is.multiple()&&w.filterActive(),w.select.firstUnfiltered(),w.has.allResultsFiltered()?C.onNoResults.call(U,t)?C.allowAdditions||(w.verbose("All items filtered, showing message",t),w.add.message(S.noResults)):(w.verbose("All items filtered, hiding dropdown",t),w.hideMenu()):w.remove.message(),C.allowAdditions&&w.add.userChoice(e),w.is.searchSelection()&&w.can.show()&&w.is.focusedOnSearch()&&w.show()};w.has.maxSelections()||(C.apiSettings?w.can.useAPI()?w.queryRemote(t,function(){n()}):w.error(D.noAPI):(w.filterItems(t),n()))},queryRemote:function(t,n){var i={errorDuration:!1,urlData:{query:t},onError:function(){w.add.message(S.serverError),n()},onFailure:function(){w.add.message(S.serverError),n()},onSuccess:function(e){w.remove.message(),w.setup.menu({values:e.results}),n()}};E.api("get request")||w.setup.api(),i=e.extend(!0,{},i,C.apiSettings),E.api("setting",i).api("abort").api("query")},filterItems:function(t){var n=t!==i?t:w.get.query(),a=e(),o=w.escape.regExp(n),s=new RegExp("^"+o,"igm");""===n?a=z:(w.verbose("Searching for matching values",n),z.each(function(){var t,i,o=e(this);if("both"==C.match||"text"==C.match){if(t=String(w.get.choiceText(o,!1)),-1!==t.search(s))return a=a.add(o),!0;if(C.fullTextSearch&&w.fuzzySearch(n,t))return a=a.add(o),!0}if("both"==C.match||"value"==C.match){if(i=String(w.get.choiceValue(o,t)),-1!==i.search(s))return a=a.add(o),!0;if(C.fullTextSearch&&w.fuzzySearch(n,i))return a=a.add(o),!0}})),w.debug("Showing only matched items",n),w.remove.filteredItem(),z.not(a).addClass(x.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(){C.useLabels&&z.filter("."+x.active).addClass(x.filtered)},focusSearch:function(){w.is.search()&&!w.is.focusedOnSearch()&&R[0].focus()},forceSelection:function(){var e=z.not(x.filtered).filter("."+x.selected).eq(0),t=z.not(x.filtered).filter("."+x.active).eq(0),n=e.length>0?e:t,i=n.size()>0;i?(w.debug("Forcing partial selection to selected item",n),w.event.item.click.call(n)):w.hide()},event:{focus:function(){C.showOnFocus&&!H&&w.is.hidden()&&w.show()},click:function(t){var n=e(t.target);!n.is(E)&&!n.is(F)||w.is.focusedOnSearch()||R.focus()},blur:function(e){var t=n.activeElement===this;H||t||(w.remove.activeLabel(),w.hide())},mousedown:function(){H=!0},mouseup:function(){H=!1},search:{focus:function(){H=!0,w.is.multiple()&&w.remove.activeLabel(),C.showOnFocus&&w.show()},blur:function(e){var t=n.activeElement===this;j||t||(w.is.multiple()?(w.remove.activeLabel(),w.hide()):C.forceSelection?w.forceSelection():w.hide())}},text:{focus:function(e){H=!0,R.focus()}},input:function(e){(w.is.multiple()||w.is.searchSelection())&&w.set.filtered(),clearTimeout(w.timer),w.timer=setTimeout(w.search,C.delay.search)},label:{click:function(t){var n=e(this),i=E.find(k.label),a=i.filter("."+x.active),o=n.nextAll("."+x.active),s=n.prevAll("."+x.active),r=o.length>0?n.nextUntil(o).add(a).add(n):n.prevUntil(s).add(a).add(n);t.shiftKey?(a.removeClass(x.active),r.addClass(x.active)):t.ctrlKey?n.toggleClass(x.active):(a.removeClass(x.active),n.addClass(x.active)),C.onLabelSelect.apply(this,i.filter("."+x.active))}},remove:{click:function(){var t=e(this).parent();t.hasClass(x.active)?w.remove.labels():w.remove.labels(t)}},test:{toggle:function(e){var t=w.is.multiple()?w.show:w.toggle;w.determine.eventOnElement(e,t)&&e.preventDefault()},touch:function(e){w.determine.eventOnElement(e,function(){"touchstart"==e.type?w.timer=setTimeout(w.hide,C.delay.touch):"touchmove"==e.type&&clearTimeout(w.timer)}),e.stopPropagation()},hide:function(e){w.determine.eventInModule(e,w.hide)}},menu:{mousedown:function(){j=!0},mouseup:function(){j=!1}},item:{mouseenter:function(t){var n=e(this).children(k.menu),i=e(this).siblings(k.item).children(k.menu);n.length>0&&(clearTimeout(w.itemTimer),w.itemTimer=setTimeout(function(){w.verbose("Showing sub-menu",n),e.each(i,function(){w.animate.hide(!1,e(this))}),w.animate.show(!1,n)},C.delay.show),t.preventDefault())},mouseleave:function(t){var n=e(this).children(k.menu);n.length>0&&(clearTimeout(w.itemTimer),w.itemTimer=setTimeout(function(){w.verbose("Hiding sub-menu",n),w.animate.hide(!1,n)},C.delay.hide))},click:function(t){var n=e(this),i=e(t?t.target:""),a=n.find(k.menu),o=w.get.choiceText(n),s=w.get.choiceValue(n,o),r=a.length>0,l=a.find(i).length>0;l||r&&!C.allowCategorySelection||w.determine.selectAction.call(this,o,s)}},document:{keydown:function(e){var t=e.which,n=w.get.shortcutKeys(),i=w.is.inObject(t,n);if(i){var a=E.find(k.label),o=a.filter("."+x.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=w.is.searchSelection(),m=w.is.focusedOnSearch(),f=w.is.focused(),h=m&&0===w.get.caretPosition();if(v&&!l&&!m)return;t==n.leftArrow?!f&&!h||l?l&&(e.shiftKey?w.verbose("Adding previous label to selection"):(w.verbose("Selecting previous label"),a.removeClass(x.active)),u&&!c?o.addClass(x.active):o.prev(k.siblingLabel).addClass(x.active).end(),e.preventDefault()):(w.verbose("Selecting previous label"),a.last().addClass(x.active)):t==n.rightArrow?(f&&!l&&a.first().addClass(x.active),l&&(e.shiftKey?w.verbose("Adding next label to selection"):(w.verbose("Selecting next label"),a.removeClass(x.active)),d?v?m?a.removeClass(x.active):w.focusSearch():c?o.next(k.siblingLabel).addClass(x.active):o.addClass(x.active):o.next(k.siblingLabel).addClass(x.active),e.preventDefault())):t==n.deleteKey||t==n.backspace?l?(w.verbose("Removing active labels"),d&&v&&!m&&w.focusSearch(),o.last().next(k.siblingLabel).addClass(x.active),w.remove.labels(o),e.preventDefault()):h&&!l&&t==n.backspace&&(w.verbose("Removing last label on input backspace"),o=a.last().addClass(x.active),w.remove.labels(o)):o.removeClass(x.active)}}},keydown:function(e){var t=e.which,n=w.get.shortcutKeys(),i=w.is.inObject(t,n);if(i){var a,o,s=z.not(k.unselectable).filter("."+x.selected).eq(0),r=M.children("."+x.active).eq(0),l=s.length>0?s:r,c=l.length>0?l.siblings(":not(."+x.filtered+")").andSelf():M.children(":not(."+x.filtered+")"),u=l.children(k.menu),d=l.closest(k.menu),v=d.hasClass(x.visible)||d.hasClass(x.animating)||d.parent(k.menu).length>0,m=u.length>0,f=l.length>0,h=l.not(k.unselectable).length>0;if(w.is.visible()){if((t==n.enter||t==n.delimiter)&&(t==n.enter&&f&&m&&!C.allowCategorySelection?(w.verbose("Pressed enter on unselectable category, opening sub menu"),t=n.rightArrow):h&&(w.verbose("Selecting item from keyboard shortcut",l),w.event.item.click.call(l,e),C.useLabels&&w.is.searchSelection()?w.hideAndClear():w.remove.searchTerm()),e.preventDefault()),t==n.leftArrow&&(o=d[0]!==M[0],o&&(w.verbose("Left key pressed, closing sub-menu"),w.animate.hide(!1,d),l.removeClass(x.selected),d.closest(k.item).addClass(x.selected),e.preventDefault())),t==n.rightArrow&&m&&(w.verbose("Right key pressed, opening sub-menu"),w.animate.show(!1,u),l.removeClass(x.selected),u.find(k.item).eq(0).addClass(x.selected),e.preventDefault()),t==n.upArrow){if(a=f&&v?l.prevAll(k.item+":not("+k.unselectable+")").eq(0):z.eq(0),c.index(a)<0)return w.verbose("Up key pressed but reached top of current menu"),void e.preventDefault();w.verbose("Up key pressed, changing active item"),l.removeClass(x.selected),a.addClass(x.selected),w.set.scrollPosition(a),e.preventDefault()}if(t==n.downArrow){if(a=f&&v?a=l.nextAll(k.item+":not("+k.unselectable+")").eq(0):z.eq(0),0===a.length)return w.verbose("Down key pressed but reached bottom of current menu"),void e.preventDefault();w.verbose("Down key pressed, changing active item"),z.removeClass(x.selected),a.addClass(x.selected),w.set.scrollPosition(a),e.preventDefault()}t==n.pageUp&&(w.scrollPage("up"),e.preventDefault()),t==n.pageDown&&(w.scrollPage("down"),e.preventDefault()),t==n.escape&&(w.verbose("Escape key pressed, closing dropdown"),w.hide())}else t==n.delimiter&&e.preventDefault(),t==n.downArrow&&(w.verbose("Down key pressed, showing dropdown"),w.show(),e.preventDefault())}else w.is.selection()&&!w.is.search()&&w.set.selectedLetter(String.fromCharCode(t))}},determine:{selectAction:function(t,n){w.verbose("Determining action",C.action),e.isFunction(w.action[C.action])?(w.verbose("Triggering preset action",C.action,t,n),w.action[C.action].call(this,t,n)):e.isFunction(C.action)?(w.verbose("Triggering user action",C.action,t,n),C.action.call(this,t,n)):w.error(D.action,C.action)},eventInModule:function(t,n){return n=e.isFunction(n)?n:function(){},0===e(t.target).closest(E).length?(w.verbose("Triggering event",n),n(),!0):(w.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(M).length?(w.verbose("Triggering event",n),n(),!0):(w.verbose("Event occurred in dropdown menu, canceling callback"),!1)}},action:{nothing:function(){},activate:function(t,n){n=n!==i?n:t,w.set.selected(n,e(this)),(!w.is.multiple()||w.is.allFiltered())&&w.hideAndClear()},select:function(e,t){w.action.activate.call(this)},combo:function(t,n){n=n!==i?n:t,w.set.selected(n,e(this)),w.hideAndClear()},hide:function(){w.hideAndClear()}},get:{id:function(){return g},text:function(){return V.text()},query:function(){return e.trim(R.val())},searchWidth:function(e){return e*C.glyphWidth+"em"},selectionCount:function(){var t=w.get.values();return w.is.multiple()?e.isArray(t)?t.length:0:""!==w.get.value()?1:0},transition:function(e){return"auto"==C.transition?w.is.upward(e)?"slide up":"slide down":C.transition},userValues:function(){var t=w.get.values();return t?e.grep(t,function(e){return w.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=R.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 O.length>0?O.val():E.data(y.value)},values:function(){var e=w.get.value();return""===e?"":!O.is("select")&&w.is.multiple()?"string"==typeof e?e.split(C.delimiter):"":e},remoteValues:function(){var t=w.get.values(),n=!1;return t&&("string"==typeof t&&(t=[t]),n={},e.each(t,function(e,t){var i=w.read.remoteData(t);n[t]=i?i:t})),n},choiceText:function(e,t){return t=t!==i?t:C.preserveHTML,e?(e.find(k.menu).length>0&&(w.verbose("Retreiving text of element with sub-menu"),e=e.clone(),e.find(k.menu).remove(),e.find(k.menuIcon).remove()),e.data(y.text)!==i?e.data(y.text):t?e.html().trim():e.text().trim()):void 0},choiceValue:function(e,t){return t=t||w.get.choiceText(e),e?e.data(y.value)!==i?e.data(y.value):"string"==typeof t?t.toLowerCase().trim():t:!1},inputEvent:function(){var e=R[0];return e?e.oninput!==i?"input":e.onpropertychange!==i?"propertychange":"keyup":!1},selectValues:function(){var t={};return t.values=[],E.find("option").each(function(){var n=e(this).html(),a=e(this).attr("value")!==i?e(this).attr("value"):n;"auto"===C.placeholder&&""===a?t.placeholder=n:t.values.push({name:n,value:a})}),C.placeholder&&"auto"!==C.placeholder&&(w.debug("Setting placeholder value to",C.placeholder),t.placeholder=C.placeholder),C.sortSelect?(t.values.sort(function(e,t){return e.name>t.name?1:-1}),w.debug("Retrieved and sorted values from select",t)):w.debug("Retreived values from select",t),t},activeItem:function(){return z.filter("."+x.active)},selectedItem:function(){var e=z.not(k.unselectable).filter("."+x.selected);return e.length>0?e:z.eq(0)},item:function(t,n){var a,o=!1;return t=t!==i?t:w.get.values()!==i?w.get.values():w.get.text(),a=w.is.multiple()&&e.isArray(t),n=""===t||0===t?!0:n||!1,t!==i&&z.each(function(){var i=e(this),s=w.get.choiceText(i),r=w.get.choiceValue(i,s);if(a)(-1!==e.inArray(r.toString(),t)||-1!==e.inArray(s,t))&&(o=o?o.add(i):i);else if(n){if(w.verbose("Ambiguous dropdown value using strict type check",i,t),r===t||s===t)return o=i,!0}else if(r==t||s==t)return w.verbose("Found select item by value",r,t),o=i,!0}),o}},check:{maxSelections:function(e){return C.maxSelections?(e=e!==i?e:w.get.selectionCount(),e>=C.maxSelections?(w.debug("Maximum selection count reached"),z.addClass(x.filtered),w.add.message(S.maxSelections),!0):(w.verbose("No longer at maximum selection count"),w.remove.message(),w.remove.filteredItem(),w.is.searchSelection()&&w.filterItems(),!1)):!0}},restore:{defaults:function(){w.restore.defaultText(),w.restore.defaultValue()},defaultText:function(){var e=E.data(y.defaultText);w.debug("Restoring default text",e),w.set.text(e),V.addClass(x.placeholder)},defaultValue:function(){var e=E.data(y.defaultValue);e!==i&&(w.debug("Restoring default value",e),e.length?w.set.selected(e):(w.remove.activeItem(),w.remove.selectedItem()))},remoteValues:function(){var t=w.get.remoteValues();w.debug("Recreating selected from session data",t),t&&(w.is.single()?(console.log("single text",t,name),e.each(t,function(e,t){w.set.text(t)})):e.each(t,function(e,t){console.log(e,t),w.add.label(e,t)}))}},read:{remoteData:function(e){var n;return t.Storage===i?void w.error(D.noStorage):(n=sessionStorage.getItem(e),console.log("reading",e,n),n!==i?n:!1)}},save:{defaults:function(){w.save.defaultText(),w.save.placeholderText(),w.save.defaultValue()},defaultValue:function(){E.data(y.defaultValue,w.get.value())},defaultText:function(){E.data(y.defaultText,V.text())},placeholderText:function(){V.hasClass(x.placeholder)&&E.data(y.placeholderText,V.text())},remoteData:function(e,n){return t.Storage===i?void w.error(D.noStorage):(console.log("saving",n,e),void sessionStorage.setItem(n,e))}},clear:function(){w.set.placeholderText(),w.clearValue(),w.remove.activeItem(),w.remove.selectedItem()},clearValue:function(){w.set.value("")},scrollPage:function(e,t){var n,i,a,o=M.outerHeight(),s=M.scrollTop(),r=z.eq(0).outerHeight(),l=Math.floor(o/r),c=(M.prop("scrollHeight"),"up"==e?s-r*l:s+r*l),u=z.not(k.unselectable);t=t||w.get.selectedItem(),a="up"==e?u.index(t)-l:u.index(t)+l,n="up"==e?a>=0:a<u.length,i=n?u.eq(a):"up"==e?u.first():u.last(),i.length>0&&(w.debug("Scrolling page",e,i),t.removeClass(x.selected),i.addClass(x.selected),M.scrollTop(c))},set:{filtered:function(){var e=w.is.multiple(),t=w.is.searchSelection(),n=e&&t,i=t?w.get.query():"",a="string"==typeof i&&i.length>0,o=w.get.searchWidth(i.length),s=""!==i;e&&a&&(w.verbose("Adjusting input width",o,C.glyphWidth),R.css("width",o)),a||n&&s?(w.verbose("Hiding placeholder text"),V.addClass(x.filtered)):(!e||n&&!s)&&(w.verbose("Showing placeholder text"),V.removeClass(x.filtered))},loading:function(){E.addClass(x.loading)},placeholderText:function(e){w.debug("Restoring placeholder text"),e=e||E.data(y.placeholderText),w.set.text(e),V.addClass(x.placeholder)},tabbable:function(){w.has.search()?(w.debug("Added tabindex to searchable dropdown"),R.val("").attr("tabindex",0),M.attr("tabindex",-1)):(w.debug("Added tabindex to dropdown"),E.attr("tabindex")||(E.attr("tabindex",0),M.attr("tabindex",-1)))},scrollPosition:function(e,t){var n,a,o,s,r,l,c,u,d,v=5;e=e||w.get.selectedItem(),n=e.closest(k.menu),a=e&&e.length>0,t=t!==i?t:!1,e&&n.length>0&&a&&(s=e.position().top,n.addClass(x.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),w.debug("Scrolling to active item",o),(t||u||d)&&n.scrollTop(o),n.removeClass(x.loading))},text:function(e){"select"!==C.action&&("combo"==C.action?(w.debug("Changing combo button text",e,P),C.preserveHTML?P.html(e):P.text(e)):(w.debug("Changing text",e,V),V.removeClass(x.filtered).removeClass(x.placeholder),C.preserveHTML?V.html(e):V.text(e)))},selectedLetter:function(t){var n=z.filter("."+x.selected),i=!1;z.each(function(){var n=e(this),a=w.get.choiceText(n,!1),o=String(a).charAt(0).toLowerCase(),s=t.toLowerCase();return o==s?(i=n,!1):void 0}),i&&(w.verbose("Scrolling to next value with letter",t),w.set.scrollPosition(i),n.removeClass(x.selected),i.addClass(x.selected))},upward:function(e){var t=e||E;t.addClass(x.upward)},value:function(t,n,i){var a=(O.length>0,!w.has.value(t),w.get.values());if(O.length>0){if(w.is.multiple()){if(""===t)return void w.debug("Cannot select blank values from multiselect");t=[t],e.isArray(a)&&(t=a.concat(t),t=w.get.uniqueArray(t)),O.is("select")?(w.debug("Setting multiple <select> values",t,O),C.allowAdditions&&w.add.optionValue(t)):(t=t.join(C.delimiter),w.debug("Setting hidden input to delimited values",t,O))}if(t==a)return void w.verbose("Skipping value update already same value",t,a);w.debug("Updating input value",t,a),O.val(t).trigger("change"),C.onChange.call(U,t,n,i)}else w.verbose("Storing value in metadata",t,O),t!==a&&(E.data(y.value,t),C.onChange.call(U,t,n,i));w.check.maxSelections()},active:function(){E.addClass(x.active)},multiple:function(){E.addClass(x.multiple)},visible:function(){E.addClass(x.visible)},selected:function(t,n){var i=w.is.multiple();return(n=n||w.get.item(t))?(w.debug("Setting selected menu item to",n),w.is.single()?(w.remove.activeItem(),w.remove.selectedItem()):C.useLabels&&w.remove.selectedItem(),void n.each(function(){var t=e(this),a=w.get.choiceText(t),o=w.get.choiceValue(t,a),s=t.hasClass(x.filtered),r=t.hasClass(x.active),l=t.hasClass(x.addition),c=i&&1==n.length;i?!r||l?(C.apiSettings&&C.saveRemoteData&&w.save.remoteData(a,o),C.useLabels?(w.add.label(o,a,c),w.set.value(o,a,t),t.addClass(x.active),w.filterActive(),w.select.nextAvailable(n)):(w.set.value(o,a,t),w.set.text(w.add.variables(S.count)),t.addClass(x.active))):s||(w.debug("Selected active value, removing label"),w.remove.selected(o)):(w.set.value(o,a,t),w.set.text(a),t.addClass(x.active).addClass(x.selected))})):!1}},add:{label:function(t,n,i){var a,o=w.is.searchSelection()?R:V;return a=e("<a />").addClass(x.label).attr("data-value",t).html(L.label(t,n)),a=C.onLabelCreate.call(a,t,n),w.has.label(t)?void w.debug("Label already exists, skipping",t):(C.label.variation&&a.addClass(C.label.variation),void(i===!0?(w.debug("Animating in label",a),a.addClass(x.hidden).insertBefore(o).transition(C.label.transition,C.label.duration)):(w.debug("Adding selection label",a),a.insertBefore(o))))},message:function(t){var n=M.children(k.message),i=C.templates.message(w.add.variables(t));n.length>0?n.html(i):n=e("<div/>").html(i).addClass(x.message).appendTo(M)},optionValue:function(t){return O.is("select")?(p&&p.disconnect(),e.each(t,function(t,n){var i=O.find('option[value="'+n+'"]'),a=i.length>0;a||(e("<option/>").prop("value",n).html(n).appendTo(O),w.verbose("Adding user addition as an <option>",n))}),void(p&&p.observe(O[0],{childList:!0,subtree:!0}))):!1},userChoice:function(t){var n,i=w.get.item(t),a=M.children(k.addition);if(!w.has.maxSelections()){if(""===t||i)return void a.remove();n=C.templates.addition(t),z.removeClass(x.selected),a.length>0?a.html(n).data(y.value,t).removeClass(x.filtered).addClass(x.selected):a=e("<div/>").html(n).data(y.value,t).addClass(x.addition).addClass(x.item).prependTo(M).addClass(x.selected)}},variables:function(e){var t,n,i=-1!==e.search("{count}"),a=-1!==e.search("{maxCount}"),o=-1!==e.search("{term}");return i&&(t=w.get.selectionCount(),e=e.replace("{count}",t)),a&&(t=w.get.selectionCount(),e=e.replace("{maxCount}",C.maxSelections)),o&&(n=w.get.query(),e=e.replace("{term}",n)),e}},remove:{active:function(){E.removeClass(x.active)},activeLabel:function(){E.find(k.label).removeClass(x.active)},loading:function(){E.removeClass(x.loading)},upward:function(e){var t=e||E;t.removeClass(x.upward)},visible:function(){E.removeClass(x.visible)},activeItem:function(){z.removeClass(x.active)},filteredItem:function(){w.has.maxSelections()||(C.useLabels?z.not("."+x.active).removeClass(x.filtered):z.removeClass(x.filtered))},message:function(){M.children(k.message).remove()},searchTerm:function(){w.verbose("Cleared search term"),R.val(""),w.set.filtered()},selected:function(e){var t=w.get.item(e),n=w.get.choiceValue(t);return t?(w.remove.value(n),w.is.multiple()&&(C.useLabels?w.remove.label(n):w.set.text(w.add.variables(S.count))),t.removeClass(x.filtered).removeClass(x.active),void(C.useLabels&&t.removeClass(x.selected))):!1},selectedItem:function(){z.removeClass(x.selected)},value:function(e){var t=O.val();O.is("select")?(w.verbose("Input is <select> removing selected"),O.find('option[value="'+e+'"]').prop("selected",!1)):(w.verbose("Input is csv removing value"),t=w.remove.delimitedValue(e,t),O.val(t).trigger("change")),w.check.maxSelections()},delimitedValue:function(t,n){return"string"!=typeof n?!1:(n=n.split(C.delimiter),n=e.grep(n,function(e){return t!=e}),n=n.join(C.delimiter),w.verbose("Removed value from delimited string",t,n),n)},label:function(e){var t=E.find(k.label),n=t.filter('[data-value="'+e+'"]'),i=t.length,a=t.index(n)+1==i,o=(!w.is.searchSelection()||!w.is.focusedOnSearch())&&a;o?(w.verbose("Animating and removing label",n),n.transition(C.label.transition,C.label.duration,function(){n.remove()})):(w.verbose("Removing label",n),n.remove())},labels:function(t){t=t||E.find(k.label).filter("."+x.active),w.verbose("Removing active label selections",t),t.each(function(){var t=e(this).data("value"),n=w.is.userValue(t);n?(w.remove.value(t),w.remove.label(t)):w.remove.selected(t)})},tabbable:function(){w.has.search()?(w.debug("Searchable dropdown initialized"),R.attr("tabindex","-1"),M.attr("tabindex","-1")):(w.debug("Simple selection dropdown initialized"),E.attr("tabindex","-1"),M.attr("tabindex","-1"))}},has:{search:function(){return R.length>0},input:function(){return O.length>0},menu:function(){return M.length>0},message:function(){return 0!==M.children(k.message).length},label:function(e){var t=E.find(k.label);return t.filter('[data-value="'+e+'"]').length>0},maxSelections:function(){return C.maxSelections&&w.get.selectionCount()>=C.maxSelections},allResultsFiltered:function(){return z.filter(k.unselectable).length===z.length},value:function(t){var n=w.get.values(),i=e.isArray(n)?n&&-1!==e.inArray(t,n):n==t;return i?!0:!1}},is:{active:function(){return E.hasClass(x.active)},alreadySetup:function(){return E.is("select")&&E.parent(k.dropdown).length>0&&0===E.prev().length},animating:function(e){return e?e.transition&&e.transition("is animating"):M.transition&&M.transition("is animating")},focused:function(){return n.activeElement===E[0]},focusedOnSearch:function(){return n.activeElement===R[0]},allFiltered:function(){return(w.is.multiple()||w.has.search())&&!w.has.message()&&w.has.allResultsFiltered()},hidden:function(e){return!w.is.visible(e)},onScreen:function(n){var a,o=n||M;return o.addClass(x.loading),a=e.fn.visibility!==i?o.visibility("bottom visible"):e(t).scrollTop()+e(t).height()>=o.offset().top+o.height(),w.debug("Checking if menu can fit on screen",a,M),o.removeClass(x.loading),a},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(x.multiple)},single:function(){return!w.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(x.search)},searchSelection:function(){return w.has.search()&&0===R.closest(k.menu).length},selection:function(){return E.hasClass(x.selection)},userValue:function(t){return-1!==e.inArray(t,w.get.userValues())},upward:function(e){var t=e||E;return t.hasClass(x.upward)},visible:function(e){return e?e.hasClass(x.visible):M.hasClass(x.visible)}},can:{click:function(){return c||"click"==C.on},show:function(){return!E.hasClass(x.disabled)&&z.length>0},useAPI:function(){return e.fn.api!==i}},animate:{show:function(t,n){var a,o=n||M,s=n?function(){}:function(){w.hideSubMenus(),w.hideOthers(),w.set.active()};t=e.isFunction(t)?t:function(){},w.verbose("Doing menu show animation",o),C.keepOnScreen&&(w.is.onScreen(n)?w.remove.upward(n):w.set.upward(n)),a=w.get.transition(n),w.is.selection()&&w.set.scrollPosition(w.get.selectedItem(),!0),(w.is.hidden(o)||w.is.animating(o))&&("none"==a?(s(),o.transition("show"),t.call(U)):e.fn.transition!==i&&E.transition("is supported")?o.transition({animation:a+" in",debug:C.debug,verbose:C.verbose,duration:C.duration,queue:!0,onStart:s,onComplete:function(){t.call(U)}}):w.error(D.noTransition,a))},hide:function(t,n){var a=n||M,o=(n?.9*C.duration:C.duration,n?function(){}:function(){w.can.click()&&w.unbind.intent(),w.focusSearch(),w.remove.active()}),s=w.get.transition(n);t=e.isFunction(t)?t:function(){},(w.is.visible(a)||w.is.animating(a))&&(w.verbose("Doing menu hide animation",a),"none"==s?(o(),a.transition("hide"),t.call(U)):e.fn.transition!==i&&E.transition("is supported")?a.transition({animation:s+" out",duration:C.duration,debug:C.debug,verbose:C.verbose,queue:!0,onStart:o,onComplete:function(){w.remove.upward(n),t.call(U)}}):w.error(D.transition))}},hideAndClear:function(){w.is.searchSelection()?(w.remove.searchTerm(),w.hide(function(){w.remove.filteredItem()})):w.hide()},delay:{show:function(){w.verbose("Delaying show event to ensure user intent"),
|
|
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(.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+='<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);
|