UNPKG

@firestitch/filter

Version:
1,520 lines (1,515 loc) 121 kB
import { FormsModule } from '@angular/forms'; import { MatIconModule, MatInputModule, MatSelectModule, MatChipsModule, MatAutocompleteModule, MatButtonModule, MatCheckboxModule, MAT_LABEL_GLOBAL_OPTIONS } from '@angular/material'; import { FlexLayoutModule } from '@angular/flex-layout'; import { FsDatePickerModule } from '@firestitch/datepicker'; import { FsChipModule } from '@firestitch/chip'; import { FsLabelModule } from '@firestitch/label'; import { Location, CommonModule } from '@angular/common'; import { ActivatedRoute, Router, RouterModule } from '@angular/router'; import { FsStore, FsStoreModule } from '@firestitch/store'; import { Alias, Model } from 'tsmodels'; import { isObservable } from 'rxjs/internal/util/isObservable'; import { isDate, isValid, parse, format, isAfter, subMinutes } from 'date-fns'; import { Subject } from 'rxjs'; import { isEmpty, list, filter, remove, FsCommonModule } from '@firestitch/common'; import { isFunction, isObject, toString, clone, isString, cloneDeep } from 'lodash-es'; import { take, takeUntil, debounceTime, distinctUntilChanged, map } from 'rxjs/operators'; import { __extends, __assign, __spread, __decorate, __metadata, __values } from 'tslib'; import { Component, EventEmitter, Input, ViewChild, ViewEncapsulation, HostListener, ChangeDetectionStrategy, ChangeDetectorRef, IterableDiffers, Output, KeyValueDiffers, Pipe, NgModule } from '@angular/core'; import { toUTC, simpleFormat, format as format$1 } from '@firestitch/date'; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @enum {string} */ var ItemType = { Text: 'text', Select: 'select', Range: 'range', Date: 'date', DateTime: 'datetime', DateRange: 'daterange', DateTimeRange: 'datetimerange', AutoComplete: 'autocomplete', AutoCompleteChips: 'autocompletechips', Checkbox: 'checkbox', Chips: 'chips', }; var FsFilterConfigItem = /** @class */ (function (_super) { __extends(FsFilterConfigItem, _super); function FsFilterConfigItem(data, _config, _route, _persists) { if (data === void 0) { data = {}; } var _this = _super.call(this) || this; _this._config = _config; _this._route = _route; _this._persists = _persists; _this.initialLoading = false; _this.valueChanged = false; _this._pendingValues = false; _this._fromJSON(data); return _this; } Object.defineProperty(FsFilterConfigItem.prototype, "hasPendingValues", { get: /** * @return {?} */ function () { return this._pendingValues; }, enumerable: true, configurable: true }); Object.defineProperty(FsFilterConfigItem.prototype, "model", { get: /** * @return {?} */ function () { return this._model; }, set: /** * @param {?} val * @return {?} */ function (val) { this._model = val; this._tmpModel = val; this.checkIfValueChanged(); }, enumerable: true, configurable: true }); Object.defineProperty(FsFilterConfigItem.prototype, "tmpModel", { get: /** * @return {?} */ function () { return this._tmpModel; }, set: /** * @param {?} val * @return {?} */ function (val) { this._tmpModel = val; }, enumerable: true, configurable: true }); /** * @param {?} data * @return {?} */ FsFilterConfigItem.prototype._fromJSON = /** * @param {?} data * @return {?} */ function (data) { _super.prototype._fromJSON.call(this, data); if (this.name && isObject(this.name)) { this.names = this.name; this.name = Object.keys(this.names).join('-'); } if (this._config.persist) { /** @type {?} */ var persisted = this._persists[this._config.persist.name].data; if (persisted[this.name]) { this.parseAndSetValue(persisted[this.name]); } } if (isFunction(data.values) && this.type !== ItemType.AutoComplete && this.type !== ItemType.AutoCompleteChips) { this.values = data.values(); if (isObservable(this.values)) { this._pendingValues = true; } else { /** @type {?} */ var values = Array.isArray(this.values) ? ((/** @type {?} */ (this.values))).slice() : this.values; this.sanitizeItem(values); } } else { /** @type {?} */ var values = Array.isArray(data.values) ? data.values.slice() : data.values; this.sanitizeItem(values); } }; /** * @param {?} values * @return {?} */ FsFilterConfigItem.prototype.sanitizeItem = /** * @param {?} values * @return {?} */ function (values) { switch (this.type) { case ItemType.Text: break; case ItemType.Select: { this.sanitizeSelectItem(values); } break; case ItemType.Chips: { this.sanitizeChipsItem(values); } break; case ItemType.Range: { this.sanitizeRange(); } break; case ItemType.Checkbox: { this.sanitizeCheckbox(); } break; } if (this.model === undefined) { if (this.type == 'checkbox') { this.model = this.checked == this.defaultValue; } else { this.model = this.defaultValue; } } if (this.model === undefined) { if (this.type == 'checkbox') { this.model = false; } else if (this.type == 'select') { if (this.multiple) { if (!Array.isArray(this.defaultValue)) { this.model = []; } } else { if (this.defaultValue === undefined) { this.model = '__all'; } } } else if (this.type == ItemType.AutoCompleteChips || this.type == ItemType.Chips) { this.model = []; } } }; /** * @param {?} values * @return {?} */ FsFilterConfigItem.prototype.sanitizeSelectItem = /** * @param {?} values * @return {?} */ function (values) { this.values = values; this.groups = null; // let data = []; // if (this.nested) { // // generate a list of values from objects that have not been nested. // if (!this.multiple) { // data.push({value: '__all', name: 'All', depth: 0}); // } // // Array.prototype.push.apply(data, this.walkSelectNestedValues(filter, null, this.values)); // } else { // // data = this.walkSelectValues(filter, this.values); // } // this.values = data; if (this.isolate) { for (var index in this.values) { if (this.values.hasOwnProperty(index)) { if (!this.values[index]) { continue; } if (this.values[index].value == this.isolate.value) { this.values.splice(index, 1); } } } if (Array.isArray(this.model)) { if (this.model.length == this.values.length) { this.model = null; this.isolate.enabled = false; } else if (this.model[0] == this.isolate.value) { this.isolate.enabled = true; } } } }; /** * @param {?} values * @return {?} */ FsFilterConfigItem.prototype.sanitizeChipsItem = /** * @param {?} values * @return {?} */ function (values) { var _this = this; this.values = values; this.groups = null; if (this.model && Array.isArray(this.model)) { if (Number.isInteger(this.model[0])) { this.model = this.model.map((/** * @param {?} id * @return {?} */ function (id) { return _this.values.find((/** * @param {?} value * @return {?} */ function (value) { return value.value === id; })); })); } } }; /** * @return {?} */ FsFilterConfigItem.prototype.sanitizeCheckbox = /** * @return {?} */ function () { this.checked = this.checked ? toString(this.checked) : true; this.unchecked = this.unchecked ? toString(this.unchecked) : false; this.defaultValue = this.defaultValue === undefined ? this.unchecked : toString(this.defaultValue); }; /** * @param {?} value * @return {?} */ FsFilterConfigItem.prototype.updateValue = /** * @param {?} value * @return {?} */ function (value) { var _this = this; var _a; switch (this.type) { case ItemType.Select: { if (value === '__all' || value === null) { this.model = value; return; } /** @type {?} */ var valueExists = null; /** @type {?} */ var isolated = null; if (this.multiple) { isolated = this.isolate && Array.isArray(value) && value[0] === this.isolate.value; valueExists = Array.isArray(this.values) ? value.every((/** * @param {?} val * @return {?} */ function (val) { return _this.values.find((/** * @param {?} valueItem * @return {?} */ function (valueItem) { return valueItem.value === val; })); })) || isolated : false; } else { valueExists = Array.isArray(this.values) ? this.values.some((/** * @param {?} valueItem * @return {?} */ function (valueItem) { return valueItem.value === value; })) : false; } if (valueExists) { this.model = value; if (this.isolate) { this.isolate.enabled = isolated; } return; } } break; case ItemType.Range: { this.model = isObject(value) ? __assign({}, this.model, value) : {}; } break; case ItemType.Chips: { this.model = []; } break; case ItemType.Date: case ItemType.DateTime: { this.model = value; } break; case ItemType.AutoCompleteChips: { if (Array.isArray(value)) { (_a = this.model).push.apply(_a, __spread(value)); } else if (isObject(value)) { this.model.push(value); } else { this.model = []; } } break; default: { this.model = value; } } }; /** * @return {?} */ FsFilterConfigItem.prototype.loadRemoteValues = /** * @return {?} */ function () { var _this = this; if (!this.initialLoading && this.hasPendingValues) { this.initialLoading = true; this.values .pipe(take(1), takeUntil(this._config.destroy$)) .subscribe((/** * @param {?} values * @return {?} */ function (values) { _this._pendingValues = false; _this.sanitizeItem(values); _this.initialLoading = false; })); } }; /** * @return {?} */ FsFilterConfigItem.prototype.clear = /** * @return {?} */ function () { this.valueChanged = false; this.model = undefined; switch (this.type) { case ItemType.AutoComplete: { this.model = null; this.tmpModel = null; this.search = ''; } break; case ItemType.AutoCompleteChips: case ItemType.Chips: { this.model = []; this.tmpModel = []; this.search = ''; } break; case ItemType.Checkbox: { this.model = false; this.tmpModel = false; } break; case ItemType.Select: { if (this.multiple) { this.model = []; this.tmpModel = []; } else { this.model = Array.isArray(this.values) && this.values.some((/** * @param {?} val * @return {?} */ function (val) { return val.value === '__all'; })) ? '__all' : null; this.tmpModel = this.model; } if (this.isolate) { this.isolate.enabled = false; } } break; case ItemType.Range: { this.model = {}; this.tmpModel = {}; } break; case ItemType.Text: { this.model = ''; this.tmpModel = ''; } break; case ItemType.Date: case ItemType.DateTime: { this.model = null; this.tmpModel = null; } break; } }; /** * @return {?} */ FsFilterConfigItem.prototype.checkIfValueChanged = /** * @return {?} */ function () { switch (this.type) { case ItemType.AutoCompleteChips: { this.valueChanged = this.model && this.model.length; } break; case ItemType.Checkbox: { this.valueChanged = this.model && this.model !== false; } break; case ItemType.Select: { if (this.multiple) { this.valueChanged = this.model && this.model.length; } else { /** @type {?} */ var hasAllOption = Array.isArray(this.values) && this.values.some((/** * @param {?} val * @return {?} */ function (val) { return val.value === '__all'; })); if (hasAllOption && this.model && this.model !== '__all') { this.valueChanged = true; } else { this.valueChanged = !!this.model; } } } break; case ItemType.Range: { if (this.model && Object.keys(this.model).length > 0) { this.valueChanged = true; } } break; case ItemType.Text: { this.valueChanged = this.model && this.model !== ''; } break; case ItemType.AutoComplete: case ItemType.Date: case ItemType.DateTime: { this.valueChanged = !!this.model; } break; default: { this.valueChanged = false; } } }; /** * @param {?} value * @return {?} */ FsFilterConfigItem.prototype.parseAndSetValue = /** * @param {?} value * @return {?} */ function (value) { if (value) { if (this.type === ItemType.DateRange || this.type === ItemType.DateTimeRange) { value.from = value.from ? toUTC(value.from) : null; value.to = value.to ? toUTC(value.to) : null; } else if (this.type === ItemType.Date || this.type === ItemType.DateTime) { if (!isDate(value) || !isValid(value)) { value = parse(value, 'yyyy-MM-dd\'T\'HH:mm:ssxxxxx', new Date()); } } else if (this.type === ItemType.Checkbox && this.checked !== undefined) { value = value == this.checked; } else if (this.type === ItemType.Select && this.multiple) { value = clone(value); } else if (this.type === ItemType.Select || this.type === ItemType.AutoComplete) { value = +value; } } this.model = value; }; /** * @private * @return {?} */ FsFilterConfigItem.prototype.sanitizeRange = /** * @private * @return {?} */ function () { if (!this.placeholder) { this.placeholder = ['Min', 'Max']; } if (!this.model) { this.model = {}; } }; __decorate([ Alias(), __metadata("design:type", String) ], FsFilterConfigItem.prototype, "name", void 0); __decorate([ Alias(), __metadata("design:type", String) ], FsFilterConfigItem.prototype, "type", void 0); __decorate([ Alias(), __metadata("design:type", String) ], FsFilterConfigItem.prototype, "label", void 0); __decorate([ Alias(), __metadata("design:type", String) ], FsFilterConfigItem.prototype, "chipLabel", void 0); __decorate([ Alias(), __metadata("design:type", String) ], FsFilterConfigItem.prototype, "children", void 0); __decorate([ Alias(), __metadata("design:type", Boolean) ], FsFilterConfigItem.prototype, "multiple", void 0); __decorate([ Alias(), __metadata("design:type", Object) ], FsFilterConfigItem.prototype, "groups", void 0); __decorate([ Alias(), __metadata("design:type", Boolean) ], FsFilterConfigItem.prototype, "wait", void 0); __decorate([ Alias(), __metadata("design:type", String) ], FsFilterConfigItem.prototype, "query", void 0); __decorate([ Alias(), __metadata("design:type", Object) ], FsFilterConfigItem.prototype, "values", void 0); __decorate([ Alias(), __metadata("design:type", Object) ], FsFilterConfigItem.prototype, "values$", void 0); __decorate([ Alias(), __metadata("design:type", Object) ], FsFilterConfigItem.prototype, "selectedValue", void 0); __decorate([ Alias(), __metadata("design:type", Object) ], FsFilterConfigItem.prototype, "isolate", void 0); __decorate([ Alias(), __metadata("design:type", Object) ], FsFilterConfigItem.prototype, "names", void 0); __decorate([ Alias(), __metadata("design:type", Boolean) ], FsFilterConfigItem.prototype, "primary", void 0); __decorate([ Alias(), __metadata("design:type", Object) ], FsFilterConfigItem.prototype, "search", void 0); __decorate([ Alias(), __metadata("design:type", Object) ], FsFilterConfigItem.prototype, "unchecked", void 0); __decorate([ Alias(), __metadata("design:type", Object) ], FsFilterConfigItem.prototype, "checked", void 0); __decorate([ Alias(), __metadata("design:type", Object) ], FsFilterConfigItem.prototype, "alias", void 0); __decorate([ Alias(), __metadata("design:type", Object) ], FsFilterConfigItem.prototype, "placeholder", void 0); __decorate([ Alias('default'), __metadata("design:type", Object) ], FsFilterConfigItem.prototype, "defaultValue", void 0); return FsFilterConfigItem; }(Model)); /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ var SORT_BY_FIELD = 'system_sort_by'; /** @type {?} */ var SORT_DIRECTION_FIELD = 'system_sort_direction'; var FsFilterConfig = /** @class */ (function (_super) { __extends(FsFilterConfig, _super); function FsFilterConfig(data) { if (data === void 0) { data = {}; } var _this = _super.call(this) || this; _this.load = true; _this.persist = false; _this.inline = false; _this.autofocus = false; _this.chips = false; _this.sortValues = null; _this.sort = null; _this.sortDirection = null; _this.queryParam = false; _this.namespace = 'filter'; _this.items = []; _this.sortByItem = null; _this.sortDirectionItem = null; _this.searchInput = null; _this.singleTextFilter = false; _this._filtersNames = []; _this._destroy$ = new Subject(); _this._fromJSON(data); return _this; } Object.defineProperty(FsFilterConfig.prototype, "destroy$", { get: /** * @return {?} */ function () { return this._destroy$.asObservable(); }, enumerable: true, configurable: true }); /** * @param {?} items * @param {?} route * @param {?} persists * @return {?} */ FsFilterConfig.prototype.initItems = /** * @param {?} items * @param {?} route * @param {?} persists * @return {?} */ function (items, route, persists) { var _this = this; if (items && Array.isArray(items)) { this.items = items.map((/** * @param {?} item * @return {?} */ function (item) { if (item && item.name && _this._filtersNames.indexOf(item.name) === -1) { _this._filtersNames.push(item.name); return new FsFilterConfigItem(item, _this, route, persists); } else { throw Error('Filter init error. Items name must be uniq.'); } })); } this.initSorting(route, persists); this.searchInput = this.items.find((/** * @param {?} item * @return {?} */ function (item) { return item.type === ItemType.Text; })); if (this.items.length === 1 && this.items[0].type === ItemType.Text) { this.singleTextFilter = true; } }; /** * @param {?} route * @param {?} persists * @return {?} */ FsFilterConfig.prototype.initSorting = /** * @param {?} route * @param {?} persists * @return {?} */ function (route, persists) { if (this.sortValues) { /** @type {?} */ var sortByItem = { name: SORT_BY_FIELD, type: ItemType.Select, label: 'Sort By', values: this.sortValues }; if (this.sort && this.sort.value) { sortByItem['default'] = this.sort.value; } this.sortByItem = new FsFilterConfigItem(sortByItem, this, route, persists); /** @type {?} */ var sortDirectionItem = { name: SORT_DIRECTION_FIELD, type: ItemType.Select, label: 'Sort Direction', values: [ { name: 'Ascending', value: 'asc' }, { name: 'Descending', value: 'desc' } ] }; if (this.sort && this.sort.direction) { sortDirectionItem['default'] = this.sort.direction; } this.sortDirectionItem = new FsFilterConfigItem(sortDirectionItem, this, route, persists); } }; /** * @return {?} */ FsFilterConfig.prototype.updateModelValues = /** * @return {?} */ function () { this.items.forEach((/** * @param {?} filter * @return {?} */ function (filter$$1) { filter$$1.model = clone(filter$$1.tmpModel); })); if (this.sortByItem) { this.sortByItem.model = clone(this.sortByItem.tmpModel); } if (this.sortDirectionItem) { this.sortDirectionItem.model = clone(this.sortDirectionItem.tmpModel); } }; /** * @param {?=} opts * @return {?} */ FsFilterConfig.prototype.gets = /** * @param {?=} opts * @return {?} */ function (opts) { if (opts === void 0) { opts = {}; } var e_1, _a; /** @type {?} */ var query = {}; try { for (var _b = __values(this.items), _c = _b.next(); !_c.done; _c = _b.next()) { var filter$$1 = _c.value; /** @type {?} */ var value = clone(filter$$1.model); if (filter$$1.type == ItemType.Select) { if (filter$$1.multiple) { if (filter$$1.isolate) { if (!Array.isArray(filter$$1.model) || !filter$$1.model.length) { value = list(filter$$1.values, 'value'); } } if (filter$$1.model && filter$$1.model.indexOf('__all') > -1) { value = null; } } else { if (filter$$1.isolate) { if (filter$$1.model == '__all') { value = list(filter$$1.values, 'value'); } } else { if (filter$$1.model == '__all') { value = null; } } } } else if (filter$$1.type == ItemType.AutoCompleteChips || filter$$1.type === ItemType.Chips) { if (Array.isArray(filter$$1.model) && filter$$1.model.length && !opts.expand) { value = list(filter$$1.model, 'value'); } } else if (filter$$1.type == ItemType.Checkbox) { value = filter$$1.model ? filter$$1.checked : filter$$1.unchecked; } // @TODO if (isEmpty(value, { zero: true })) { continue; } if (filter$$1.type == ItemType.Date || filter$$1.type == ItemType.DateTime) { if (value && isValid(value) && isDate(value)) { value = simpleFormat(value); } } else if (filter$$1.type == ItemType.DateRange || filter$$1.type == ItemType.DateTimeRange) { /** @type {?} */ var from = value.from; /** @type {?} */ var to = value.to; value = {}; if (from) { value.from = format(from, 'yyyy-MM-dd\THH:mm:ssxxxxx'); } if (to) { value.to = format(to, 'yyyy-MM-dd\THH:mm:ssxxxxx'); } } else if (filter$$1.type == ItemType.AutoComplete) { if (isEmpty(filter$$1.model.value, { zero: true })) { continue; } value = opts.expand ? filter$$1.model : filter$$1.model.value; } if (isObject(filter$$1.names) && opts.names !== false) { for (var key in filter$$1.names) { if (value[filter$$1.names[key]]) { query[key] = value[filter$$1.names[key]]; } } } else { query[filter$$1.name] = value; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } if (opts.flatten) { for (var name_1 in query) { if (Array.isArray(query[name_1])) { query[name_1] = query[name_1].join(','); } } } return query; }; /** * @return {?} */ FsFilterConfig.prototype.getSort = /** * @return {?} */ function () { /** @type {?} */ var sortBy = this.getSortByValue(); sortBy = sortBy === '__all' ? null : sortBy; /** @type {?} */ var sortDirection = this.getSortDirectionValue(); sortDirection = sortDirection === '__all' ? null : sortDirection; return { value: sortBy, direction: sortDirection, }; }; /** * @return {?} */ FsFilterConfig.prototype.getSortByValue = /** * @return {?} */ function () { return this.sortByItem ? this.sortByItem.model : null; }; /** * @return {?} */ FsFilterConfig.prototype.getSortDirectionValue = /** * @return {?} */ function () { return this.sortDirectionItem ? this.sortDirectionItem.model : null; }; /** * @param {?} sort * @return {?} */ FsFilterConfig.prototype.updateSort = /** * @param {?} sort * @return {?} */ function (sort) { if (sort.sortBy) { this.sortByItem.model = sort.sortBy; } if (sort.sortDirection) { this.sortDirectionItem.model = sort.sortDirection; } }; /** * @return {?} */ FsFilterConfig.prototype.getFilledItems = /** * @return {?} */ function () { var _this = this; return this.items.reduce((/** * @param {?} acc * @param {?} filter * @return {?} */ function (acc, filter$$1) { switch (filter$$1.type) { case ItemType.Select: { /** @type {?} */ var multipleIsoldated = filter$$1.multiple && filter$$1.isolate && Array.isArray(filter$$1.model) && !!filter$$1.model.length && filter$$1.model.indexOf('__all') === -1; /** @type {?} */ var multipleHasSelectedValues = filter$$1.multiple && Array.isArray(filter$$1.model) && filter$$1.model.length && filter$$1.model.indexOf('__all') === -1; /** @type {?} */ var selectedValues = !filter$$1.multiple && filter$$1.model && filter$$1.model !== '__all'; if (multipleIsoldated || multipleHasSelectedValues || selectedValues) { acc.push(filter$$1); } } break; case ItemType.AutoCompleteChips: { if (Array.isArray(filter$$1.model) && filter$$1.model.length) { acc.push(filter$$1); } } break; case ItemType.Checkbox: { if (filter$$1.model) { acc.push(filter$$1); } } break; case ItemType.DateRange: case ItemType.DateTimeRange: { if (filter$$1.model.from || filter$$1.model.to) { acc.push(filter$$1); } } break; default: { if (filter$$1.model && filter$$1 !== _this.searchInput && (!isEmpty(filter$$1.model, { zero: true }) || !isEmpty(filter$$1.model.value, { zero: true }))) { acc.push(filter$$1); } } } return acc; }), []); }; /** * @return {?} */ FsFilterConfig.prototype.filtersClear = /** * @return {?} */ function () { var e_2, _a; try { for (var _b = __values(this.items), _c = _b.next(); !_c.done; _c = _b.next()) { var filter$$1 = _c.value; filter$$1.clear(); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_2) throw e_2.error; } } if (this.sortByItem) { if (this.sort) { this.sortByItem.model = this.sort.value; } else { this.sortByItem.clear(); } } if (this.sortDirectionItem) { if (this.sort) { this.sortDirectionItem.model = this.sort.direction; } else { this.sortDirectionItem.clear(); } } }; /** * @return {?} */ FsFilterConfig.prototype.loadValuesForPendingItems = /** * @return {?} */ function () { this.items .filter((/** * @param {?} item * @return {?} */ function (item) { return item.hasPendingValues; })) .forEach((/** * @param {?} item * @return {?} */ function (item) { return item.loadRemoteValues(); })); }; /** * @return {?} */ FsFilterConfig.prototype.destroy = /** * @return {?} */ function () { this._destroy$.next(); this._destroy$.complete(); }; __decorate([ Alias(), __metadata("design:type", Object) ], FsFilterConfig.prototype, "load", void 0); __decorate([ Alias(), __metadata("design:type", Object) ], FsFilterConfig.prototype, "persist", void 0); __decorate([ Alias(), __metadata("design:type", Object) ], FsFilterConfig.prototype, "inline", void 0); __decorate([ Alias(), __metadata("design:type", Object) ], FsFilterConfig.prototype, "autofocus", void 0); __decorate([ Alias(), __metadata("design:type", Object) ], FsFilterConfig.prototype, "chips", void 0); __decorate([ Alias('sorts'), __metadata("design:type", Array) ], FsFilterConfig.prototype, "sortValues", void 0); __decorate([ Alias(), __metadata("design:type", Object) ], FsFilterConfig.prototype, "sort", void 0); __decorate([ Alias(), __metadata("design:type", Object) ], FsFilterConfig.prototype, "sortDirection", void 0); __decorate([ Alias(), __metadata("design:type", Object) ], FsFilterConfig.prototype, "queryParam", void 0); __decorate([ Alias(), __metadata("design:type", Object) ], FsFilterConfig.prototype, "namespace", void 0); __decorate([ Alias(), __metadata("design:type", Function) ], FsFilterConfig.prototype, "init", void 0); __decorate([ Alias(), __metadata("design:type", Function) ], FsFilterConfig.prototype, "change", void 0); __decorate([ Alias(), __metadata("design:type", Function) ], FsFilterConfig.prototype, "reload", void 0); __decorate([ Alias(), __metadata("design:type", Function) ], FsFilterConfig.prototype, "sortChange", void 0); return FsFilterConfig; }(Model)); /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @param {?} obj1 * @param {?} obj2 * @return {?} */ function objectsAreEquals(obj1, obj2) { /** @type {?} */ var oldKeys = Object.keys(obj1); /** @type {?} */ var currKeys = Object.keys(obj2); if (oldKeys.length !== currKeys.length) { return false; } for (var key in obj1) { if (obj1.hasOwnProperty(key)) { /** @type {?} */ var oldItem = obj1[key]; /** @type {?} */ var currItem = obj2[key]; /** @type {?} */ var isArrays = Array.isArray(oldItem) && Array.isArray(currItem); /** @type {?} */ var isObjects = isObject(oldItem) && isObject(currItem); if (isArrays && !arraysAreEquals(oldItem, currItem)) { return false; } else if (isObjects && !objectsAreEquals(oldItem, currItem)) { return false; } else if (!isArrays && !isObjects && oldItem !== currItem) { return false; } } } return true; } /** * @param {?} arr1 * @param {?} arr2 * @return {?} */ function arraysAreEquals(arr1, arr2) { var e_1, _a; if (arr1.length !== arr2.length) { return false; } try { for (var arr1_1 = __values(arr1), arr1_1_1 = arr1_1.next(); !arr1_1_1.done; arr1_1_1 = arr1_1.next()) { var el = arr1_1_1.value; if (arr2.indexOf(el) === -1) { return false; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (arr1_1_1 && !arr1_1_1.done && (_a = arr1_1.return)) _a.call(arr1_1); } finally { if (e_1) throw e_1.error; } } return true; } /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var QueryParams = /** @class */ (function () { function QueryParams(_router, _route, _filterItems) { this._router = _router; this._route = _route; this._filterItems = _filterItems; this._queryKeys = []; this._queryParamsToFilter(this._route.snapshot.queryParams, this._filterItems); } /** * Update query with filter values * @param filterParams */ /** * Update query with filter values * @param {?} filterParams * @return {?} */ QueryParams.prototype.updateQueryParams = /** * Update query with filter values * @param {?} filterParams * @return {?} */ function (filterParams) { // transform selected filter values to query string /** @type {?} */ var newParams = this._filterToQueryParams(filterParams, this._filterItems); // Remove empty keys this._clearKeys(newParams); // Store query keys this._queryKeys = Object.keys(newParams); // Update query this._router.navigate([], { relativeTo: this._route, queryParams: newParams, queryParamsHandling: 'merge' }).then((/** * @return {?} */ function () { })); }; /** * Transformation for selected filter values to query string * @param params * @param items */ /** * Transformation for selected filter values to query string * @private * @param {?} params * @param {?} items * @return {?} */ QueryParams.prototype._filterToQueryParams = /** * Transformation for selected filter values to query string * @private * @param {?} params * @param {?} items * @return {?} */ function (params, items) { // selected filter keys /** @type {?} */ var filterKeys = Object.keys(params); return filterKeys.reduce((/** * @param {?} acc * @param {?} filterKey * @return {?} */ function (acc, filterKey) { // looking filter item /** @type {?} */ var filterItem = items.find((/** * @param {?} item * @return {?} */ function (item) { return item.name === filterKey; })); if (filterItem) { if (filterItem.type === ItemType.Range) { acc[filterKey] = [params[filterKey].min, params[filterKey].max].join(','); } else if (filterItem.type === ItemType.Select && filterItem.multiple && filterItem.model.length > 0) { acc[filterKey] = filterItem.model.join(','); } else if (filterItem.type === ItemType.AutoComplete) { acc[filterKey] = [filterItem.model.value, filterItem.model.name].join(','); } else if (filterItem.type === ItemType.AutoCompleteChips) { acc[filterKey] = filterItem.model.map((/** * @param {?} item * @return {?} */ function (item) { return [item.value, item.name].join(','); })).join(';'); } else { acc[filterKey] = params[filterKey]; } } return acc; }), {}); }; /** * Parse query and update filter values * @param params * @param items */ /** * Parse query and update filter values * @private * @param {?} params * @param {?} items * @return {?} */ QueryParams.prototype._queryParamsToFilter = /** * Parse query and update filter values * @private * @param {?} params * @param {?} items * @return {?} */ function (params, items) { this._queryKeys = Object.keys(params); return this._queryKeys.forEach((/** * @param {?} queryKey * @return {?} */ function (queryKey) { /** @type {?} */ var filterItem = items.find((/** * @param {?} item * @return {?} */ function (item) { return item.name === queryKey; })); if (filterItem) { if (filterItem.type === ItemType.Select && filterItem.multiple) { /** @type {?} */ var values = params[queryKey] .split(','); filterItem.parseAndSetValue(values); } else if (filterItem.type === ItemType.Range) { if (params[queryKey] && isString(params[queryKey])) { /** @type {?} */ var filterParts = params[queryKey].split(','); filterItem.model = { min: filterParts[0], max: filterParts[1] }; } } else if (filterItem.type === ItemType.Chips) { /** @type {?} */ var chipIds = params[queryKey] .split(',') .map((/** * @param {?} value * @return {?} */ function (value) { return +value; })); filterItem.parseAndSetValue(chipIds); } else if (filterItem.type === ItemType.Checkbox) { filterItem.parseAndSetValue(params[queryKey] === 'true'); } else if (filterItem.type === ItemType.AutoComplete) { /** @type {?} */ var filterParts = params[queryKey].split(','); filterItem.model = { name: filterParts[1], value: +filterParts[0] }; } else if (filterItem.type === ItemType.AutoCompleteChips) { /** @type {?} */ var filterParts = params[queryKey].split(';'); filterItem.model = filterParts.reduce((/** * @param {?} acc * @param {?} value * @return {?} */ function (acc, value) { /** @type {?} */ var chipParts = value.split(','); acc.push({ name: chipParts[1], value: +chipParts[0], }); return acc; }), []); } else { filterItem.parseAndSetValue(params[queryKey]); } } })); }; /** * @private * @param {?} params * @return {?} */ QueryParams.prototype._clearKeys = /** * @private * @param {?} params * @return {?} */ function (params) { this._queryKeys.forEach((/** * @param {?} key * @return {?} */ function (key) { if (!params[key]) { params[key] = null; } })); }; return QueryParams; }()); /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var FilterComponent = /** @class */ (function () { function FilterComponent(_store, _location, _route, _router) { this._store = _store; this._location = _location; this._route = _route; this._router = _router; this.filter = null; this.sortUpdate = null; this.showSortBy = true; this.showFilterInput = true; this.changedFilters = []; this.searchText = ''; this.persists = null; this.activeFiltersCount = 0; this.activeFiltersWithInputCount = 0; this.showFilterMenu = false; this.modelChanged = new EventEmitter(); this._searchTextInput = null; this._firstOpen = true; this._query = {}; this._sort = {}; } /** * @param {?} event * @return {?} */ FilterComponent.prototype.keyEvent = /** * @param {?} event * @return {?} */ function (event) { if (event.code === 'Escape' && this.showFilterMenu) { this.changeVisibility(false); } }; Object.defineProperty(FilterComponent.prototype, "searchTextInput", { set: /** * @param {?} value * @return {?} */ function (value) { this._searchTextInput = value; }, enumerable: true, configurable: true }); /** * @return {?} */ FilterComponent.prototype.ngOnInit = /** * @return {?} */ function () { var _this = this; this.config = new FsFilterConfig(this.filter); this.restorePersistValues(); this.config.initItems(this.filter.items, this._route, this.persists); if (this.config.queryParam) { this._queryParams = new QueryParams(this._router, this._route, this.config.items); } // Set search i