diff --git a/back/methods/user-config-view/save.js b/back/methods/user-config-view/save.js index da8a27083..b2144c01e 100644 --- a/back/methods/user-config-view/save.js +++ b/back/methods/user-config-view/save.js @@ -7,8 +7,7 @@ module.exports = function(Self) { required: true, description: `Code of the table you ask its configuration`, http: {source: 'body'} - } - ], + }], returns: { type: 'object', root: true @@ -29,6 +28,6 @@ module.exports = function(Self) { config.userFk = ctx.req.accessToken.userId; - return await Self.app.models.UserConfigView.create(config); + return Self.app.models.UserConfigView.create(config); }; }; diff --git a/back/model-config.json b/back/model-config.json index 18bf4cf98..8ad15a16a 100644 --- a/back/model-config.json +++ b/back/model-config.json @@ -29,6 +29,9 @@ "ChatConfig": { "dataSource": "vn" }, + "DefaultViewConfig": { + "dataSource": "vn" + }, "Delivery": { "dataSource": "vn" }, diff --git a/back/models/default-view-config.json b/back/models/default-view-config.json new file mode 100644 index 000000000..88164692d --- /dev/null +++ b/back/models/default-view-config.json @@ -0,0 +1,25 @@ +{ + "name": "DefaultViewConfig", + "base": "VnModel", + "options": { + "mysql": { + "table": "salix.defaultViewConfig" + } + }, + "properties": { + "tableCode": { + "id": true, + "type": "string", + "required": true + }, + "columns": { + "type": "object" + } + }, + "acls": [{ + "accessType": "READ", + "principalType": "ROLE", + "principalId": "$everyone", + "permission": "ALLOW" + }] +} diff --git a/db/changes/10380-allsaints/00-defaultViewConfig.sql b/db/changes/10380-allsaints/00-defaultViewConfig.sql new file mode 100644 index 000000000..6f66bd6eb --- /dev/null +++ b/db/changes/10380-allsaints/00-defaultViewConfig.sql @@ -0,0 +1,6 @@ +CREATE TABLE `salix`.`defaultViewConfig` +( + tableCode VARCHAR(25) not null, + columns JSON not null +) +comment 'The default configuration of columns for views'; \ No newline at end of file diff --git a/front/core/components/smart-table/index.html b/front/core/components/smart-table/index.html index 2a7a45b18..68c29dcb5 100644 --- a/front/core/components/smart-table/index.html +++ b/front/core/components/smart-table/index.html @@ -2,10 +2,9 @@
-
@@ -32,7 +31,6 @@ vn-tooltip="Save data">
-
- \ No newline at end of file + + + + + + +
+ + + + + + + + +
+
+
\ No newline at end of file diff --git a/front/core/components/smart-table/index.js b/front/core/components/smart-table/index.js index 628f25e47..353cbb050 100644 --- a/front/core/components/smart-table/index.js +++ b/front/core/components/smart-table/index.js @@ -2,12 +2,15 @@ import ngModule from '../../module'; import Component from '../../lib/component'; import {buildFilter} from 'vn-loopback/util/filter'; import './style.scss'; +import angular from 'angular'; export default class SmartTable extends Component { constructor($element, $, $transclude) { super($element, $); + this.currentUserId = window.localStorage.currentUserWorkerId; this.$transclude = $transclude; this.sortCriteria = []; + this.columns = []; this.autoSave = false; } @@ -23,6 +26,52 @@ export default class SmartTable extends Component { } } + 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(); + } + get checkedRows() { const model = this.model; if (model && model.data) @@ -31,16 +80,63 @@ export default class SmartTable extends Component { return null; } + 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); + }); + } + registerColumns() { const header = this.element.querySelector('thead > tr'); if (!header) return; const columns = header.querySelectorAll('th'); // Click handler - for (let column of columns) { + for (const [index, column] of columns.entries()) { const field = column.getAttribute('field'); - if (field) + if (field) { + const columnElement = angular.element(column); + const caption = columnElement.text().trim(); + + this.columns.push({field, caption, index}); + column.addEventListener('click', () => this.orderHandler(column)); + } } } @@ -157,7 +253,6 @@ export default class SmartTable extends Component { } filterSanitizer(field) { - // tenemos que eliminar ands vacios al ir borrando filtros const userFilter = this.model.userFilter; const userParams = this.model.userParams; const where = userFilter && userFilter.where; @@ -199,15 +294,17 @@ export default class SmartTable extends Component { return {userFilter, userParams}; } - removeFilter(field) { - // + removeFilter() { this.model.applyFilter(userFilter, userParams); } createRow() { - this.model.insert({ - nickname: 'New row' - }); + let data = {}; + + if (this.defaultNewData) + data = this.defaultNewData(); + + this.model.insert(data); } deleteAll() { @@ -244,7 +341,9 @@ ngModule.vnComponent('smartTable', { }, bindings: { model: ' + diff --git a/modules/ticket/front/index/index.html b/modules/ticket/front/index/index.html index f5e3e1591..dc0f49369 100644 --- a/modules/ticket/front/index/index.html +++ b/modules/ticket/front/index/index.html @@ -11,9 +11,10 @@ + default-new-data="$ctrl.defaultNewData()">