salix/front/core/directives/smart-table.js

117 lines
4.4 KiB
JavaScript
Raw Normal View History

import ngModule from '../module';
2020-07-20 09:31:47 +00:00
import template from './smart-table.html';
import './smart-table.scss';
2020-08-26 14:33:47 +00:00
/**
* Directive to hide/show selected columns of a table, don't use with rowspan.
*/
directive.$inject = ['$http', '$compile', 'vnApp', '$translate'];
export function directive($http, $compile, vnApp, $translate) {
function getHeaderList($element, $scope) {
2020-08-26 14:33:47 +00:00
let filtrableHeaders = $element[0].querySelectorAll('vn-th[field]');
let headerList = Array.from(filtrableHeaders);
let ids = [];
let titles = {};
headerList.forEach(header => {
2020-08-26 14:33:47 +00:00
let id = header.getAttribute('field');
ids.push(id);
titles[id] = header.innerText || id.charAt(0).toUpperCase() + id.slice(1);
});
$scope.fields = ids;
$scope.titles = titles;
2020-08-26 14:33:47 +00:00
$scope.allHeaders = Array.from($element[0].querySelectorAll('vn-th'));
return headerList;
}
function getTableConfig(tableCode) {
return $http.get(`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) {
2020-08-26 14:33:47 +00:00
index = $scope.allHeaders.findIndex(el => {
return el.getAttribute('field') == key;
});
2020-07-20 09:31:47 +00:00
let baseSelector = `vn-table[vn-smart-table=${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})`);
}
});
2020-07-20 09:31:47 +00:00
if (document.querySelector('style[id="smart-table"]')) {
let child = document.querySelector('style[id="smart-table"]');
child.parentNode.removeChild(child);
}
if (selectors.length) {
let rule = selectors.join(', ') + '{display: none;}';
$scope.css = document.createElement('style');
2020-07-20 09:31:47 +00:00
$scope.css.setAttribute('id', 'smart-table');
document.head.appendChild($scope.css);
$scope.css.appendChild(document.createTextNode(rule));
}
$scope.$on('$destroy', () => {
2020-07-20 09:31:47 +00:00
if ($scope.css && document.querySelector('style[id="smart-table"]')) {
let child = document.querySelector('style[id="smart-table"]');
child.parentNode.removeChild(child);
}
});
}
function saveConfiguration(tableConfiguration) {
tableConfiguration.configuration = JSON.parse(JSON.stringify(tableConfiguration.configuration));
return $http.post(`UserConfigViews/save`, tableConfiguration);
}
return {
restrict: 'A',
link: async function($scope, $element, $attrs) {
let cTemplate = $compile(template)($scope)[0];
2020-07-20 09:31:47 +00:00
$scope.$ctrl.showSmartTable = event => {
event.preventDefault();
event.stopImmediatePropagation();
2020-07-20 09:31:47 +00:00
$scope.smartTable.show(event.target);
};
2020-07-20 09:31:47 +00:00
let tableCode = $attrs.vnSmartTable.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);
2020-07-20 09:31:47 +00:00
let table = document.querySelector(`vn-table[vn-smart-table=${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);
2020-07-20 09:31:47 +00:00
$scope.smartTable.hide();
};
}
};
}
2020-07-20 09:31:47 +00:00
ngModule.directive('vnSmartTable', directive);