-
Notifications
You must be signed in to change notification settings - Fork 74
/
umd.js
1 lines (1 loc) · 12.6 KB
/
umd.js
1
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).VueSimpleSuggest=e()}(this,function(){"use strict";var t={selectionUp:[38],selectionDown:[40],select:[13],hideList:[27],showList:[40],autocomplete:[32,13]},e={input:String,select:Object};function r(t,e){return i(t,e.keyCode)}function i(t,e){if(t.length<=0)return!1;function s(t){return t.some(function(t){return t===e})}return Array.isArray(t[0])?t.some(function(t){return s(t)}):s(t)}var n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t};function o(t,e,s){return s?e?e(t):t:(t&&t.then||(t=Promise.resolve(t)),e?t.then(e):t)}function u(){}function l(t,e){var s=t();return s&&s.then?s.then(e):e(s)}function c(t,e){try{var s=t()}catch(t){return e(t)}return s&&s.then?s.then(void 0,e):s}function h(t,e){try{var s=t()}catch(t){return e()}return s&&s.then?s.then(e,e):e()}var s={render:function(){var s=this,t=s.$createElement,i=s._self._c||t;return i("div",{staticClass:"vue-simple-suggest",class:[s.styles.vueSimpleSuggest,{designed:!s.destyled,focus:s.isInFocus}],on:{keydown:function(t){if(!t.type.indexOf("key")&&s._k(t.keyCode,"tab",9,t.key,"Tab"))return null;s.isTabbed=!0}}},[i("div",{ref:"inputSlot",staticClass:"input-wrapper",class:s.styles.inputWrapper,attrs:{role:"combobox","aria-haspopup":"listbox","aria-owns":s.listId,"aria-expanded":s.listShown&&!s.removeList?"true":"false"}},[s._t("default",[i("input",s._b({staticClass:"default-input",class:s.styles.defaultInput,domProps:{value:s.text||""}},"input",s.$attrs,!1))])],2),s._v(" "),i("transition",{attrs:{name:"vue-simple-suggest"}},[s.listShown&&!s.removeList?i("ul",{staticClass:"suggestions",class:s.styles.suggestions,attrs:{id:s.listId,role:"listbox","aria-labelledby":s.listId}},[this.$scopedSlots["misc-item-above"]?i("li",{class:s.styles.miscItemAbove},[s._t("misc-item-above",null,{suggestions:s.suggestions,query:s.text})],2):s._e(),s._v(" "),s._l(s.suggestions,function(e,t){return i("li",{key:s.getId(e,t),staticClass:"suggest-item",class:[s.styles.suggestItem,{selected:s.isSelected(e),hover:s.isHovered(e)}],attrs:{role:"option","aria-selected":s.isHovered(e)||s.isSelected(e)?"true":"false",id:s.getId(e,t)},on:{mouseenter:function(t){return s.hover(e,t.target)},mouseleave:function(){return s.hover(void 0)},click:function(t){return s.suggestionClick(e,t)}}},[s._t("suggestion-item",[i("span",[s._v(s._s(s.displayProperty(e)))])],{autocomplete:function(){return s.autocompleteText(e)},suggestion:e,query:s.text})],2)}),s._v(" "),this.$scopedSlots["misc-item-below"]?i("li",{class:s.styles.miscItemBelow},[s._t("misc-item-below",null,{suggestions:s.suggestions,query:s.text})],2):s._e()],2):s._e()])],1)},staticRenderFns:[],name:"vue-simple-suggest",inheritAttrs:!1,model:{prop:"value",event:"input"},props:{styles:{type:Object,default:function(){return{}}},controls:{type:Object,default:function(){return t}},minLength:{type:Number,default:1},maxSuggestions:{type:Number,default:10},displayAttribute:{type:String,default:"title"},valueAttribute:{type:String,default:"id"},list:{type:[Function,Array],default:function(){return[]}},removeList:{type:Boolean,default:!1},destyled:{type:Boolean,default:!1},filterByQuery:{type:Boolean,default:!1},filter:{type:Function,default:function(t,e){return!e||~this.displayProperty(t).toLowerCase().indexOf(e.toLowerCase())}},debounce:{type:Number,default:0},nullableSelect:{type:Boolean,default:!1},value:{},mode:{type:String,default:"input",validator:function(t){return!!~Object.keys(e).indexOf(t.toLowerCase())}},preventHide:{type:Boolean,default:!1}},watch:{mode:{handler:function(t){var e=this;this.constructor.options.model.event=t,this.$parent&&this.$parent.$forceUpdate(),this.$nextTick(function(){"input"===t?e.$emit("input",e.text):e.$emit("select",e.selected)})},immediate:!0},value:{handler:function(t){"string"!=typeof t&&(t=this.displayProperty(t)),this.updateTextOutside(t)},immediate:!0}},data:function(){return{selected:null,hovered:null,suggestions:[],listShown:!1,inputElement:null,canSend:!0,timeoutInstance:null,text:this.value,isPlainSuggestion:!1,isClicking:!1,isInFocus:!1,isFalseFocus:!1,isTabbed:!1,controlScheme:{},listId:this._uid+"-suggestions"}},computed:{listIsRequest:function(){return"function"==typeof this.list},inputIsComponent:function(){return this.$slots.default&&0<this.$slots.default.length&&!!this.$slots.default[0].componentInstance},input:function(){return this.inputIsComponent?this.$slots.default[0].componentInstance:this.inputElement},on:function(){return this.inputIsComponent?"$on":"addEventListener"},off:function(){return this.inputIsComponent?"$off":"removeEventListener"},hoveredIndex:function(){for(var t=0;t<this.suggestions.length;t++){var e=this.suggestions[t];if(this.hovered&&this.valueProperty(this.hovered)==this.valueProperty(e))return t}return-1},textLength:function(){return this.text&&this.text.length||this.inputElement.value.length||0},isSelectedUpToDate:function(){return!!this.selected&&this.displayProperty(this.selected)===this.text}},created:function(){this.controlScheme=Object.assign({},t,this.controls)},mounted:function(){try{var t=this;return o(t.$slots.default,function(){t.$nextTick(function(){t.inputElement=t.$refs.inputSlot.querySelector("input"),t.inputElement?(t.setInputAriaAttributes(),t.prepareEventHandlers(!0)):console.error("No input element found")})})}catch(t){return Promise.reject(t)}},beforeDestroy:function(){this.prepareEventHandlers(!1)},methods:{isEqual:function(t,e){return e&&this.valueProperty(t)==this.valueProperty(e)},isSelected:function(t){return this.isEqual(t,this.selected)},isHovered:function(t){return this.isEqual(t,this.hovered)},setInputAriaAttributes:function(){this.inputElement.setAttribute("aria-activedescendant",""),this.inputElement.setAttribute("aria-autocomplete","list"),this.inputElement.setAttribute("aria-controls",this.listId)},prepareEventHandlers:function(t){var e=this[t?"on":"off"],s={click:this.showSuggestions,keydown:this.onKeyDown,keyup:this.onListKeyUp},i=Object.assign({blur:this.onBlur,focus:this.onFocus,input:this.onInput},s);for(var n in i)this.input[e](n,i[n]);var o=t?"addEventListener":"removeEventListener";for(var r in s)this.inputElement[o](r,s[r])},isScopedSlotEmpty:function(t){if(t){var e=t(this);return!(Array.isArray(e)||e&&(e.tag||e.context||e.text||e.children))}return!0},miscSlotsAreEmpty:function(){var e=this,t=["misc-item-above","misc-item-below"].map(function(t){return e.$scopedSlots[t]});if(t.every(function(t){return!!t}))return t.every(this.isScopedSlotEmpty.bind(this));var s=t.find(function(t){return!!t});return this.isScopedSlotEmpty.call(this,s)},getPropertyByAttribute:function(t,e){return this.isPlainSuggestion?t:void 0!==(void 0===t?"undefined":n(t))?(s=t,e.split(".").reduce(function(t,e){return t===Object(t)?t[e]:t},s)):t;var s},displayProperty:function(t){if(this.isPlainSuggestion)return t;var e=this.getPropertyByAttribute(t,this.displayAttribute);return void 0===e&&(e=JSON.stringify(t),process&&~process.env.NODE_ENV.indexOf("dev")&&console.warn("[vue-simple-suggest]: Please, provide `display-attribute` as a key or a dotted path for a property from your object.")),String(e||"")},valueProperty:function(t){if(this.isPlainSuggestion)return t;var e=this.getPropertyByAttribute(t,this.valueAttribute);return void 0===e&&console.error("[vue-simple-suggest]: Please, check if you passed 'value-attribute' (default is 'id') and 'display-attribute' (default is 'title') props correctly.\n Your list objects should always contain a unique identifier."),e},autocompleteText:function(t){this.setText(this.displayProperty(t))},setText:function(t){var e=this;this.$nextTick(function(){e.inputElement.value=t,e.text=t,e.$emit("input",t)})},select:function(t){(this.selected!==t||this.nullableSelect&&!t)&&(this.selected=t,this.$emit("select",t),t&&this.autocompleteText(t)),this.hover(null)},hover:function(t,e){var s=t?this.getId(t,this.hoveredIndex):"";this.inputElement.setAttribute("aria-activedescendant",s),t&&t!==this.hovered&&this.$emit("hover",t,e),this.hovered=t},hideList:function(){this.listShown&&(this.listShown=!1,this.hover(null),this.$emit("hide-list"))},showList:function(){this.listShown||this.textLength>=this.minLength&&(0<this.suggestions.length||!this.miscSlotsAreEmpty())&&(this.listShown=!0,this.$emit("show-list"))},showSuggestions:function(){try{var t=this;return l(function(){if(0===t.suggestions.length&&t.minLength<=t.textLength)return t.showList(),function(t,e){if(!e)return t&&t.then?t.then(u):Promise.resolve()}(t.research())},function(){t.showList()})}catch(t){return Promise.reject(t)}},onShowList:function(t){r(this.controlScheme.showList,t)&&this.showSuggestions()},moveSelection:function(t){if(this.listShown&&this.suggestions.length&&r([this.controlScheme.selectionUp,this.controlScheme.selectionDown],t)){t.preventDefault();var e=r(this.controlScheme.selectionDown,t),s=2*e-1,i=e?0:this.suggestions.length-1,n=e?this.hoveredIndex<this.suggestions.length-1:0<this.hoveredIndex,o=null;o=this.hovered?n?this.suggestions[this.hoveredIndex+s]:this.suggestions[i]:this.selected||this.suggestions[i],this.hover(o)}},onKeyDown:function(t){var e=this.controlScheme.select,s=this.controlScheme.hideList;"Enter"===t.key&&this.listShown&&i([e,s],13)&&t.preventDefault(),"Tab"===t.key&&this.hovered&&this.select(this.hovered),this.onShowList(t),this.moveSelection(t),this.onAutocomplete(t)},onListKeyUp:function(t){var e=this.controlScheme.select,s=this.controlScheme.hideList;this.listShown&&r([e,s],t)&&(t.preventDefault(),r(e,t)&&this.select(this.hovered),this.hideList())},onAutocomplete:function(t){r(this.controlScheme.autocomplete,t)&&(t.ctrlKey||t.shiftKey)&&0<this.suggestions.length&&this.suggestions[0]&&this.listShown&&(t.preventDefault(),this.hover(this.suggestions[0]),this.autocompleteText(this.suggestions[0]))},suggestionClick:function(t,e){var s=this;this.$emit("suggestion-click",t,e),this.select(t),this.preventHide||this.hideList(),this.isClicking&&setTimeout(function(){s.inputElement.focus(),s.isClicking=!1},0)},onBlur:function(t){this.isInFocus?(this.isClicking=this.hovered&&!this.isTabbed,this.isClicking?t&&t.isTrusted&&!this.isTabbed&&(this.isFalseFocus=!0):(this.isInFocus=!1,this.hideList(),this.$emit("blur",t))):(this.inputElement.blur(),console.error("This should never happen!\n If you encountered this error, please make sure that your input component emits 'focus' events properly.\n For more info see https://github.com/KazanExpress/vue-simple-suggest#custom-input.\n\n If your 'vue-simple-suggest' setup does not include a custom input component - please,\n report to https://github.com/KazanExpress/vue-simple-suggest/issues/new")),this.isTabbed=!1},onFocus:function(t){this.isInFocus=!0,t&&!this.isFalseFocus&&this.$emit("focus",t),this.isClicking||this.isFalseFocus||this.showSuggestions(),this.isFalseFocus=!1},onInput:function(t){var e=t.target?t.target.value:t;this.updateTextOutside(e),this.$emit("input",e)},updateTextOutside:function(t){this.text!==t&&(this.text=t,this.hovered&&this.hover(null),this.text.length<this.minLength?this.hideList():this.debounce?(clearTimeout(this.timeoutInstance),this.timeoutInstance=setTimeout(this.research,this.debounce)):this.research())},research:function(){try{var s=this;return h(function(){return c(function(){return function(t){var e=t();if(e&&e.then)return e.then(u)}(function(){if(s.canSend){s.canSend=!1;var e=s.text;return o(s.getSuggestions(s.text),function(t){e===s.text&&s.$set(s,"suggestions",t)})}})},function(t){throw s.clearSuggestions(),t})},function(){return s.canSend=!0,0===s.suggestions.length&&s.miscSlotsAreEmpty()?s.hideList():s.isInFocus&&s.showList(),s.suggestions})}catch(t){return Promise.reject(t)}},getSuggestions:function(e){try{var s=this;if((e=e||"").length<s.minLength)return[];s.selected=null,s.listIsRequest&&s.$emit("request-start",e);var t=!1,i=[];return h(function(){return c(function(){return l(function(){if(s.listIsRequest)return o(s.list(e),function(t){i=t||[]});i=s.list},function(){Array.isArray(i)||(i=[i]),t="object"!==n(i[0])&&void 0!==i[0]||Array.isArray(i[0]),s.filterByQuery&&(i=i.filter(function(t){return s.filter(t,e)})),s.listIsRequest&&s.$emit("request-done",i)})},function(t){if(!s.listIsRequest)throw t;s.$emit("request-failed",t)})},function(){return s.maxSuggestions&&i.splice(s.maxSuggestions),s.isPlainSuggestion=t,i})}catch(t){return Promise.reject(t)}},clearSuggestions:function(){this.suggestions.splice(0)},getId:function(t,e){return this.listId+"-suggestion-"+(this.isPlainSuggestion?e:this.valueProperty(t)||e)}}};return(Vue||window&&window.Vue)&&(Vue||window.Vue).component("vue-simple-suggest",s),s});