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-23 12:26:42 +00:00
|
|
|
import './style.scss';
|
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-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-10-27 15:11:33 +00:00
|
|
|
this.columns = [];
|
2021-10-25 15:18:57 +00:00
|
|
|
this.autoSave = false;
|
2021-10-23 12:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get model() {
|
|
|
|
return this._model;
|
|
|
|
}
|
|
|
|
|
|
|
|
set model(value) {
|
|
|
|
this._model = value;
|
|
|
|
if (value) {
|
|
|
|
this.$.model = value;
|
|
|
|
this.transclude();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 15:11:33 +00:00
|
|
|
get viewConfigId() {
|
|
|
|
return this._viewConfigId;
|
|
|
|
}
|
|
|
|
|
|
|
|
set viewConfigId(value) {
|
|
|
|
this._viewConfigId = value;
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
this.defaultViewConfig = {};
|
|
|
|
|
|
|
|
const url = 'DefaultViewConfigs';
|
|
|
|
this.$http.get(url, {where: {tableCode: value}})
|
|
|
|
.then(res => {
|
|
|
|
if (res && res.data.length) {
|
|
|
|
const columns = res.data[0].columns;
|
|
|
|
this.defaultViewConfig = columns;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get viewConfig() {
|
|
|
|
return this._viewConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
set viewConfig(value) {
|
|
|
|
this._viewConfig = value;
|
|
|
|
|
|
|
|
if (!value) return;
|
|
|
|
|
|
|
|
if (!value.length) {
|
|
|
|
const userViewModel = this.$.userViewModel;
|
|
|
|
|
|
|
|
for (const column of this.columns) {
|
|
|
|
if (this.defaultViewConfig[column.field] == undefined)
|
|
|
|
this.defaultViewConfig[column.field] = true;
|
|
|
|
}
|
|
|
|
userViewModel.insert({
|
|
|
|
userFk: this.currentUserId,
|
|
|
|
tableConfig: this.viewConfigId,
|
|
|
|
configuration: this.defaultViewConfig ? this.defaultViewConfig : {}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.applyViewConfig();
|
|
|
|
}
|
|
|
|
|
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-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!')))
|
|
|
|
.then(() => this.applyViewConfig());
|
|
|
|
}
|
|
|
|
|
|
|
|
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`;
|
|
|
|
selectors.push(`${baseSelector} thead > tr > th:nth-child(${column.index + 1})`);
|
|
|
|
selectors.push(`${baseSelector} tbody > tr > td:nth-child(${column.index + 1})`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const styleElement = document.querySelector('style[id="smart-table"]');
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$.$on('$destroy', () => {
|
|
|
|
if (this.$.css && styleElement)
|
|
|
|
styleElement.parentNode.removeChild(styleElement);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-23 12:26:42 +00:00
|
|
|
registerColumns() {
|
|
|
|
const header = this.element.querySelector('thead > tr');
|
|
|
|
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();
|
|
|
|
|
|
|
|
this.columns.push({field, caption, index});
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creo que se puede hacer directamente desde ng-transclude
|
|
|
|
transclude() {
|
|
|
|
const slotTable = this.element.querySelector('#table');
|
|
|
|
this.$transclude(($clone, $scope) => {
|
|
|
|
const table = $clone[0];
|
|
|
|
$scope.hasChanges = this.hasChanges;
|
|
|
|
slotTable.appendChild(table);
|
|
|
|
this.registerColumns();
|
|
|
|
}, null, 'table');
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
this.sortCriteria.push({field: field, sortType: 'ASC'});
|
|
|
|
element.classList.remove('desc');
|
|
|
|
element.classList.add('asc');
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
}
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.applySort();
|
|
|
|
}
|
|
|
|
|
2021-10-26 15:16:01 +00:00
|
|
|
displaySearch() {
|
|
|
|
const header = this.element.querySelector('thead > tr');
|
|
|
|
if (!header) return;
|
|
|
|
|
|
|
|
const tbody = this.element.querySelector('tbody');
|
|
|
|
const columns = header.querySelectorAll('th');
|
|
|
|
|
|
|
|
const hasSearchRow = tbody.querySelector('tr#searchRow');
|
|
|
|
if (hasSearchRow) return hasSearchRow.remove();
|
|
|
|
|
|
|
|
const searchRow = document.createElement('tr');
|
|
|
|
searchRow.setAttribute('id', 'searchRow');
|
|
|
|
for (let column of columns) {
|
|
|
|
const field = column.getAttribute('field');
|
|
|
|
const cell = document.createElement('td');
|
|
|
|
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);
|
|
|
|
|
|
|
|
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-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"
|
|
|
|
/>`)(this.$);
|
2021-10-29 13:52:04 +00:00
|
|
|
} else {
|
|
|
|
input = this.$compile(`
|
|
|
|
<vn-textfield
|
|
|
|
name="${field}"
|
|
|
|
ng-model="searchProps['${field}']"
|
|
|
|
ng-keydown="$ctrl.searchWithEvent($event, '${field}')"
|
|
|
|
clear-disabled="true"
|
|
|
|
/>`)(this.$);
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
searchByColumn(field) {
|
|
|
|
const searchCriteria = this.$.searchProps[field];
|
2021-10-26 15:16:01 +00:00
|
|
|
const emptySearch = searchCriteria == '' || null;
|
|
|
|
|
|
|
|
const filters = this.filterSanitizer(field);
|
|
|
|
|
|
|
|
if (filters && filters.userFilter)
|
|
|
|
this.model.userFilter = filters.userFilter;
|
|
|
|
|
|
|
|
if (!emptySearch)
|
2021-10-29 13:52:04 +00:00
|
|
|
this.addFilter(field, this.$.searchProps[field]);
|
2021-10-26 15:16:01 +00:00
|
|
|
else this.model.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
addFilter(field, value) {
|
|
|
|
let where = {[field]: value};
|
|
|
|
|
|
|
|
if (this.exprBuilder) {
|
|
|
|
where = buildFilter(where, (param, value) =>
|
|
|
|
this.exprBuilder({param, value})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.model.addFilter({where});
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
this.model.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);
|
|
|
|
|
|
|
|
if (!Object.keys(where))
|
|
|
|
delete userFilter.where;
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeProp(instance, findProp, prop) {
|
|
|
|
if (prop == findProp)
|
|
|
|
delete instance[prop];
|
|
|
|
|
|
|
|
if (prop === 'and') {
|
|
|
|
for (let [index, param] of instance[prop].entries()) {
|
|
|
|
const [key] = Object.keys(param);
|
|
|
|
if (key == findProp)
|
|
|
|
instance[prop].splice(index, 1);
|
|
|
|
|
|
|
|
if (param[key] instanceof Array)
|
|
|
|
removeProp(param, field, key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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'));
|
|
|
|
|
|
|
|
this.model.save()
|
|
|
|
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')));
|
|
|
|
}
|
|
|
|
|
|
|
|
hasChanges() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SmartTable.$inject = ['$element', '$scope', '$transclude'];
|
|
|
|
|
|
|
|
ngModule.vnComponent('smartTable', {
|
|
|
|
template: require('./index.html'),
|
|
|
|
controller: SmartTable,
|
|
|
|
transclude: {
|
|
|
|
table: '?slotTable',
|
|
|
|
actions: '?slotActions'
|
|
|
|
},
|
|
|
|
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: '&?',
|
|
|
|
options: '<?'
|
2021-10-23 12:26:42 +00:00
|
|
|
}
|
|
|
|
});
|