Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated service reconfigure to use dialog component #877

Merged
merged 3 commits into from
Aug 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 0 additions & 44 deletions client/app/core/auto-refresh.service.js

This file was deleted.

61 changes: 0 additions & 61 deletions client/app/core/auto-refresh.service.spec.js

This file was deleted.

2 changes: 0 additions & 2 deletions client/app/core/core.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
// Core
import { ApplianceInfo } from './appliance-info.service.js';
import { AuthenticationApiFactory } from './authentication-api.factory.js';
import { AutoRefreshFactory } from './auto-refresh.service.js';
import { BaseModalController } from './modal/base-modal-controller.js';
import { BaseModalFactory } from './modal/base-modal.factory.js';
import { ChargebackFactory } from './chargeback.service.js';
Expand Down Expand Up @@ -81,7 +80,6 @@ export const CoreModule = angular
.directive('languageSwitcher', LanguageSwitcherDirective)
.factory('ApplianceInfo', ApplianceInfo)
.factory('AuthenticationApi', AuthenticationApiFactory)
.factory('AutoRefresh', AutoRefreshFactory)
.factory('Chargeback', ChargebackFactory)
.factory('CollectionsApi', CollectionsApiFactory)
.factory('DialogFieldRefresh', DialogFieldRefreshFactory)
Expand Down
160 changes: 1 addition & 159 deletions client/app/core/dialog-field-refresh.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
/* eslint angular/angularelement: "off" */

/** @ngInject */
export function DialogFieldRefreshFactory(CollectionsApi, EventNotifications, AutoRefresh) {
export function DialogFieldRefreshFactory(CollectionsApi) {
var service = {
refreshSingleDialogField: refreshSingleDialogField,
setupDialogData: setupDialogData,
triggerAutoRefresh: triggerAutoRefresh,
refreshDialogField: refreshDialogField,
};

Expand All @@ -32,159 +29,4 @@ export function DialogFieldRefreshFactory(CollectionsApi, EventNotifications, Au
});
});
}

function refreshSingleDialogField(allDialogFields, dialogField, url, resourceId, autoRefreshOptions) {
function refreshSuccess(result) {
var resultObj = result.result[dialogField.name];

updateAttributesForDialogField(dialogField, resultObj);
if (dialogField.type === 'DialogFieldDropDownList') {
updateDialogSortOrder(dialogField);
}

triggerAutoRefresh(dialogField, false, autoRefreshOptions);
}

function refreshFailure(result) {
EventNotifications.error('There was an error refreshing this dialog: ' + result);
}

dialogField.beingRefreshed = true;

return fetchDialogFieldInfo(allDialogFields, [dialogField.name], url, resourceId, refreshSuccess, refreshFailure);
}

function selectDefaultValue(dialogField, newDialogField) {
const dropDownValues = [];
if (angular.isObject(newDialogField.values)) {
newDialogField.values.forEach(function (value) {
if (parseInt(value[0], 10)) {
dropDownValues.push([parseInt(value[0], 10), value[1]]);
} else {
dropDownValues.push(value);
}
});
newDialogField.values = dropDownValues;
dialogField.values = dropDownValues;
if (angular.isDefined(newDialogField.default_value) && newDialogField.default_value !== null) {
dialogField.default_value = newDialogField.default_value;
} else {
dialogField.default_value = newDialogField.values[0][0];
}
} else {
if (dialogField.type === 'DialogFieldDateControl' || dialogField.type === 'DialogFieldDateTimeControl') {
dialogField.default_value = new Date(newDialogField.values);
} else {
if (isBlank(newDialogField.default_value) || dialogField.type === 'DialogFieldCheckBox') {
dialogField.default_value = newDialogField.values;
}
}
}
if (parseInt(dialogField.default_value, 10)) {
dialogField.default_value = parseInt(dialogField.default_value, 10);
}
}

function setupDialogData(dialogs, allDialogFields, autoRefreshableDialogFields) {
angular.forEach(dialogs, function(dialog) {
angular.forEach(dialog.dialog_tabs, function(dialogTab) {
angular.forEach(dialogTab.dialog_groups, function(dialogGroup) {
angular.forEach(dialogGroup.dialog_fields, function(dialogField) {
allDialogFields.push(dialogField);

selectDefaultValue(dialogField, dialogField);

if (dialogField.auto_refresh === true || dialogField.trigger_auto_refresh === true) {
dialogField.refreshableFieldIndex = autoRefreshableDialogFields.push(dialogField) - 1;
}

dialogField.triggerAutoRefresh = function() {
triggerAutoRefresh(dialogField, true);
};
});
});
});
});
}

function triggerAutoRefresh(dialogField, initialTrigger, autoRefreshOptions) {
if (dialogField.trigger_auto_refresh === true || dialogField.triggerOverride === true) {
const triggerOptions = {};

if (initialTrigger === true) {
triggerOptions.initializingIndex = dialogField.refreshableFieldIndex;
triggerOptions.currentIndex = 0;
} else {
triggerOptions.initializingIndex = autoRefreshOptions.initializingIndex;
triggerOptions.currentIndex = autoRefreshOptions.currentIndex;
}

AutoRefresh.triggerAutoRefresh(triggerOptions);
}
}

// Private

function updateAttributesForDialogField(dialogField, newDialogField) {
copyDynamicAttributes(dialogField, newDialogField);
selectDefaultValue(dialogField, newDialogField);

dialogField.beingRefreshed = false;

function copyDynamicAttributes(currentDialogField, newDialogField) {
currentDialogField.data_type = newDialogField.data_type;
currentDialogField.options = newDialogField.options;
currentDialogField.read_only = newDialogField.read_only;
currentDialogField.required = newDialogField.required;
currentDialogField.visible = newDialogField.visible;
}
}

function updateDialogSortOrder(dialogField) {
var values = dialogField.values;
var sortDirection = dialogField.options.sort_order;
var sortByValue = 0; // These are constants that are used to refer to array positions
var sortByDescription = 1; // These are constants that are used to refer to array positions
var sortBy = (dialogField.options.sort_by === 'value' ? sortByValue : sortByDescription);
dialogField.values = values.sort((option1, option2) => {
var trueValue = -1;
var falseValue = 1;
if (sortDirection !== 'ascending') {
trueValue = 1;
falseValue = -1;
}

return option2[sortBy] > option1[sortBy] ? trueValue : falseValue;
});
}

function fetchDialogFieldInfo(allDialogFields, dialogFieldsToFetch, url, resourceId, successCallback, failureCallback) {
return CollectionsApi.post(
url,
resourceId,
{},
angular.toJson({
action: 'refresh_dialog_fields',
resource: {
dialog_fields: dialogFieldInfoToSend(allDialogFields),
fields: dialogFieldsToFetch,
},
})
).then(successCallback, failureCallback);
}

function dialogFieldInfoToSend(allDialogFields) {
var fieldValues = {};
angular.forEach(allDialogFields, function(dialogField) {
fieldValues[dialogField.name] = dialogField.default_value;
});

return fieldValues;
}

function isBlank(value) {
return angular.isUndefined(value)
|| value === null
|| value === '';
}
}
Loading