Skip to content

Commit

Permalink
Merge pull request #1197 from eclarizio/BZ1442811
Browse files Browse the repository at this point in the history
Fix for Service Catalogs: Dialogs are hanging and keeps buffering
(cherry picked from commit 27a33c9)

https://bugzilla.redhat.com/show_bug.cgi?id=1447088
  • Loading branch information
Dan Clarizio authored and simaishi committed May 1, 2017
1 parent 3e98922 commit dde21c3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 14 deletions.
20 changes: 12 additions & 8 deletions app/assets/javascripts/dialog_field_refresh.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
/* global miqInitSelectPicker miqSelectPickerEvent miqSparkle miqSparkleOn */

var dialogFieldRefresh = {
unbindAllPreviousListeners: function() {
$(document).off('dialog::autoRefresh');
},

listenForAutoRefreshMessages: function(autoRefreshOptions, callbackFunction) {
var thisIsTheFieldToUpdate = function(event) {
var tabIndex = event.data.tabIndex;
var groupIndex = event.data.groupIndex;
var fieldIndex = event.data.fieldIndex;
var thisIsTheFieldToUpdate = function(data) {
var tabIndex = data.tabIndex;
var groupIndex = data.groupIndex;
var fieldIndex = data.fieldIndex;
return tabIndex === autoRefreshOptions.tab_index && groupIndex === autoRefreshOptions.group_index && fieldIndex === autoRefreshOptions.field_index;
};

window.addEventListener('message', function(event) {
if (thisIsTheFieldToUpdate(event)) {
$(document).on('dialog::autoRefresh', function(_event, data) {
if (thisIsTheFieldToUpdate(data)) {
callbackFunction.call();
}
});
Expand Down Expand Up @@ -181,11 +185,11 @@ var dialogFieldRefresh = {
nextAvailable = nextAvailable[0];

if (nextAvailable !== undefined) {
parent.postMessage({
$(document).trigger('dialog::autoRefresh', {
tabIndex: nextAvailable.tab_index,
groupIndex: nextAvailable.group_index,
fieldIndex: nextAvailable.field_index,
}, '*');
});
}
}
},
Expand Down
4 changes: 4 additions & 0 deletions app/views/shared/dialogs/_dialog_provision.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
- wf = @edit[:wf] if @edit && @edit[:wf]
= render :partial => "layouts/flash_msg"

:javascript
dialogFieldRefresh.unbindAllPreviousListeners();

.row
.col-md-12.col-lg-12
#dialog_tabs
Expand Down
61 changes: 55 additions & 6 deletions spec/javascripts/dialog_field_refresh_spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,53 @@
describe('dialogFieldRefresh', function() {
describe('#listenForAutoRefreshMessages', function() {
context('when an autoRefresh event gets triggered', function() {
var callback;
var autoRefreshOptions;

beforeEach(function() {
callback = jasmine.createSpyObj('callback', ['call']);
autoRefreshOptions = {
tab_index: 1,
group_index: 2,
field_index: 3
};
dialogFieldRefresh.listenForAutoRefreshMessages(autoRefreshOptions, callback);
});

context('when the tab index, group index, and field index match the corresponding field', function() {
beforeEach(function() {
$(document).trigger('dialog::autoRefresh', {tabIndex: 1, groupIndex: 2, fieldIndex: 3});
});

it('executes the callback', function() {
expect(callback.call).toHaveBeenCalled();
});
});

context('when the tab index, group index, and field index do not match the corresponding field', function() {
beforeEach(function() {
$(document).trigger('dialog::autoRefresh', {tabIndex: 1, groupIndex: 1, fieldIndex: 3});
});

it('does not execute the callback', function() {
expect(callback.call).not.toHaveBeenCalled();
});
});
});
});

describe('#unbindAllPreviousListeners', function() {
beforeEach(function() {
spyOn($.fn, 'off');
});

it('unbinds all autoRefresh messages from the document', function() {
dialogFieldRefresh.unbindAllPreviousListeners();
expect($.fn.off.calls.mostRecent().object).toEqual(document);
expect($.fn.off).toHaveBeenCalledWith('dialog::autoRefresh');
});
});

describe('#addOptionsToDropDownList', function() {
var data = {};

Expand Down Expand Up @@ -581,13 +630,13 @@ describe('dialogFieldRefresh', function() {

describe('#triggerAutoRefresh', function() {
beforeEach(function() {
spyOn(parent, 'postMessage');
spyOn($.fn, 'trigger');
});

context('when the trigger passed in falsy', function() {
it('does not post any messages', function() {
dialogFieldRefresh.triggerAutoRefresh({trigger: ""});
expect(parent.postMessage).not.toHaveBeenCalled();
expect($.fn.trigger).not.toHaveBeenCalled();
});
});

Expand All @@ -602,7 +651,7 @@ describe('dialogFieldRefresh', function() {
});

it('does not post a message', function() {
expect(parent.postMessage).not.toHaveBeenCalled();
expect($.fn.trigger).not.toHaveBeenCalled();
});
});

Expand All @@ -619,7 +668,7 @@ describe('dialogFieldRefresh', function() {
});

it('posts a message', function() {
expect(parent.postMessage).toHaveBeenCalledWith({tabIndex: 1, groupIndex: 1, fieldIndex: 2}, '*');
expect($.fn.trigger).toHaveBeenCalledWith('dialog::autoRefresh', {tabIndex: 1, groupIndex: 1, fieldIndex: 2});
});
});
});
Expand All @@ -635,7 +684,7 @@ describe('dialogFieldRefresh', function() {
});

it('does not post a message', function() {
expect(parent.postMessage).not.toHaveBeenCalled();
expect($.fn.trigger).not.toHaveBeenCalled();
});
});

Expand All @@ -652,7 +701,7 @@ describe('dialogFieldRefresh', function() {
});

it('posts a message', function() {
expect(parent.postMessage).toHaveBeenCalledWith({tabIndex: 1, groupIndex: 1, fieldIndex: 2}, '*');
expect($.fn.trigger).toHaveBeenCalledWith('dialog::autoRefresh', {tabIndex: 1, groupIndex: 1, fieldIndex: 2});
});
});
});
Expand Down

0 comments on commit dde21c3

Please sign in to comment.