SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/common/launch-actions-directive.js

159 lines
9.3 KiB
JavaScript

"use strict";
/**
* Created by sohan.rao on 06/03/2015.
*/
(function () {
'use strict';
angular.module('myitsmApp')
.directive('launchActionsDropdown', ['$rootScope', '$filter', 'events', 'screenConfigurationModel', 'screenConfigurationService', 'metadataModel', 'i18nService',
function ($rootScope, $filter, events, screenConfigurationModel, screenConfigurationService, metadataModel, i18nService) {
return {
restrict: 'E',
scope: {
context: '=',
dropDownOptions: '=',
bulkContext: '=',
bulkContextType: '@',
launchActionCallback: '&',
hideDropdown: '=?'
},
templateUrl: 'views/common/launch-actions-dropdown.html',
controller: 'LaunchActionController',
link: function (scope) {
var contextType, metadata, showMoreButton = false;
scope.checkValidActions = true;
scope.hideDropdown = true;
var init = function (actions) {
if (actions && actions.actionList && actions.actionList.length) {
_.forEach(actions.actionList, function (actionItem) {
actionItem.label = actionItem.labels[i18nService.language] || actionItem.labels.default;
if (actionItem.mappedFields && actionItem.mappedFields.length === 0) {
showMoreButton = true;
}
});
if (actions.actionOrder === 'alphabetical') {
actions.actionList = _.sortBy(actions.actionList, 'label');
}
else {
actions.actionList = _.sortBy(actions.actionList, 'sequenceNo');
}
// actions are already populated and stored in the scope
scope.actions = actions;
// call updateActionItemsDisplay method depends on context. If we showing it from asset profile screen, send isBulk = 'false' as parameter
// And if its bulk context from asset console, send isBulk = 'true'
if (scope.context && scope.context.ticketType === EntityVO.TYPE_ASSET) {
updateActionItemsDisplay(false);
}
else if (scope.bulkContextType === EntityVO.TYPE_ASSET) {
updateActionItemsDisplay(true);
}
}
if (showMoreButton || (scope.dropDownOptions && scope.dropDownOptions.actions && scope.dropDownOptions.actions.length)) {
scope.hideDropdown = false;
}
};
var updateActionItemsDisplay = function (isBulk) {
var isInvalid, selectedTypes, selectedClassIds, actionClassIds;
if (isBulk && scope.bulkContext && scope.bulkContext.length) {
selectedTypes = _.uniqBy(_.map(scope.bulkContext, 'type'));
selectedClassIds = _.uniqBy(_.map(scope.bulkContext, 'classId'));
}
else if (scope.context && scope.context.ticketType === EntityVO.TYPE_ASSET) {
selectedTypes = [scope.context.type];
selectedClassIds = [scope.context.classId];
}
else {
return;
}
if (!scope.actions) {
return;
}
_.forEach(scope.actions.actionList, function (action) {
if (isBulk && action.actionType !== 'launch') {
action.invalid = true;
return;
}
isInvalid = action.scope.assetClass.ALL; // if All then action is valid for all selections
if (isInvalid) {
action.invalid = !isInvalid;
return;
}
isInvalid = _.some(selectedTypes, function (type) {
return !_.includes(_.keys(action.scope.assetClass), type);
});
if (isInvalid) { //all selected asset types are not valid for this action
action.invalid = isInvalid;
return;
}
//if all selected types are valid, check for subtypes/class ids
actionClassIds = [];
_.forIn(action.scope.assetClass, function (value, key) {
if (action.scope.assetClass[key][0] === 'ALL') {
//get all subtypes for this type from metadata for comparison
actionClassIds = actionClassIds.concat(_.map(_.find(metadata.assetTypes, { 'name': key }).subType, 'name'));
}
else {
actionClassIds = actionClassIds.concat(value);
}
});
isInvalid = _.some(selectedClassIds, function (classId) {
return !_.includes(actionClassIds, classId);
});
action.invalid = isInvalid;
});
scope.checkValidActions = _.some(scope.actions.actionList, function (action) {
return !action.invalid;
});
};
if (angular.isDefined(scope.context) && (scope.context.type || scope.context.ticketType)) {
contextType = scope.context.ticketType && scope.context.ticketType === EntityVO.TYPE_ASSET ?
scope.context.ticketType : scope.context.type;
}
else if (scope.bulkContextType) {
contextType = scope.bulkContextType;
}
if (contextType) {
metadataModel.getMetadataByType(contextType).then(function (data) {
metadata = data;
if (contextType === EntityVO.TYPE_INCIDENT || contextType === EntityVO.TYPE_CHANGE || contextType === EntityVO.TYPE_WORKORDER || contextType === EntityVO.TYPE_TASK) {
screenConfigurationModel.loadActionsForRuntimeV2(contextType).then(function () {
var contextActions = screenConfigurationModel.runtimeActionsCache[contextType];
contextActions.actionList = _.reject(contextActions.actionList, function (action) {
return !_.includes(action.supportedPlatforms, 'web');
});
_.forEach(contextActions.actionList, function (action) {
action.invalid = false;
if (contextType === scope.bulkContextType && action.actionType !== 'launch') {
action.invalid = true;
}
});
init(contextActions);
});
}
else {
screenConfigurationModel.loadActionsForRuntime(contextType).then(function () {
var contextActions = screenConfigurationModel.runtimeActionsCache[contextType];
contextActions.actionList = _.reject(contextActions.actionList, function (action) {
return !_.includes(action.supportedPlatforms, 'web');
});
_.forEach(contextActions.actionList, function (action) {
action.invalid = false;
if (contextType === scope.bulkContextType && action.actionType !== 'launch') {
action.invalid = true;
}
});
init(contextActions);
});
}
});
}
scope.$watch('bulkContext', function () {
if (scope.bulkContextType === EntityVO.TYPE_ASSET) {
updateActionItemsDisplay(true);
}
}, true);
}
};
}]);
}());