SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/common/history-model.js

52 lines
2.5 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('myitsmApp')
.factory('historyModel', ['localStorageService', 'metadataModel', 'configurationModel',
function (localStorageService, metadataModel, configurationModel) {
var historyModel = {
historyList: angular.fromJson(sessionStorage.getItem('historyItems')) || [],
groupedHistoryList: {},
userId: localStorageService.get('user.userId')
}, historySize = 15;
historyModel.historyList = _.filter(historyModel.historyList, { userId: historyModel.userId });
metadataModel.getMetadataByType('global').then(function () {
setGroupedHistoryList();
});
setGroupedHistoryList();
historyModel.addToHistory = function (type, data) {
var historyObj = new HistoryVO().build(data);
if (data.type == EntityVO.TYPE_KNOWLEDGE) {
historyObj.isDecisionTree = data.isDecisionTree();
}
historyObj.userId = historyModel.userId;
setHistoryItem(historyObj);
if (historyModel.historyList.length > historySize) {
historyModel.historyList.shift();
}
sessionStorage.setItem('historyItems', angular.toJson(historyModel.historyList));
setGroupedHistoryList();
};
function setHistoryItem(item) {
var existingItem = _.find(historyModel.historyList, { id: item.id });
if (existingItem) {
angular.extend(existingItem, item);
}
else {
historyModel.historyList.push(item);
}
}
historyModel.clearHistory = function () {
historyModel.historyList = [];
sessionStorage.removeItem('historyItems');
};
function setGroupedHistoryList() {
var groupedObj = _.groupBy(historyModel.historyList, 'type'), groupedList = configurationModel.comaroundEnabled ? ([Array.prototype.concat(groupedObj[EntityVO.TYPE_TICKET] || [], groupedObj[EntityVO.TYPE_SBEREQUEST] || [])])
: ([Array.prototype.concat(groupedObj[EntityVO.TYPE_TICKET] || [], groupedObj[EntityVO.TYPE_SBEREQUEST] || []), groupedObj[EntityVO.TYPE_KNOWLEDGE]]);
historyModel.groupedHistoryList = groupedList;
}
return historyModel;
}
]);
}());