Skip to content

Commit 263537f

Browse files
committed
[Lib] Fix previous attempts to correct wrong behavior of collapsible library
1 parent 7ffabd0 commit 263537f

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

lib/web/mage/collapsible.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ define([
1414
var hideProps = {},
1515
showProps = {};
1616

17-
hideProps.height = 'hide';
18-
showProps.height = 'show';
17+
hideProps.height = 'hide';
18+
showProps.height = 'show';
1919

2020
$.widget('mage.collapsible', {
2121
options: {
@@ -65,7 +65,7 @@ define([
6565

6666
this.element.on('dimensionsChanged', function (e) {
6767
if (e.target && e.target.classList.contains('active')) {
68-
this._scrollToTopIfVisible(e.target);
68+
this._scrollToTopIfNotVisible();
6969
}
7070
}.bind(this));
7171

@@ -308,7 +308,7 @@ define([
308308

309309
if (event) {
310310
$.each(event.split(' '), function (index, eventName) {
311-
self.events[ eventName ] = '_eventHandler';
311+
self.events[eventName] = '_eventHandler';
312312
});
313313
}
314314
this._off(this.trigger);
@@ -566,24 +566,35 @@ define([
566566
},
567567

568568
/**
569-
* @param {HTMLElement} elem
570569
* @private
571570
*/
572-
_scrollToTopIfVisible: function (elem) {
573-
if (!this._isElementOutOfViewport(elem)) {
574-
elem.scrollIntoView();
571+
_scrollToTopIfNotVisible: function () {
572+
if (this._isElementOutOfViewport()) {
573+
this.header[0].scrollIntoView();
575574
}
576575
},
577576

578577
/**
579-
* @param {HTMLElement} elem
580578
* @private
581579
* @return {Boolean}
582580
*/
583-
_isElementOutOfViewport: function (elem) {
584-
var rect = elem.getBoundingClientRect();
585-
586-
return rect.bottom < 0 || rect.right < 0 || rect.left > window.innerWidth || rect.top > window.innerHeight;
581+
_isElementOutOfViewport: function () {
582+
var headerRect = this.header[0].getBoundingClientRect(),
583+
contentRect = this.content.get().length ? this.content[0].getBoundingClientRect() : false,
584+
headerOut,
585+
contentOut;
586+
587+
headerOut = headerRect.bottom - headerRect.height < 0 ||
588+
headerRect.right - headerRect.width < 0 ||
589+
headerRect.left + headerRect.width > window.innerWidth ||
590+
headerRect.top + headerRect.height > window.innerHeight;
591+
592+
contentOut = contentRect ? contentRect.bottom - contentRect.height < 0 ||
593+
contentRect.right - contentRect.width < 0 ||
594+
contentRect.left + contentRect.width > window.innerWidth ||
595+
contentRect.top + contentRect.height > window.innerHeight : false;
596+
597+
return headerOut ? headerOut : contentOut;
587598
}
588599
});
589600

0 commit comments

Comments
 (0)