UNPKG

@appbaseio/reactivesearch-vue

Version:

A Vue UI components library for building search experiences

478 lines (474 loc) 19.6 kB
import { Actions, helper } from '@appbaseio/reactivecore'; import { componentTypes } from '@appbaseio/reactivecore/lib/utils/constants'; import { b as _objectWithoutPropertiesLoose, c as _toPropertyKey, a as _extends } from './_rollupPluginBabelHelpers-5e8399d7.js'; import { createVNode, createTextVNode } from 'vue'; import VueTypes from 'vue-types'; import '@appbaseio/vue-emotion'; import '@emotion/css'; import 'polished'; import { t as types } from './vueTypes-5d575822.js'; import '@appbaseio/reactivecore/lib/utils/transform'; import 'redux'; import { j as updateCustomQuery, u as updateDefaultQuery, i as isQueryIdentical, a as isFunction, m as isEvent, n as parseValueArray, g as getComponent, h as hasCustomRenderer, c as connect } from './index-3af85a74.js'; import { C as ComponentWrapper } from './ComponentWrapper-90d42a29.js'; import { P as PreferencesConsumer } from './PreferencesConsumer-a2bd59db.js'; import { T as Title } from './Title-100fe896.js'; import { I as Input } from './Input-cda7f4ad.js'; import { C as Container } from './Container-e2261542.js'; import { replaceDiacritics } from '@appbaseio/reactivecore/lib/utils/suggestions'; import { U as UL, C as Checkbox } from './FormControlList-968ff972.js'; import { g as getAggsQuery } from './utils-badbb2a8.js'; var updateQuery = Actions.updateQuery, setQueryOptions = Actions.setQueryOptions, setCustomQuery = Actions.setCustomQuery, setDefaultQuery = Actions.setDefaultQuery; var isEqual = helper.isEqual, getQueryOptions = helper.getQueryOptions, checkValueChange = helper.checkValueChange, getClassName = helper.getClassName, extractQueryFromCustomQuery = helper.extractQueryFromCustomQuery, getOptionsForCustomQuery = helper.getOptionsForCustomQuery; var MultiList = { name: 'MultiList', props: { defaultValue: types.stringArray, value: types.stringArray, queryFormat: VueTypes.oneOf(['and', 'or']).def('or'), showCheckbox: VueTypes.bool.def(true), beforeValueChange: types.func, className: VueTypes.string.def(''), componentId: types.stringRequired, compoundClause: types.compoundClause, customQuery: types.func, dataField: types.stringRequired, defaultQuery: types.func, filterLabel: types.string, innerClass: types.style, placeholder: VueTypes.string.def('Search'), react: types.react, render: types.func, renderItem: types.func, renderError: types.title, renderNoResults: VueTypes.any, transformData: types.func, selectAllLabel: types.string, showCount: VueTypes.bool.def(true), showFilter: VueTypes.bool.def(true), showSearch: VueTypes.bool.def(true), size: VueTypes.number, sortBy: VueTypes.oneOf(['asc', 'desc', 'count']).def('count'), title: types.title, URLParams: VueTypes.bool.def(false), showMissing: VueTypes.bool.def(false), missingLabel: VueTypes.string.def('N/A'), nestedField: types.string, index: VueTypes.string, endpoint: types.endpointConfig }, data: function data() { var props = this.$props; this.__state = { currentValue: {}, modifiedOptions: [], searchTerm: '' }; this.internalComponent = props.componentId + "__internal"; return this.__state; }, 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 updateCustomQuery(this.componentId, this.setCustomQuery, this.$props, this.currentValue); updateDefaultQuery(this.componentId, this.setDefaultQuery, this.$props, this.currentValue); this.updateQueryHandlerOptions(this.$props); var value = this.selectedValue || this.$props.value || this.$props.defaultValue; this.setValue(value, !this.selectedValue); }, mounted: function mounted() { var currentValue = Object.keys(this.$data.currentValue); if (this.$props.value !== undefined && !isEqual(this.$props.value, currentValue)) { this.$emit('change', currentValue); } }, watch: { options: function options(newVal) { if (newVal) { this.modifiedOptions = newVal[this.$props.dataField] ? newVal[this.$props.dataField].buckets : []; } }, size: function size() { this.updateQueryHandlerOptions(this.$props); }, sortBy: function sortBy() { this.updateQueryHandlerOptions(this.$props); }, dataField: function dataField() { this.updateQueryHandlerOptions(this.$props); this.updateQueryHandler(this.$data.currentValue, this.$props); }, value: function value(newVal, oldVal) { if (!isEqual(oldVal, newVal)) { this.setValue(newVal, true); } }, defaultValue: function defaultValue(newVal, oldVal) { if (!isEqual(oldVal, newVal)) { this.setValue(newVal, true); } }, 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)) { if (this.value === undefined) { this.setValue(newVal, true); } else { this.$emit('change', newVal); } } }, defaultQuery: function defaultQuery(newVal, oldVal) { if (!isQueryIdentical(newVal, oldVal, this.$data.currentValue, this.$props)) { this.updateDefaultQueryHandler(this.$data.currentValue, this.$props); } }, customQuery: function customQuery(newVal, oldVal) { if (!isQueryIdentical(newVal, oldVal, this.$data.currentValue, this.$props)) { this.updateQueryHandler(this.componentId, this.$data.currentValue, this.$props); } } }, render: function render() { var _this2 = this; var _this$$props = this.$props, selectAllLabel = _this$$props.selectAllLabel, renderItem = _this$$props.renderItem, renderError = _this$$props.renderError; var renderItemCalc = this.$slots.renderItem || renderItem; var renderErrorCalc = this.$slots.renderError || renderError; if (renderErrorCalc && this.error) { return isFunction(renderErrorCalc) ? renderErrorCalc(this.error) : renderErrorCalc; } if (!this.hasCustomRenderer && this.modifiedOptions.length === 0 && !this.isLoading) { if (this.renderNoResults) { this.renderNoResult(); } else { return null; } } var itemsToRender = this.$data.modifiedOptions; if (this.$props.transformData) { itemsToRender = this.$props.transformData(itemsToRender); } var filteredItemsToRender = itemsToRender.filter(function (item) { if (String(item.key).length) { if (_this2.$props.showSearch && _this2.$data.searchTerm) { return replaceDiacritics(String(item.key)).toLowerCase().includes(replaceDiacritics(_this2.$data.searchTerm).toLowerCase()); } return true; } return false; }); return createVNode(Container, { "class": this.$props.className }, { "default": function _default() { return [_this2.$props.title && createVNode(Title, { "class": getClassName(_this2.$props.innerClass, 'title') }, { "default": function _default() { return [_this2.$props.title]; } }), _this2.renderSearch(), _this2.hasCustomRenderer ? _this2.getComponent() : createVNode(UL, null, { "default": function _default() { return [selectAllLabel ? createVNode("li", { "key": selectAllLabel, "class": "" + (_this2.currentValue[selectAllLabel] ? 'active' : '') }, [createVNode(Checkbox, { "type": "checkbox", "class": getClassName(_this2.$props.innerClass, 'checkbox'), "id": _this2.$props.componentId + "-" + selectAllLabel, "name": selectAllLabel, "value": selectAllLabel, "onClick": _this2.handleClick, "checked": !!_this2.currentValue[selectAllLabel], "show": _this2.$props.showCheckbox }, null), createVNode("label", { "class": getClassName(_this2.$props.innerClass, 'label'), "for": _this2.$props.componentId + "-" + selectAllLabel }, [createVNode("span", null, [selectAllLabel, _this2.$props.showCount && createVNode("span", { "class": getClassName(_this2.$props.innerClass, 'count') }, [createTextVNode("\xA0("), _this2.totalDocumentCount, createTextVNode(")")])])])]) : null, !_this2.hasCustomRenderer && filteredItemsToRender.length === 0 && !_this2.isLoading ? _this2.renderNoResult() : filteredItemsToRender.map(function (item) { return createVNode("li", { "key": item.key, "class": "" + (_this2.$data.currentValue[item.key] ? 'active' : '') }, [createVNode(Checkbox, { "type": "checkbox", "class": getClassName(_this2.$props.innerClass, 'checkbox'), "id": _this2.$props.componentId + "-" + item.key, "name": _this2.$props.componentId, "value": item.key, "onClick": _this2.handleClick, "show": _this2.$props.showCheckbox, "checked": !!_this2.$data.currentValue[item.key] }, null), createVNode("label", { "class": getClassName(_this2.$props.innerClass, 'label'), "for": _this2.$props.componentId + "-" + item.key }, [renderItemCalc ? renderItemCalc({ label: item.key, count: item.doc_count, isChecked: !!_this2.$data.currentValue[item.key] }) : createVNode("span", null, [item.key, _this2.$props.showCount && createVNode("span", { "class": getClassName(_this2.$props.innerClass, 'count') }, [createTextVNode("\xA0("), item.doc_count, createTextVNode(")")])])])]); })]; } })]; } }); }, methods: { setValue: function setValue(value, isDefaultValue, props) { var _this3 = 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 && (Array.isArray(value) && value.includes(selectAllLabel) || typeof value === 'string' && value === 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 (value && value.length) { value.forEach(function (item) { currentValue[item] = true; }); } if (selectAllLabel && selectAllLabel in currentValue) { var _currentValue = currentValue, del = _currentValue[selectAllLabel], obj = _objectWithoutPropertiesLoose(_currentValue, [selectAllLabel].map(_toPropertyKey)); currentValue = _extends({}, obj); } } else { if (currentValue[value]) { var _currentValue2 = currentValue, _del = _currentValue2[value], rest = _objectWithoutPropertiesLoose(_currentValue2, [value].map(_toPropertyKey)); currentValue = _extends({}, rest); } else if (Array.isArray(value)) { value.forEach(function (val) { currentValue[val] = true; }); } else { currentValue[value] = true; } if (selectAllLabel && selectAllLabel in currentValue) { var _currentValue3 = currentValue, _del2 = _currentValue3[selectAllLabel], _obj = _objectWithoutPropertiesLoose(_currentValue3, [selectAllLabel].map(_toPropertyKey)); currentValue = _extends({}, _obj); } finalValues = Object.keys(currentValue); } var performUpdate = function performUpdate() { _this3.currentValue = Object.assign({}, currentValue); _this3.updateQueryHandler(finalValues, props); _this3.$emit('valueChange', finalValues); _this3.$emit('value-change', finalValues); }; checkValueChange(props.componentId, finalValues, props.beforeValueChange, performUpdate); }, updateDefaultQueryHandler: function updateDefaultQueryHandler(value, props) { var query = MultiList.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 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: componentTypes.multiList }); }, updateQueryHandler: function updateQueryHandler(value, props) { var customQuery = props.customQuery; var query = MultiList.defaultQuery(value, props); if (customQuery) { var customQueryCalc = customQuery(value, props); query = extractQueryFromCustomQuery(customQueryCalc); 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: componentTypes.multiList }); }, generateQueryOptions: function generateQueryOptions(props) { var queryOptions = getQueryOptions(props); return getAggsQuery(queryOptions, props); }, updateQueryHandlerOptions: function updateQueryHandlerOptions(props) { var queryOptions = MultiList.generateQueryOptions(props); if (props.defaultQuery) { var value = Object.keys(this.$data.currentValue); var defaultQueryOptions = getOptionsForCustomQuery(props.defaultQuery(value, props)); this.setQueryOptions(this.internalComponent, _extends({}, queryOptions, defaultQueryOptions)); } else { this.setQueryOptions(this.internalComponent, queryOptions); } }, handleInputChange: function handleInputChange(e) { var value = e.target.value; this.searchTerm = value; }, renderSearch: function renderSearch() { if (this.$props.showSearch) { return createVNode(Input, { "class": getClassName(this.$props.innerClass, 'input') || '', "onInput": this.handleInputChange, "value": this.$data.searchTerm, "placeholder": this.$props.placeholder, "style": { margin: '0 0 8px' }, "themePreset": this.$props.themePreset }, null); } return null; }, handleClick: function handleClick(e) { var currentValue = e; if (isEvent(e)) { currentValue = e.target.value; } var value = this.$props.value; if (value === undefined) { this.setValue(currentValue); } else { var values = parseValueArray(value || [], currentValue); this.$emit('change', values); } }, getComponent: function getComponent$1() { var _this$$data = this.$data, currentValue = _this$$data.currentValue, modifiedOptions = _this$$data.modifiedOptions; var transformData = this.$props.transformData; var itemsToRender = modifiedOptions; if (transformData) { itemsToRender = transformData(itemsToRender); } var data = { error: this.error, loading: this.isLoading, value: currentValue, data: itemsToRender, rawData: this.rawData, handleChange: this.handleClick }; return getComponent(data, this); }, renderNoResult: function renderNoResult() { var renderNoResults = this.$slots.renderNoResults || this.$props.renderNoResults; return createVNode("p", { "class": getClassName(this.$props.innerClass, 'noResults') || null }, [isFunction(renderNoResults) ? renderNoResults() : renderNoResults]); } }, computed: { hasCustomRenderer: function hasCustomRenderer$1() { return hasCustomRenderer(this); } } }; MultiList.defaultQuery = function (value, props) { return { query: { queryFormat: props.queryFormat, dataField: props.dataField, value: value, nestedField: props.nestedField, selectAllLabel: props.selectAllLabel, showMissing: props.showMissing } }; }; MultiList.generateQueryOptions = function (props) { var queryOptions = getQueryOptions(props); return getAggsQuery(queryOptions, props); }; 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, themePreset: state.config.themePreset, totalDocumentCount: state.hits[props.componentId] && state.hits[props.componentId].total, error: state.error[props.componentId], componentProps: state.props[props.componentId] }; }; var mapDispatchtoProps = { setQueryOptions: setQueryOptions, updateQuery: updateQuery, setCustomQuery: setCustomQuery, setDefaultQuery: setDefaultQuery }; MultiList.hasInternalComponent = function () { return true; }; var ListConnected = PreferencesConsumer(ComponentWrapper(connect(mapStateToProps, mapDispatchtoProps)(MultiList), { componentType: componentTypes.multiList, internalComponent: MultiList.hasInternalComponent() })); ListConnected.name = MultiList.name; ListConnected.defaultQuery = MultiList.defaultQuery; ListConnected.generateQueryOptions = MultiList.generateQueryOptions; ListConnected.hasInternalComponent = MultiList.hasInternalComponent; // Add componentType for SSR ListConnected.componentType = componentTypes.multiList; ListConnected.install = function (Vue) { Vue.component(ListConnected.name, ListConnected); }; export default ListConnected; export { ListConnected };