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

Fix for service catalog service dialog refresh function behaving differently #814

Merged
merged 3 commits into from
Jun 12, 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: 44 additions & 0 deletions client/app/core/auto-refresh.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/** @ngInject */
export function AutoRefreshFactory() {
const callbacks = [];
const service = {
triggerAutoRefresh: triggerAutoRefresh,
listenForAutoRefresh: listenForAutoRefresh,
callbacks: callbacks,
};

function triggerAutoRefresh(data) {
callbacks.forEach((callback) => {
callback(data);
});
}

function listenForAutoRefresh(allDialogFields, autoRefreshableDialogFields, url, resourceId, refreshCallback) {
const nextFieldToRefresh = function(field, data, currentIndex) {
return (field.auto_refresh === true && data.initializingIndex !== currentIndex && data.currentIndex < currentIndex);
};

const listenerFunction = function(data) {
const autoRefreshOptions = {
initializingIndex: data.initializingIndex,
};

const dialogFieldToRefresh = autoRefreshableDialogFields.filter(function(field, currentIndex) {
if (nextFieldToRefresh(field, data, currentIndex)) {
return field;
}
});

if (dialogFieldToRefresh.length > 0) {
dialogFieldToRefresh[0].beingRefreshed = true;
dialogFieldToRefresh[0].triggerOverride = true;
autoRefreshOptions.currentIndex = dialogFieldToRefresh[0].refreshableFieldIndex;
refreshCallback(allDialogFields, dialogFieldToRefresh[0], url, resourceId, autoRefreshOptions);
}
};

callbacks.push(listenerFunction);
}

return service;
}
61 changes: 61 additions & 0 deletions client/app/core/auto-refresh.service.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
describe('AutoRefresh Service', function() {
beforeEach(function() {
module('app.services');
bard.inject('AutoRefresh');
});

describe('#triggerAutoRefresh', function() {
var testFunction = sinon.stub();

beforeEach(function() {
AutoRefresh.callbacks.push(testFunction);
});

it('calls all of the callbacks passing the data through', function() {
AutoRefresh.triggerAutoRefresh('the data');
expect(testFunction).to.have.been.calledWith('the data');
});
});

describe('#listenForAutoRefresh', function() {
var listenerFunction;
var refreshCallback = sinon.stub();
var allDialogFields = 'alldialogfields';
var url = 'url';
var resourceId = 'resourceid';
var dialog1 = {};
var dialog2 = {auto_refresh: true, refreshableFieldIndex: 123};
var autoRefreshableDialogFields = [dialog1, dialog2];

beforeEach(function() {
AutoRefresh.listenForAutoRefresh(allDialogFields, autoRefreshableDialogFields, url, resourceId, refreshCallback);
listenerFunction = AutoRefresh.callbacks[0];
});

describe('#listenForAutoRefresh listenerFunction', function() {
describe('when the list of fields to refresh is greater than 0', function() {
var data = {initializingIndex: 0, currentIndex: 0};

beforeEach(function() {
listenerFunction(data);
});

it('sets the beingRefreshed property on the dialog field to true', function() {
expect(dialog2.beingRefreshed).to.equal(true);
});

it('sets the triggerOverride property on the dialog field to true', function() {
expect(dialog2.triggerOverride).to.equal(true);
});

it('calls the refresh callback with the right arugments', function() {
var autoRefreshOptions = {currentIndex: 123, initializingIndex: 0};

expect(refreshCallback).to.have.been.calledWith(
'alldialogfields', dialog2, 'url', 'resourceid', autoRefreshOptions
);
});
});
});
});
});
2 changes: 2 additions & 0 deletions client/app/core/core.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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 @@ -80,6 +81,7 @@ 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
83 changes: 38 additions & 45 deletions client/app/core/dialog-field-refresh.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,34 @@
/* eslint angular/angularelement: "off" */

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

return service;

function listenForAutoRefreshMessages(allDialogFields, autoRefreshableDialogFields, url, resourceId) {
var listenerFunction = function(event) {
var dialogFieldToRefresh = autoRefreshableDialogFields.filter(function(field, currentIndex) {
if (field.auto_refresh === true && event.originalEvent.data.refreshableFieldIndex < currentIndex) {
return field;
}
});

if (dialogFieldToRefresh.length > 0) {
dialogFieldToRefresh[0].beingRefreshed = true;
dialogFieldToRefresh[0].triggerOverride = true;
refreshSingleDialogField(allDialogFields, dialogFieldToRefresh[0], url, resourceId);
}
};

$(window).off('message'); // Unbind all previous message listeners
$(window).on('message', listenerFunction);
}

function refreshSingleDialogField(allDialogFields, dialogField, url, resourceId) {
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);
}
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;
});
triggerAutoRefresh(dialogField, false, autoRefreshOptions);
}

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

dialogField.beingRefreshed = true;
fetchDialogFieldInfo(allDialogFields, [dialogField.name], url, resourceId, refreshSuccess, refreshFailure);

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

function selectDefaultValue(dialogField, newDialogField) {
Expand Down Expand Up @@ -103,17 +68,27 @@ export function DialogFieldRefreshFactory(CollectionsApi, EventNotifications) {
}

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

function triggerAutoRefresh(dialogField) {
function triggerAutoRefresh(dialogField, initialTrigger, autoRefreshOptions) {
if (dialogField.trigger_auto_refresh === true || dialogField.triggerOverride === true) {
parent.postMessage({refreshableFieldIndex: dialogField.refreshableFieldIndex}, '*');
const triggerOptions = {};

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

AutoRefresh.triggerAutoRefresh(triggerOptions);
}
}

Expand All @@ -134,8 +109,26 @@ export function DialogFieldRefreshFactory(CollectionsApi, EventNotifications) {
}
}

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) {
CollectionsApi.post(
return CollectionsApi.post(
url,
resourceId,
{},
Expand Down
Loading