UNPKG

@appbaseio/reactivesearch-vue

Version:

A Vue UI components library for building search experiences

425 lines (418 loc) 17.6 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'); var _rollupPluginBabelHelpers = require('./_rollupPluginBabelHelpers-1a877b17.js'); var vue = require('vue'); var VueTypes = _interopDefault(require('vue-types')); require('@appbaseio/vue-emotion'); require('@emotion/css'); require('polished'); 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'); var Input = require('./Input-55fbd8e1.js'); var Container = require('./Container-1c05785a.js'); var suggestions = require('@appbaseio/reactivecore/lib/utils/suggestions'); var FormControlList = require('./FormControlList-2c10e01a.js'); var utils = require('./utils-a3db8765.js'); var updateQuery = reactivecore.Actions.updateQuery, setQueryOptions = reactivecore.Actions.setQueryOptions, setCustomQuery = reactivecore.Actions.setCustomQuery, setDefaultQuery = reactivecore.Actions.setDefaultQuery; var getQueryOptions = reactivecore.helper.getQueryOptions, checkValueChange = reactivecore.helper.checkValueChange, getClassName = reactivecore.helper.getClassName, isEqual = reactivecore.helper.isEqual, extractQueryFromCustomQuery = reactivecore.helper.extractQueryFromCustomQuery, getOptionsForCustomQuery = reactivecore.helper.getOptionsForCustomQuery; var SingleList = { name: 'SingleList', 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.string, value: vueTypes.types.value, defaultQuery: vueTypes.types.func, filterLabel: vueTypes.types.string, innerClass: vueTypes.types.style, placeholder: VueTypes.string.def('Search'), react: vueTypes.types.react, render: vueTypes.types.func, renderItem: vueTypes.types.func, renderNoResults: VueTypes.any, transformData: vueTypes.types.func, selectAllLabel: vueTypes.types.string, showCount: VueTypes.bool.def(true), showFilter: VueTypes.bool.def(true), showRadio: VueTypes.bool.def(true), showSearch: 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'), nestedField: vueTypes.types.string, index: VueTypes.string, enableStrictSelection: VueTypes.bool.def(false), endpoint: vueTypes.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 index.updateCustomQuery(this.componentId, this.setCustomQuery, this.$props, this.currentValue); index.updateDefaultQuery(this.componentId, this.setDefaultQuery, this.$props, this.currentValue); this.updateQueryHandlerOptions(this.$props); if (this.selectedValue) { this.setValue(this.selectedValue); } else if (this.$props.value) { this.setValue(this.$props.value); } else if (this.$props.defaultValue) { this.setValue(this.$props.defaultValue); } }, watch: { selectedValue: function selectedValue(newVal) { if (this.$data.currentValue !== newVal) { this.setValue(newVal || ''); } }, 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); }, defaultValue: function defaultValue(newVal) { this.setValue(newVal); }, value: function value(newVal, oldVal) { if (!isEqual(newVal, oldVal)) { this.setValue(newVal); } }, 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 _this = 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 index.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 (_this.$props.showSearch && _this.$data.searchTerm) { return suggestions.replaceDiacritics(String(item.key)).toLowerCase().includes(suggestions.replaceDiacritics(_this.$data.searchTerm.toLowerCase())); } return true; } return false; }); return vue.createVNode(Container.Container, { "class": this.$props.className }, { "default": function _default() { return [_this.$props.title && vue.createVNode(Title.Title, { "class": getClassName(_this.$props.innerClass, 'title') || '' }, { "default": function _default() { return [_this.$props.title]; } }), _this.renderSearch(), _this.hasCustomRenderer ? _this.getComponent() : vue.createVNode(FormControlList.UL, { "class": getClassName(_this.$props.innerClass, 'list') || '' }, { "default": function _default() { return [selectAllLabel ? vue.createVNode("li", { "key": selectAllLabel, "class": "" + (_this.$data.currentValue === selectAllLabel ? 'active' : '') }, [vue.createVNode(FormControlList.Radio, { "class": getClassName(_this.$props.innerClass, 'radio'), "id": _this.$props.componentId + "-" + selectAllLabel, "name": _this.$props.componentId, "value": selectAllLabel, "onClick": _this.handleClick, "readOnly": true, "type": "radio", "show": _this.$props.showRadio, "checked": _this.$data.currentValue === selectAllLabel }, null), vue.createVNode("label", { "class": getClassName(_this.$props.innerClass, 'label') || null, "for": _this.$props.componentId + "-" + selectAllLabel }, [vue.createVNode("span", null, [selectAllLabel, _this.$props.showCount && vue.createVNode("span", { "class": getClassName(_this.$props.innerClass, 'count') || null }, [vue.createTextVNode("\xA0("), _this.totalDocumentCount, vue.createTextVNode(")")])])])]) : null, !_this.hasCustomRenderer && filteredItemsToRender.length === 0 && !_this.isLoading ? _this.renderNoResult() : filteredItemsToRender.map(function (item) { return vue.createVNode("li", { "key": item.key, "class": "" + (_this.currentValue === String(item.key) ? 'active' : '') }, [vue.createVNode(FormControlList.Radio, { "class": getClassName(_this.$props.innerClass, 'radio'), "id": _this.$props.componentId + "-" + item.key, "name": _this.$props.componentId, "value": item.key, "readOnly": true, "onClick": _this.handleClick, "type": "radio", "show": _this.$props.showRadio, "checked": _this.currentValue === String(item.key) }, null), vue.createVNode("label", { "class": getClassName(_this.$props.innerClass, 'label') || null, "for": _this.$props.componentId + "-" + item.key }, [renderItemCalc ? renderItemCalc({ label: item.key, count: item.doc_count, isChecked: _this.currentValue === String(item.key) }) : vue.createVNode("span", null, [item.key, _this.$props.showCount && vue.createVNode("span", { "class": getClassName(_this.$props.innerClass, 'count') || null }, [vue.createTextVNode("\xA0("), item.doc_count, vue.createTextVNode(")")])])])]); })]; } })]; } }); }, methods: { setValue: function setValue(nextValue, props) { var _this2 = this; if (props === void 0) { props = this.$props; } var value = nextValue; if (nextValue === this.$data.currentValue) { value = ''; } var performUpdate = function performUpdate() { _this2.currentValue = value; _this2.updateQueryHandler(value, props); _this2.$emit('valueChange', value); _this2.$emit('value-change', value); }; checkValueChange(props.componentId, value, props.beforeValueChange, performUpdate); }, updateDefaultQueryHandler: function updateDefaultQueryHandler(value, props) { var query = SingleList.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.singleList }); }, updateQueryHandler: function updateQueryHandler(value, props) { var customQuery = props.customQuery; var query = SingleList.defaultQuery(value, props); if (customQuery) { var customQueryCalc = customQuery(value, props); query = extractQueryFromCustomQuery(customQueryCalc); var customQueryOptions = getOptionsForCustomQuery(customQueryCalc); index.updateCustomQuery(props.componentId, this.setCustomQuery, props, value); 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.singleList }); }, generateQueryOptions: function generateQueryOptions(props) { var queryOptions = getQueryOptions(props); return utils.getAggsQuery(queryOptions, props); }, updateQueryHandlerOptions: function updateQueryHandlerOptions(props) { var queryOptions = SingleList.generateQueryOptions(props); if (props.defaultQuery) { var value = this.$data.currentValue; var defaultQueryOptions = getOptionsForCustomQuery(props.defaultQuery(value, props)); this.setQueryOptions(this.internalComponent, _rollupPluginBabelHelpers._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 vue.createVNode(Input.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; }, getComponent: function getComponent() { 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 index.getComponent(data, this); }, handleClick: function handleClick(e) { var currentValue = e; if (index.isEvent(e)) { currentValue = e.target.value; } if (this.enableStrictSelection && currentValue === this.currentValue) { return false; } var value = this.$props.value; if (value === undefined) { this.setValue(currentValue); } else { this.$emit('change', currentValue); } return true; }, renderNoResult: function renderNoResult() { var renderNoResults = this.$slots.renderNoResults || this.$props.renderNoResults; return vue.createVNode("p", { "class": getClassName(this.$props.innerClass, 'noResults') || null }, [index.isFunction(renderNoResults) ? renderNoResults() : renderNoResults]); } }, computed: { hasCustomRenderer: function hasCustomRenderer() { return index.hasCustomRenderer(this); } } }; SingleList.generateQueryOptions = function (props) { var queryOptions = getQueryOptions(props); return utils.getAggsQuery(queryOptions, props); }; SingleList.defaultQuery = function (value, props) { return { query: { queryFormat: props.queryFormat, dataField: props.dataField, value: value, nestedField: props.nestedField, selectAllLabel: props.selectAllLabel, showMissing: props.showMissing } }; }; SingleList.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 || '', 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)(SingleList), { componentType: constants.componentTypes.singleList, internalComponent: SingleList.hasInternalComponent() })); ListConnected.name = SingleList.name; ListConnected.defaultQuery = SingleList.defaultQuery; ListConnected.generateQueryOptions = SingleList.generateQueryOptions; ListConnected.hasInternalComponent = SingleList.hasInternalComponent; // Add componentType for SSR ListConnected.componentType = constants.componentTypes.singleList; ListConnected.install = function (Vue) { Vue.component(ListConnected.name, ListConnected); }; exports.ListConnected = ListConnected; exports.default = ListConnected;