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

Search fixes #929

Merged
merged 2 commits into from
Sep 12, 2024
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
37 changes: 24 additions & 13 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 @@ -421,7 +429,10 @@ SearchPad.prototype._preselect = function(node) {
domClasses(node).add(SearchPad.RESULT_SELECTED_CLASS);

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

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

this._selection.select(element);

Expand All @@ -441,7 +452,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
120 changes: 90 additions & 30 deletions test/spec/features/search-pad/SearchPadSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var EVENTS = {
closed: 'searchPad.closed',
opened: 'searchPad.opened',
preselected: 'searchPad.preselected',
restored: 'searchPad.restored',
selected: 'searchPad.selected'
};

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
Loading