import ngModule from '../module'; import './style.scss'; class Controller { constructor($scope, $state) { this.$scope = $scope; this.$state = $state; this.$stateParams = $state.params; // Static autocomplete data this.orderWays = [ {way: 'ASC', name: 'Ascendant'}, {way: 'DESC', name: 'Descendant'}, ]; this.defaultOrderFields = [ {field: 'relevancy DESC, name', name: 'Relevancy'}, {field: 'showOrder, price', name: 'Color'}, {field: 'name', name: 'Name'}, {field: 'price', name: 'Price'} ]; this.orderFields = [].concat(this.defaultOrderFields); this._orderWay = this.orderWays[0].way; this._orderField = this.orderFields[0].field; } /** * Fills order autocomplete with tags * obtained from last filtered */ onDataChange() { const items = this.$scope.model.data; const newFilterList = []; if (!items) return; items.forEach(item => { // Add new tag filters item.tags.forEach(itemTag => { const alreadyAdded = newFilterList.findIndex(filter => { return filter.field == itemTag.tagFk; }); if (alreadyAdded == -1) { newFilterList.push({ name: itemTag.name, field: itemTag.tagFk, isTag: true }); } }); }); // Add default filters - Replaces tags with same name this.defaultOrderFields.forEach(orderField => { const index = newFilterList.findIndex(newfield => { return newfield.name == orderField.name; }); if (index > -1) newFilterList[index] = orderField; else newFilterList.push(orderField); }); this.orderFields = newFilterList; } /** * Get order way ASC/DESC */ get orderWay() { return this._orderWay; } set orderWay(value) { this._orderWay = value; if (value) this.applyOrder(); } /** * Get order fields */ get orderField() { return this._orderField; } set orderField(value) { this._orderField = value; if (value) this.applyOrder(); } /** * Returns order param * * @return {Object} - Order param */ getOrderBy() { return { field: this.orderField, way: this.orderWay, isTag: !!(this.orderSelection && this.orderSelection.isTag) }; } /** * Apply order to model */ applyOrder() { this.$scope.model.addFilter(null, {orderBy: this.getOrderBy()}); } preview(event, item) { event.preventDefault(); this.$scope.pricesPopover.show(event, item); } onDescriptorLoad() { this.$scope.popover.relocate(); } $onChanges() { if (this.order && this.order.isConfirmed) this.$state.go('order.card.line'); } } Controller.$inject = ['$scope', '$state']; ngModule.component('vnOrderCatalog', { template: require('./index.html'), controller: Controller, bindings: { order: '<', } });