48 lines
2.1 KiB
JavaScript
48 lines
2.1 KiB
JavaScript
"use strict";
|
|
(function () {
|
|
'use strict';
|
|
angular.module('myitsmApp')
|
|
.factory('historyModel', ['localStorageService',
|
|
function (localStorageService) {
|
|
var historyModel = {
|
|
historyList: angular.fromJson(sessionStorage.getItem('historyItems')) || [],
|
|
groupedHistoryList: {},
|
|
userId: localStorageService.get('user.userId')
|
|
}, historySize = 15;
|
|
historyModel.historyList = _.filter(historyModel.historyList, { userId: historyModel.userId });
|
|
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');
|
|
historyModel.groupedHistoryList = ([groupedObj[EntityVO.TYPE_TICKET] || [], groupedObj[EntityVO.TYPE_KNOWLEDGE]]);
|
|
}
|
|
return historyModel;
|
|
}
|
|
]);
|
|
}());
|