Skip to content

Commit ba3dee4

Browse files
committed
new features for the exposed function
1 parent f7578e3 commit ba3dee4

File tree

5 files changed

+143
-41
lines changed

5 files changed

+143
-41
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,22 @@ To use retina.js the old-school way, download **retina.min.js** and put it on yo
185185

186186
Using this technique, retina.js will run automatically on page load. It will also create a globally available function called `retinajs`. Whenever you'd like to manually re-initialize the script, simply call `window.retinajs()`.
187187

188+
If you don't pass any arguments to the `retinajs` function, it will only attempt to process images that have not previously been processed by the script. Optionally, you can pass a collection of HTML elements to the script, in which case it will only attempt to process elements in that collection, specifically the ones that have not been processed before. Your collection may take the form of an Array, NodeList, or jQuery selection.
189+
190+
```javascript
191+
retinajs();
192+
// Finds all images not previously processed and processes them.
193+
194+
retinajs( [img, img, img] );
195+
// Only attempts to process the images in the collection.
196+
197+
retinajs( $('img') );
198+
// Same.
199+
200+
retinajs( document.querySelectorAll('img') );
201+
// Same.
202+
```
203+
188204
#### New-School
189205

190206
To use retina.js the new-school way, you'll want to `require` it (or `import` it if you're using ES6) into your Gulp/Webpack/Grunt/CommonJS/etc application. In this case, the script won't run automatically. Instead, it'll let you determine when you'd like it to run.
@@ -197,6 +213,22 @@ window.addEventListener('load', retina);
197213

198214
Notice that the `retina` function can be called as often as you need in order to re-initialize the image swapping.
199215

216+
If you don't pass any arguments to the `retina` function, it will only attempt to process images that have not previously been processed by the script. Optionally, you can pass a collection of HTML elements to the script, in which case it will only attempt to process elements in that collection, specifically the ones that have not been processed before. Your collection may take the form of an Array, NodeList, or jQuery selection.
217+
218+
```javascript
219+
retina();
220+
// Finds all images not previously processed and processes them.
221+
222+
retina( [img, img, img] );
223+
// Only attempts to process the images in the collection.
224+
225+
retina( $('img') );
226+
// Same.
227+
228+
retina( document.querySelectorAll('img') );
229+
// Same.
230+
```
231+
200232
### CSS Preprocessors
201233

202234
The process for including the CSS mixins is relatively straightforward. Here is a breakdown for each:

dist/retina.js

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Retina.js v2.0.0
2+
* Retina.js v2.1.0
33
*
44
* Copyright 2016 Axial, LLC
55
* Released under the MIT license
@@ -34,6 +34,22 @@ var inlineReplace = /url\(('|")?([^\)'"]+)('|")?\)/i;
3434
*/
3535
var selector = '[data-rjs]';
3636

37+
/*
38+
* Define the attribute we'll use to mark an image as having been processed.
39+
*/
40+
var processedAttr = 'data-rjs-processed';
41+
42+
/**
43+
* Shortcut for turning some iterable object into an array.
44+
*
45+
* @param {Iterable} object Any iterable object.
46+
*
47+
* @return {Array}
48+
*/
49+
function arrayify(object) {
50+
return Array.prototype.slice.call(object);
51+
}
52+
3753
/**
3854
* Chooses the actual image size to fetch, (for example 2 or 3) that
3955
* will be used to create a suffix like "@2x" or "@3x".
@@ -120,6 +136,11 @@ function setSourceIfAvailable(image, retinaURL) {
120136
* image resource.
121137
*/
122138
testImage.setAttribute('src', retinaURL);
139+
140+
/*
141+
* Mark our image as processed so that it won't be processed again.
142+
*/
143+
image.setAttribute(processedAttr, true);
123144
}
124145

125146
/**
@@ -164,10 +185,17 @@ function manualSwapImage(image, src, hdsrc) {
164185
* Collects all images matching our selector, and converts our
165186
* NodeList into an Array so that Array methods will be available to it.
166187
*
167-
* @return {Array} Contains all elements matching our selector.
188+
* @param {Iterable} images Optional. An Array, jQuery selection, or NodeList
189+
* of elements to affect with retina.js.
190+
*
191+
* @return {Iterable} Contains all elements matching our selector.
168192
*/
169-
function getImages() {
170-
return typeof document !== 'undefined' ? Array.prototype.slice.call(document.querySelectorAll(selector)) : [];
193+
function getImages(images) {
194+
if (!images) {
195+
return typeof document !== 'undefined' ? arrayify(document.querySelectorAll(selector)) : [];
196+
} else {
197+
return typeof images.forEach === 'function' ? images : arrayify(images);
198+
}
171199
}
172200

173201
/**
@@ -186,23 +214,30 @@ function cleanBgImg(img) {
186214
* retina equivalent taking into account the environment capabilities and
187215
* the densities for which the user has provided images.
188216
*
217+
* @param {Iterable} images Optional. An Array, jQuery selection, or NodeList
218+
* of elements to affect with retina.js. If not
219+
* provided, retina.js will grab all images on the
220+
* page.
221+
*
189222
* @return {undefined}
190223
*/
191-
function retina() {
192-
getImages().forEach(function (img) {
193-
var isImg = img.nodeName.toLowerCase() === 'img';
194-
var src = isImg ? img.getAttribute('src') : cleanBgImg(img);
195-
var rjs = img.getAttribute('data-rjs');
196-
var rjsIsNumber = !isNaN(parseInt(rjs, 10));
197-
198-
/*
199-
* If the user provided a number, dynamically swap out the image.
200-
* If the user provided a url, do it manually.
201-
*/
202-
if (rjsIsNumber) {
203-
dynamicSwapImage(img, src, rjs);
204-
} else {
205-
manualSwapImage(img, src, rjs);
224+
function retina(images) {
225+
getImages(images).forEach(function (img) {
226+
if (!img.getAttribute(processedAttr)) {
227+
var isImg = img.nodeName.toLowerCase() === 'img';
228+
var src = isImg ? img.getAttribute('src') : cleanBgImg(img);
229+
var rjs = img.getAttribute('data-rjs');
230+
var rjsIsNumber = !isNaN(parseInt(rjs, 10));
231+
232+
/*
233+
* If the user provided a number, dynamically swap out the image.
234+
* If the user provided a url, do it manually.
235+
*/
236+
if (rjsIsNumber) {
237+
dynamicSwapImage(img, src, rjs);
238+
} else {
239+
manualSwapImage(img, src, rjs);
240+
}
206241
}
207242
});
208243
}

dist/retina.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "retinajs",
3-
"version": "2.0.1",
3+
"version": "2.1.0",
44
"license": "MIT",
55
"homepage": "https://github.com/imulus/retinajs",
66
"bugs": "https://github.com/imulus/retinajs/issues",

src/retina.js

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ const inlineReplace = /url\(('|")?([^\)'"]+)('|")?\)/i;
2020
*/
2121
const selector = '[data-rjs]';
2222

23+
/*
24+
* Define the attribute we'll use to mark an image as having been processed.
25+
*/
26+
const processedAttr = 'data-rjs-processed';
27+
28+
/**
29+
* Shortcut for turning some iterable object into an array.
30+
*
31+
* @param {Iterable} object Any iterable object.
32+
*
33+
* @return {Array}
34+
*/
35+
function arrayify(object) {
36+
return Array.prototype.slice.call(object);
37+
}
38+
2339
/**
2440
* Chooses the actual image size to fetch, (for example 2 or 3) that
2541
* will be used to create a suffix like "@2x" or "@3x".
@@ -106,6 +122,11 @@ function setSourceIfAvailable(image, retinaURL) {
106122
* image resource.
107123
*/
108124
testImage.setAttribute('src', retinaURL);
125+
126+
/*
127+
* Mark our image as processed so that it won't be processed again.
128+
*/
129+
image.setAttribute(processedAttr, true);
109130
}
110131

111132
/**
@@ -148,12 +169,19 @@ function manualSwapImage(image, src, hdsrc) {
148169
* Collects all images matching our selector, and converts our
149170
* NodeList into an Array so that Array methods will be available to it.
150171
*
151-
* @return {Array} Contains all elements matching our selector.
172+
* @param {Iterable} images Optional. An Array, jQuery selection, or NodeList
173+
* of elements to affect with retina.js.
174+
*
175+
* @return {Iterable} Contains all elements matching our selector.
152176
*/
153-
function getImages() {
154-
return typeof document !== 'undefined' ? Array.prototype.slice.call(
155-
document.querySelectorAll(selector)
156-
) : [];
177+
function getImages(images) {
178+
if (!images) {
179+
return typeof document !== 'undefined' ? arrayify(
180+
document.querySelectorAll(selector)
181+
) : [];
182+
} else {
183+
return typeof images.forEach === 'function' ? images : arrayify(images);
184+
}
157185
}
158186

159187
/**
@@ -172,23 +200,30 @@ function cleanBgImg(img) {
172200
* retina equivalent taking into account the environment capabilities and
173201
* the densities for which the user has provided images.
174202
*
203+
* @param {Iterable} images Optional. An Array, jQuery selection, or NodeList
204+
* of elements to affect with retina.js. If not
205+
* provided, retina.js will grab all images on the
206+
* page.
207+
*
175208
* @return {undefined}
176209
*/
177-
function retina() {
178-
getImages().forEach(img => {
179-
const isImg = img.nodeName.toLowerCase() === 'img';
180-
const src = isImg ? img.getAttribute('src') : cleanBgImg(img);
181-
const rjs = img.getAttribute('data-rjs');
182-
const rjsIsNumber = !isNaN(parseInt(rjs, 10));
183-
184-
/*
185-
* If the user provided a number, dynamically swap out the image.
186-
* If the user provided a url, do it manually.
187-
*/
188-
if (rjsIsNumber) {
189-
dynamicSwapImage(img, src, rjs);
190-
} else {
191-
manualSwapImage(img, src, rjs);
210+
function retina(images) {
211+
getImages(images).forEach(img => {
212+
if (!img.getAttribute(processedAttr)) {
213+
const isImg = img.nodeName.toLowerCase() === 'img';
214+
const src = isImg ? img.getAttribute('src') : cleanBgImg(img);
215+
const rjs = img.getAttribute('data-rjs');
216+
const rjsIsNumber = !isNaN(parseInt(rjs, 10));
217+
218+
/*
219+
* If the user provided a number, dynamically swap out the image.
220+
* If the user provided a url, do it manually.
221+
*/
222+
if (rjsIsNumber) {
223+
dynamicSwapImage(img, src, rjs);
224+
} else {
225+
manualSwapImage(img, src, rjs);
226+
}
192227
}
193228
});
194229
}

0 commit comments

Comments
 (0)