UNPKG

@appbaseio/reactivesearch-vue

Version:

A Vue UI components library for building search experiences

426 lines (417 loc) 16.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'); require('./_rollupPluginBabelHelpers-1a877b17.js'); var vue = require('vue'); var VueTypes = _interopDefault(require('vue-types')); require('@appbaseio/vue-emotion'); var vueTypes = require('./vueTypes-adf43075.js'); require('redux'); var index = require('./index-7ca9570e.js'); var PreferencesConsumer = require('./PreferencesConsumer-37cab7a0.js'); var Title = require('./Title-d513ac26.js'); var Container = require('./Container-1c05785a.js'); var VueSlider = _interopDefault(require('vue-slider-component/dist-css/vue-slider-component.umd.min.js')); require('vue-slider-component/dist-css/vue-slider-component.css'); require('vue-slider-component/theme/default.css'); var ssr = require('./ssr-15d936a0.js'); var addComponent = reactivecore.Actions.addComponent, removeComponent = reactivecore.Actions.removeComponent, watchComponent = reactivecore.Actions.watchComponent, updateQuery = reactivecore.Actions.updateQuery, setQueryListener = reactivecore.Actions.setQueryListener, setQueryOptions = reactivecore.Actions.setQueryOptions, setComponentProps = reactivecore.Actions.setComponentProps, setCustomQuery = reactivecore.Actions.setCustomQuery, updateComponentProps = reactivecore.Actions.updateComponentProps; var checkValueChange = reactivecore.helper.checkValueChange, getClassName = reactivecore.helper.getClassName, isEqual = reactivecore.helper.isEqual, checkSomePropChange = reactivecore.helper.checkSomePropChange, extractQueryFromCustomQuery = reactivecore.helper.extractQueryFromCustomQuery, getOptionsForCustomQuery = reactivecore.helper.getOptionsForCustomQuery; var DynamicRangeSlider = { name: 'DynamicRangeSlider', components: ssr.getComponents(), props: { beforeValueChange: vueTypes.types.func, className: VueTypes.string.def(''), rangeLabels: vueTypes.types.func, componentId: vueTypes.types.stringRequired, compoundClause: vueTypes.types.compoundClause, customQuery: vueTypes.types.func, data: vueTypes.types.data, dataField: vueTypes.types.stringRequired, defaultValue: vueTypes.types.func, filterLabel: vueTypes.types.string, innerClass: vueTypes.types.style, react: vueTypes.types.react, showFilter: VueTypes.bool.def(true), destroyOnUnmount: VueTypes.bool, showCheckbox: VueTypes.bool.def(true), title: vueTypes.types.title, URLParams: VueTypes.bool.def(false), sliderOptions: VueTypes.object.def({}), nestedField: vueTypes.types.string, index: VueTypes.string, value: vueTypes.types.range, endpoint: vueTypes.types.endpointConfig }, data: function data() { this.internalRangeComponent = this.$props.componentId + "__range__internal"; return { currentValue: null, stats: [] }; }, created: function created() { var _this = this; this.$timestamp = new Date().getTime(); var components = []; if (this.$$store) { var _this$$$store$getStat = this.$$store.getState(); components = _this$$$store$getStat.components; } var value = this.$props.value; if (this.destroyOnUnmount || components.indexOf(this.componentId) === -1) { this.addComponent(this.componentId, this.$timestamp); this.addComponent(this.internalRangeComponent, this.$timestamp); if (Array.isArray(this.selectedValue)) { this.handleChange(this.selectedValue); } else if (this.selectedValue) { this.handleChange(DynamicRangeSlider.parseValue(this.selectedValue, this.$props)); } else if (value) { this.handleChange(DynamicRangeSlider.parseValue(value, this.$props)); } // get range before executing other queries this.updateRangeQueryOptions(); } var onQueryChange = function onQueryChange() { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this.$emit.apply(_this, ['queryChange'].concat(args)); _this.$emit.apply(_this, ['query-change'].concat(args)); }; this.setQueryListener(this.$props.componentId, onQueryChange, null); // Update props in store this.setComponentProps(this.componentId, this.$props, constants.componentTypes.dynamicRangeSlider); this.setComponentProps(this.internalRangeComponent, this.$props, constants.componentTypes.dynamicRangeSlider); // Set custom query in store index.updateCustomQuery(this.componentId, this.setCustomQuery, this.$props, this.currentValue); this.setReact(false); }, mounted: function mounted() { this.setReact(true); }, beforeUpdate: function beforeUpdate() { if (!this.currentValue) { this.setDefaultValue(this.range); } }, beforeUnmount: function beforeUnmount() { if (this.destroyOnUnmount) { this.removeComponent(this.$props.componentId); this.removeComponent(this.internalRangeComponent); } }, methods: { isControlled: function isControlled() { if (this.$props.value && this.$attrs) { return true; } return false; }, setDefaultValue: function setDefaultValue(_ref) { var start = _ref.start, end = _ref.end; if (this.$props.defaultValue) { var _this$$props$defaultV = this.$props.defaultValue(start, end), defaultStart = _this$$props$defaultV.start, defaultEnd = _this$$props$defaultV.end; this.handleChange([defaultStart, defaultEnd]); } else if (this.isControlled()) { this.handleChange(DynamicRangeSlider.parseValue(this.$props.value), 'change'); } else { this.currentValue = [start, end]; } }, setReact: function setReact(shouldExecute) { if (this.$props.react) { this.watchComponent(this.internalRangeComponent, this.$props.react, shouldExecute); this.watchComponent(this.$props.componentId, this.$props.react, shouldExecute); } else { this.watchComponent(this.internalRangeComponent, {}, shouldExecute); this.watchComponent(this.$props.componentId, {}, shouldExecute); } }, rangeQuery: function rangeQuery() { return { min: { min: { field: this.$props.dataField } }, max: { max: { field: this.$props.dataField } } }; }, updateRangeQueryOptions: function updateRangeQueryOptions() { var aggs = {}; if (this.$props.nestedField) { var _aggs; aggs = (_aggs = {}, _aggs[this.$props.nestedField] = { nested: { path: this.$props.nestedField }, aggs: this.rangeQuery() }, _aggs); } else { aggs = this.rangeQuery(); } this.setQueryOptions(this.internalRangeComponent, { aggs: aggs }); }, handleSlider: function handleSlider() { var sliderValues = this.$refs.slider.getValue(); var value = this.$props.value; if (value === undefined) { this.handleChange(sliderValues); } else { this.$emit('change', { start: sliderValues[0], end: sliderValues[1] }); } }, handleChange: function handleChange(currentValue) { var _this2 = this; // Always keep the values within range var normalizedValue = [this.range ? Math.max(this.range.start, currentValue[0]) : currentValue[0], this.range ? Math.min(this.range.end, currentValue[1]) : currentValue[1]]; var performUpdate = function performUpdate() { _this2.currentValue = normalizedValue; _this2.updateQueryHandler(normalizedValue, _this2.$props); _this2.$emit('valueChange', { start: normalizedValue[0], end: normalizedValue[1] }); _this2.$emit('value-change', { start: normalizedValue[0], end: normalizedValue[1] }); }; checkValueChange(this.$props.componentId, { start: normalizedValue[0], end: normalizedValue[1] }, this.$props.beforeValueChange, performUpdate); }, updateQueryHandler: function updateQueryHandler(value) { var query = DynamicRangeSlider.defaultQuery(value, this.$props); if (this.$props.customQuery) { var customQueryTobeSet = this.$props.customQuery(value, this.$props); var queryTobeSet = extractQueryFromCustomQuery(customQueryTobeSet); if (queryTobeSet) { query = queryTobeSet; } var customQueryOptions = getOptionsForCustomQuery(customQueryTobeSet); index.updateCustomQuery(this.componentId, this.setCustomQuery, this.$props, value); this.setQueryOptions(this.$props.componentId, customQueryOptions, false); } var _ref2 = this.range || { start: value[0], end: value[1] }, start = _ref2.start, end = _ref2.end; var currentStart = value[0], currentEnd = value[1]; // check if the slider is at its initial position var isInitialValue = currentStart === start && currentEnd === end; this.updateQuery({ componentId: this.$props.componentId, query: query, value: value, label: this.$props.filterLabel, showFilter: this.$props.showFilter && !isInitialValue, URLParams: this.$props.URLParams, componentType: constants.componentTypes.dynamicRangeSlider }); } }, computed: { labels: function labels() { if (!this.rangeLabels) return null; return this.rangeLabels(this.range.start, this.range.end); } }, watch: { $props: { deep: true, handler: function handler(newVal) { var _this3 = this; var propsKeys = index.getValidPropsKeys(newVal); checkSomePropChange(newVal, this.componentProps, propsKeys, function () { _this3.updateComponentProps(_this3.componentId, newVal, constants.componentTypes.dynamicRangeSlider); _this3.updateComponentProps(_this3.internalRangeComponent, newVal, constants.componentTypes.dynamicRangeSlider); }); } }, react: function react() { this.setReact(); }, selectedValue: function selectedValue(newValue) { if (isEqual(newValue, this.currentValue)) return; var value = newValue || { start: this.range.start, end: this.range.end }; this.$emit('change', value); this.handleChange(DynamicRangeSlider.parseValue(value, this.$props)); }, range: function range(newValue, oldValue) { if (isEqual(newValue, oldValue) || !this.currentValue) return; var _ref3 = this.currentValue || [], currentStart = _ref3[0], currentEnd = _ref3[1]; var _ref4 = oldValue || {}, oldStart = _ref4.start, oldEnd = _ref4.end; var newStart = currentStart === oldStart ? newValue.start : currentStart; var newEnd = currentEnd === oldEnd ? newValue.end : currentEnd; this.handleChange([newStart, newEnd]); }, customQuery: function customQuery(newVal, oldVal) { if (!index.isQueryIdentical(newVal, oldVal, this.$data.currentValue, this.$props)) { this.updateQueryHandler(this.$data.currentValue); } }, value: function value(newVal, oldVal) { if (!isEqual(newVal, oldVal)) { this.handleChange(DynamicRangeSlider.parseValue(newVal, this.$props)); } } }, render: function render() { var _this4 = this; if (!this.range || !this.currentValue || this.range.start === null || this.range.end === null || this.range.start === this.range.end) { return null; } var _this$range = this.range, start = _this$range.start, end = _this$range.end; return vue.createVNode(Container.Container, { "class": this.$props.className }, { "default": function _default() { return [_this4.$props.title && vue.createVNode(Title.Title, { "class": getClassName(_this4.$props.innerClass, 'title') }, { "default": function _default() { return [_this4.$props.title]; } }), vue.createVNode(ssr.NoSSR, null, { "default": function _default() { return [vue.createVNode(ssr.Slider, { "class": getClassName(_this4.$props.innerClass, 'slider') }, { "default": function _default() { return [vue.createVNode(VueSlider, vue.mergeProps({ "ref": "slider", "modelValue": [Math.floor(Math.max(start, _this4.currentValue[0])), Math.ceil(Math.min(end, _this4.currentValue[1]))], "min": Math.floor(Math.min(start, _this4.currentValue[0])), "max": Math.ceil(Math.max(end, _this4.currentValue[1])), "onChange": _this4.handleSlider, "lazy": true, "dotSize": 20, "height": 4, "enable-cross": false, "tooltip": "always", "useKeyboard": false }, _this4.$props.sliderOptions), null), _this4.labels ? vue.createVNode("div", { "class": "label-container" }, [vue.createVNode("label", { "class": getClassName(_this4.$props.innerClass, 'label') || 'range-label-left' }, [_this4.labels.start]), vue.createVNode("label", { "class": getClassName(_this4.$props.innerClass, 'label') || 'range-label-right' }, [_this4.labels.end])]) : null]; } })]; } })]; } }); } }; DynamicRangeSlider.defaultQuery = function (value, props) { return { query: { queryFormat: props.queryFormat, dataField: props.dataField, value: value, showMissing: props.showMissing } }; }; DynamicRangeSlider.parseValue = function (value) { if (value) { return Array.isArray(value) ? value : [value.start, value.end]; } return []; }; DynamicRangeSlider.hasInternalComponent = function () { return true; }; var mapStateToProps = function mapStateToProps(state, props) { var componentId = state.aggregations[props.componentId]; var internalRange = state.aggregations[props.componentId + "__range__internal"]; var options = componentId && componentId[props.dataField]; var range = state.aggregations[props.componentId + "__range__internal"]; if (props.nestedField) { options = options && componentId[props.dataField][props.nestedField] && componentId[props.dataField][props.nestedField].buckets ? componentId[props.dataField][props.nestedField].buckets : []; range = range && internalRange[props.nestedField].min ? { start: internalRange[props.nestedField].min.value, end: internalRange[props.nestedField].max.value } : null; } else { options = options && componentId[props.dataField].buckets ? componentId[props.dataField].buckets : []; range = range && internalRange.min ? { start: internalRange.min.value, end: internalRange.max.value } : null; } return { options: options, range: range, selectedValue: state.selectedValues[props.componentId] ? state.selectedValues[props.componentId].value : null, componentProps: state.props[props.componentId] }; }; var mapDispatchtoProps = { addComponent: addComponent, removeComponent: removeComponent, updateQuery: updateQuery, watchComponent: watchComponent, setQueryListener: setQueryListener, setQueryOptions: setQueryOptions, setComponentProps: setComponentProps, setCustomQuery: setCustomQuery, updateComponentProps: updateComponentProps }; var RangeConnected = PreferencesConsumer.PreferencesConsumer(index.connect(mapStateToProps, mapDispatchtoProps)(DynamicRangeSlider)); RangeConnected.defaultQuery = DynamicRangeSlider.defaultQuery; RangeConnected.parseValue = DynamicRangeSlider.parseValue; RangeConnected.hasInternalComponent = DynamicRangeSlider.hasInternalComponent; RangeConnected.name = DynamicRangeSlider.name; // Add componentType for SSR RangeConnected.componentType = constants.componentTypes.dynamicRangeSlider; RangeConnected.install = function (Vue) { Vue.component(RangeConnected.name, RangeConnected); }; exports.RangeConnected = RangeConnected; exports.default = RangeConnected;