From 8fa7abe191b76fbed84a2313234f5c19d90ef4a1 Mon Sep 17 00:00:00 2001 From: Gerard Date: Tue, 12 Feb 2019 17:24:37 +0100 Subject: [PATCH] #1128 User view directive first version --- back/methods/user-config-view/getConfig.js | 32 ++++++++ back/methods/user-config-view/save.js | 34 ++++++++ back/models/user-config-view.js | 4 + back/models/user-config-view.json | 7 +- front/core/components/table/index.html | 2 +- front/core/components/table/index.js | 2 +- front/core/directives/index.js | 1 + front/core/directives/uvc.html | 30 +++++++ front/core/directives/uvc.js | 95 ++++++++++++++++++++++ front/core/locale/es.yml | 3 +- 10 files changed, 205 insertions(+), 5 deletions(-) create mode 100644 back/methods/user-config-view/getConfig.js create mode 100644 back/methods/user-config-view/save.js create mode 100644 back/models/user-config-view.js create mode 100644 front/core/directives/uvc.html create mode 100644 front/core/directives/uvc.js diff --git a/back/methods/user-config-view/getConfig.js b/back/methods/user-config-view/getConfig.js new file mode 100644 index 000000000..f61a54d8d --- /dev/null +++ b/back/methods/user-config-view/getConfig.js @@ -0,0 +1,32 @@ +module.exports = function(Self) { + Self.remoteMethodCtx('getConfig', { + description: 'returns the information from UserConfig model for the active user', + accepts: [ + { + arg: 'ctx', + type: 'Object', + http: {source: 'context'} + }, { + arg: 'tableCode', + type: 'String', + description: `Code of the table you ask its configuration` + } + ], + returns: { + type: 'object', + root: true + }, + http: { + path: `/getConfig`, + verb: 'get' + } + }); + + Self.getConfig = async ctx => { + let userView = await Self.app.models.UserConfigView.findOne({ + where: {tableCode: ctx.args.tableCode, userFk: ctx.req.accessToken.userId} + }); + + return userView; + }; +}; diff --git a/back/methods/user-config-view/save.js b/back/methods/user-config-view/save.js new file mode 100644 index 000000000..da8a27083 --- /dev/null +++ b/back/methods/user-config-view/save.js @@ -0,0 +1,34 @@ +module.exports = function(Self) { + Self.remoteMethodCtx('save', { + description: 'returns the information from UserConfig model for the active user', + accepts: [{ + arg: 'config', + type: 'Object', + required: true, + description: `Code of the table you ask its configuration`, + http: {source: 'body'} + } + ], + returns: { + type: 'object', + root: true + }, + http: { + path: `/save`, + verb: 'post' + } + }); + + Self.save = async(ctx, config) => { + let userView = await Self.app.models.UserConfigView.findOne({ + where: {tableCode: config.tableCode, userFk: ctx.req.accessToken.userId} + }); + + if (userView) + return userView.updateAttributes(config); + + config.userFk = ctx.req.accessToken.userId; + + return await Self.app.models.UserConfigView.create(config); + }; +}; diff --git a/back/models/user-config-view.js b/back/models/user-config-view.js new file mode 100644 index 000000000..4c43c1bab --- /dev/null +++ b/back/models/user-config-view.js @@ -0,0 +1,4 @@ +module.exports = Self => { + require('../methods/user-config-view/getConfig')(Self); + require('../methods/user-config-view/save')(Self); +}; diff --git a/back/models/user-config-view.json b/back/models/user-config-view.json index 1cb933079..8ffbc6f48 100644 --- a/back/models/user-config-view.json +++ b/back/models/user-config-view.json @@ -9,7 +9,10 @@ "properties": { "id": { "id": true, - "type": "Number", + "type": "Number" + }, + "userFk": { + "type": "String", "required": true }, "tableCode": { @@ -17,7 +20,7 @@ "required": true }, "configuration": { - "type": "String" + "type": "Object" } }, "relations": { diff --git a/front/core/components/table/index.html b/front/core/components/table/index.html index b0a63bdf3..45754b49e 100644 --- a/front/core/components/table/index.html +++ b/front/core/components/table/index.html @@ -1,4 +1,4 @@ -
+
Enter a new search diff --git a/front/core/components/table/index.js b/front/core/components/table/index.js index d28ece3a4..b905212e5 100644 --- a/front/core/components/table/index.js +++ b/front/core/components/table/index.js @@ -10,7 +10,7 @@ export default class Table { this.autoLoad = true; $transclude($scope.$parent, clone => { - angular.element($element[0].querySelector('div')).append(clone); + angular.element($element[0].querySelector('.table')).append(clone); }); } diff --git a/front/core/directives/index.js b/front/core/directives/index.js index a97c88b3c..9aed1aea3 100644 --- a/front/core/directives/index.js +++ b/front/core/directives/index.js @@ -10,3 +10,4 @@ import './visible-by'; import './bind'; import './repeat-last'; import './title'; +import './uvc'; diff --git a/front/core/directives/uvc.html b/front/core/directives/uvc.html new file mode 100644 index 000000000..be96cb35e --- /dev/null +++ b/front/core/directives/uvc.html @@ -0,0 +1,30 @@ +
+
+ + + + + +
Fields to show
+
+
+ + + + + + +
+
+
+
+
diff --git a/front/core/directives/uvc.js b/front/core/directives/uvc.js new file mode 100644 index 000000000..d880162ee --- /dev/null +++ b/front/core/directives/uvc.js @@ -0,0 +1,95 @@ +import ngModule from '../module'; +import template from './uvc.html'; + +directive.$inject = ['$http', '$compile', 'vnApp', '$translate']; +export function directive($http, $compile, vnApp, $translate) { + function getHeaderList($element, $scope) { + let allHeaders = $element[0].querySelectorAll(`vn-th[field], vn-th[th-id]`); + let headerList = Array.from(allHeaders); + let ids = []; + + headerList.forEach(header => { + ids.push(header.getAttribute('th-id') || header.getAttribute('field')); + }); + + $scope.fields = ids; + + return headerList; + } + + function getTableConfig(tableCode) { + return $http.get(`/api/UserConfigViews/getConfig?tableCode=${tableCode}`); + } + + function createViewConfig(config, fields) { + fields.forEach(field => { + if (config.configuration[field] == null) + config.configuration[field] = true; + }); + } + + function addHideClass($scope, headerList, userConfig) { + let selectors = []; + Object.keys(userConfig.configuration).forEach(key => { + let index; + if (userConfig.configuration[key] === false) { + index = headerList.findIndex(el => { + return (el.getAttribute('th-id') == key || el.getAttribute('field') == key); + }); + + let baseSelector = `vn-table[vn-uvc=${userConfig.tableCode}] > div`; + selectors.push(`${baseSelector} vn-thead > vn-tr > vn-th:nth-child(${index + 1})`); + selectors.push(`${baseSelector} vn-tbody > vn-tr > vn-td:nth-child(${index + 1})`); + selectors.push(`${baseSelector} vn-tbody > .vn-tr > vn-td:nth-child(${index + 1})`); + } + }); + + let rule = selectors.join(', ') + '{display: none;}'; + if ($scope.css) + document.head.removeChild($scope.css); + + $scope.css = document.createElement('style'); + document.head.appendChild($scope.css); + $scope.css.appendChild(document.createTextNode(rule)); + } + + function saveConfiguration(tableConfiguration) { + tableConfiguration.configuration = JSON.parse(JSON.stringify(tableConfiguration.configuration)); + return $http.post(`/api/UserConfigViews/save`, tableConfiguration); + } + + return { + restrict: 'A', + link: async function($scope, $element, $attrs) { + let cTemplate = $compile(template)($scope)[0]; + $scope.$ctrl.showUvc = event => { + event.preventDefault(); + event.stopImmediatePropagation(); + $scope.uvc.show(); + }; + + let tableCode = $attrs.vnUvc.trim(); + let headerList = getHeaderList($element, $scope); + + let defaultConfigView = {tableCode: tableCode, configuration: {}}; + let userView = await getTableConfig(tableCode); + let config = userView.data || defaultConfigView; + + $scope.tableConfiguration = config; + createViewConfig(config, $scope.fields); + + addHideClass($scope, headerList, config); + + let table = document.querySelector(`vn-table[vn-uvc=${tableCode}]`); + + table.insertBefore(cTemplate, table.firstChild); + $scope.$ctrl.saveConfiguration = async tableConfiguration => { + let newConfig = await saveConfiguration(tableConfiguration); + vnApp.showSuccess($translate.instant('Data saved!')); + addHideClass($scope, headerList, newConfig.data); + $scope.uvc.hide(); + }; + } + }; +} +ngModule.directive('vnUvc', directive); diff --git a/front/core/locale/es.yml b/front/core/locale/es.yml index 3cd6428f3..2a5af89ce 100644 --- a/front/core/locale/es.yml +++ b/front/core/locale/es.yml @@ -38,4 +38,5 @@ October: Octubre November: Noviembre December: Diciembre Has delivery: Hay reparto -Loading: Cargando \ No newline at end of file +Loading: Cargando +Fields to show: Campos a mostrar \ No newline at end of file