Skip to content

Commit

Permalink
fix: do not restore cached when closing on click outside
Browse files Browse the repository at this point in the history
  • Loading branch information
philippfromme committed Sep 10, 2024
1 parent 7d41f25 commit ab6969a
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 43 deletions.
32 changes: 20 additions & 12 deletions lib/features/search-pad/SearchPad.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ SearchPad.prototype._bindEvents = function() {

// close search on clicking anywhere outside
listen(document, 'html', 'click', function(e) {
self.close();
self.close(false);
});

// stop event from propagating and closing search
Expand Down Expand Up @@ -136,7 +136,7 @@ SearchPad.prototype._bindEvents = function() {
if (isKey('Enter', e)) {
var selected = self._getCurrentResult();

return selected ? self._select(selected) : self.close();
return selected ? self._select(selected) : self.close(false);
}

if (isKey('ArrowUp', e)) {
Expand Down Expand Up @@ -348,23 +348,31 @@ SearchPad.prototype.open = function() {
/**
* Close search pad.
*/
SearchPad.prototype.close = function() {
SearchPad.prototype.close = function(restoreCached = true) {
if (!this.isOpen()) {
return;
}

if (this._cachedRootElement) {
this._canvas.setRootElement(this._cachedRootElement);
}
if (restoreCached) {
if (this._cachedRootElement) {
this._canvas.setRootElement(this._cachedRootElement);
}

if (this._cachedSelection) {
this._selection.select(this._cachedSelection);
}
if (this._cachedSelection) {
this._selection.select(this._cachedSelection);
}

if (this._cachedViewbox) {
this._canvas.viewbox(this._cachedViewbox);
}

if (this._cachedViewbox) {
this._canvas.viewbox(this._cachedViewbox);
this._eventBus.fire('searchPad.restored');
}

this._cachedRootElement = null;
this._cachedSelection = null;
this._cachedViewbox = null;

this._unbindEvents();

this._open = false;
Expand Down Expand Up @@ -441,7 +449,7 @@ SearchPad.prototype._select = function(node) {
this._cachedSelection = null;
this._cachedViewbox = null;

this.close();
this.close(false);

this._canvas.scrollToElement(element, { top: 400 });

Expand Down
122 changes: 91 additions & 31 deletions test/spec/features/search-pad/SearchPadSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ var EVENTS = {
closed: 'searchPad.closed',
opened: 'searchPad.opened',
preselected: 'searchPad.preselected',
restored: 'searchPad.restored',
selected: 'searchPad.selected'
};


describe('features/searchPad', function() {
describe.only('features/searchPad', function() {

Check failure on line 28 in test/spec/features/search-pad/SearchPadSpec.js

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

Unexpected exclusive mocha test

Check failure on line 28 in test/spec/features/search-pad/SearchPadSpec.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-20.04)

Unexpected exclusive mocha test

beforeEach(bootstrapDiagram({ modules: [ searchPadModule ] }));

Expand Down Expand Up @@ -193,46 +194,90 @@ describe('features/searchPad', function() {
}));


it('should close', inject(function(searchPad) {
describe('close', function() {

// given
searchPad.open();
it('should close and restore', inject(function(searchPad) {

// when
searchPad.close();
// given
searchPad.open();

// then
expect(searchPad.isOpen()).to.equal(false);
expect(capturedEvents).to.eql([ EVENTS.opened, EVENTS.closed ]);
}));
// when
searchPad.close();

// then
expect(searchPad.isOpen()).to.equal(false);
expect(capturedEvents).to.eql([ EVENTS.opened, EVENTS.restored, EVENTS.closed ]);
}));

it('should close on <drag.init>', inject(function(eventBus, searchPad) {

// given
searchPad.open();
it('should close and not restore', inject(function(searchPad) {

// when
eventBus.fire('drag.init');
// given
searchPad.open();

// then
expect(searchPad.isOpen()).to.equal(false);
expect(capturedEvents).to.eql([ EVENTS.opened, EVENTS.closed ]);
}));
// when
searchPad.close(false);

// then
expect(searchPad.isOpen()).to.equal(false);
expect(capturedEvents).to.eql([ EVENTS.opened, EVENTS.closed ]);
}));

it('should close on <elements.changed>', inject(function(eventBus, searchPad) {

// given
searchPad.open();
it('should close on <drag.init> and restore', inject(function(eventBus, searchPad) {

// when
eventBus.fire('elements.changed');
// given
searchPad.open();

// then
expect(searchPad.isOpen()).to.equal(false);
expect(capturedEvents).to.eql([ EVENTS.opened, EVENTS.closed ]);
}));
// when
eventBus.fire('drag.init');

// then
expect(searchPad.isOpen()).to.equal(false);
expect(capturedEvents).to.eql([ EVENTS.opened, EVENTS.restored, EVENTS.closed ]);
}));


it('should close on <elements.changed> and restore', inject(function(eventBus, searchPad) {

// given
searchPad.open();

// when
eventBus.fire('elements.changed');

// then
expect(searchPad.isOpen()).to.equal(false);
expect(capturedEvents).to.eql([ EVENTS.opened, EVENTS.restored, EVENTS.closed ]);
}));


it('should close on <Escape> and restore', inject(function(searchPad) {

// given
searchPad.open();

// when
triggerKeyEvent(input_node, 'keyup', 'Escape');

// then
expect(capturedEvents).to.eql([ EVENTS.opened, EVENTS.restored, EVENTS.closed ]);
}));


it('should close on click and not restore', inject(function(canvas, searchPad) {

// given
searchPad.open();

// when
triggerMouseEvent(canvas.getContainer(), 'click', 0, 0);

// then
expect(capturedEvents).to.eql([ EVENTS.opened, EVENTS.closed ]);
}));

});


it('should toggle open/close', inject(function(searchPad) {
Expand All @@ -243,14 +288,14 @@ describe('features/searchPad', function() {

// then
expect(searchPad.isOpen()).to.equal(false);
expect(capturedEvents).to.eql([ EVENTS.opened, EVENTS.closed ]);
expect(capturedEvents).to.eql([ EVENTS.opened, EVENTS.restored, EVENTS.closed ]);

// when
searchPad.toggle();

// then
expect(searchPad.isOpen()).to.equal(true);
expect(capturedEvents).to.eql([ EVENTS.opened, EVENTS.closed, EVENTS.opened ]);
expect(capturedEvents).to.eql([ EVENTS.opened, EVENTS.restored, EVENTS.closed, EVENTS.opened ]);
}));


Expand Down Expand Up @@ -461,7 +506,7 @@ describe('features/searchPad', function() {

// then
expect(searchPad.isOpen()).to.equal(false);
expect(capturedEvents).to.eql([ EVENTS.opened, EVENTS.closed ]);
expect(capturedEvents).to.eql([ EVENTS.opened, EVENTS.restored, EVENTS.closed ]);
}));


Expand Down Expand Up @@ -560,6 +605,21 @@ describe('features/searchPad', function() {
});


function triggerMouseEvent(element, eventType, x, y) {
var event = new MouseEvent(eventType, {
view: window,
bubbles: true,
cancelable: true,
clientX: x,
clientY: y
});

element.dispatchEvent(event);

return event;
}


function triggerKeyEvent(element, eventType, code) {
var event = createKeyEvent(code, { type: eventType });

Expand Down

0 comments on commit ab6969a

Please sign in to comment.