Skip to content

Commit

Permalink
have attribute when image is loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
SquadraCorse committed Jun 10, 2015
1 parent 75dc895 commit 34bbb24
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 16 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ The attributes are using the [srcset](http://picture.responsiveimages.org/) setu
* '{"className": "own-classname-image"}' className will be apended to the image
- "class" : `afkl-lazy-wrapper` will use height 0 trick, `afkl-img-ratio-1-1` sets correct aspect ratio so container is 100% responsive as well (optional, include css)
- Using directive `afkl-image-container` to have a scrollable container
- Attribute `afklLazyImageLoaded` will be set when first image is really loaded. This can be used if you want to preload every image before showing your app.


## Todo
1. Angular directives don't have any notion of style encapsulation, but Angular is expected to incorporate that functionality eventually. At the moment our optional css is delivered in an own stylesheet.
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ng-directive-lazy-image",
"main": "release/lazy-image.js",
"version": "0.1.1",
"version": "0.1.2",
"homepage": "https://github.com/afklm/ng-lazy-image",
"authors": [
"KLM Royal Dutch Airlines",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "angular-lazy-image",
"main": "./release/lazy-image.js",
"version": "0.1.1",
"version": "0.1.2",
"homepage": "http://afklm.github.io/ng-lazy-image/",
"repository": {
"type": "git",
Expand Down
6 changes: 6 additions & 0 deletions release/lazy-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ angular.module('afkl.lazyImage')
IMAGECLASSNAME = IMAGECLASSNAME + ' ' + options.className;
}

attrs.afklLazyImageLoaded = false;

var _containerScrollTop = function () {
// See if we can use jQuery, with extra check
// TODO: check if number is returned
Expand Down Expand Up @@ -466,7 +468,11 @@ angular.module('afkl.lazyImage')
_checkIfNewImage();

var _loaded = function () {

attrs.$set('afklLazyImageLoaded', true);

element.removeClass(LOADING);

};


Expand Down
2 changes: 1 addition & 1 deletion release/lazy-image.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions sample/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ <h3>Using image alt and className</h3>
<!-- example how to use image set using srcset and ratio -->
<div ng-repeat="image in images">

<div afkl-lazy-image="//placehold.it/768x384/00a1de/ffffff" class="afkl-lazy-wrapper afkl-img-ratio-2-1" afkl-lazy-image-options='{"alt": "blue bird", "className": "optional-image-class"}'></div>
<div sample-count afkl-lazy-image="//placehold.it/768x384/00a1de/ffffff" class="afkl-lazy-wrapper afkl-img-ratio-2-1" afkl-lazy-image-options='{"alt": "blue bird", "className": "optional-image-class"}'></div>

</div>

Expand Down Expand Up @@ -208,10 +208,10 @@ <h3>Runtime image change after loaded</h3>
<div afkl-lazy-image="{{runtimeImageSrc}}" class="demo-no-use-of-aspect-ratio"></div>
<button ng-click="changeImage()">Change Now</button>

<!--
<h3>Check if image has been loaded</h3>
<div sample afkl-lazy-image="//placehold.it/300x300/00a1de/ffffff" class="demo-no-use-of-aspect-ratio"></div>
-->

<h3>Check if image has been loaded (attribute 'afklLazyImageLoaded' will be set to true)</h3>
<div sample-count afkl-lazy-image="//placehold.it/300x300/00a1de/ffffff" class="demo-no-use-of-aspect-ratio"></div>



<p class="demo-link"><a href="#" ng-click="destroy()">Destroy $scope</a> (no leaks)</p>
Expand Down
17 changes: 9 additions & 8 deletions sample/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
angular.module('sample-app', ['afkl.lazyImage'])
.controller('appCtrl', ['$scope', function mainCtrl($scope) {

$scope.count = 0;

$scope.images = [
{
srcset: '//placehold.it/480x240/00a1de/ffffff 480w, //placehold.it/768x384/00a1de/ffffff 768w',
Expand All @@ -22,21 +24,20 @@

$scope.changeImage();


}])
.directive('sample', function () {
.directive('sampleCount', function () {
'use strict';
var count = 0;

return {
restrict: 'A',
link: function ($scope, $element, $attrs) {

$attrs.$observe('afklLazyImageLoaded', function (newValue) {
if (window.console && newValue) {
window.console.log('IMAGE HAS BEEN LOADED');
link: function (scope, element, attrs) {
return attrs.$observe("afklLazyImageLoaded", function (value) {
if (window.console) {
window.console.log('IMAGE LOADED:', value);
}
});


}
};

Expand Down
6 changes: 6 additions & 0 deletions src/lazy-image.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ angular.module('afkl.lazyImage')
IMAGECLASSNAME = IMAGECLASSNAME + ' ' + options.className;
}

attrs.afklLazyImageLoaded = false;

var _containerScrollTop = function () {
// See if we can use jQuery, with extra check
// TODO: check if number is returned
Expand Down Expand Up @@ -167,7 +169,11 @@ angular.module('afkl.lazyImage')
_checkIfNewImage();

var _loaded = function () {

attrs.$set('afklLazyImageLoaded', true);

element.removeClass(LOADING);

};


Expand Down

0 comments on commit 34bbb24

Please sign in to comment.