SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/admin/screen-configuration/custom-action-editor-contro...

893 lines
45 KiB
JavaScript

"use strict";
/*
* Author: Srinivas R
*/
(function () {
'use strict';
angular.module('adminModule').controller('CustomActionEditorController', ['$filter', '$modalInstance', '$rootScope', '$scope', '$timeout', 'events', 'screenObj', 'screenConfigurationModel', 'systemAlertService', 'i18nService', '$q', 'metadataModel',
function ($filter, $modalInstance, $rootScope, $scope, $timeout, events, screenObj, screenConfigurationModel, systemAlertService, i18nService, $q, metadataModel) {
$scope.acceleratorsList = [];
$scope.providerFieldList = [];
$scope.providerMode = [{ label: i18nService.getLocalizedString('customization.globalMenu.action.mode.both'), value: 'both' }, { label: i18nService.getLocalizedString('customization.globalMenu.action.mode.view'), value: 'view' }, { label: i18nService.getLocalizedString('customization.globalMenu.action.mode.edit'), value: 'edit' }];
$scope.mappedSourceList = [{ label: i18nService.getLocalizedString('customization.globalMenu.action.fromTicket'), value: 'ticket' }, { label: i18nService.getLocalizedString('customization.globalMenu.action.userPrompt'), value: 'prompt' }, { label: i18nService.getLocalizedString('customization.globalMenu.action.defaultValue'), value: 'default' }];
$scope.isTemplateLoaded = false;
var typeAheadMode = false, gotFullMatch = false, actualTypeAheadText, expressionStart = 0, expressionEnd = 0;
var textFieldActionURL;
var keyCodes = {
enter: 13,
escape: 27,
tab: 9,
leftArrow: 37,
rightArrow: 39,
upArrow: 38,
downArrow: 40,
space: 32
};
var isV2CompatibleScreen = false;
$scope.actionObj = {};
$scope.hasInputFields = false;
$scope.hasOutputFields = false;
var preventDefaultForKeys = [keyCodes.leftArrow, keyCodes.rightArrow, keyCodes.enter, keyCodes.upArrow, keyCodes.downArrow, keyCodes.tab];
$scope.accelerators = {
expression: '',
showAcceleratorsList: false
};
$scope.acceleratorList = [];
$scope.handleBodyKeyDown = function ($event) {
if (($event.which === 219 || $event.keyCode === 219 || $event.key === '[')) {
$scope.accelerators.showAcceleratorsList = true;
expressionStart = $event.target.selectionStart;
typeAheadMode = true;
}
if ($scope.accelerators.showAcceleratorsList && (preventDefaultForKeys.indexOf($event.keyCode) >= 0 || ($event.keyCode === keyCodes.space && gotFullMatch))) {
$event.preventDefault();
}
};
$scope.handleBodyChange = function ($event) {
textFieldActionURL = $event.target; //angular.element('.active .action-url')[0]; /* IE 9 fix */
var actionText = textFieldActionURL.value;
var pos = -1, flag = false, temp;
if (!$scope.accelerators.showAcceleratorsList) {
for (temp = 0; temp < actionText.length; temp++) {
if (actionText[temp] === '[' && !flag) {
flag = true;
pos = temp;
}
else if (actionText[temp] === ']' && flag) {
flag = false;
pos = -1;
}
}
if (pos > -1) {
$scope.accelerators.showAcceleratorsList = true;
expressionStart = pos;
typeAheadMode = true;
if (preventDefaultForKeys.indexOf($event.keyCode) >= 0 || ($event.keyCode === keyCodes.space && gotFullMatch)) {
$event.preventDefault();
}
}
}
expressionEnd = $event.target.selectionEnd;
if (textFieldActionURL.value.length > 1 && typeAheadMode && (expressionStart > expressionEnd)) {
$scope.accelerators.showAcceleratorsList = false;
return;
}
if ($scope.accelerators.showAcceleratorsList) {
if (gotFullMatch && [keyCodes.tab, keyCodes.rightArrow, keyCodes.space].indexOf($event.keyCode) >= 0) {
gotFullMatch = false;
return;
}
if (typeAheadMode && ($event.keyCode === keyCodes.tab)) {
var urlText = textFieldActionURL.value;
($scope.typeAheadListPos === $scope.acceleratorsList.length - 1) ? $scope.typeAheadListPos = 0 : $scope.typeAheadListPos++;
var selectedItem = $scope.acceleratorsList[$scope.typeAheadListPos] ? $scope.acceleratorsList[$scope.typeAheadListPos].name : '';
var updatedText = [urlText.slice(0, expressionStart), selectedItem, urlText.slice(expressionEnd)].join('');
expressionEnd = parseInt(expressionStart + selectedItem.length);
actualTypeAheadText = updatedText.substring(expressionStart, expressionEnd);
//$scope.openActionItem.url = updatedText;
$timeout(function () {
textFieldActionURL.setSelectionRange(expressionStart, expressionEnd);
textFieldActionURL.selectionStart = expressionStart;
textFieldActionURL.selectionEnd = expressionEnd;
});
return;
}
// ignore if control or alternate key was pressed
if ($event.ctrlKey === true || $event.altKey === true || $event.keyCode === keyCodes.leftArrow || $event.keyCode === keyCodes.rightArrow) {
return;
}
else if ($event.keyCode === keyCodes.upArrow) {
if ($scope.typeAheadListPos > 0) {
$scope.typeAheadListPos--;
}
return;
}
else if ($event.keyCode === keyCodes.downArrow) {
if ($scope.typeAheadListPos < ($scope.acceleratorsList.length - 1)) {
$scope.typeAheadListPos++;
}
return;
}
else if ($event.keyCode === keyCodes.enter) {
$scope.accelerators.expression = actualTypeAheadText;
$scope.insertAcceleratorText();
return;
}
else if ($event.keyCode === keyCodes.escape) {
$scope.disableTypeAheadMode();
return;
}
}
if ($scope.openActionItem.url && $scope.openActionItem.url.length) {
if (typeAheadMode) {
actualTypeAheadText = textFieldActionURL.value.substring(expressionStart, expressionEnd);
$scope.typeAheadListPos = 0;
}
}
else if (typeAheadMode) {
$scope.hideTypeAheadPopup();
}
if (typeAheadMode) {
if (actualTypeAheadText.length > 0 && actualTypeAheadText !== $scope.accelerators.expression) {
$scope.acceleratorsList = $filter('filter')($scope.accelerators.availableExpressions, { name: actualTypeAheadText.substring(1) });
if ($scope.acceleratorsList.length > 0 || $scope.accelerators.expression.length === 0) {
($scope.acceleratorsList.length > 0) ? $scope.accelerators.showAcceleratorsList = true : angular.noop();
$scope.accelerators.expression = actualTypeAheadText;
$scope.popupLeftPosition = 0;
$scope.popupTopPosition = 0;
}
else if ($scope.acceleratorsList.length === 0 && $scope.accelerators.expression.length > 1) {
$scope.hideTypeAheadPopup();
}
}
}
};
$scope.handleBodyClick = function ($event) {
expressionEnd = $event.target.selectionEnd;
if ($event.target.value.length > 1 && $scope.accelerators.showAcceleratorsList && expressionStart > expressionEnd) {
$scope.accelerators.showAcceleratorsList = false;
return;
}
};
$scope.acceleratorMouseover = function ($index) {
$scope.typeAheadListPos = $index;
};
$scope.insertAcceleratorText = function (selectedItem) {
var urlText = textFieldActionURL.value, accelerator = selectedItem || $scope.acceleratorsList[$scope.typeAheadListPos], insertText = accelerator.name, updatedText = urlText;
if ($scope.accelerators.expression) {
if (expressionStart === 0) {
updatedText = insertText + ' ';
}
else {
if (expressionEnd < urlText.length) {
updatedText = [urlText.slice(0, expressionStart), insertText, urlText.slice(expressionEnd)].join('');
}
else if (expressionEnd === urlText.length) {
var temp = urlText.substr(0, urlText.length - actualTypeAheadText.length) + insertText;
updatedText = temp;
}
}
}
else {
if (urlText.length === 0) {
updatedText = insertText;
}
else {
if (expressionEnd > 0) {
updatedText = [urlText.slice(0, expressionEnd), ' ' + insertText + ' ', urlText.slice(expressionEnd)].join('');
}
else {
updatedText += ' ' + insertText;
}
}
}
$scope.disableTypeAheadMode();
$scope.openActionItem.url = updatedText;
};
$scope.hideTypeAheadPopup = function () {
$scope.accelerators.showAcceleratorsList = false;
};
$scope.disableTypeAheadMode = function () {
actualTypeAheadText = '';
$scope.accelerators.expression = '';
$scope.accelerators.showAcceleratorsList = false;
$scope.typeAheadListPos = 0;
$scope.acceleratorsList = null;
typeAheadMode = false;
expressionStart = 0;
expressionEnd = 0;
};
$scope.acceleratorsHelp = function () {
actualTypeAheadText = '[';
$scope.accelerators.expression = '';
$scope.accelerators.showAcceleratorsList = true;
$scope.typeAheadListPos = 0;
$scope.acceleratorsList = $scope.accelerators.availableExpressions;
expressionStart = angular.element('.active .action-url')[0].value.length;
};
//action accelerator stuff above..
$scope.sortableOptions = {
update: function (e, ui) {
if ($scope.actionOrder !== 'custom') {
ui.item.sortable.cancel();
}
},
stop: function () {
// update field ordering
$scope.actionList.forEach(function (action, index) {
action.sequenceNo = index + 1;
});
}
};
/**
* Handle cancel click
*/
$scope.onCancelClick = function () {
closeEditor();
};
/**
* Handle save click
*/
$scope.onSaveClick = function () {
var postActionList = [];
//Sort Actions on saving
$scope.reOrderActionList($scope.actionOrder);
for (var i = 0; i < $scope.actionList.length; i++) {
var item = $scope.actionList[i];
//group validation of action items
if ((item.actionType === 'provider' && item.actionName === '') ||
(item.actionType === 'client' && item.url === '') ||
item.isLabelEmpty()) {
if (isV2CompatibleScreen) {
if (item.actionType === 'provider' && !item.delete) {
item.parseItem(screenObj);
}
}
item.closeItem();
$scope.openAction(item, true);
$scope.postValidate = true;
return;
}
else if (item.actionType === 'launch') {
var subAction = _.find(item.subActions, { name: 'assetUpdate' });
item.convertToOldVO();
if ((subAction && !subAction.fields.length) || !item.subActions.length) {
item.closeItem();
$scope.openAction(item);
$scope.postValidate = true;
return;
}
}
else if (isV2CompatibleScreen) {
if (item.actionType === 'provider' && !item.delete) {
item.parseItem(screenObj);
if (!isValidProviderV3(item)) {
item.closeItem();
$scope.openAction(item);
return;
}
else {
if (item.expressionCondition) {
if (!item.expressions) {
item.expressions = [];
item.expressions.push({
condition: item.expressionCondition,
resource: item.resource,
sequenceNo: item.sequenceNo,
executeOn: item.selectedExecuteOnProperty.value
});
}
else {
item.expressions[0].condition = item.expressionCondition;
item.expressions[0].executeOn = item.selectedExecuteOnProperty.value;
}
delete item.expressionCondition;
}
else {
if (item.expressions) {
delete item.expressions;
delete item.expressionCondition;
}
}
delete item.isTemplateFromList;
}
}
else if (item.actionType === 'client') {
item.screenId = screenObj.id;
}
}
else {
item.convertToOldVO();
}
//remove label before saving, as this is breaking delete for non V2 screens
delete item.label;
postActionList.push(item);
}
$scope.dataLoading = true;
if (isV2CompatibleScreen) {
screenConfigurationModel.providerHardReload = true;
screenConfigurationModel.createUrlAction(postActionList, $scope.actionOrder, $scope.resouceType)
.then(handleActionConfigurationSaveSuccess, handleActionConfigurationSaveFault);
}
else {
screenConfigurationModel.saveActionConfiguration(postActionList, $scope.actionOrder, $scope.resouceType)
.then(handleActionConfigurationSaveSuccess, handleActionConfigurationSaveFault);
}
};
$scope.clear = function () {
$scope.actionObj.actionName = '';
$scope.isTemplateLoaded = false;
};
$scope.clearTemplateFields = function () {
$scope.isTemplateLoaded = false;
};
/**
* Handle new provider validation
*/
function isValidProviderV3(item) {
var isValid = true, matchedFieldArray, matchedTemplateArray;
matchedTemplateArray = _.filter($scope.templateList, function (obj) {
return obj.name === item.actionName;
});
if (matchedTemplateArray.length === 0 && !item.delete) {
item.isTemplateFromList = false;
isValid = false;
}
_.forEach(item.mappings, function (mappedObj) {
mappedObj.error = false;
if (mappedObj.type === 'input-ticket' || mappedObj.type === 'output') {
matchedFieldArray = _.filter($scope.acceleratorList, function (obj) {
return obj.key === mappedObj.mappedFieldValue;
});
if (matchedFieldArray.length === 0) {
mappedObj.error = true;
isValid = false;
}
}
if (mappedObj.type === 'input-default' && ((mappedObj.mappedFieldValue === '' && mappedObj.dataType === 'text') || ((mappedObj.mappedFieldValue.trim() === '' || isNaN(mappedObj.mappedFieldValue)) && mappedObj.dataType === 'number'))) {
mappedObj.error = true;
isValid = false;
}
if (!mappedObj.error) {
delete mappedObj.error;
}
});
return isValid;
}
/**
* Handle successful configuration update
*/
function handleActionConfigurationSaveSuccess() {
$scope.actionList = screenConfigurationModel.actionList;
$scope.actionOrder = screenConfigurationModel.actionOrder;
$scope.dataLoading = false;
var message = $filter('i18n')('screenConfiguration.customActionEditor.configurationUpdatedSuccessfully');
systemAlertService.success({ text: message, clear: true, hide: 10000 });
closeEditor();
//auto-update runtime actions
if (isV2CompatibleScreen) {
screenConfigurationModel.loadActionsForRuntimeV2($scope.resouceType, true).then(function () {
if ($scope.resouceType === 'global') {
$rootScope.$broadcast(events.GLOBAL_MENU_ACTIONS_UPDATE);
}
});
}
else {
screenConfigurationModel.loadActionsForRuntime($scope.resouceType, true).then(function () {
if ($scope.resouceType === 'global') {
$rootScope.$broadcast(events.GLOBAL_MENU_ACTIONS_UPDATE);
}
});
}
}
/**
* Handle configuration update fault
*/
function handleActionConfigurationSaveFault() {
$scope.dataLoading = false;
var message = $filter('i18n')('screenConfiguration.customActionEditor.configurationUpdateFailure');
systemAlertService.error({ text: message, clear: false });
closeEditor();
}
/**
*
*/
$scope.reOrderActionList = function (orderBy) {
$scope.closeAllAction();
$scope.actionOrder = orderBy;
if (orderBy !== 'custom') {
$scope.sortableOptions.disabled = true;
$scope.actionList = _.sortBy($scope.actionList, function (a) {
return a.labels.default || $filter('i18n')('customization.globalMenu.action.label.default');
});
$scope.actionList.forEach(function (action, index) {
action.sequenceNo = index + 1;
});
}
else {
$scope.sortableOptions.disabled = false;
}
};
/**
*
*/
$scope.closeAllAction = function () {
$scope.actionList.forEach(function (item) {
item.closeItem();
});
};
/**
*
*/
var hasIOFields = function (mappings) {
$scope.hasInputFields = false;
$scope.hasOutputFields = false;
if (mappings && mappings.length > 0) {
for (var i in mappings) {
if (mappings[i].type.indexOf('input') === 0) {
$scope.hasInputFields = true;
}
if (mappings[i].type === 'output') {
$scope.hasOutputFields = true;
}
}
}
};
var loadTemplateById = function (templateId, isActionInValid) {
$scope.isTemplateLoading = true;
screenConfigurationModel.getTemplateById(templateId).then(function (templateObj) {
$scope.actionObj = $scope.openActionItem;
if (templateObj && $scope.actionObj.isV3ProviderAction()) {
if ($scope.actionObj.templateId === templateObj.id && !isActionInValid) {
_.forEach($scope.actionObj.mappings, function (mapObj) {
var tempObj = _.find(templateObj.mappings, { mappedFieldId: mapObj.mappedFieldId, type: (mapObj.type.indexOf('input') === 0) ? 'input' : 'output' });
if (!tempObj) {
mapObj = _.assign(mapObj, { delete: true }); // mark for delete
}
else {
mapObj = _.assign(mapObj, { paramType: (mapObj.type.indexOf('input') === 0) ? 'input' : 'output' }); // add core type
}
});
_.remove($scope.actionObj.mappings, { delete: true }); // remove delete marked fields
_.forEach(templateObj.mappings, function (tempObj) {
var mapObj = _.find($scope.actionObj.mappings, { mappedFieldId: tempObj.mappedFieldId, paramType: tempObj.type });
if (!mapObj) {
$scope.actionObj.mappings.push(mapNewTemplateField(tempObj)); // add new fields
}
else {
mapObj.mappedFieldName = (tempObj.mappedField && tempObj.mappedField.label) || tempObj.mappedFieldName;
mapObj.sequence = tempObj.sequence;
getMappedDataTypeAndOptions(mapObj, tempObj);
mapObj.mappedSource = _.find($scope.mappedSourceList, { value: mapObj.type.substring(6) });
if (!mapObj.mappedFieldValue) {
mapObj.mappedFieldValue = '';
}
}
});
}
else {
$scope.actionObj.mode = _.find($scope.providerMode, { value: 'both' });
$scope.actionObj.templateId = templateObj.id;
$scope.actionObj.formName = templateObj.formName;
_.forEach(templateObj.mappings, function (mappObj) {
mapNewTemplateField(mappObj);
});
$scope.actionObj.mappings = templateObj.mappings;
$scope.actionObj.isSynchronous = !_.isUndefined(_.find(templateObj.mappings, { type: 'output' }));
}
}
hasIOFields($scope.actionObj.mappings);
$scope.isTemplateLoaded = true;
$scope.isTemplateLoading = false;
}, function () {
});
};
var mapNewTemplateField = function (mappedObj) {
mappedObj.mappedSource = _.find($scope.mappedSourceList, { value: 'ticket' });
getMappedDataTypeAndOptions(mappedObj, mappedObj);
return mappedObj;
};
var getMappedDataTypeAndOptions = function (mappedObj, tempMappedObj) {
mappedObj.dataType = tempMappedObj.mappedField && tempMappedObj.mappedField.dataType ? tempMappedObj.mappedField.dataType : 'text';
mappedObj.mappingOptions = $scope.mappedSourceList;
/*
if(_.includes(['currency', 'datetime', 'date', 'time', 'dropdown', 'menu', 'enum'], tempMappedObj.mappedField.dataType)){
mappedObj.mappingOptions.push($scope.mappedSourceList[0]);
} else {
mappedObj.mappingOptions = $scope.mappedSourceList;
mappedObj.dataType = _.includes(['integer', 'decimal', 'real'], tempMappedObj.mappedField.dataType) ? 'number' : 'text';
}*/
};
$scope.openAction = function (action, isActionInValid) {
if (action.isOpen) {
action.closeItem();
}
else {
$scope.closeAllAction();
$scope.openActionItem = action;
action.openItem();
if (action.actionType !== 'client') {
if (action.mode && !action.mode.value) {
action.mode = _.find($scope.providerMode, { value: action.mode });
}
loadTemplateById(action.templateId, isActionInValid);
}
}
};
/**
*
*/
$scope.createAction = function () {
var newAction = new ActionVO();
var res = $scope.resouceType || 'global';
newAction.setResource(res);
if (!$scope.actionList) {
$scope.actionList = [];
}
if ($scope.resouceType === 'asset') {
newAction.setAssetScope();
}
newAction.mode = 'both';
var type = $scope.resouceType.toLowerCase();
if (screenConfigurationModel.isTicketEnabledForProviderActionExpression(type)) {
newAction.selectedExecuteOnProperty = $scope.providerActionExecuteOnPropOptions[0];
}
$scope.actionList.push(newAction);
$scope.reOrderActionList($scope.actionOrder);
$scope.openAction(newAction);
};
/**
*
*/
$scope.addLabel = function (action) {
for (var key in $scope.supportedLocales) {
if ($scope.supportedLocales.hasOwnProperty(key) && !action.labels.hasOwnProperty(key)) {
action.labels[key] = '';
return;
}
}
};
/**
*
*/
$scope.removeLabel = function (action, label) {
action.removeLabel(label);
};
/**
*
*/
$scope.updateLabel = function (action, locale, selLocale) {
action.labels[selLocale] = action.labels[locale];
$scope.removeLabel(action, locale);
};
/**
*
*/
$scope.deleteAction = function ($event, action) {
$event.stopPropagation();
var modalConfirmationInstance = systemAlertService.modal({
title: i18nService.getLocalizedString('common.notification.delete.action.title'),
text: i18nService.getLocalizedString('common.notification.delete.action.message'),
buttons: [
{
text: i18nService.getLocalizedString('common.labels.yes'),
data: action
},
{
text: i18nService.getLocalizedString('common.labels.no')
}
]
});
modalConfirmationInstance.result.then(function (action) {
if (!_.isEmpty(action)) {
action.deleteItem();
_.remove($scope.actionList, function (actionObj) {
return !actionObj.id && actionObj.delete;
});
}
});
};
// /**
// *
// */
// $scope.changeActionType = function (action, type) {
// action.actionType = type;
// };
/**
*
*/
$scope.toggleSupportedType = function (action, type) {
if (action.isSupported(type)) {
for (var i = 0; i < action.supportedPlatforms.length; i++) {
if (action.supportedPlatforms[i] === type) {
break;
}
}
action.supportedPlatforms.splice(i, 1);
}
else {
action.supportedPlatforms.push(type);
}
};
$scope.toggleUserInput = function (action, subactiontype) {
if (!_.find(action.subActions, { name: subactiontype })) {
var subaction = {
resource: 'asset',
name: subactiontype
};
if (subactiontype === 'assetUpdate') {
subaction.fields = [];
}
action.subActions.push(subaction);
}
else {
action.subActions = _.reject(action.subActions, { name: subactiontype });
}
};
$scope.providerActionExecuteOnPropOptions = [
{
name: $filter('i18n')('screenConfiguration.providerAction.executeOn.OnFieldValueChange'),
value: 'On Field Value Change'
},
{
name: $filter('i18n')('screenConfiguration.providerAction.executeOn.OnLoad'),
value: 'On Ticket Load'
}
];
$scope.updateproviderActionExecuteOnProperty = function (item, action) {
action.selectedExecuteOnProperty = item;
};
/**
* Close editor
*/
function closeEditor() {
$modalInstance.dismiss();
}
/**
* get supported locales
*
* @returns {Array} loacles
*/
function getSupportedLocales() {
var temp = {}, langs = ['default', 'en', 'es', 'fr', 'zh', 'it', 'ko', 'de', 'ru', 'ja', 'he', 'pt', 'pl'];
_.forEach(langs, function (value) {
temp[value] = $filter('i18n')('customization.locale.label.' + value);
console.log(value);
});
return temp;
}
$scope.checkItem = function (action, type, subType) {
var assetClassMap = action.scope.assetClass, checkFlag = subType ? subType.name : "ALL", classMapType = type.name || type.label;
action.scope.assetClass.ALL = false;
if (!action.isAssetClassChecked(classMapType, checkFlag)) {
if (checkFlag === 'ALL') {
assetClassMap[classMapType] = [checkFlag];
}
else if (assetClassMap[classMapType] && assetClassMap[classMapType].length) {
assetClassMap[classMapType].push(checkFlag);
}
else {
assetClassMap[classMapType] = [checkFlag];
}
}
else {
assetClassMap[classMapType].splice(assetClassMap[classMapType].indexOf(checkFlag), 1);
}
};
$scope.expandType = function (type, $event) {
if ($event.target.className !== "dropdown-item-inline_selected" && $event.target.className !== "dropdown-item-inline") {
_.forOwn($scope.assetTypes, function (assetType) {
if (type.label !== assetType.label) {
assetType.expanded = false;
}
});
type.expanded = !type.expanded;
}
};
$scope.addAssetFieldToUpdate = function (action, field) {
_.forOwn(action.subActions, function (subAction) {
if (subAction.name === 'assetUpdate') {
var index = subAction.fields.indexOf(field);
if (index === -1) {
subAction.fields.push(field);
}
else {
if (subAction.fields.length === 1) {
subAction.fields = [];
}
else {
subAction.fields.splice(index, 1);
}
}
}
});
};
$scope.checkAll = function (action) {
action.scope.assetClass.ALL = !action.scope.assetClass.ALL;
};
var setFieldList = function (fieldList, type) {
$scope[type + 'List'] = [];
for (var i in fieldList) {
if (type === 'accelerator') {
$scope[type + 'List'].push({
label: fieldList[i],
key: i
});
}
else {
$scope[type + 'List'].push(fieldList[i]);
}
}
};
var loadTemplateList = function (type) {
screenConfigurationModel.getTemplateList(false, type)
.then(function (data) {
setFieldList(data, 'template');
}, function () {
});
};
$scope.loadAcceleratorList = function (type) {
screenConfigurationModel.getAccelerators(type, $scope.screenObj.name)
.then(function (data) {
setFieldList(data, 'accelerator');
}, function () {
});
};
$scope.onTemplateSelect = function (templateObj) {
$scope.isTemplateLoaded = false;
loadTemplateById(templateObj.id);
};
$scope.updateFieldType = function (field, item) {
field.mappedSource = item;
};
$scope.getFieldLabel = function (fieldId) {
var fieldObj = _.find($scope.acceleratorList, { key: fieldId });
if (fieldObj) {
return fieldObj.label;
}
else {
return i18nService.getLocalizedString('asset.explorer.field.na');
}
};
$scope.toggleExecuteOn = function (actionObj) {
actionObj.expressionCondition = actionObj.showExecuteOn ? actionObj.expressionCondition : null;
};
/**
* Entry point
*/
function init() {
$scope.dataLoading = true;
var type, acceleratorsPromise, screenConfigPromise;
$scope.screenObj = screenObj;
$scope.resouceType = (typeof screenObj !== 'undefined' && screenObj.hasOwnProperty('datasource')) ? screenObj.datasource.toLowerCase() : 'global';
if ($scope.resouceType !== 'global') {
isV2CompatibleScreen = screenObj.isV2Compatible();
}
if (isV2CompatibleScreen) {
loadTemplateList($scope.resouceType);
}
var populateAccelerators = function (dataMap, isNew) {
$scope.acceleratorsList = [];
$scope.reverseMap = {};
if (dataMap) {
for (var itsmField in dataMap) {
if (dataMap.hasOwnProperty(itsmField)) {
$scope.reverseMap[dataMap[itsmField]] = itsmField;
if (isNew) {
$scope.acceleratorsList.push({
name: '[' + itsmField + ']',
desc: dataMap[itsmField]
});
}
else {
$scope.acceleratorsList.push({
name: '[' + dataMap[itsmField] + ']',
desc: itsmField
});
}
}
}
}
};
var populateAcceleratorsForExpressions = function (dataMap) {
$scope.acceleratorsListForExpressions = [];
$scope.reverseMap = {};
var accelarator;
if (dataMap) {
for (var itsmField in dataMap) {
if (!dataMap.hasOwnProperty(itsmField)) {
continue;
}
accelarator = '$' + itsmField;
if (/[^a-zA-Z0-9_$]+/g.test(accelarator)) {
//has special char in field name so wrap it and escape any '
accelarator = "'" + accelarator.replace("'", "\'") + "'";
}
$scope.acceleratorsListForExpressions.push({
name: accelarator,
desc: dataMap[itsmField]
});
}
$scope.acceleratorsListForExpressions = _.sortBy($scope.acceleratorsListForExpressions, 'name');
}
};
if (isV2CompatibleScreen) {
type = $scope.resouceType.toLowerCase();
acceleratorsPromise = screenConfigurationModel.getAccelerators(type, $scope.screenObj.name).then(function (dataMap) {
setFieldList(dataMap, 'accelerator');
populateAccelerators(dataMap, true);
if (screenConfigurationModel.isTicketEnabledForProviderActionExpression(type)) {
populateAcceleratorsForExpressions(dataMap);
}
});
screenConfigurationModel.providerHardReload = true;
screenConfigPromise = screenConfigurationModel.loadActionsNew($scope.resouceType).then(function () {
$scope.actionList = screenConfigurationModel.actionList;
$scope.actionOrder = screenConfigurationModel.actionOrder;
if (screenConfigurationModel.isTicketEnabledForProviderActionExpression(type)) {
$scope.isExpressionDrivenAction = true;
if ($scope.actionList.length) {
_.forEach($scope.actionList, function (action) {
if (action.expressions && action.expressions.length) {
_.forEach(action.expressions, function (expression) {
action.expressionCondition = expression.condition;
action.showExecuteOn = true;
action.selectedExecuteOnProperty = expression.executeOn ?
_.find($scope.providerActionExecuteOnPropOptions, { value: expression.executeOn }) : $scope.providerActionExecuteOnPropOptions[0];
});
}
else {
action.expressionCondition = "";
action.showExecuteOn = false;
action.selectedExecuteOnProperty = $scope.providerActionExecuteOnPropOptions[0];
}
});
}
}
else {
$scope.isExpressionDrivenAction = false;
}
});
}
else {
type = $scope.resouceType.toLowerCase();
acceleratorsPromise = screenConfigurationModel.getAccelerators(type, $scope.screenObj.name).then(function (dataMap) {
setFieldList(dataMap, 'accelerator');
populateAccelerators(dataMap);
});
screenConfigPromise = screenConfigurationModel.loadActions($scope.resouceType).then(function () {
$scope.actionList = screenConfigurationModel.actionList;
$scope.actionOrder = screenConfigurationModel.actionOrder;
});
}
$scope.assetTypes = [];
$scope.assetTypesSelected = [];
$q.all([acceleratorsPromise, screenConfigPromise]).then(function () {
$scope.accelerators.availableExpressions = $scope.acceleratorsList;
$scope.supportedLocales = getSupportedLocales();
if ($scope.actionOrder !== 'custom') {
$scope.sortableOptions.disabled = true;
}
else {
$scope.sortableOptions.disabled = false;
}
$scope.postValidate = false;
if ($scope.resouceType === EntityVO.TYPE_ASSET) {
metadataModel.getMetadataByType($scope.resouceType).then(function (data) {
$scope.assetTypes = data.assetTypes;
$scope.allChecked = true; //get it from eachobject
$scope.dataLoading = false;
});
$scope.assetUpdateFields = [
'name', 'status', 'statusReason', 'company', 'partNumber', 'productName', 'model', 'manufacturer', 'supplier', 'installationDate', 'room', 'floor', 'site', 'primaryCapability', 'productCategoryTier1', 'productCategoryTier2', 'productCategoryTier3'
];
}
else {
$scope.dataLoading = false;
}
});
}
init();
}]);
})();