Skip to content

Commit

Permalink
Bugfix/Feature Enchancement - Able to set responsive settings with
Browse files Browse the repository at this point in the history
setOption method.

Previously was impossible to set the responsive settings via setOption due
to:
1. Settings would never get merged, always would lose previous breakpoints
2. The responsive settings were registered at init, and not at reinit.

Added in a specific check for setOption and Responsive to merge new
responsive breakpoint options, and over-write existing ones.

Moved the breakpoint registration into it's own method and call it on init
and reInit. Also breakpoint registration will clear out duplicate
breakpoints.
  • Loading branch information
simeydotme committed May 13, 2015
1 parent f73f1e4 commit 0cc9031
Showing 1 changed file with 75 additions and 37 deletions.
112 changes: 75 additions & 37 deletions slick/slick.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@

function Slick(element, settings) {

var _ = this,
dataSettings, responsiveSettings, breakpoint;
var _ = this, dataSettings;

_.defaults = {
accessibility: true,
Expand Down Expand Up @@ -144,27 +143,6 @@
_.currentSlide = _.options.initialSlide;

_.originalSettings = _.options;
responsiveSettings = _.options.responsive || null;

if (responsiveSettings && responsiveSettings.length > -1) {
_.respondTo = _.options.respondTo || 'window';
for (breakpoint in responsiveSettings) {
if (responsiveSettings.hasOwnProperty(breakpoint)) {
_.breakpoints.push(responsiveSettings[
breakpoint].breakpoint);
_.breakpointSettings[responsiveSettings[
breakpoint].breakpoint] =
responsiveSettings[breakpoint].settings;
}
}
_.breakpoints.sort(function(a, b) {
if (_.options.mobileFirst === true) {
return a - b;
} else {
return b - a;
}
});
}

if (typeof document.mozHidden !== 'undefined') {
_.hidden = 'mozHidden';
Expand Down Expand Up @@ -192,8 +170,9 @@
// Extracted from jQuery v1.11 source
_.htmlExpr = /^(?:\s*(<[\w\W]+>)[^>]*)$/;

_.init(true);

_.registerBreakpoints();
_.init(true);
_.checkResponsive(true);

}
Expand Down Expand Up @@ -574,7 +553,7 @@

};

Slick.prototype.checkResponsive = function(initial) {
Slick.prototype.checkResponsive = function(initial, forceUpdate) {

var _ = this,
breakpoint, targetBreakpoint, respondToWidth, triggerBreakpoint = false;
Expand All @@ -589,8 +568,9 @@
respondToWidth = Math.min(windowWidth, sliderWidth);
}

if (_.originalSettings.responsive && _.originalSettings
.responsive.length > -1 && _.originalSettings.responsive !== null) {
if ( _.options.responsive &&
_.options.responsive.length &&
_.options.responsive !== null) {

targetBreakpoint = null;

Expand All @@ -610,7 +590,7 @@

if (targetBreakpoint !== null) {
if (_.activeBreakpoint !== null) {
if (targetBreakpoint !== _.activeBreakpoint) {
if (targetBreakpoint !== _.activeBreakpoint || forceUpdate) {
_.activeBreakpoint =
targetBreakpoint;
if (_.breakpointSettings[targetBreakpoint] === 'unslick') {
Expand Down Expand Up @@ -1129,6 +1109,7 @@
if (!$(_.$slider).hasClass('slick-initialized')) {

$(_.$slider).addClass('slick-initialized');

_.buildRows();
_.buildOut();
_.setProps();
Expand All @@ -1137,6 +1118,7 @@
_.initializeEvents();
_.updateArrows();
_.updateDots();

}

if (creation) {
Expand Down Expand Up @@ -1474,6 +1456,46 @@

};

Slick.prototype.registerBreakpoints = function() {

var _ = this, breakpoint, currentBreakpoint, l,
responsiveSettings = _.options.responsive || null;

if ( $.type(responsiveSettings) === "array" && responsiveSettings.length ) {

_.respondTo = _.options.respondTo || 'window';

for ( breakpoint in responsiveSettings ) {

l = _.breakpoints.length-1;
currentBreakpoint = responsiveSettings[breakpoint].breakpoint;

if (responsiveSettings.hasOwnProperty(breakpoint)) {

// loop through the breakpoints and cut out any existing
// ones with the same breakpoint number, we don't want dupes.
while( l >= 0 ) {
if( _.breakpoints[l] && _.breakpoints[l] === currentBreakpoint ) {
_.breakpoints.splice(l,1);
}
l--;
}

_.breakpoints.push(currentBreakpoint);
_.breakpointSettings[currentBreakpoint] = responsiveSettings[breakpoint].settings;

}

}

_.breakpoints.sort(function(a, b) {
return ( _.options.mobileFirst ) ? a-b : b-a;
});

}

};

Slick.prototype.reinit = function() {

var _ = this;
Expand All @@ -1491,22 +1513,19 @@
_.currentSlide = 0;
}

_.setProps();
_.registerBreakpoints();

_.setProps();
_.setupInfinite();

_.buildArrows();

_.updateArrows();

_.initArrowEvents();

_.buildDots();

_.updateDots();

_.initDotEvents();

_.checkResponsive(false, true);

if (_.options.focusOnSelect === true) {
$(_.$slideTrack).children().on('click.slick', _.selectHandler);
}
Expand Down Expand Up @@ -1682,8 +1701,27 @@

Slick.prototype.setOption = Slick.prototype.slickSetOption = function(option, value, refresh) {

var _ = this;
_.options[option] = value;
var _ = this, l, item;

if( option === "responsive" && $.type(value) === "array" ) {
for ( item in value ) {
if( $.type( _.options.responsive ) !== "array" ) {
_.options.responsive = [ value[item] ];
} else {
l = _.options.responsive.length-1;
// loop through the responsive object and splice out duplicates.
while( l >= 0 ) {
if( _.options.responsive[l].breakpoint === value[item].breakpoint ) {
_.options.responsive.splice(l,1);
}
l--;
}
_.options.responsive.push( value[item] );
}
}
} else {
_.options[option] = value;
}

if (refresh === true) {
_.unload();
Expand Down

0 comments on commit 0cc9031

Please sign in to comment.