"use strict"; (function () { 'use strict'; angular.module('changeModule') .factory('ciRelationsModel', ['consoleService', 'configurationModel', 'metadataModel', 'userModel', '$rootScope', '$q', '$window', function (consoleService, configurationModel, metadataModel, userModel, $rootScope, $q, $window) { var ciRelationsModel = {}, //column header cell template for server side sorting, sorting works on all columns except for // Asset Type column (backend does not support this, Reason - Asset Type is a Logical representation of CI, // its not really a parameter in back-end form) and Related column (this is just a UI column) //colHeaderCellTemplate is with sorting colHeaderCellTemplate = '
' + '
{{\'asset.attributes.\' + col.displayName | i18n | uppercase}}
' + '
' + '
' + '
{{col.sortPriority}}
' + '
' + '
', //linkedColHeaderCellTemplate is without sorting linkedColHeaderCellTemplate = '
' + '
{{\'asset.attributes.\' + col.displayName | i18n | uppercase}}
' + '
' + '
', //nameCellTemplate nameCellTemplate = '
' + '{{row.getProperty(col.field)}}
', //linkedCellTemplate is for related columns rows linkedCellTemplate = '
' + '
', columnPreferenceGroup = 'CISearchColumns', columnPreferenceDetailsId = '', extendColumnConfig = function () { _.forEach(ciRelationsModel.columnsConfig, function (column) { var headerCellTemplate, cellTemplate; //set header cell template if (column.name === 'name' || column.name === 'classId' || column.name === 'status' || column.name === 'serialNumber' || column.name === 'site' || column.name === 'productName') { headerCellTemplate = colHeaderCellTemplate; } else if (column.name === 'type' || column.name === 'related') { headerCellTemplate = linkedColHeaderCellTemplate; } //set cell template if (column.name === 'name') { cellTemplate = nameCellTemplate; } if (column.name === 'related') { var minWidth = 270; //min width is needed only for Related column cellTemplate = linkedCellTemplate; var maxWidth = 300; } //set initial width for columns var gridWidth = $window.innerWidth - 640; column.width = column.width ? column.width : gridWidth / 7; angular.extend(column, { headerCellTemplate: headerCellTemplate, cellTemplate: cellTemplate, minWidth: minWidth, maxWidth: maxWidth, width: minWidth ? minWidth : column.width }); }); }; ciRelationsModel.finalLinkedCount = 0; ciRelationsModel.populateConfiguration = function () { if (!_.isEmpty(ciRelationsModel.columnsConfig)) { return $q.when(1); } return $q.all([userModel.getUserPreferences(columnPreferenceGroup)]).then(function (config) { if (config[0]) { var columnConfig = config[0], columns = columnConfig.length ? columnConfig[0].value : {}; } else { columnConfig = []; } columnPreferenceDetailsId = columnConfig.length ? columnConfig[0].id : null; ciRelationsModel.columnsConfig = _.merge(configurationModel.get('ciSearch.columns'), columns); extendColumnConfig(); return ciRelationsModel.columnsConfig; }); }; ciRelationsModel.updateColumnConfig = function (columnsConfig) { userModel.updateUserPreferences(columnPreferenceGroup, { id: columnPreferenceDetailsId, name: 'columns', value: columnsConfig }).then(function (props) { if (!columnPreferenceDetailsId) { columnPreferenceDetailsId = props[0].id; } }); }; return ciRelationsModel; } ]); }());