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

[READY] README demo for defining availableWidths as a function does not work #66

Merged
merged 3 commits into from
Feb 3, 2014
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
33 changes: 15 additions & 18 deletions Imager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

'use strict';

var defaultWidths, getKeys, isArray, nextTick, addEvent;
var defaultWidths, getKeys, nextTick, addEvent;

nextTick = window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
Expand Down Expand Up @@ -53,10 +53,6 @@
return keys;
};

isArray = function isArray (object) {
return Object.prototype.toString.call(object) === '[object Array]';
};


/*
Construct a new Imager instance, passing an optional configuration object.
Expand Down Expand Up @@ -119,24 +115,25 @@
this.lazyload = opts.hasOwnProperty('lazyload') ? opts.lazyload : false;
this.scrolled = false;
this.availablePixelRatios = opts.availablePixelRatios || [1, 2];
this.availableWidths = opts.availableWidths || defaultWidths;
this.widthMap = {};
this.refreshPixelRatio();

if (opts.availableWidths === undefined) {
opts.availableWidths = defaultWidths;
}

if (isArray(opts.availableWidths)) {
this.availableWidths = opts.availableWidths;
if (typeof this.availableWidths !== 'function'){
if (typeof this.availableWidths.length === 'number') {
this.widthsMap = Imager.createWidthsMap(this.availableWidths);
}
else {
this.availableWidths = getKeys(opts.availableWidths);
this.widthsMap = opts.availableWidths;
}
}
else {
this.widthsMap = this.availableWidths;
this.availableWidths = getKeys(this.availableWidths);
}

this.availableWidths = this.availableWidths.sort(function (a, b) {
this.availableWidths = this.availableWidths.sort(function (a, b) {
return a - b;
});
});
}



if (elements) {
this.divs = applyEach(elements, returnDirectValue);
Expand Down
13 changes: 7 additions & 6 deletions test/unit/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,15 @@ describe('Imager.js', function () {

it('can be a function computing a value for you', function (done) {
// this example will always compute sizes 8 pixels by 8 pixels
// we need to stub it for now as events are triggered automatically and generates exceptions we can escape
var imgr = new Imager();
var imgr = new Imager({
onResize: false,
lazyload: false,
availableWidths: function (image) {
return image.clientWidth - image.clientWidth % 8 + (1 * (image.clientWidth % 8 ? 8 : 0));
}
});

setTimeout(function () {
imgr.availableWidths = function (image) {
return image.clientWidth - image.clientWidth % 8 + (1 * (image.clientWidth % 8 ? 8 : 0));
};

var img = { clientWidth: 320 };
var spy = sandbox.spy(imgr, 'availableWidths');

Expand Down