2019-02-12 16:24:37 +00:00
|
|
|
import ngModule from '../module';
|
2020-07-20 09:31:47 +00:00
|
|
|
import template from './smart-table.html';
|
|
|
|
import './smart-table.scss';
|
2019-02-12 16:24:37 +00:00
|
|
|
|
2020-08-26 14:33:47 +00:00
|
|
|
/**
|
|
|
|
* Directive to hide/show selected columns of a table, don't use with rowspan.
|
2020-09-02 11:09:30 +00:00
|
|
|
* Property smart-table-ignore ignores one or more vn-th with prop field.
|
2020-08-26 14:33:47 +00:00
|
|
|
*/
|
2019-02-12 16:24:37 +00:00
|
|
|
directive.$inject = ['$http', '$compile', 'vnApp', '$translate'];
|
|
|
|
export function directive($http, $compile, vnApp, $translate) {
|
|
|
|
function getHeaderList($element, $scope) {
|
2020-09-02 11:09:30 +00:00
|
|
|
let filtrableHeaders = $element[0].querySelectorAll('vn-th[field]:not([smart-table-ignore])');
|
2020-08-26 14:33:47 +00:00
|
|
|
let headerList = Array.from(filtrableHeaders);
|
2019-02-12 16:24:37 +00:00
|
|
|
let ids = [];
|
2019-02-15 16:30:22 +00:00
|
|
|
let titles = {};
|
2019-02-12 16:24:37 +00:00
|
|
|
|
|
|
|
headerList.forEach(header => {
|
2020-08-26 14:33:47 +00:00
|
|
|
let id = header.getAttribute('field');
|
2019-02-15 16:30:22 +00:00
|
|
|
ids.push(id);
|
2019-02-18 10:35:16 +00:00
|
|
|
titles[id] = header.innerText || id.charAt(0).toUpperCase() + id.slice(1);
|
2019-02-12 16:24:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
$scope.fields = ids;
|
2019-02-15 16:30:22 +00:00
|
|
|
$scope.titles = titles;
|
2020-08-26 14:33:47 +00:00
|
|
|
$scope.allHeaders = Array.from($element[0].querySelectorAll('vn-th'));
|
2019-02-12 16:24:37 +00:00
|
|
|
|
|
|
|
return headerList;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTableConfig(tableCode) {
|
2019-10-24 22:53:53 +00:00
|
|
|
return $http.get(`UserConfigViews/getConfig?tableCode=${tableCode}`);
|
2019-02-12 16:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-02-12 16:24:37 +00:00
|
|
|
});
|
|
|
|
|
2020-07-20 09:31:47 +00:00
|
|
|
let baseSelector = `vn-table[vn-smart-table=${userConfig.tableCode}] > div`;
|
2019-02-12 16:24:37 +00:00
|
|
|
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"]');
|
2019-03-06 10:05:29 +00:00
|
|
|
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');
|
2019-03-06 10:05:29 +00:00
|
|
|
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"]');
|
2019-03-06 10:05:29 +00:00
|
|
|
child.parentNode.removeChild(child);
|
|
|
|
}
|
|
|
|
});
|
2019-02-12 16:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function saveConfiguration(tableConfiguration) {
|
|
|
|
tableConfiguration.configuration = JSON.parse(JSON.stringify(tableConfiguration.configuration));
|
2019-10-24 22:53:53 +00:00
|
|
|
return $http.post(`UserConfigViews/save`, tableConfiguration);
|
2019-02-12 16:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 => {
|
2019-02-12 16:24:37 +00:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
2020-07-20 09:31:47 +00:00
|
|
|
$scope.smartTable.show(event.target);
|
2019-02-12 16:24:37 +00:00
|
|
|
};
|
|
|
|
|
2020-07-20 09:31:47 +00:00
|
|
|
let tableCode = $attrs.vnSmartTable.trim();
|
2019-02-12 16:24:37 +00:00
|
|
|
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}]`);
|
2019-02-12 16:24:37 +00:00
|
|
|
|
|
|
|
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();
|
2019-02-12 16:24:37 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-07-20 09:31:47 +00:00
|
|
|
ngModule.directive('vnSmartTable', directive);
|