Skip to content

Commit

Permalink
Presenting both strategies on the demo page
Browse files Browse the repository at this point in the history
  • Loading branch information
thom4parisot committed Aug 29, 2013
1 parent 67b921d commit 2a9ef71
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 35 deletions.
42 changes: 15 additions & 27 deletions Demo - Grunt/index.html
Expand Up @@ -11,34 +11,22 @@ <h1>Imager.js</h1>
<p>Here we're using Grunt to generate a list of image sizes for us.</p>
<p>We then pass those sizes into Imager</p>

<div class="delayed-image-load" data-src="Assets/Images/Generated/A-320.jpg" data-width="1024"></div>
<div class="delayed-image-load" data-src="Assets/Images/Generated/B-320.jpg" data-width="1024"></div>
<div class="delayed-image-load" data-src="Assets/Images/Generated/C-320.jpg" data-width="1024"></div>
<div class="delayed-image-load" data-src="Assets/Images/Generated/A-{width}.jpg" data-width="1024"></div>
<div class="delayed-image-load" data-src="Assets/Images/Generated/B-{width}.jpg" data-width="1024"></div>
<div class="delayed-image-load" data-src="Assets/Images/Generated/C-{width}.jpg" data-width="1024"></div>

<script src="../Imager.js"></script>
<script src="../src/imager.js"></script>
<script src="../src/strategies/legacy.js"></script>
<script>
var imager = new Imager({
availableWidths: [320, 640, 1024],

/*
This regex captures 3 pieces of information:
1. the path to the image -> Assets/Images/Generated/
^(Assets\/Images\/Generated\/)
2. the start of the file name -> A-, B-, C-
(\w-)
3. the file extension -> .jpg
(\.(?:jpg|png|gif))$
The only other part of the regex isn't captured and that is the number specified in the file name.
One other thing to note is the use of the non-capturing group -> (?: )
This means we can use parenthesis as a way to 'group' a part of the regex without needing to capture it
*/
regex: /^(Assets\/Images\/Generated\/)(\w-)\d+(\.(?:jpg|png|gif))$/i
});
var elements = document.querySelectorAll('.delayed-image-load'),
imager = Imager.init(elements, {
availableWidths: [320, 640, 1024],
strategy: 'legacy'
}),
replace = (function(imgr){ return function(){ imgr.process(); }; })(imager);

window.addEventListener('resize', replace);
window.addEventListener('orientationchange', replace);
</script>
</body>
</html>
</html>
33 changes: 27 additions & 6 deletions Demo/index.html
Expand Up @@ -14,13 +14,34 @@ <h1>Imager.js</h1>
<img src="http://placehold.it/340">

<p>Below are three <code>&lt;div&gt;</code> elements that are enhanced using Imager.js and so they will lazy-load more images (as long as JavaScript is enabled).</p>
<div class="delayed-image-load" data-src="http://placehold.it/340" data-width="340"></div>
<div class="delayed-image-load" data-src="http://placehold.it/340" data-width="340"></div>
<div class="delayed-image-load" data-src="http://placehold.it/340" data-width="340"></div>

<script src="../Imager.js"></script>
<div class="strategy-legacy">
<h2>BBC Strategy</h2>
<div class="delayed-image-load" data-src="http://placehold.it/{width}/000.jpg" data-width="340"></div>
<div class="delayed-image-load" data-src="http://placehold.it/{width}/111.jpg" data-width="340"></div>
<div class="delayed-image-load" data-src="http://placehold.it/{width}" data-width="340"></div>
</div>

<div class="strategy-container">
<h2>Enclosing DIV Strategy</h2>
<div class="delayed-image-load" data-src="http://placehold.it/{width}/000.jpg" data-width="340"></div>
<div class="delayed-image-load" data-src="http://placehold.it/{width}/111.jpg" data-width="340"></div>
<div class="delayed-image-load" data-src="http://placehold.it/{width}" data-width="340"></div>
</div>


<script src="../src/imager.js"></script>
<script src="../src/strategies/legacy.js"></script>
<script src="../src/strategies/container.js"></script>
<script>
var imager = new Imager();
['legacy', 'container'].forEach(function(strategy){
var elements = document.querySelectorAll('.strategy-'+strategy+' .delayed-image-load'),
imager = Imager.init(elements, { strategy: strategy }),
replace = (function(imgr){ return function(){ imgr.process(); }; })(imager);

window.addEventListener('resize', replace);
window.addEventListener('orientationchange', replace);
});
</script>
</body>
</html>
</html>
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -9,7 +9,7 @@
},
"scripts": {
"test": "grunt test"
}
},
"repository": {
"type": "git",
"url": "git://github.com/BBC-News/Imager.js.git"
Expand Down
2 changes: 1 addition & 1 deletion src/strategies/legacy.js
Expand Up @@ -58,7 +58,7 @@
placeholder.setAttribute('data-src', element.getAttribute('data-src'));
placeholder.setAttribute('data-width', element.getAttribute('data-width'));

element.parentNode.replaceChild(placeholder, element);
return element.parentNode.replaceChild(placeholder, element);
};

/**
Expand Down

0 comments on commit 2a9ef71

Please sign in to comment.