UNPKG

@appbaseio/reactivesearch-vue

Version:

A Vue UI components library for building search experiences

492 lines (484 loc) 20.2 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var reactivecore = require('@appbaseio/reactivecore'); var constants = require('@appbaseio/reactivecore/lib/utils/constants'); require('@vue/babel-helper-vue-transform-on'); var _rollupPluginBabelHelpers = require('./_rollupPluginBabelHelpers-1a877b17.js'); var vue = require('vue'); var VueTypes = _interopDefault(require('vue-types')); require('@appbaseio/reactivecore/lib/utils/helper'); require('@appbaseio/vue-emotion'); require('@emotion/css'); require('polished'); var Button = require('./Button-7aee17e3.js'); var vueTypes = require('./vueTypes-adf43075.js'); require('@appbaseio/reactivecore/lib/utils/transform'); require('redux'); var index = require('./index-7ca9570e.js'); var ComponentWrapper = require('./ComponentWrapper-3bedbe57.js'); var PreferencesConsumer = require('./PreferencesConsumer-37cab7a0.js'); var Title = require('./Title-d513ac26.js'); require('./CancelSvg-6094cd37.js'); require('./Input-55fbd8e1.js'); require('compute-scroll-into-view'); var Container = require('./Container-1c05785a.js'); require('@appbaseio/reactivecore/lib/utils/suggestions'); var utils = require('./utils-a3db8765.js'); var DropDown = require('./DropDown-7ee915d1.js'); function _isSlot(s) { return typeof s === 'function' || Object.prototype.toString.call(s) === '[object Object]' && !vue.isVNode(s); } var updateQuery = reactivecore.Actions.updateQuery, setQueryOptions = reactivecore.Actions.setQueryOptions, setCustomQuery = reactivecore.Actions.setCustomQuery, setDefaultQuery = reactivecore.Actions.setDefaultQuery; var isEqual = reactivecore.helper.isEqual, getQueryOptions = reactivecore.helper.getQueryOptions, checkValueChange = reactivecore.helper.checkValueChange, checkPropChange = reactivecore.helper.checkPropChange, getClassName = reactivecore.helper.getClassName, getCompositeAggsQuery = reactivecore.helper.getCompositeAggsQuery, extractQueryFromCustomQuery = reactivecore.helper.extractQueryFromCustomQuery, getOptionsForCustomQuery = reactivecore.helper.getOptionsForCustomQuery; var MultiDropdownList = { name: 'MultiDropdownList', data: function data() { var props = this.$props; this.__state = { currentValue: {}, modifiedOptions: [], after: {}, // for composite aggs isLastBucket: false }; this.internalComponent = props.componentId + "__internal"; return this.__state; }, props: { beforeValueChange: vueTypes.types.func, className: VueTypes.string.def(''), componentId: vueTypes.types.stringRequired, compoundClause: vueTypes.types.compoundClause, customQuery: vueTypes.types.func, dataField: vueTypes.types.stringRequired, defaultValue: vueTypes.types.stringArray, value: vueTypes.types.stringArray, defaultQuery: vueTypes.types.func, filterLabel: vueTypes.types.string, innerClass: vueTypes.types.style, placeholder: VueTypes.string.def('Select values'), queryFormat: VueTypes.oneOf(['and', 'or']).def('or'), react: vueTypes.types.react, renderLabel: vueTypes.types.func, render: vueTypes.types.func, renderItem: vueTypes.types.func, renderError: vueTypes.types.title, renderNoResults: VueTypes.any, transformData: vueTypes.types.func, selectAllLabel: vueTypes.types.string, showCount: VueTypes.bool.def(true), showFilter: VueTypes.bool.def(true), size: VueTypes.number, sortBy: VueTypes.oneOf(['asc', 'desc', 'count']).def('count'), title: vueTypes.types.title, URLParams: VueTypes.bool.def(false), showMissing: VueTypes.bool.def(false), missingLabel: VueTypes.string.def('N/A'), showSearch: VueTypes.bool.def(false), showClear: VueTypes.bool.def(false), showLoadMore: VueTypes.bool.def(false), loadMoreLabel: VueTypes.oneOfType([VueTypes.string, VueTypes.nullable]).def('Load More'), nestedField: vueTypes.types.string, index: VueTypes.string, searchPlaceholder: VueTypes.string.def('Type here to search...'), endpoint: vueTypes.types.endpointConfig }, created: function created() { var props = this.$props; this.modifiedOptions = this.options && this.options[props.dataField] ? this.options[props.dataField].buckets : []; // Set custom and default queries in store index.updateCustomQuery(this.componentId, this.setCustomQuery, this.$props, this.currentValue); index.updateDefaultQuery(this.componentId, this.setDefaultQuery, this.$props, this.currentValue); this.updateQueryOptions(this.$props); if (this.selectedValue) { this.setValue(this.selectedValue, true); } else if (this.$props.value) { this.setValue(this.$props.value, true); } else if (this.$props.defaultValue) { this.setValue(this.$props.defaultValue, true); } }, watch: { selectedValue: function selectedValue(newVal) { var _this = this; var selectedValue = Object.keys(this.$data.currentValue); if (this.$props.selectAllLabel) { selectedValue = selectedValue.filter(function (val) { return val !== _this.$props.selectAllLabel; }); if (this.$data.currentValue[this.$props.selectAllLabel]) { selectedValue = [this.$props.selectAllLabel]; } } if (!isEqual(selectedValue, newVal)) { this.setValue(newVal || [], true); } }, options: function options(newVal, oldVal) { var _this2 = this; if (newVal) { checkPropChange(oldVal, newVal, function () { var _this2$$props = _this2.$props, showLoadMore = _this2$$props.showLoadMore, dataField = _this2$$props.dataField; var modifiedOptions = _this2.$data.modifiedOptions; if (showLoadMore) { // append options with showLoadMore var buckets = newVal[dataField].buckets; var nextOptions = [].concat(modifiedOptions, buckets.map(function (bucket) { return { key: bucket.key[dataField], doc_count: bucket.doc_count }; })); var after = newVal[dataField].after_key; // detect the last bucket by checking if the next set of buckets were empty var isLastBucket = !buckets.length; _this2.after = { after: after }; _this2.isLastBucket = isLastBucket; _this2.modifiedOptions = nextOptions; } else { _this2.modifiedOptions = newVal[_this2.$props.dataField] ? newVal[_this2.$props.dataField].buckets : []; } }); } }, size: function size() { this.updateQueryOptions(this.$props); }, sortBy: function sortBy() { this.updateQueryOptions(this.$props); }, dataField: function dataField() { this.updateQueryOptions(this.$props); this.updateQueryHandler(this.$data.currentValue, this.$props); }, defaultValue: function defaultValue(newVal) { this.setValue(newVal, true); }, value: function value(newVal, oldVal) { if (!isEqual(newVal, oldVal)) { this.setValue(newVal, true); } }, defaultQuery: function defaultQuery(newVal, oldVal) { if (!index.isQueryIdentical(newVal, oldVal, this.$data.currentValue, this.$props)) { this.updateDefaultQueryHandler(this.$data.currentValue, this.$props); } }, customQuery: function customQuery(newVal, oldVal) { if (!index.isQueryIdentical(newVal, oldVal, this.$data.currentValue, this.$props)) { this.updateQueryHandler(this.componentId, this.$data.currentValue, this.$props); } } }, render: function render() { var _this3 = this; var _this$$props = this.$props, showLoadMore = _this$$props.showLoadMore, loadMoreLabel = _this$$props.loadMoreLabel, renderItem = _this$$props.renderItem, renderError = _this$$props.renderError, renderLabel = _this$$props.renderLabel; var renderItemCalc = this.$slots.renderItem || renderItem; var renderErrorCalc = this.$slots.renderError || renderError; var renderLabelCalc = this.$slots.renderLabel || renderLabel; var isLastBucket = this.$data.isLastBucket; var renderNoResults = this.$slots.renderNoResults || this.$props.renderNoResults; var selectAll = []; if (renderErrorCalc && this.error) { return index.isFunction(renderErrorCalc) ? renderErrorCalc(this.error) : renderErrorCalc; } if (!this.hasCustomRenderer && this.$data.modifiedOptions.length === 0 && !this.isLoading) { if (renderNoResults && index.isFunction(renderNoResults)) { return vue.createVNode("div", null, [renderNoResults()]); } if (renderNoResults && !index.isFunction(renderNoResults)) { return renderNoResults; } return null; } if (this.$props.selectAllLabel) { selectAll = [{ key: this.$props.selectAllLabel, doc_count: this.totalDocumentCount }]; } return vue.createVNode(Container.Container, { "class": this.$props.className }, { "default": function _default() { return [_this3.$props.title && vue.createVNode(Title.Title, { "class": getClassName(_this3.$props.innerClass, 'title') || '' }, { "default": function _default() { return [_this3.$props.title]; } }), vue.createVNode(DropDown.Dropdown, { "innerClass": _this3.$props.innerClass, "items": [].concat(selectAll, _this3.$data.modifiedOptions.filter(function (item) { return String(item.key).trim().length; }).map(function (item) { return _rollupPluginBabelHelpers._extends({}, item, { key: String(item.key) }); })), "hasCustomRenderer": _this3.hasCustomRenderer, "customRenderer": _this3.getComponent, "handleChange": _this3.handleChange, "selectedItem": _this3.$data.currentValue, "placeholder": _this3.$props.placeholder, "labelField": "key", "multi": true, "showCount": _this3.$props.showCount, "themePreset": _this3.themePreset, "renderItem": renderItemCalc, "renderNoResults": _this3.$slots.renderNoResults || _this3.$props.renderNoResults, "showSearch": _this3.$props.showSearch, "showClear": _this3.$props.showClear, "searchPlaceholder": _this3.$props.searchPlaceholder, "transformData": _this3.$props.transformData, "footer": showLoadMore && !isLastBucket && vue.createVNode("div", { "css": Button.loadMoreContainer }, [vue.createVNode(Button.Button, { "onClick": _this3.handleLoadMore }, _isSlot(loadMoreLabel) ? loadMoreLabel : { "default": function _default() { return [loadMoreLabel]; } })]), "customLabelRenderer": renderLabelCalc }, null)]; } }); }, methods: { handleChange: function handleChange(item) { var value = this.$props.value; if (value === undefined) { this.setValue(item); } else { var values = index.parseValueArray(this.currentValue, item); this.$emit('change', values); } }, setValue: function setValue(value, isDefaultValue, props) { var _this4 = this; if (isDefaultValue === void 0) { isDefaultValue = false; } if (props === void 0) { props = this.$props; } var selectAllLabel = this.$props.selectAllLabel; var currentValue = this.$data.currentValue; var finalValues = null; if (selectAllLabel && value.includes(selectAllLabel)) { if (currentValue[selectAllLabel]) { currentValue = {}; finalValues = []; } else { this.$data.modifiedOptions.forEach(function (item) { currentValue[item.key] = true; }); currentValue[selectAllLabel] = true; finalValues = [selectAllLabel]; } } else if (isDefaultValue) { finalValues = value; currentValue = {}; if (Array.isArray(value)) { value.forEach(function (item) { currentValue[item] = true; }); } if (selectAllLabel && selectAllLabel in currentValue) { var _currentValue = currentValue, del = _currentValue[selectAllLabel], obj = _rollupPluginBabelHelpers._objectWithoutPropertiesLoose(_currentValue, [selectAllLabel].map(_rollupPluginBabelHelpers._toPropertyKey)); currentValue = _rollupPluginBabelHelpers._extends({}, obj); } } else { if (currentValue[value]) { var _currentValue2 = currentValue, _del = _currentValue2[value], rest = _rollupPluginBabelHelpers._objectWithoutPropertiesLoose(_currentValue2, [value].map(_rollupPluginBabelHelpers._toPropertyKey)); currentValue = _rollupPluginBabelHelpers._extends({}, rest); } else { currentValue[value] = true; } if (selectAllLabel && selectAllLabel in currentValue) { var _currentValue3 = currentValue, _del2 = _currentValue3[selectAllLabel], _obj = _rollupPluginBabelHelpers._objectWithoutPropertiesLoose(_currentValue3, [selectAllLabel].map(_rollupPluginBabelHelpers._toPropertyKey)); currentValue = _rollupPluginBabelHelpers._extends({}, _obj); } finalValues = Object.keys(currentValue); } var performUpdate = function performUpdate() { _this4.currentValue = _rollupPluginBabelHelpers._extends({}, currentValue); _this4.updateQueryHandler(finalValues, props); _this4.$emit('valueChange', finalValues); _this4.$emit('value-change', finalValues); }; checkValueChange(props.componentId, finalValues, props.beforeValueChange, performUpdate); }, updateDefaultQueryHandler: function updateDefaultQueryHandler(value, props) { var query = MultiDropdownList.defaultQuery(value, props); if (this.defaultQuery) { var defaultQueryToBeSet = this.defaultQuery(value, props) || {}; var defaultQueryObj = extractQueryFromCustomQuery(defaultQueryToBeSet); if (defaultQueryObj) { query = defaultQueryObj; } // Update calculated default query in store index.updateDefaultQuery(props.componentId, this.setDefaultQuery, props, value); var defaultQueryOptions = getOptionsForCustomQuery(defaultQueryToBeSet); this.setQueryOptions(this.internalComponent, defaultQueryOptions, false); } this.updateQuery({ componentId: this.internalComponent, query: query, value: value, componentType: constants.componentTypes.multiDropdownList }); }, updateQueryHandler: function updateQueryHandler(value, props) { var customQuery = props.customQuery; var query = MultiDropdownList.defaultQuery(value, props); if (customQuery) { var customQueryCalc = customQuery(value, props); query = extractQueryFromCustomQuery(customQueryCalc); index.updateCustomQuery(props.componentId, this.setCustomQuery, props, value); var customQueryOptions = getOptionsForCustomQuery(customQueryCalc); this.setQueryOptions(props.componentId, customQueryOptions, false); } this.updateQuery({ componentId: props.componentId, query: query, value: value, label: props.filterLabel, showFilter: props.showFilter, URLParams: props.URLParams, componentType: constants.componentTypes.multiDropdownList }); }, generateQueryOptions: function generateQueryOptions(props, after) { var queryOptions = getQueryOptions(props); return props.showLoadMore ? getCompositeAggsQuery({ query: queryOptions, props: props, after: after }) : utils.getAggsQuery(queryOptions, props); }, updateQueryOptions: function updateQueryOptions(props, addAfterKey) { if (addAfterKey === void 0) { addAfterKey = false; } // when using composite aggs flush the current options for a fresh query if (props.showLoadMore && !addAfterKey) { this.modifiedOptions = []; } // for a new query due to other changes don't append after to get fresh results var queryOptions = MultiDropdownList.generateQueryOptions(props, addAfterKey ? this.$data.after : {}); if (props.defaultQuery) { var value = Object.keys(this.$data.currentValue); var defaultQueryOptions = getOptionsForCustomQuery(props.defaultQuery(value, props)); this.setQueryOptions(this.internalComponent, _rollupPluginBabelHelpers._extends({}, queryOptions, defaultQueryOptions)); } else { this.setQueryOptions(this.internalComponent, queryOptions); } }, handleLoadMore: function handleLoadMore() { this.updateQueryOptions(this.$props, true); }, getComponent: function getComponent(items, downshiftProps) { if (downshiftProps === void 0) { downshiftProps = {}; } var currentValue = this.$data.currentValue; var data = { error: this.error, loading: this.isLoading, value: currentValue, data: items || [], rawData: this.rawData, handleChange: this.handleChange, downshiftProps: downshiftProps }; return index.getComponent(data, this); } }, computed: { hasCustomRenderer: function hasCustomRenderer() { return index.hasCustomRenderer(this); } } }; MultiDropdownList.defaultQuery = function (value, props) { return { query: { queryFormat: props.queryFormat, dataField: props.dataField, value: value, nestedField: props.nestedField, selectAllLabel: props.selectAllLabel, showMissing: props.showMissing } }; }; MultiDropdownList.generateQueryOptions = function (props, after) { var queryOptions = getQueryOptions(props); return props.showLoadMore ? getCompositeAggsQuery({ query: queryOptions, props: props, after: after }) : utils.getAggsQuery(queryOptions, props); }; MultiDropdownList.hasInternalComponent = function () { return true; }; var mapStateToProps = function mapStateToProps(state, props) { return { options: props.nestedField && state.aggregations[props.componentId] ? state.aggregations[props.componentId].reactivesearch_nested : state.aggregations[props.componentId], rawData: state.rawData[props.componentId], isLoading: state.isLoading[props.componentId], selectedValue: state.selectedValues[props.componentId] && state.selectedValues[props.componentId].value || null, totalDocumentCount: state.hits[props.componentId] && state.hits[props.componentId].total, themePreset: state.config.themePreset, error: state.error[props.componentId], componentProps: state.props[props.componentId] }; }; var mapDispatchtoProps = { setQueryOptions: setQueryOptions, updateQuery: updateQuery, setCustomQuery: setCustomQuery, setDefaultQuery: setDefaultQuery }; var ListConnected = PreferencesConsumer.PreferencesConsumer(ComponentWrapper.ComponentWrapper(index.connect(mapStateToProps, mapDispatchtoProps)(MultiDropdownList), { componentType: constants.componentTypes.multiDropdownList, internalComponent: MultiDropdownList.hasInternalComponent() })); ListConnected.name = MultiDropdownList.name; // Add componentType for SSR ListConnected.componentType = constants.componentTypes.multiDropdownList; ListConnected.defaultQuery = MultiDropdownList.defaultQuery; ListConnected.generateQueryOptions = MultiDropdownList.generateQueryOptions; ListConnected.hasInternalComponent = MultiDropdownList.hasInternalComponent; ListConnected.install = function (Vue) { Vue.component(ListConnected.name, ListConnected); }; exports.ListConnected = ListConnected; exports.default = ListConnected;