Skip to content

Commit

Permalink
1.7.3
Browse files Browse the repository at this point in the history
- fixed issue #58
- created gulp build script for all files
- fixed some bugs and warnings from jshint
- fixed some readme anchors
- changed some file headers to be consistent
  • Loading branch information
dkern committed Aug 9, 2016
1 parent 5b27a01 commit ed8d2f0
Show file tree
Hide file tree
Showing 19 changed files with 269 additions and 78 deletions.
31 changes: 27 additions & 4 deletions README.md
Expand Up @@ -20,6 +20,8 @@
* [Custom Content Loaders](#custom-content-loaders)
* [Loader Plugins](#loader-plugins)
* [Configuration Parameters](#configuration-parameters)
* [Build and Validation](#build-and-validation)
* [Available gulp Tasks](#available-gulp-tasks)
* [Bugs / Feature request](#bugs--feature-request)
* [License](#license)
* [Donation](#donation)
Expand Down Expand Up @@ -57,12 +59,12 @@ Some examples below:
Lazy and all plugins are available over [cdnjs](http://cdnjs.com) and [jsDelivr](http://jsdelivr.com) CDN and can directly included to every page.
```HTML
<!-- jsDeliver -->
<script type="text/javascript" src="//cdn.jsdelivr.net/jquery.lazy/1.7.2/jquery.lazy.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/jquery.lazy/1.7.2/jquery.lazy.plugins.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/jquery.lazy/1.7.3/jquery.lazy.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/jquery.lazy/1.7.3/jquery.lazy.plugins.min.js"></script>

<!-- cdnjs -->
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.lazy/1.7.2/jquery.lazy.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.lazy/1.7.2/jquery.plugins.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.lazy/1.7.3/jquery.lazy.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.lazy/1.7.3/jquery.plugins.min.js"></script>
```

#### Self-Hosted
Expand Down Expand Up @@ -218,6 +220,27 @@ onError | *function* | *undefined* | Callback function, which
onFinishedAll | *function* | *undefined* | Callback function, which will be called after all elements was loaded or returned an error. The callback has no parameters. `this` is the current Lazy instance.


## Build and Validation
This project includes an automated build script using `gulp`.
To build your own versions of Lazy you need to [install it](#package-managers) via [npm](https://www.npmjs.com/package/jquery-lazy) first.
Afterwards you can use the following command in your console to automatically generate all productive files.
While building these files everything will be checked with `jshint` for validity too.
```sh
$ gulp build
```

### Available `gulp` Tasks:

Name | Description
-------------- | -----------
build | check & build everything
build-main | check & build main project file
build-plugins | check & build single plugin files
concat-plugins | build concatenated plugins file
validate | check all files with `jshint`
watch | watches all files to check & build everything on change


## Bugs / Feature request
Please [report](http://github.com/eisbehr-/jquery.lazy/issues) bugs and feel free to [ask](http://github.com/eisbehr-/jquery.lazy/issues) for new features directly on GitHub.

Expand Down
2 changes: 1 addition & 1 deletion bower.json
@@ -1,7 +1,7 @@
{
"name": "jquery-lazy",
"description": "Lazy is a fast, feature-rich and lightweight delayed content loading plugin for jQuery and Zepto. It's designed to speed up page loading times and decrease traffic to your users by only loading the content in view. You can use Lazy in all vertical and horizontal scroll ways. It supports images in 'img' tags and backgrounds, supplied with css like 'background-image', by default. On those elements Lazy can set an default image or a placeholder while loading and supports retina displays as well. But Lazy is even able to load any other content you want by plugins and custom loaders.",
"version": "1.7.2",
"version": "1.7.3",
"main": "jquery.lazy.min.js",
"license": [
"MIT",
Expand Down
152 changes: 152 additions & 0 deletions gulpfile.js
@@ -0,0 +1,152 @@
"use strict";

var gulp = require("gulp");
var util = require("gulp-util");
var data = require("gulp-data");
var rename = require("gulp-rename");
var header = require("gulp-header");
var jshint = require("gulp-jshint");
var uglify = require("gulp-uglify");
var concat = require("gulp-concat-util");
var pkg = require("./package.json");
var nl = require("os").EOL;



/*
** CONFIG & PATHS
*/



var config = {
header : "/*! jQuery & Zepto Lazy <%= info %> - <%= pkg.homepage %> - MIT&GPL-2.0 license - Copyright 2012-<%= year %> <%= pkg.author.name %> */",
main : pkg.main,
plugins : [
"plugins/jquery.lazy.*.js",
"!plugins/jquery.lazy.*.min.js"
]
};



/*
** PIPES
*/



var pipes = {};

// check files with jshint
pipes.validateFiles = function(files) {
return gulp.src(files, {base: "./"})
.pipe(jshint())
.pipe(jshint.reporter("jshint-stylish"))
};

// check all files
pipes.validate = function() {
return pipes.validateFiles([config.main].concat(config.plugins));
};

// build main project file
pipes.buildMain = function() {
return pipes.validateFiles(config.main)
.pipe(uglify())
.pipe(header(config.header + nl, {
pkg : pkg,
info : "v" + pkg.version,
year : new Date().getFullYear()
}))
.pipe(rename(function(path) {
path.extname = ".min" + path.extname;
}))
.pipe(gulp.dest("./"));
};

// build plugin files
pipes.buildPlugins = function() {
return pipes.validateFiles(config.plugins)
.pipe(data(function(file) {
var string = String(file.contents).split("\n")[1];
var matches = string.match(/\s-\s([a-z ]+)\s-\sv([0-9.]+)/i);

return {
plugin: {
name : matches[1],
version : matches[2]
}
};
}))
.pipe(uglify())
.pipe(header(config.header.replace("<%= info %>", "- <%= file.data.plugin.name %> v<%= file.data.plugin.version %>") + nl, {
pkg : pkg,
year : new Date().getFullYear()
}))
.pipe(rename(function(path) {
path.extname = ".min" + path.extname;
}))
.pipe(gulp.dest("./"));
};

// concat plugin files
pipes.concatPlugins = function() {
return gulp.src(config.plugins)
.pipe(concat(config.main.replace(".js", ".plugins.js"), {
sep: nl + nl
}))
.pipe(gulp.dest("./"))
.pipe(uglify())
.pipe(header(config.header + nl, {
pkg : pkg,
info : "- All Plugins v" + pkg.version,
year : new Date().getFullYear()
}))
.pipe(rename(function(path) {
path.extname = ".min" + path.extname;
}))
.pipe(gulp.dest("./"));
};



/*
** TASKS
*/



// check & build everything
gulp.task("build", ["build-main", "build-plugins", "concat-plugins"]);

// check & build main project file
gulp.task("build-main", pipes.buildMain);

// check & build single plugin files
gulp.task("build-plugins", pipes.buildPlugins);

// build concatenated plugins file
gulp.task("concat-plugins", pipes.concatPlugins);

// check all files
gulp.task("validate", pipes.validate);

// check, build & watch live changes
gulp.task("watch", ["build"], function() {
// watch main file
gulp.watch(config.main, function() {
var task = pipes.buildMain();
util.log("updated", "'" + util.colors.red("main file") + "'");
return task;
});

// watch plugins
gulp.watch(config.plugins, function() {
var task = pipes.buildPlugins();
util.log("updated", "'" + util.colors.red("plugins") + "'");
pipes.concatPlugins();
util.log("updated", "'" + util.colors.red("concatenated plugins file") + "'");
return task;
});
});
81 changes: 40 additions & 41 deletions jquery.lazy.js
@@ -1,5 +1,5 @@
/*!
* jQuery & Zepto Lazy - v1.7.2
* jQuery & Zepto Lazy - v1.7.3
* http://jquery.eisbehr.de/lazy/
*
* Copyright 2012 - 2016, Daniel 'Eisbehr' Kern
Expand Down Expand Up @@ -250,7 +250,7 @@
tag = _getElementTagName(this);

return !element.data(config.handledName) &&
(element.attr(config.attribute) || element.attr(srcsetAttribute) || element.attr(loaderAttribute) || forcedTags[tag] != undefined);
(element.attr(config.attribute) || element.attr(srcsetAttribute) || element.attr(loaderAttribute) || forcedTags[tag] !== undefined);
})

// append plugin instance to all elements
Expand All @@ -266,7 +266,7 @@
element.attr(srcsetAttribute, _getCorrectedSrcSet(element.attr(srcsetAttribute), elementImageBase));

// add loader to forced element types
if( forcedTags[tag] != undefined && !element.attr(loaderAttribute) )
if( forcedTags[tag] !== undefined && !element.attr(loaderAttribute) )
element.attr(loaderAttribute, forcedTags[tag]);

// set default image on every element without source
Expand Down Expand Up @@ -302,39 +302,38 @@
handledName = config.handledName;

// loop all available items
for( var i = 0, l = items.length; i < l; i++ )
(function(item) {
// item is at least in loadable area
if( allItems || _isInLoadableArea(item) ) {
var element = $(item),
tag = _getElementTagName(item),
attribute = element.attr(config.attribute),
elementImageBase = element.attr(config.imageBaseAttribute) || imageBase,
customLoader = element.attr(config.loaderAttribute);

// is not already handled
if( !element.data(handledName) &&
// and is visible or visibility doesn't matter
(!config.visibleOnly || element.is(":visible")) && (
// and image source or source set attribute is available
(attribute || element.attr(srcsetAttribute)) && (
// and is image tag where attribute is not equal source or source set
(tag == _img && (elementImageBase + attribute != element.attr(_src) || element.attr(srcsetAttribute) != element.attr(_srcset))) ||
// or is non image tag where attribute is not equal background
(tag != _img && elementImageBase + attribute != element.css(_backgroundImage))
) ||
// or custom loader is available
customLoader ))
{
// mark element always as handled as this point to prevent double handling
loadTriggered = true;
element.data(handledName, true);

// load item
_handleItem(element, tag, elementImageBase, customLoader);
}
for( var i = 0; i < items.length; i++ ) {
// item is at least in loadable area
if( allItems || _isInLoadableArea(items[i]) ) {
var element = $(items[i]),
tag = _getElementTagName(items[i]),
attribute = element.attr(config.attribute),
elementImageBase = element.attr(config.imageBaseAttribute) || imageBase,
customLoader = element.attr(config.loaderAttribute);

// is not already handled
if( !element.data(handledName) &&
// and is visible or visibility doesn't matter
(!config.visibleOnly || element.is(":visible")) && (
// and image source or source set attribute is available
(attribute || element.attr(srcsetAttribute)) && (
// and is image tag where attribute is not equal source or source set
(tag == _img && (elementImageBase + attribute != element.attr(_src) || element.attr(srcsetAttribute) != element.attr(_srcset))) ||
// or is non image tag where attribute is not equal background
(tag != _img && elementImageBase + attribute != element.css(_backgroundImage))
) ||
// or custom loader is available
customLoader ))
{
// mark element always as handled as this point to prevent double handling
loadTriggered = true;
element.data(handledName, true);

// load item
_handleItem(element, tag, elementImageBase, customLoader);
}
})(items[i]);
}
}

// when something was loaded remove them from remaining items
if( loadTriggered )
Expand Down Expand Up @@ -473,7 +472,7 @@
.attr(_src, imageSrc ? imageBase + imageSrc : null);

// call after load even on cached image
imageObj.complete && imageObj.load();
imageObj.complete && imageObj.load(); // jshint ignore : line
}
}

Expand Down Expand Up @@ -569,7 +568,7 @@
callback.call(instance, event);
}

timeout && clearTimeout(timeout);
timeout && clearTimeout(timeout); // jshint ignore : line

if( elapsed > delay || !config.enableThrottle || ignoreThrottle ) run();
else timeout = setTimeout(run, delay - elapsed);
Expand Down Expand Up @@ -652,7 +651,7 @@
* @access private
* @type {string}
*/
_namespace = _config.name + "-" + ++lazyInstanceId;
_namespace = _config.name + "-" + (++lazyInstanceId);

// noinspection JSUndefinedPropertyAssignment
/**
Expand Down Expand Up @@ -680,7 +679,7 @@
* @return {LazyPlugin}
*/
_instance.addItems = function(items) {
_events.a && _events.a($.type(items) === "string" ? $(items) : items);
_events.a && _events.a($.type(items) === "string" ? $(items) : items); // jshint ignore : line
return _instance;
};

Expand All @@ -704,7 +703,7 @@
* @return {LazyPlugin}
*/
_instance.update = function(useThrottle) {
_events.e && _events.e({}, !useThrottle);
_events.e && _events.e({}, !useThrottle); // jshint ignore : line
return _instance;
};

Expand All @@ -717,7 +716,7 @@
* @return {LazyPlugin}
*/
_instance.loadAll = function() {
_events.e && _events.e({all: true}, true);
_events.e && _events.e({all: true}, true); // jshint ignore : line
return _instance;
};

Expand Down

0 comments on commit ed8d2f0

Please sign in to comment.