Skip to content

Commit aa45503

Browse files
authored
ENGCOM-8183: Fix gallery rendering on page load so current breakpoint configuration isn't replaced by any other configuration #29934
2 parents c952484 + b830d0c commit aa45503

File tree

2 files changed

+67
-12
lines changed

2 files changed

+67
-12
lines changed

dev/tests/js/jasmine/tests/lib/mage/gallery/gallery.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,53 @@ define([
9898
expect(gallery.settings.fotoramaApi).toBeDefined();
9999
expect(gallery.settings.data).toBeDefined();
100100
expect(gallery.settings.api).toBeDefined();
101+
expect(gallery.settings.activeBreakpoint).toEqual({});
101102

102103
$.fn.data = originSpy;
103104
});
105+
106+
it('Verify gallery navigation is set properly as dots if specified in options', function () {
107+
// added
108+
options.breakpoints = {
109+
mobile: {
110+
conditions: {
111+
'max-width': '767px'
112+
},
113+
options: {
114+
options: {
115+
nav: 'dots'
116+
}
117+
}
118+
},
119+
desktop: {
120+
conditions: {
121+
'min-width': '1024px'
122+
},
123+
options: {
124+
options: {
125+
nav: 'thumbs'
126+
}
127+
}
128+
}
129+
};
130+
131+
originSpy = $.fn.data;
132+
jqueryDataMock = {
133+
setOptions: jasmine.createSpy().and.returnValue(true),
134+
updateOptions: jasmine.createSpy().and.returnValue(true)
135+
};
136+
spyOn($.fn, 'data').and.callFake(function () {
137+
return jqueryDataMock;
138+
});
139+
140+
gallery = new Gallery(options, element);
141+
142+
options.breakpoints.mobile.options.options.arrows = false;
143+
expect(JSON.stringify(gallery.settings.activeBreakpoint))
144+
.toEqual(JSON.stringify(options.breakpoints.mobile.options));
145+
146+
$.fn.data = originSpy;
147+
});
148+
104149
});
105150
});

lib/web/mage/gallery/gallery.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,17 @@ define([
340340
settings = this.settings,
341341
config = this.config,
342342
startConfig = this.startConfig,
343+
isInitialized = {},
343344
isTouchEnabled = this.isTouchEnabled;
344345

345346
if (_.isObject(settings.breakpoints)) {
346347
pairs = _.pairs(settings.breakpoints);
347348
_.each(pairs, function (pair) {
349+
var mediaQuery = pair[0];
350+
351+
isInitialized[mediaQuery] = false;
348352
mediaCheck({
349-
media: pair[0],
353+
media: mediaQuery,
350354

351355
/**
352356
* Is triggered when breakpoint enties.
@@ -361,29 +365,35 @@ define([
361365
}
362366

363367
if (isTouchEnabled) {
364-
settings.breakpoints[pair[0]].options.arrows = false;
368+
settings.breakpoints[mediaQuery].options.arrows = false;
365369

366-
if (settings.breakpoints[pair[0]].options.fullscreen) {
367-
settings.breakpoints[pair[0]].options.fullscreen.arrows = false;
370+
if (settings.breakpoints[mediaQuery].options.fullscreen) {
371+
settings.breakpoints[mediaQuery].options.fullscreen.arrows = false;
368372
}
369373
}
370374

371-
settings.api.updateOptions(settings.breakpoints[pair[0]].options, true);
372-
$.extend(true, config, settings.breakpoints[pair[0]]);
373-
settings.activeBreakpoint = settings.breakpoints[pair[0]];
375+
settings.api.updateOptions(settings.breakpoints[mediaQuery].options, true);
376+
$.extend(true, config, settings.breakpoints[mediaQuery]);
377+
settings.activeBreakpoint = settings.breakpoints[mediaQuery];
378+
379+
isInitialized[mediaQuery] = true;
374380
},
375381

376382
/**
377383
* Is triggered when breakpoint exits.
378384
*/
379385
exit: function () {
380-
$.extend(true, config, _.clone(startConfig));
381-
settings.api.updateOptions(settings.defaultConfig.options, true);
386+
if (isInitialized[mediaQuery]) {
387+
$.extend(true, config, _.clone(startConfig));
388+
settings.api.updateOptions(settings.defaultConfig.options, true);
382389

383-
if (settings.isFullscreen) {
384-
settings.api.updateOptions(settings.fullscreenConfig, true);
390+
if (settings.isFullscreen) {
391+
settings.api.updateOptions(settings.fullscreenConfig, true);
392+
}
393+
settings.activeBreakpoint = {};
394+
} else {
395+
isInitialized[mediaQuery] = true;
385396
}
386-
settings.activeBreakpoint = {};
387397
}
388398
});
389399
});

0 commit comments

Comments
 (0)