2021-10-23 12:26:42 +00:00
|
|
|
import ngModule from '../../module';
|
|
|
|
import Component from '../../lib/component';
|
2021-10-26 15:16:01 +00:00
|
|
|
import {buildFilter} from 'vn-loopback/util/filter';
|
2021-10-27 15:11:33 +00:00
|
|
|
import angular from 'angular';
|
2021-10-29 13:52:04 +00:00
|
|
|
import {camelToKebab} from '../../lib/string';
|
2021-11-10 16:07:16 +00:00
|
|
|
import './style.scss';
|
|
|
|
import './table.scss';
|
2021-10-23 12:26:42 +00:00
|
|
|
|
|
|
|
export default class SmartTable extends Component {
|
|
|
|
constructor($element, $, $transclude) {
|
|
|
|
super($element, $);
|
2021-10-27 15:11:33 +00:00
|
|
|
this.currentUserId = window.localStorage.currentUserWorkerId;
|
2021-10-23 12:26:42 +00:00
|
|
|
this.$transclude = $transclude;
|
2021-10-25 12:20:17 +00:00
|
|
|
this.sortCriteria = [];
|
2021-11-12 13:15:35 +00:00
|
|
|
this.$inputsScope;
|
2021-10-27 15:11:33 +00:00
|
|
|
this.columns = [];
|
2021-10-25 15:18:57 +00:00
|
|
|
this.autoSave = false;
|
2022-09-27 13:17:54 +00:00
|
|
|
this.autoState = true;
|
2021-11-08 13:13:28 +00:00
|
|
|
this.transclude();
|
2021-10-23 12:26:42 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 08:40:09 +00:00
|
|
|
$onChanges() {
|
|
|
|
if (this.model) {
|
2022-09-27 13:17:54 +00:00
|
|
|
this.defaultFilter();
|
2022-11-02 08:40:09 +00:00
|
|
|
this.defaultOrder();
|
|
|
|
}
|
2022-09-27 13:17:54 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 11:13:50 +00:00
|
|
|
$onDestroy() {
|
|
|
|
const styleElement = document.querySelector('style[id="smart-table"]');
|
|
|
|
if (this.$.css && styleElement)
|
|
|
|
styleElement.parentNode.removeChild(styleElement);
|
|
|
|
}
|
|
|
|
|
2021-10-29 14:55:19 +00:00
|
|
|
get options() {
|
|
|
|
return this._options;
|
|
|
|
}
|
|
|
|
|
|
|
|
set options(options) {
|
|
|
|
this._options = options;
|
|
|
|
if (!options) return;
|
|
|
|
|
2023-06-13 14:08:11 +00:00
|
|
|
options.defaultSearch = true;
|
|
|
|
|
2022-03-17 08:14:01 +00:00
|
|
|
if (options.defaultSearch)
|
|
|
|
this.displaySearch();
|
|
|
|
|
2021-10-29 14:55:19 +00:00
|
|
|
const activeButtons = options.activeButtons;
|
|
|
|
const missingId = activeButtons && activeButtons.shownColumns && !this.viewConfigId;
|
|
|
|
if (missingId)
|
|
|
|
throw new Error('vnSmartTable: View identifier not defined');
|
|
|
|
}
|
|
|
|
|
2021-10-23 12:26:42 +00:00
|
|
|
get model() {
|
|
|
|
return this._model;
|
|
|
|
}
|
|
|
|
|
|
|
|
set model(value) {
|
|
|
|
this._model = value;
|
2022-11-02 08:40:09 +00:00
|
|
|
if (value)
|
2021-10-23 12:26:42 +00:00
|
|
|
this.$.model = value;
|
2021-11-12 16:51:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getDefaultViewConfig() {
|
|
|
|
const url = 'DefaultViewConfigs';
|
|
|
|
const filter = {where: {tableCode: this.viewConfigId}};
|
|
|
|
return this.$http.get(url, {filter})
|
|
|
|
.then(res => {
|
|
|
|
if (res && res.data.length)
|
|
|
|
return res.data[0].columns;
|
|
|
|
});
|
2021-10-27 15:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get viewConfig() {
|
|
|
|
return this._viewConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
set viewConfig(value) {
|
|
|
|
this._viewConfig = value;
|
|
|
|
|
|
|
|
if (!value) return;
|
|
|
|
|
|
|
|
if (!value.length) {
|
2021-11-12 16:51:27 +00:00
|
|
|
this.getDefaultViewConfig().then(columns => {
|
|
|
|
const defaultViewConfig = columns ? columns : {};
|
|
|
|
|
|
|
|
const userViewModel = this.$.userViewModel;
|
|
|
|
for (const column of this.columns) {
|
|
|
|
if (defaultViewConfig[column.field] == undefined)
|
|
|
|
defaultViewConfig[column.field] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
userViewModel.insert({
|
|
|
|
userFk: this.currentUserId,
|
|
|
|
tableConfig: this.viewConfigId,
|
|
|
|
configuration: defaultViewConfig
|
|
|
|
});
|
|
|
|
}).finally(() => this.applyViewConfig());
|
|
|
|
} else
|
|
|
|
this.applyViewConfig();
|
2021-10-27 15:11:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 15:18:57 +00:00
|
|
|
get checkedRows() {
|
|
|
|
const model = this.model;
|
|
|
|
if (model && model.data)
|
|
|
|
return model.data.filter(row => row.$checked);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-11-11 16:18:44 +00:00
|
|
|
get checkAll() {
|
|
|
|
return this._checkAll;
|
|
|
|
}
|
|
|
|
|
|
|
|
set checkAll(value) {
|
|
|
|
this._checkAll = value;
|
|
|
|
if (value !== undefined) {
|
|
|
|
const shownColumns = this.viewConfig[0].configuration;
|
|
|
|
for (let param in shownColumns)
|
|
|
|
shownColumns[param] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 13:13:28 +00:00
|
|
|
transclude() {
|
|
|
|
const slotTable = this.element.querySelector('#table');
|
2021-11-12 13:15:35 +00:00
|
|
|
this.$transclude($clone => {
|
2021-11-08 13:13:28 +00:00
|
|
|
const table = $clone[0];
|
|
|
|
slotTable.appendChild(table);
|
|
|
|
this.registerColumns();
|
2021-11-12 13:15:35 +00:00
|
|
|
this.emptyDataRows();
|
2021-11-08 13:13:28 +00:00
|
|
|
}, null, 'table');
|
|
|
|
}
|
|
|
|
|
2021-10-27 15:11:33 +00:00
|
|
|
saveViewConfig() {
|
|
|
|
const userViewModel = this.$.userViewModel;
|
|
|
|
const [viewConfig] = userViewModel.data;
|
|
|
|
viewConfig.configuration = Object.assign({}, viewConfig.configuration);
|
|
|
|
userViewModel.save()
|
|
|
|
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')))
|
2021-11-12 13:15:35 +00:00
|
|
|
.then(() => this.applyViewConfig())
|
|
|
|
.then(() => this.$.smartTableColumns.hide());
|
2021-10-27 15:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
applyViewConfig() {
|
|
|
|
const userViewModel = this.$.userViewModel;
|
|
|
|
const [viewConfig] = userViewModel.data;
|
|
|
|
|
|
|
|
const selectors = [];
|
|
|
|
for (const column of this.columns) {
|
|
|
|
if (viewConfig.configuration[column.field] == false) {
|
|
|
|
const baseSelector = `smart-table[view-config-id="${this.viewConfigId}"] table`;
|
2022-12-16 12:42:23 +00:00
|
|
|
selectors.push(`${baseSelector} thead > tr:not([second-header]) > th:nth-child(${column.index + 1})`);
|
2021-10-27 15:11:33 +00:00
|
|
|
selectors.push(`${baseSelector} tbody > tr > td:nth-child(${column.index + 1})`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-18 10:04:51 +00:00
|
|
|
const styleElement = document.querySelector('style[id="smart-table"]');
|
2021-10-27 15:11:33 +00:00
|
|
|
|
|
|
|
if (styleElement)
|
|
|
|
styleElement.parentNode.removeChild(styleElement);
|
|
|
|
|
|
|
|
if (selectors.length) {
|
|
|
|
const rule = selectors.join(', ') + '{display: none}';
|
|
|
|
this.$.css = document.createElement('style');
|
|
|
|
this.$.css.setAttribute('id', 'smart-table');
|
|
|
|
document.head.appendChild(this.$.css);
|
|
|
|
this.$.css.appendChild(document.createTextNode(rule));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 13:17:54 +00:00
|
|
|
defaultFilter() {
|
2022-11-02 08:40:09 +00:00
|
|
|
if (this.disabledTableFilter || !this.$params.q) return;
|
|
|
|
|
2022-09-27 13:17:54 +00:00
|
|
|
const stateFilter = JSON.parse(this.$params.q).tableQ;
|
|
|
|
if (!stateFilter || !this.exprBuilder) return;
|
|
|
|
|
|
|
|
const columns = this.columns.map(column => column.field);
|
|
|
|
|
|
|
|
this.displaySearch();
|
|
|
|
if (!this.$inputsScope.searchProps)
|
|
|
|
this.$inputsScope.searchProps = {};
|
|
|
|
|
|
|
|
for (let param in stateFilter) {
|
|
|
|
if (columns.includes(param)) {
|
|
|
|
const whereParams = {[param]: stateFilter[param]};
|
|
|
|
Object.assign(this.$inputsScope.searchProps, whereParams);
|
|
|
|
this.addFilter(param, stateFilter[param]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-21 08:19:28 +00:00
|
|
|
defaultOrder() {
|
2022-11-02 08:40:09 +00:00
|
|
|
if (this.disabledTableOrder) return;
|
|
|
|
|
2022-09-28 09:22:29 +00:00
|
|
|
let stateOrder;
|
|
|
|
if (this.$params.q)
|
|
|
|
stateOrder = JSON.parse(this.$params.q).tableOrder;
|
|
|
|
|
|
|
|
const order = stateOrder ? stateOrder : this.model.order;
|
|
|
|
|
2021-12-21 08:19:28 +00:00
|
|
|
if (!order) return;
|
|
|
|
|
|
|
|
const orderFields = order.split(', ');
|
|
|
|
|
|
|
|
for (const fieldString of orderFields) {
|
|
|
|
const field = fieldString.split(' ');
|
|
|
|
const fieldName = field[0];
|
|
|
|
|
|
|
|
let sortType = 'ASC';
|
|
|
|
if (field.length === 2)
|
|
|
|
sortType = field[1];
|
|
|
|
|
2022-01-20 08:34:53 +00:00
|
|
|
const priority = this.sortCriteria.length + 1;
|
2021-12-21 08:19:28 +00:00
|
|
|
const column = this.columns.find(column => column.field == fieldName);
|
|
|
|
if (column) {
|
2022-01-20 08:34:53 +00:00
|
|
|
this.sortCriteria.push({field: fieldName, sortType: sortType, priority: priority});
|
2021-12-21 08:19:28 +00:00
|
|
|
|
|
|
|
const isASC = sortType == 'ASC';
|
|
|
|
const isDESC = sortType == 'DESC';
|
|
|
|
|
|
|
|
if (isDESC) {
|
|
|
|
column.element.classList.remove('asc');
|
|
|
|
column.element.classList.add('desc');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isASC) {
|
|
|
|
column.element.classList.remove('desc');
|
|
|
|
column.element.classList.add('asc');
|
|
|
|
}
|
2022-01-20 08:34:53 +00:00
|
|
|
|
|
|
|
this.setPriority(column.element, priority);
|
2021-12-21 08:19:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-28 09:22:29 +00:00
|
|
|
|
|
|
|
this.model.order = order;
|
2023-01-24 13:45:35 +00:00
|
|
|
this.refresh();
|
2021-12-21 08:19:28 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 12:26:42 +00:00
|
|
|
registerColumns() {
|
2022-12-16 12:42:23 +00:00
|
|
|
const header = this.element.querySelector('thead > tr:not([second-header])');
|
2021-10-23 12:26:42 +00:00
|
|
|
if (!header) return;
|
|
|
|
const columns = header.querySelectorAll('th');
|
|
|
|
|
|
|
|
// Click handler
|
2021-10-27 15:11:33 +00:00
|
|
|
for (const [index, column] of columns.entries()) {
|
2021-10-23 12:26:42 +00:00
|
|
|
const field = column.getAttribute('field');
|
2021-10-27 15:11:33 +00:00
|
|
|
if (field) {
|
|
|
|
const columnElement = angular.element(column);
|
|
|
|
const caption = columnElement.text().trim();
|
|
|
|
|
2021-12-21 08:19:28 +00:00
|
|
|
this.columns.push({field, caption, index, element: column});
|
2021-10-27 15:11:33 +00:00
|
|
|
|
2021-10-23 12:26:42 +00:00
|
|
|
column.addEventListener('click', () => this.orderHandler(column));
|
2021-10-27 15:11:33 +00:00
|
|
|
}
|
2021-10-23 12:26:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 13:15:35 +00:00
|
|
|
emptyDataRows() {
|
2022-12-16 12:42:23 +00:00
|
|
|
const header = this.element.querySelector('thead > tr:not([second-header])');
|
2021-11-12 13:15:35 +00:00
|
|
|
const columns = header.querySelectorAll('th');
|
|
|
|
const tbody = this.element.querySelector('tbody');
|
|
|
|
if (tbody) {
|
|
|
|
const noSearch = this.$compile(`
|
|
|
|
<tr class="empty-rows" ng-if="!model.data">
|
|
|
|
<td colspan="${columns.length}" translate>Enter a new search</td>
|
|
|
|
</tr>
|
|
|
|
`)(this.$);
|
|
|
|
tbody.appendChild(noSearch[0]);
|
|
|
|
|
|
|
|
const noRows = this.$compile(`
|
|
|
|
<tr class="empty-rows" ng-if="model.data.length == 0">
|
|
|
|
<td colspan="${columns.length}" translate>No data</td>
|
|
|
|
</tr>
|
|
|
|
`)(this.$);
|
|
|
|
tbody.appendChild(noRows[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 12:26:42 +00:00
|
|
|
orderHandler(element) {
|
|
|
|
const field = element.getAttribute('field');
|
2021-10-25 12:20:17 +00:00
|
|
|
const existingCriteria = this.sortCriteria.find(criteria => {
|
|
|
|
return criteria.field == field;
|
|
|
|
});
|
|
|
|
|
2021-10-26 07:18:31 +00:00
|
|
|
const isASC = existingCriteria && existingCriteria.sortType == 'ASC';
|
|
|
|
const isDESC = existingCriteria && existingCriteria.sortType == 'DESC';
|
|
|
|
|
2021-10-25 12:20:17 +00:00
|
|
|
if (!existingCriteria) {
|
2022-01-20 08:34:53 +00:00
|
|
|
const priority = this.sortCriteria.length + 1;
|
|
|
|
|
2022-01-20 09:43:03 +00:00
|
|
|
this.sortCriteria.push({field: field, sortType: 'ASC', priority: priority});
|
2021-10-25 12:20:17 +00:00
|
|
|
element.classList.remove('desc');
|
|
|
|
element.classList.add('asc');
|
2022-01-20 08:34:53 +00:00
|
|
|
|
|
|
|
this.setPriority(element, priority);
|
2021-10-25 12:20:17 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 07:18:31 +00:00
|
|
|
if (isDESC) {
|
2021-10-25 12:20:17 +00:00
|
|
|
this.sortCriteria.splice(this.sortCriteria.findIndex(criteria => {
|
|
|
|
return criteria.field == field;
|
|
|
|
}), 1);
|
|
|
|
element.classList.remove('desc');
|
|
|
|
element.classList.remove('asc');
|
2022-01-20 08:34:53 +00:00
|
|
|
|
|
|
|
element.querySelector('sort-priority').remove();
|
2021-10-25 12:20:17 +00:00
|
|
|
}
|
2021-10-26 15:16:01 +00:00
|
|
|
|
2021-10-26 07:18:31 +00:00
|
|
|
if (isASC) {
|
2021-10-25 12:20:17 +00:00
|
|
|
existingCriteria.sortType = 'DESC';
|
|
|
|
element.classList.remove('asc');
|
|
|
|
element.classList.add('desc');
|
|
|
|
}
|
|
|
|
|
2022-01-20 08:34:53 +00:00
|
|
|
let priority = 0;
|
|
|
|
for (const criteria of this.sortCriteria) {
|
|
|
|
const column = this.columns.find(column => column.field == criteria.field);
|
|
|
|
if (column) {
|
|
|
|
criteria.priority = priority;
|
|
|
|
priority++;
|
|
|
|
|
|
|
|
column.element.querySelector('sort-priority').remove();
|
|
|
|
|
|
|
|
this.setPriority(column.element, priority);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 12:20:17 +00:00
|
|
|
this.applySort();
|
|
|
|
}
|
|
|
|
|
2022-01-20 08:34:53 +00:00
|
|
|
setPriority(column, priority) {
|
|
|
|
const sortPriority = document.createElement('sort-priority');
|
|
|
|
sortPriority.setAttribute('class', 'sort-priority');
|
|
|
|
sortPriority.innerHTML = priority;
|
|
|
|
column.appendChild(sortPriority);
|
|
|
|
}
|
|
|
|
|
2021-10-26 15:16:01 +00:00
|
|
|
displaySearch() {
|
2022-12-16 12:42:23 +00:00
|
|
|
const header = this.element.querySelector('thead > tr:not([second-header])');
|
2021-10-26 15:16:01 +00:00
|
|
|
if (!header) return;
|
|
|
|
|
|
|
|
const tbody = this.element.querySelector('tbody');
|
|
|
|
const columns = header.querySelectorAll('th');
|
|
|
|
|
|
|
|
const hasSearchRow = tbody.querySelector('tr#searchRow');
|
2021-11-12 11:13:50 +00:00
|
|
|
if (hasSearchRow) {
|
2021-11-12 13:15:35 +00:00
|
|
|
if (this.$inputsScope)
|
|
|
|
this.$inputsScope.$destroy();
|
|
|
|
|
2021-11-12 11:13:50 +00:00
|
|
|
return hasSearchRow.remove();
|
|
|
|
}
|
2021-10-26 15:16:01 +00:00
|
|
|
|
|
|
|
const searchRow = document.createElement('tr');
|
|
|
|
searchRow.setAttribute('id', 'searchRow');
|
2021-11-12 13:15:35 +00:00
|
|
|
|
|
|
|
this.$inputsScope = this.$.$new();
|
|
|
|
|
2021-10-26 15:16:01 +00:00
|
|
|
for (let column of columns) {
|
|
|
|
const field = column.getAttribute('field');
|
|
|
|
const cell = document.createElement('td');
|
2022-05-31 11:36:27 +00:00
|
|
|
cell.setAttribute('centered', '');
|
|
|
|
|
2021-10-26 15:16:01 +00:00
|
|
|
if (field) {
|
2021-10-29 13:52:04 +00:00
|
|
|
let input;
|
|
|
|
let options;
|
|
|
|
const columnOptions = this.options && this.options.columns;
|
|
|
|
|
|
|
|
if (columnOptions)
|
|
|
|
options = columnOptions.find(column => column.field == field);
|
|
|
|
|
2021-11-09 10:47:54 +00:00
|
|
|
if (options && options.searchable == false) {
|
|
|
|
searchRow.appendChild(cell);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-06-07 12:59:18 +00:00
|
|
|
input = this.$compile(`
|
|
|
|
<vn-textfield
|
|
|
|
class="dense"
|
|
|
|
name="${field}"
|
|
|
|
ng-model="searchProps['${field}']"
|
|
|
|
ng-keydown="$ctrl.searchWithEvent($event, '${field}')"
|
|
|
|
clear-disabled="true"
|
|
|
|
/>`)(this.$inputsScope);
|
|
|
|
|
2021-10-29 13:52:04 +00:00
|
|
|
if (options && options.autocomplete) {
|
|
|
|
let props = ``;
|
|
|
|
|
|
|
|
const autocomplete = options.autocomplete;
|
|
|
|
for (const prop in autocomplete)
|
|
|
|
props += `${camelToKebab(prop)}="${autocomplete[prop]}"\n`;
|
|
|
|
input = this.$compile(`
|
|
|
|
<vn-autocomplete
|
2021-11-10 16:07:16 +00:00
|
|
|
class="dense"
|
2021-10-26 15:16:01 +00:00
|
|
|
name="${field}"
|
|
|
|
ng-model="searchProps['${field}']"
|
2021-10-29 13:52:04 +00:00
|
|
|
${props}
|
|
|
|
on-change="$ctrl.searchByColumn('${field}')"
|
2021-10-26 15:16:01 +00:00
|
|
|
clear-disabled="true"
|
2021-11-12 13:15:35 +00:00
|
|
|
/>`)(this.$inputsScope);
|
2022-06-07 12:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options && options.checkbox) {
|
2022-05-31 11:36:27 +00:00
|
|
|
input = this.$compile(`
|
|
|
|
<vn-check
|
|
|
|
class="dense"
|
|
|
|
name="${field}"
|
|
|
|
ng-model="searchProps['${field}']"
|
|
|
|
on-change="$ctrl.searchByColumn('${field}')"
|
|
|
|
triple-state="true"
|
|
|
|
/>`)(this.$inputsScope);
|
2022-06-07 12:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options && options.datepicker) {
|
2022-05-31 11:36:27 +00:00
|
|
|
input = this.$compile(`
|
|
|
|
<vn-date-picker
|
|
|
|
class="dense"
|
|
|
|
name="${field}"
|
|
|
|
ng-model="searchProps['${field}']"
|
|
|
|
on-change="$ctrl.searchByColumn('${field}')"
|
|
|
|
/>`)(this.$inputsScope);
|
2021-10-29 13:52:04 +00:00
|
|
|
}
|
2022-06-07 12:59:18 +00:00
|
|
|
|
2021-10-26 15:16:01 +00:00
|
|
|
cell.appendChild(input[0]);
|
|
|
|
}
|
|
|
|
searchRow.appendChild(cell);
|
|
|
|
}
|
|
|
|
|
|
|
|
tbody.prepend(searchRow);
|
|
|
|
}
|
|
|
|
|
2021-10-29 13:52:04 +00:00
|
|
|
searchWithEvent($event, field) {
|
2021-10-26 15:16:01 +00:00
|
|
|
if ($event.key != 'Enter') return;
|
|
|
|
|
2021-10-29 13:52:04 +00:00
|
|
|
this.searchByColumn(field);
|
|
|
|
}
|
|
|
|
|
2022-09-29 12:28:50 +00:00
|
|
|
searchByColumn(field) {
|
|
|
|
const filters = this.filterSanitizer(field);
|
|
|
|
|
|
|
|
if (filters && filters.userFilter)
|
|
|
|
this.model.userFilter = filters.userFilter;
|
2023-03-07 06:47:30 +00:00
|
|
|
|
2022-09-29 12:28:50 +00:00
|
|
|
this.addFilter(field, this.$inputsScope.searchProps[field]);
|
|
|
|
}
|
|
|
|
|
2022-09-27 13:17:54 +00:00
|
|
|
searchPropsSanitizer() {
|
|
|
|
if (!this.$inputsScope || !this.$inputsScope.searchProps) return null;
|
|
|
|
let searchProps = this.$inputsScope.searchProps;
|
|
|
|
const searchPropsArray = Object.entries(searchProps);
|
|
|
|
searchProps = searchPropsArray.filter(
|
|
|
|
([key, value]) => value && value != ''
|
|
|
|
);
|
2021-10-26 15:16:01 +00:00
|
|
|
|
2022-09-27 13:17:54 +00:00
|
|
|
return Object.fromEntries(searchProps);
|
|
|
|
}
|
|
|
|
|
2021-10-26 15:16:01 +00:00
|
|
|
addFilter(field, value) {
|
2023-03-07 06:47:30 +00:00
|
|
|
if (value === '') value = null;
|
2021-10-26 15:16:01 +00:00
|
|
|
|
2022-09-28 09:22:29 +00:00
|
|
|
let stateFilter = {tableQ: {}};
|
2022-09-27 13:17:54 +00:00
|
|
|
if (this.$params.q) {
|
2022-09-28 09:22:29 +00:00
|
|
|
stateFilter = JSON.parse(this.$params.q);
|
|
|
|
if (!stateFilter.tableQ)
|
|
|
|
stateFilter.tableQ = {};
|
|
|
|
delete stateFilter.tableQ[field];
|
2022-09-27 13:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const whereParams = {[field]: value};
|
2023-03-07 06:47:30 +00:00
|
|
|
if (value !== '' && value !== null && value !== undefined) {
|
2022-09-27 13:17:54 +00:00
|
|
|
let where = {[field]: value};
|
|
|
|
if (this.exprBuilder) {
|
|
|
|
where = buildFilter(whereParams, (param, value) =>
|
|
|
|
this.exprBuilder({param, value})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
this.model.addFilter({where});
|
2021-10-26 15:16:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 13:17:54 +00:00
|
|
|
const searchProps = this.searchPropsSanitizer();
|
|
|
|
|
2022-09-28 09:22:29 +00:00
|
|
|
Object.assign(stateFilter.tableQ, searchProps);
|
2022-09-27 13:17:54 +00:00
|
|
|
|
2022-09-28 09:22:29 +00:00
|
|
|
const params = {q: JSON.stringify(stateFilter)};
|
2022-09-27 13:17:54 +00:00
|
|
|
|
2023-01-24 13:45:35 +00:00
|
|
|
this.$state.go(this.$state.current.name, params, {location: 'replace'})
|
|
|
|
.then(() => this.refresh());
|
2021-10-26 15:16:01 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 12:20:17 +00:00
|
|
|
applySort() {
|
|
|
|
let order = this.sortCriteria.map(criteria => `${criteria.field} ${criteria.sortType}`);
|
|
|
|
order = order.join(', ');
|
|
|
|
|
|
|
|
if (order)
|
|
|
|
this.model.order = order;
|
|
|
|
|
2022-09-28 09:22:29 +00:00
|
|
|
let stateFilter = {tableOrder: {}};
|
|
|
|
if (this.$params.q) {
|
|
|
|
stateFilter = JSON.parse(this.$params.q);
|
|
|
|
if (!stateFilter.tableOrder)
|
|
|
|
stateFilter.tableOrder = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
stateFilter.tableOrder = order;
|
|
|
|
|
|
|
|
const params = {q: JSON.stringify(stateFilter)};
|
2023-01-24 13:45:35 +00:00
|
|
|
this.$state.go(this.$state.current.name, params, {location: 'replace'})
|
|
|
|
.then(() => this.refresh());
|
2021-10-23 12:26:42 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 15:16:01 +00:00
|
|
|
filterSanitizer(field) {
|
|
|
|
const userFilter = this.model.userFilter;
|
|
|
|
const userParams = this.model.userParams;
|
|
|
|
const where = userFilter && userFilter.where;
|
|
|
|
|
|
|
|
if (this.exprBuilder) {
|
|
|
|
const param = this.exprBuilder({
|
|
|
|
param: field,
|
|
|
|
value: null
|
|
|
|
});
|
|
|
|
if (param) [field] = Object.keys(param);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!where) return;
|
|
|
|
|
|
|
|
const whereKeys = Object.keys(where);
|
|
|
|
for (let key of whereKeys) {
|
|
|
|
removeProp(where, field, key);
|
|
|
|
|
2021-11-23 12:58:12 +00:00
|
|
|
if (Object.keys(where).length == 0)
|
2021-10-26 15:16:01 +00:00
|
|
|
delete userFilter.where;
|
|
|
|
}
|
|
|
|
|
2021-11-23 12:58:12 +00:00
|
|
|
function removeProp(obj, targetProp, prop) {
|
|
|
|
if (prop == targetProp)
|
|
|
|
delete obj[prop];
|
2021-10-26 15:16:01 +00:00
|
|
|
|
2021-11-23 12:58:12 +00:00
|
|
|
if (prop === 'and' || prop === 'or') {
|
|
|
|
const arrayCopy = obj[prop].slice();
|
|
|
|
for (let param of arrayCopy) {
|
2021-10-26 15:16:01 +00:00
|
|
|
const [key] = Object.keys(param);
|
2021-11-23 12:58:12 +00:00
|
|
|
const index = obj[prop].findIndex(param => {
|
|
|
|
return Object.keys(param)[0] == key;
|
|
|
|
});
|
|
|
|
if (key == targetProp)
|
|
|
|
obj[prop].splice(index, 1);
|
2021-10-26 15:16:01 +00:00
|
|
|
|
|
|
|
if (param[key] instanceof Array)
|
|
|
|
removeProp(param, field, key);
|
2021-11-23 12:58:12 +00:00
|
|
|
|
|
|
|
if (Object.keys(param).length == 0)
|
|
|
|
obj[prop].splice(index, 1);
|
2021-10-26 15:16:01 +00:00
|
|
|
}
|
2021-11-23 12:58:12 +00:00
|
|
|
|
|
|
|
if (obj[prop].length == 0)
|
|
|
|
delete obj[prop];
|
2021-10-26 15:16:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {userFilter, userParams};
|
|
|
|
}
|
|
|
|
|
2021-10-27 15:11:33 +00:00
|
|
|
removeFilter() {
|
2021-10-26 15:16:01 +00:00
|
|
|
this.model.applyFilter(userFilter, userParams);
|
|
|
|
}
|
|
|
|
|
2021-10-23 12:26:42 +00:00
|
|
|
createRow() {
|
2021-10-27 15:11:33 +00:00
|
|
|
let data = {};
|
|
|
|
|
|
|
|
if (this.defaultNewData)
|
|
|
|
data = this.defaultNewData();
|
|
|
|
|
|
|
|
this.model.insert(data);
|
2021-10-23 12:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
deleteAll() {
|
2021-10-26 15:16:01 +00:00
|
|
|
for (let row of this.checkedRows)
|
2021-10-25 15:18:57 +00:00
|
|
|
this.model.removeRow(row);
|
2021-10-23 12:26:42 +00:00
|
|
|
|
|
|
|
if (this.autoSave)
|
2021-10-24 11:35:37 +00:00
|
|
|
this.saveAll();
|
2021-10-23 12:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
saveAll() {
|
|
|
|
const model = this.model;
|
2021-10-25 15:18:57 +00:00
|
|
|
|
2021-10-23 12:26:42 +00:00
|
|
|
if (!model.isChanged)
|
|
|
|
return this.vnApp.showError(this.$t('No changes to save'));
|
|
|
|
|
2021-11-23 12:58:12 +00:00
|
|
|
return this.model.save()
|
2021-10-23 12:26:42 +00:00
|
|
|
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')));
|
|
|
|
}
|
2022-09-07 08:30:50 +00:00
|
|
|
|
|
|
|
refresh() {
|
|
|
|
this.isRefreshing = true;
|
|
|
|
this.model.refresh()
|
2023-01-26 07:19:52 +00:00
|
|
|
.finally(() => this.isRefreshing = false);
|
2022-09-07 08:30:50 +00:00
|
|
|
}
|
2021-10-23 12:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SmartTable.$inject = ['$element', '$scope', '$transclude'];
|
|
|
|
|
|
|
|
ngModule.vnComponent('smartTable', {
|
|
|
|
template: require('./index.html'),
|
|
|
|
controller: SmartTable,
|
|
|
|
transclude: {
|
|
|
|
table: '?slotTable',
|
2022-05-03 12:08:46 +00:00
|
|
|
actions: '?slotActions',
|
|
|
|
pagination: '?slotPagination'
|
2021-10-23 12:26:42 +00:00
|
|
|
},
|
|
|
|
bindings: {
|
|
|
|
model: '<?',
|
2021-10-27 15:11:33 +00:00
|
|
|
viewConfigId: '@?',
|
2021-10-26 15:16:01 +00:00
|
|
|
autoSave: '<?',
|
2021-10-27 15:11:33 +00:00
|
|
|
exprBuilder: '&?',
|
2021-10-29 13:52:04 +00:00
|
|
|
defaultNewData: '&?',
|
2022-11-02 08:40:09 +00:00
|
|
|
options: '<?',
|
|
|
|
disabledTableFilter: '<?',
|
|
|
|
disabledTableOrder: '<?',
|
2021-10-23 12:26:42 +00:00
|
|
|
}
|
|
|
|
});
|