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

987 lines
50 KiB
JavaScript

"use strict";
/**
* Created by igor.samulenko on 5/5/2014.
*/
(function () {
'use strict';
angular.module('adminModule').controller('CustomAreaEditorController', ['$q', '$log', '$filter', '$modalInstance', '$rootScope', '$scope', '$timeout', 'customArea', 'allPanels', 'events', 'screenConfigurationModel', 'systemAlertService', 'metadataModel', 'layoutConfigurationModel', 'IconArray', 'keywordEvaluatorService', 'expressionSyntaxTreeBuilder',
function ($q, $log, $filter, $modalInstance, $rootScope, $scope, $timeout, customArea, allPanels, events, screenConfigurationModel, systemAlertService, metadataModel, layoutConfigurationModel, IconArray, keywordEvaluatorService, expressionSyntaxTreeBuilder) {
var allAvailableFields = [], fieldsSelectedInOtherPanels = [], fieldsCopy;
$scope.acceleratorsList = [];
$scope.AssociateActionLists = [];
$scope.icons = IconArray;
$scope.screenName = '';
$scope.sortableOptions = {
placeholder: 'field-item-placeholder',
connectWith: '.group-field-item-container',
update: function (e, ui) {
if ((ui.item.sortable.droptarget.hasClass('group-field-item-container') || ui.item.sortable.droptarget.hasClass('group-field-item__dropbox')) && !ui.item.sortable.model.isMenuField()) {
ui.item.sortable.cancel();
}
else if ((ui.item.sortable.droptarget.hasClass('group-field-item-container') || ui.item.sortable.droptarget.hasClass('group-field-item__dropbox')) && ui.item.sortable.model.isMenuField()) {
// Drag field inside of group
var field = ui.item.sortable.model;
if ($scope.enableExpression) {
_.remove(customArea.addedFields, { name: field.name });
}
// if (field.id && )
ui.item.sortable.model.groupMember = true;
}
if (!(ui.item.sortable.droptarget.hasClass('group-field-item-container') || ui.item.sortable.droptarget.hasClass('group-field-item__dropbox')) && ui.item.sortable.model.isGroupMember()) {
// Drag field from group
ui.item.sortable.model.groupMember = false;
if (ui.item.sortable.model.id && ui.item.sortable.model.parentId) {
if (customArea.hasAvailableSlots()) {
customArea.addField(ui.item.sortable.model);
}
else {
ui.item.sortable.cancel();
}
}
}
}
};
$scope.onSelectIcon = function (icon, field) {
field.selectedIcon = icon;
};
$scope.groupSortableOptions = {
placeholder: 'field-item-placeholder',
connectWith: '.field-item-container',
cancel: '.group-field-item__dropbox, input',
update: function (e, ui) {
$scope.markFieldUpdated(ui.item.sortable.sourceModel);
}
};
var generalFieldAvailabilityMsg = $filter('i18n')('screenConfiguration.customAreaEditor.fieldAvailabilityWarning'), headerMaxFieldsReached = $filter('i18n')('customAreaEditor.headerSection.maxFieldsWarning');
$scope.showAvailabilityWarning = false;
$scope.tooltipPosition = window.isRtl ? 'left' : 'right';
/**
* Handle add field click
* @param {FieldVO} field
*/
$scope.onAddFieldClick = function (field) {
if (!field.isAvailable()) {
//ToDo: Add custom error message when field is not available
/*$scope.showAvailabilityMsg = 'Availability state: ' + field.availability;
$timeout(function () {
$scope.showAvailabilityMsg = 'screenConfiguration.customAreaEditor.fieldAvailabilityWarning';
}, 5000);*/
return;
}
if (customArea.hasAvailableSlots()) {
if (field.id) {
var isRemovedField = false;
customArea.removedFields.forEach(function (item, index) {
if (field.isEqual(item)) {
customArea.removedFields.splice(index, 1);
isRemovedField = true;
return false;
}
});
if (!isRemovedField) {
field.isNewField = true;
}
// restore removed field
customArea.addField(field);
}
else {
var cloneField = angular.copy(field);
cloneField.sealed = field.isSealed();
cloneField.isNewField = true;
customArea.addField(cloneField);
}
refreshAvailableFieldList();
}
};
/**
* Handle expand field click
* @param {FieldVO} field
*/
$scope.onExpandFieldClick = function (field) {
expandField(field);
};
function expandField(field) {
field.expanded = !field.expanded;
return field;
}
/**
* Handle remove field click
* @param {FieldVO} field
*/
$scope.onRemoveFieldClick = function (field) {
customArea.removeField(field);
if (field.id) {
// make field available again
addFieldToAvailableFieldsList(field);
}
refreshAvailableFieldList();
};
/**
* Handle remove field click from grouped field
* @param {FieldVO} groupField, {FieldVO} field
*/
$scope.onRemoveFieldClickFromGroup = function (groupField, field) {
_.remove(groupField.members, function (member) {
return member.name === field.name;
});
// field.groupMember = false;
customArea.removeFieldFromGroup(field);
$scope.markFieldUpdated(groupField);
if (field.id) {
// make field available again
addFieldToAvailableFieldsList(field);
}
refreshAvailableFieldList();
};
$scope.generateSystemFieldRemoveWarningText = function (field) {
var isCreateScreen = (ScreenConfigurationVO.prototype.CREATE_SCREENS.indexOf(customArea.parentScreenName) !== -1), ticketTypeLabel = $filter('i18n')('ticket.type.' + customArea.dataSource), warningLabelKey = 'customAreaEditor.field.systemRequired.remove.warning';
if (field.isGroupField()) {
warningLabelKey = 'customAreaEditor.groupField.systemRequired.remove.warning';
}
else if (field.isWidget()) {
warningLabelKey = 'customAreaEditor.widget.systemRequired.remove.warning';
}
if (isCreateScreen) {
warningLabelKey += '.create';
}
else {
warningLabelKey += '.edit';
}
return $filter('i18n')(warningLabelKey, ticketTypeLabel);
};
$scope.handleRemoveGroupClick = function (groupField) {
var hasSystemFields = groupField.checkSystemRequiredFlag();
if (hasSystemFields) {
groupField.showDeleteConfirmationTooltip = true;
}
else {
$scope.removeGroupField(groupField);
}
};
/**
* Handle remove group field click
* @param {FieldVO} groupField
*/
$scope.removeGroupField = function (groupField) {
groupField.members.forEach(function (field) {
customArea.removeFieldFromGroup(field);
if (field.id) {
// make field available again
if (allAvailableFields.indexOf(field) === -1) {
allAvailableFields.push(field);
}
}
});
customArea.removeField(groupField);
refreshAvailableFieldList();
};
$scope.cancelGroupFieldRemove = function (groupField) {
groupField.showDeleteConfirmationTooltip = false;
};
$scope.onHidePropertyChange = function (field) {
if (field.hide && field.hideConditionFlag === 0) {
field.readOnly = field.readOnlyCondition ? true : false;
field.required = field.requiredCondition ? true : false;
}
if (!field.hide) {
field.hideCondition = '';
field.hideConditionFlag = 0;
}
$scope.markFieldUpdated(field);
};
$scope.onSetValuePropertyChange = function (field) {
if (!field.setValueConditionFlag) {
field.setValueCondition = '';
}
$scope.markFieldUpdated(field);
};
/**
* Handle change of "required" property
* @param {FieldVO} field
*/
$scope.onRequiredPropertyChange = function (field) {
if (field.required) {
field.editable = true;
}
if (field.required && field.readOnly && field.readOnlyConditionFlag === 0) {
field.readOnly = false;
}
if (field.required && field.hide && field.hideConditionFlag === 0) {
field.hide = false;
}
if (!field.required) {
field.requiredCondition = '';
field.requiredConditionFlag = 0;
}
$scope.markFieldUpdated(field);
};
$scope.onAssociatedActionPropertyChange = function (field) {
if (!field.isMapped) {
field.selectedIcon = '';
field.mappedAction = null;
}
};
function onReadOnlyPropertyChange(field) {
field.editable = !field.readOnly;
if (field.hide && field.hideConditionFlag === 0) {
field.hide = false;
}
if (field.readOnly && field.required && field.requiredConditionFlag === 0) {
field.required = false;
}
if (!field.readOnly) {
field.readOnlyCondition = '';
field.readOnlyConditionFlag = 0;
}
$scope.markFieldUpdated(field);
}
/**
* Handle change of "editable" property
* @param {FieldVO} field
*/
$scope.onEditablePropertyChange = function (field, reverse) {
if (reverse) {
onReadOnlyPropertyChange(field);
}
else {
if (!field.editable && field.required) {
field.required = false;
}
}
$scope.markFieldUpdated(field);
};
$scope.markFieldUpdated = function (field) {
screenConfigurationModel.markFieldUpdated(field, customArea);
};
/**
* Handle cancel click
*/
$scope.onCancelClick = function () {
closeEditor();
};
var addFieldsToProviderAction = function (field) {
var mappedFields = {}, mappedAction = {};
mappedFields.fieldId = field.itsmFieldId;
mappedFields.fieldName = field.name;
mappedFields.sequence = 1;
mappedFields.iconName = field.selectedIcon;
mappedAction = field.mappedAction;
//delete added property from field and temporary added name and value property from action.
delete field.isMapped;
delete field.mappedAction;
delete field.selectedIcon;
delete mappedAction.name;
delete mappedAction.value;
//check if mappedAction is available in actionList, if yest don't push action again.
for (var j in $scope.AssociateActionLists) {
if (mappedAction.entryId === $scope.AssociateActionLists[j].entryId) {
if ($scope.AssociateActionLists[j].mappedFields.length > 0) {
for (var k in $scope.AssociateActionLists[j].mappedFields) {
if (mappedFields.fieldId === $scope.AssociateActionLists[j].mappedFields[k].fieldId) {
$scope.AssociateActionLists[j].mappedFields.splice(k, 1);
}
}
}
$scope.AssociateActionLists[j].mappedFields.push(mappedFields);
}
else if (mappedAction.entryId !== $scope.AssociateActionLists[j].entryId) {
if ($scope.AssociateActionLists[j].mappedFields.length > 0) {
for (var l in $scope.AssociateActionLists[j].mappedFields) {
if (mappedFields.fieldId === $scope.AssociateActionLists[j].mappedFields[l].fieldId) {
$scope.AssociateActionLists[j].mappedFields.splice(l, 1);
}
}
}
}
}
};
function removeDuplicateFromActionList() {
var uniqueActionLists = _.uniqBy($scope.AssociateActionLists, function (field) { return field.entryId; });
for (var i in uniqueActionLists) {
var uniqueMappedFields = _.uniqBy(uniqueActionLists[i].mappedFields, function (field) { return field.fieldId; });
uniqueActionLists[i].mappedFields = angular.copy(uniqueMappedFields);
}
$scope.AssociateActionLists = angular.copy(uniqueActionLists);
}
function updateMappedFieldsInProviderAction(fields) {
for (var i in fields) {
if (fields[i].isMapped === false && $scope.AssociateActionLists.length > 0) {
for (var j in $scope.AssociateActionLists) {
var mappedFields = $scope.AssociateActionLists[j].mappedFields;
for (var k in mappedFields) {
if (fields[i].itsmFieldId === mappedFields[k].fieldId) {
mappedFields.splice(k, 1);
$scope.AssociateActionLists[j].mappedFields = mappedFields;
}
}
}
}
else if (fields[i].isMapped === true && fields[i].mappedAction !== null && $scope.AssociateActionLists.length > 0) {
addFieldsToProviderAction(fields[i]);
}
}
}
function removeMappedFieldsFromProviderAction(fields) {
for (var i in fields) {
if ($scope.AssociateActionLists.length > 0) {
for (var j in $scope.AssociateActionLists) {
var mappedFields = $scope.AssociateActionLists[j].mappedFields;
for (var k in mappedFields) {
if (fields[i].itsmFieldId === mappedFields[k].fieldId) {
mappedFields.splice(k, 1);
$scope.AssociateActionLists[j].mappedFields = mappedFields;
}
}
}
}
}
}
/**
* Handle save click
*/
$scope.onSaveClick = function () {
var i, j, field, memberField, parsedExp, k = 1;
for (i = 0; i < customArea.fields.length; i++) {
field = customArea.fields[i];
field.expanded = false;
if (!field.isGroupField()) {
if (field.requiredConditionFlag) {
if (!field.requiredCondition) {
expandField(field);
return false;
}
try {
parsedExp = expressionSyntaxTreeBuilder(field.requiredCondition);
}
catch (e) {
expandField(field);
return false;
}
if (screenConfigurationModel.checkExpressionValid(parsedExp, $scope.acceleratorsList)) {
expandField(field);
return false;
}
}
if (field.readOnlyConditionFlag) {
if (!field.readOnlyCondition) {
expandField(field);
return false;
}
try {
parsedExp = expressionSyntaxTreeBuilder(field.readOnlyCondition);
}
catch (e) {
expandField(field);
return false;
}
if (screenConfigurationModel.checkExpressionValid(parsedExp, $scope.acceleratorsList)) {
expandField(field);
return false;
}
}
if (field.hideConditionFlag) {
if (!field.hideCondition) {
expandField(field);
return false;
}
try {
parsedExp = expressionSyntaxTreeBuilder(field.hideCondition);
}
catch (e) {
expandField(field);
return false;
}
if (screenConfigurationModel.checkExpressionValid(parsedExp, $scope.acceleratorsList)) {
expandField(field);
return false;
}
}
if (field.setValueConditionFlag) {
if (!field.setValueCondition) {
expandField(field);
return false;
}
try {
parsedExp = expressionSyntaxTreeBuilder(field.setValueCondition);
}
catch (e) {
expandField(field);
return false;
}
if (screenConfigurationModel.checkExpressionValid(parsedExp, $scope.acceleratorsList)) {
expandField(field);
return false;
}
}
if (field.isMapped && (!field.mappedAction || !field.selectedIcon)) {
expandField(field);
return false;
}
}
else {
field.label = field.label + "_" + k;
k++;
for (j = 0; j < field.members.length; j++) {
memberField = field.members[j];
memberField.expanded = false;
if (memberField.requiredConditionFlag) {
if (!memberField.requiredCondition) {
expandField(memberField);
return false;
}
try {
expressionSyntaxTreeBuilder(memberField.requiredCondition);
}
catch (e) {
expandField(memberField);
return false;
}
}
if (memberField.readOnlyConditionFlag) {
if (!memberField.readOnlyCondition) {
expandField(memberField);
return false;
}
try {
expressionSyntaxTreeBuilder(memberField.readOnlyCondition);
}
catch (e) {
expandField(memberField);
return false;
}
}
if (memberField.hideConditionFlag) {
if (!memberField.hideCondition) {
expandField(memberField);
return false;
}
try {
expressionSyntaxTreeBuilder(memberField.hideCondition);
}
catch (e) {
expandField(memberField);
return false;
}
}
if (memberField.setValueConditionFlag) {
if (!memberField.setValueCondition) {
expandField(memberField);
return false;
}
try {
expressionSyntaxTreeBuilder(memberField.setValueCondition);
}
catch (e) {
expandField(memberField);
return false;
}
}
if (memberField.isMapped && (!memberField.mappedAction || !memberField.selectedIcon)) {
expandField(memberField);
return false;
}
}
if (field.members.length === 0) {
expandField(field);
var message = $filter('i18n')('customAreaEditor.field.groupField.empty');
systemAlertService.error({ text: message, clear: false });
return false;
}
updateMappedFieldsInProviderAction(field.members);
}
}
updateMappedFieldsInProviderAction(customArea.fields);
for (i = 0; i < customArea.addedFields.length; i++) {
var addedField = customArea.addedFields[i];
if (addedField.isMapped && addedField.mappedAction !== null && $scope.AssociateActionLists.length > 0) {
addFieldsToProviderAction(addedField);
}
if (addedField.dependency) {
for (var x = 0; x < addedField.dependency.length; x++) {
if (addedField.dependency[x].availability && addedField.dependency[x].availability !== 0) {
expandField(addedField);
var errorMessage = $filter('i18n')('customAreaEditor.field.dependency.warn');
systemAlertService.error({ text: errorMessage, clear: false });
return false;
}
}
}
if (customArea.shortId === 'typeSpecific') {
addedField.extension.push({ classId: $scope.selectedClass.name });
}
}
if (customArea.removedFields.length > 0) {
removeMappedFieldsFromProviderAction(customArea.removedFields);
}
$scope.dataLoading = true;
screenConfigurationModel.savePanelConfiguration(customArea)
.then(handlePanelConfigurationSaveSuccess, handlePanelConfigurationSaveFault);
};
/**
* Handle create group
*/
$scope.onCreateGroup = function () {
//create an empty group field
var groupField = new FieldVO().build({
type: 'groupField',
dataType: 'group',
editable: true,
name: 'Customization Group',
label: 'Customization Group'
});
customArea.addField(angular.copy(groupField));
};
$scope.checkDiffValue = function (field) {
if (!field.diffCheck) {
field.valueField = '';
}
$scope.markFieldUpdated(field);
};
$scope.updateDiffValueField = function (field, item) {
field.valueField = item.value;
$scope.markFieldUpdated(field);
};
$scope.setDiffValueFieldonBlur = function (field) {
var temp = _.get(_.find($scope.diffFields, { name: field.valueFieldName }), 'value');
if (temp) {
field.valueField = temp;
$scope.markFieldUpdated(field);
}
};
$scope.getFieldWarn = function (field) {
var message = '';
if (!field.isAvailable() || field.isSelectionDisabled()) {
if (field.isWidget()) {
if (customArea.isFieldAvailableOnPanel(field)) {
var selectedMemberNames = customArea.fields.concat(fieldsSelectedInOtherPanels).reduce(function (fieldLabels, item) {
if (item.isGroupField()) {
field.members.forEach(function (member) {
if (item.isFieldMemberOfThisGroup(member)) {
fieldLabels.push(member.label);
}
});
}
else {
var isSelectedMemberField = field.members.some(function (member) {
return member.isEqual(item);
});
if (isSelectedMemberField) {
fieldLabels.push(item.label);
}
}
return fieldLabels;
}, []);
message = $filter('i18n')('customAreaEditor.field.widget.warn', selectedMemberNames.join('; '));
}
else {
message = $filter('i18n')('customAreaEditor.field.widget.notAvailableOnPanelWarn');
}
}
else if (field.widgetMember) {
var parentWidgetLabel = '', parentWidget = _.find(customArea.fields.concat(fieldsSelectedInOtherPanels), function (selectedField) {
if (selectedField.isWidget()) {
return _.findIndex(selectedField.members, { name: field.name }) > -1;
}
return false;
});
if (parentWidget) {
parentWidgetLabel = generateWidgetLabel(parentWidget);
}
message = $filter('i18n')('customAreaEditor.field.widgetMember.warn', parentWidgetLabel);
}
else {
var forms = field.fieldUnavailableOn.join(', ');
message = '[' + getTooltipLabel(field) + ']: ' + $filter('i18n')('customAreaEditor.field.warn', forms);
}
}
else {
message = getTooltipLabel(field);
if (field.isMissingSystemRequiredField()) {
message = '[' + getTooltipLabel(field) + ']: ' + $filter('i18n')('customAreaEditor.field.systemRequired.tooltip');
}
}
return message;
};
function getTooltipLabel(field) {
var label = '';
if (field.isWidget()) {
label = $scope.getWidgetFieldLabel(field);
}
else {
label = field.arFieldName || field.label;
}
return label;
}
$scope.getDependencyFieldWarn = function (dField) {
var message = '';
if (dField.availability) {
message = $filter('i18n')('customAreaEditor.field.warn', dField.fieldUnavailableOn.join(', '));
}
else {
message = $filter('i18n')('customAreaEditor.field.dependency.ok');
}
return message;
};
$scope.getWidgetFieldLabel = function (widget) {
return generateWidgetLabel(widget) + ' ' + $filter('i18n')('customAreaEditor.fieldTypes.widget');
};
$scope.getFieldAvailabilityWarning = function () {
var message = generalFieldAvailabilityMsg;
if (customArea.isHeaderSection() && !customArea.hasAvailableSlots()) {
message = headerMaxFieldsReached;
}
return message;
};
/**
* Refresh available field list
*/
function refreshAvailableFieldList() {
// fieldsSelectedInOtherPanels will be filled with fields, selected across all panels except current
// in the screen, for screens that have layout only.
// For old screens it will be an empty array
$scope.availableFields = _.sortBy(allAvailableFields.filter(function (field) {
var flag = false;
if (field.isWidget() && !field.label) {
generateWidgetLabel(field);
}
if (!field.searchLabel) {
// searchLabel will be used for filtering of available fields by search keyword in template
// since widget labels are result of composition of two labels, which prevents full widget name search
field.searchLabel = field.isWidget() ? $scope.getWidgetFieldLabel(field) : field.label;
}
if (field.isWidget()) {
// Marking as available
field.enableFieldForSelection();
// If widget member is selected, widget should be disabled
var isMemberSelected = field.members.some(function (widgetMember) {
return customArea.fields.concat(fieldsSelectedInOtherPanels).some(function (selectedField) {
if (selectedField.isGroupField()) {
// Should also check group field members, to make sure widget member
// was not selected to group
return selectedField.members.some(function (groupMember) {
return groupMember.isEqual(widgetMember);
});
}
return widgetMember.isEqual(selectedField);
});
});
if (isMemberSelected) {
field.disableFieldSelection();
}
}
if (field.isWidgetMember()) {
// If parent widget is selected anywhere on the screen - member fields
// should be disabled in available fields list
var parentWidgetIsSelected = customArea.fields.concat(fieldsSelectedInOtherPanels).some(function (selectedField) {
if (selectedField.isWidget() && (selectedField.name !== EntityVO.WIDGET_IMPACTED_AREAS || field.name === EntityVO.FIELD_REQUESTED_BY_COMPANY)) {
return _.findIndex(selectedField.members, { name: field.name }) > -1;
}
return false;
});
if (parentWidgetIsSelected) {
field.disableFieldSelection();
}
else {
field.enableFieldForSelection();
}
}
customArea.fields.forEach(function (selectedField) {
if (!flag) {
if (field.isEqual(selectedField)) {
flag = true;
}
else {
if (!selectedField.isWidget()) {
selectedField.members.forEach(function (member) {
if (!flag && !field.isWidget() && field.name === member.name) {
flag = true;
}
});
}
}
}
});
if (!customArea.isFieldAvailableOnPanel(field)) {
field.disableFieldSelection();
}
return !flag;
}), 'label');
$scope.showAvailabilityWarning = _.some($scope.availableFields, function (field) {
return !field.isAvailable();
});
}
/**
* Close editor
*/
function closeEditor() {
$modalInstance.dismiss();
}
function isProviderActionSupportedOnTheScreen(screenName) {
return ScreenConfigurationVO.prototype.isV2Compatible(screenName) && !ScreenConfigurationVO.prototype.isCreateScreen(screenName);
}
/**
* Handle successful configuration update
*/
function handlePanelConfigurationSaveSuccess() {
var isV2CompatibleScreen = screenConfigurationModel.isV2CompatibleScreen(customArea.parentScreenName), additionalRequestParams = isV2CompatibleScreen ? { fromConfig: true } : angular.noop();
screenConfigurationModel.loadScreenConfigurationByName(customArea.parentScreenName, true, additionalRequestParams)
.then(function () {
var missingSystemRequiredFields = $scope.availableFields.some(function (field) {
return field.isMissingSystemRequiredField();
});
if ($scope.AssociateActionLists.length > 0 && isProviderActionSupportedOnTheScreen($scope.screenName)) {
removeDuplicateFromActionList();
var type = customArea.dataSource.toLowerCase();
screenConfigurationModel.providerHardReload = true;
screenConfigurationModel.createUrlAction($scope.AssociateActionLists, 'custom', type).then(function (data) {
console.log('actionlist save', data);
});
}
var message = $filter('i18n')('screenConfiguration.customAreaEditor.fieldConfigurationUpdatedSuccessfully');
systemAlertService.success({ text: message, clear: true, hide: 10000 });
$rootScope.$broadcast(events.RELOAD_APP_CONFIGURATION_COMPLETED, {
invalidCustomizations: missingSystemRequiredFields ? { missingSystemFields: true } : null,
screenName: customArea.parentScreenName
});
if (missingSystemRequiredFields && isV2CompatibleScreen) {
systemAlertService.warning({ text: $filter('i18n')('customAreaEditor.invalidCustomizations.warning'), clear: false });
}
})
.finally(function () {
$scope.dataLoading = false;
closeEditor();
// For the the moment we don't need to update layout after customization
// But this will be enabled later
// updateScreenLayout();
});
}
/**
* Handle configuration update fault
*/
function handlePanelConfigurationSaveFault() {
$scope.dataLoading = false;
var message = $filter('i18n')('screenConfiguration.customAreaEditor.fieldConfigurationUpdateFault');
systemAlertService.error({ text: message, clear: false });
}
$scope.selectClass = function (subType) {
customArea.fields = fieldsCopy;
$scope.customArea = customArea;
$scope.selectedClass = subType;
$scope.dataLoading = true;
$scope.fieldsLoading = false;
customArea.classId = subType.name;
var screenConfigPromise = screenConfigurationModel.loadFields(customArea)
.then(function () {
customArea.initFields(screenConfigurationModel.selectedFieldDictionary);
customArea.addFieldAllowed = customArea.hasAvailableSlots();
allAvailableFields = screenConfigurationModel.getAvailableFieldsForScreenByClassId(customArea.parentScreenName, $scope.selectedClass);
handleWidgetMemberFields();
refreshAvailableFieldList();
});
$q.all([screenConfigPromise]).then(function () {
customArea.initValueFieldNames($scope.valueFieldNames);
$scope.dataLoading = false;
});
};
$scope.changeCustomArea = function (panel) {
customArea.selectedPanel = false;
customArea = panel;
init();
};
function fieldPropertyMapping() {
_.forEach($scope.AssociateActionLists, function (mappedAction) {
if (mappedAction.mappedFields && mappedAction.mappedFields.length > 0) {
_.forEach(mappedAction.mappedFields, function (mappedObj) {
checkActionMapping(customArea.fields, mappedObj, mappedAction);
});
}
});
}
function checkActionMapping(fieldArray, actionMappedObj, action) {
_.forEach(fieldArray, function (field) {
if (field.name === actionMappedObj.fieldName) {
setActionMappingForField(field, action, actionMappedObj.iconName);
}
if (field.dataType === 'group') {
checkActionMapping(field.members, actionMappedObj, action);
}
});
}
function setActionMappingForField(field, action, icon) {
field.isMapped = true;
field.mappedAction = action;
field.selectedIcon = icon;
}
/**
* Entry point
*/
function init() {
var parentScreen = angular.copy(screenConfigurationModel.screensCacheByName[customArea.parentScreenName]);
$scope.dataLoading = true;
$scope.customArea = customArea;
$scope.screenName = customArea.parentScreenName;
fieldsCopy = customArea.fields;
if (allPanels && allPanels.length > 1) {
$scope.allPanels = allPanels;
$scope.allPanels.forEach(function (panel) {
panel.selectedPanel = (panel.name === customArea.name);
});
}
if (parentScreen) {
fieldsSelectedInOtherPanels = parentScreen.panels.reduce(function (res, panel) {
if (panel.name === customArea.name) {
return res;
}
return res.concat(panel.fields);
}, []);
}
var type = customArea.dataSource.toLowerCase(), acceleratorsPromise = screenConfigurationModel.getAccelerators(type, customArea.parentScreenName).then(function (dataMap) {
var accelarator;
$scope.acceleratorsList = [];
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.acceleratorsList.push({
name: accelarator,
desc: dataMap[itsmField]
});
}
$scope.acceleratorsList = _.sortBy($scope.acceleratorsList, 'name');
$scope.acceleratorsList = _.union($scope.acceleratorsList, keywordEvaluatorService.getSupportedKeywords());
}
});
if (isProviderActionSupportedOnTheScreen($scope.screenName)) {
screenConfigurationModel.providerHardReload = true; // Fix for SW00537630
var associateActionPromise = screenConfigurationModel.loadActionsNew(type).then(function () {
$scope.AssociateActionLists = screenConfigurationModel.actionList;
fieldPropertyMapping();
_.forEach($scope.AssociateActionLists, function (actionObj) {
actionObj.name = actionObj.actionType + ':' + actionObj.labels.default;
actionObj.value = actionObj.sequenceNo;
});
});
}
$scope.enableExpression = _.includes(EntityVO.ENTITIES_WITH_NEW_CUSTOMIZATION, type);
var metadataPromise = metadataModel.getMetadataByType(type).then(function (metaData) {
$scope.diffFields = [];
$scope.valueFieldNames = {};
if (metaData) {
$scope.assetTypes = metaData.assetTypes;
for (var itsmField in metaData.availableValueFields) {
if (metaData.availableValueFields.hasOwnProperty(itsmField)) {
$scope.valueFieldNames[metaData.availableValueFields[itsmField]] = itsmField;
$scope.diffFields.push({
value: metaData.availableValueFields[itsmField],
name: itsmField
});
}
}
}
});
$scope.fieldsLoading = false;
if (customArea.shortId === 'typeSpecific') {
$scope.fieldsLoading = true;
$scope.selectedClass = {};
$q.all([metadataPromise, acceleratorsPromise]).then(function () {
$scope.dataLoading = false;
});
}
else {
$scope.fieldsLoading = false;
var screenConfigPromise = screenConfigurationModel.loadFields(customArea)
.then(function () {
customArea.initFields(screenConfigurationModel.selectedFieldDictionary);
customArea.addFieldAllowed = customArea.hasAvailableSlots();
allAvailableFields = screenConfigurationModel.getAvailableFieldsForScreen(customArea.parentScreenName);
handleWidgetMemberFields();
refreshAvailableFieldList();
});
$q.all([metadataPromise, screenConfigPromise, acceleratorsPromise, associateActionPromise]).then(function () {
customArea.initValueFieldNames($scope.valueFieldNames);
customArea.fields.forEach(function (f) {
generateWidgetLabel(f);
if (f.isGroupField()) {
// Copying group members, in case user will first remove all group members
f.initialMembers = angular.copy(f.members);
}
});
$scope.dataLoading = false;
});
}
}
function generateWidgetLabel(field) {
if (field.isWidget() && !field.label) {
field.label = $filter('i18n')('field.widget.' + field.name + '.label');
}
return field.label;
}
// Temporary disabled, but will be enabled in next release
// function updateScreenLayout() {
// layoutConfigurationModel.applyCustomizationChangesToScreenLayout(customArea)
// .catch(function (e) {
// $log.error('Error occurred while updating screen layout', e);
// });
// }
function handleWidgetMemberFields() {
includeWidgetMemberToAvailableFieldsList();
var availableWidgets = allAvailableFields.filter(function (field) {
return field.isWidget();
}) || [], selectedWidgets = customArea.fields.concat(fieldsSelectedInOtherPanels).filter(function (field) {
return field.isWidget();
}) || [], otherSelectedFields = customArea.fields.reduce(function (res, field) {
if (!field.isWidget()) {
if (field.isGroupField()) {
res = res.concat(field.members);
}
else {
res.push(field);
}
}
return res;
}, []);
availableWidgets.concat(selectedWidgets).forEach(function (widget) {
widget.members.forEach(function (member) {
// Check available fields, if there are widget members
// but also widget member can be in panel selected fields
var matchingField = _.find(allAvailableFields.concat(otherSelectedFields), function (field) {
return field.isEqual(member);
});
if (matchingField) {
matchingField.fillParentInfo(widget);
}
});
});
}
function addFieldToAvailableFieldsList(field) {
if (!isFieldAlreadyInAvailableList(field)) {
allAvailableFields.push(field);
}
}
function isFieldAlreadyInAvailableList(field) {
return allAvailableFields.some(function (availableField) {
return availableField.isEqual(field);
});
}
function includeWidgetMemberToAvailableFieldsList() {
customArea.fields.forEach(function (field) {
if (field.isWidget()) {
field.members.forEach(function (memberField) {
var isMemberFieldAlreadyAvailable = isFieldAlreadyInAvailableList(memberField);
if (memberField.label && !isMemberFieldAlreadyAvailable) {
allAvailableFields.push(memberField);
}
});
}
});
}
init();
}]);
})();