forked from dkern/jquery.lazy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.lazy.plugins.js
359 lines (316 loc) · 12.2 KB
/
jquery.lazy.plugins.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*!
* jQuery & Zepto Lazy - AJAX Plugin - v1.2
* http://jquery.eisbehr.de/lazy/
*
* Copyright 2012 - 2016, Daniel 'Eisbehr' Kern
*
* Dual licensed under the MIT and GPL-2.0 licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl-2.0.html
*/
;(function($) {
// load data by ajax request and pass them to elements inner html, like:
// <div data-loader="ajax" data-src"url.html" data-method="post" data-type="html"></div>
$.lazy("ajax", function(element, response) {
ajaxRequest(this, element, response, element.attr("data-method"));
});
// load data by ajax get request and pass them to elements inner html, like:
// <div data-loader="get" data-src"url.html" data-type="html"></div>
$.lazy("get", function(element, response) {
ajaxRequest(this, element, response, "get");
});
// load data by ajax post request and pass them to elements inner html, like:
// <div data-loader="post" data-src"url.html" data-type="html"></div>
$.lazy("post", function(element, response) {
ajaxRequest(this, element, response, "post");
});
/**
* execute ajax request and handle response
* @param {object} instance
* @param {jQuery|object} element
* @param {function} response
* @param {string} [method]
*/
function ajaxRequest(instance, element, response, method) {
$.ajax({
url: element.attr("data-src"),
type: method || "get",
dataType: element.attr("data-type") || "html",
/**
* success callback
* @access private
* @param {*} content
* @return {void}
*/
success: function(content) {
// set responded data to element's inner html
element.html(content);
// use response function for Zepto
response(true);
// remove attributes
if( instance.config("removeAttribute") )
element.removeAttr("data-src data-method data-type")
},
/**
* error callback
* @access private
* @return {void}
*/
error: function() {
// pass error state to lazy
// use response function for Zepto
response(false);
}
});
}
})(window.jQuery || window.Zepto);
/*!
* jQuery & Zepto Lazy - AV Plugin - v1.3
* http://jquery.eisbehr.de/lazy/
*
* Copyright 2012 - 2016, Daniel 'Eisbehr' Kern
*
* Dual licensed under the MIT and GPL-2.0 licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl-2.0.html
*/
;(function($) {
// loads audio and video tags including tracks by two ways, like:
// <audio>
// <data-src src="audio.ogg" type="video/ogg"></data-src>
// <data-src src="audio.mp3" type="video/mp3"></data-src>
// </audio>
// <video data-poster="poster.jpg">
// <data-src src="video.ogv" type="video/ogv"></data-src>
// <data-src src="video.webm" type="video/webm"></data-src>
// <data-src src="video.mp4" type="video/mp4"></data-src>
// <data-track kind="captions" src="captions.vtt" srclang="en"></data-track>
// <data-track kind="descriptions" src="descriptions.vtt" srclang="en"></data-track>
// <data-track kind="subtitles" src="subtitles.vtt" srclang="de"></data-track>
// </video>
//
// or:
// <audio data-src="audio.ogg|video/ogg,video.mp3|video/mp3"></video>
// <video data-poster="poster.jpg" data-src="video.ogv|video/ogv,video.webm|video/webm,video.mp4|video/mp4">
// <data-track kind="captions" src="captions.vtt" srclang="en"></data-track>
// <data-track kind="descriptions" src="descriptions.vtt" srclang="en"></data-track>
// <data-track kind="subtitles" src="subtitles.vtt" srclang="de"></data-track>
// </video>
$.lazy(["av", "audio", "video"], ["audio", "video"], function(element, response) {
var elementTagName = element[0].tagName.toLowerCase();
if( elementTagName == "audio" || elementTagName == "video" ) {
var srcAttr = "data-src",
sources = element.find(srcAttr),
tracks = element.find("data-track"),
sourcesInError = 0,
// create on error callback for sources
onError = function() {
if( ++sourcesInError == sources.length )
response(false);
},
// create callback to handle a source or track entry
handleSource = function() {
var source = $(this),
type = source[0].tagName.toLowerCase(),
attributes = source.prop("attributes"),
target = $(type == srcAttr ? "<source>" : "<track>");
if( type == srcAttr )
target.one("error", onError);
$.each(attributes, function(index, attribute) {
target.attr(attribute.name, attribute.value);
});
source.replaceWith(target);
};
// create event for successfull load
element.one("loadedmetadata", function() {
response(true);
})
// remove default callbacks to ignore loading poster image
.off("load error")
// load poster image
.attr("poster", element.attr("data-poster"));
// load by child tags
if( sources.length )
sources.each(handleSource);
// load by attribute
else if( element.attr(srcAttr) ) {
// split for every entry by comma
$.each(element.attr(srcAttr).split(","), function(index, value) {
// split again for file and file type
var parts = value.split("|");
// create a source entry
element.append($("<source>")
.one("error", onError)
.attr({src: parts[0].trim(), type: parts[1].trim()}));
});
// remove now obsolete attribute
if( this.config("removeAttribute") )
element.removeAttr(srcAttr);
}
else {
// pass error state
// use response function for Zepto
response(false);
}
// load optional tracks
if( tracks.length )
tracks.each(handleSource);
}
else {
// pass error state
// use response function for Zepto
response(false);
}
});
})(window.jQuery || window.Zepto);
/*!
* jQuery & Zepto Lazy - iFrame Plugin - v1.3
* http://jquery.eisbehr.de/lazy/
*
* Copyright 2012 - 2016, Daniel 'Eisbehr' Kern
*
* Dual licensed under the MIT and GPL-2.0 licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl-2.0.html
*/
;(function($) {
// load iframe content, like:
// <iframe data-src="iframe.html"></iframe>
//
// enable content error check with:
// <iframe data-src="iframe.html" data-error-detect="true"></iframe>
$.lazy(["frame", "iframe"], "iframe", function(element, response) {
var instance = this;
if( element[0].tagName.toLowerCase() == "iframe" ) {
var srcAttr = "data-src",
errorDetectAttr = "data-error-detect",
errorDetect = element.attr(errorDetectAttr);
// default way, just replace the 'src' attribute
if( errorDetect != "true" && errorDetect != "1" ) {
// set iframe source
element.attr("src", element.attr(srcAttr));
// remove attributes
if( instance.config("removeAttribute") )
element.removeAttr(srcAttr + " " + errorDetectAttr);
}
// extended way, even check if the document is available
else {
$.ajax({
url: element.attr(srcAttr),
dataType: "html",
/**
* success callback
* @access private
* @param {*} content
* @return {void}
*/
success: function(content) {
// set responded data to element's inner html
element.html(content)
// change iframe src
.attr("src", element.attr(srcAttr));
// remove attributes
if( instance.config("removeAttribute") )
element.removeAttr(srcAttr + " " + errorDetectAttr);
},
/**
* error callback
* @access private
* @return {void}
*/
error: function() {
// pass error state to lazy
// use response function for Zepto
response(false);
}
});
}
}
else {
// pass error state to lazy
// use response function for Zepto
response(false);
}
});
})(window.jQuery || window.Zepto);
/*!
* jQuery & Zepto Lazy - NOOP Plugin - v1.2
* http://jquery.eisbehr.de/lazy/
*
* Copyright 2012 - 2016, Daniel 'Eisbehr' Kern
*
* Dual licensed under the MIT and GPL-2.0 licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl-2.0.html
*/
;(function($) {
// will do nothing, used to disable elements or for development
// use like:
// <div data-loader="noop"></div>
// does not do anything, just a 'no-operation' helper ;)
$.lazy("noop", function() {});
// does nothing, but response a successfull loading
$.lazy("noop-success", function(element, response) {
// use response function for Zepto
response(true);
});
// does nothing, but response a failed loading
$.lazy("noop-error", function(element, response) {
// use response function for Zepto
response(false);
});
})(window.jQuery || window.Zepto);
/*!
* jQuery & Zepto Lazy - Script Plugin - v1.2
* http://jquery.eisbehr.de/lazy/
*
* Copyright 2012 - 2016, Daniel 'Eisbehr' Kern
*
* Dual licensed under the MIT and GPL-2.0 licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl-2.0.html
*/
;(function($) {
// loads javascript files for script tags, like:
// <script data-src="file.js" type="text/javascript"></script>
$.lazy(["js", "javascript", "script"], "script", function(element, response) {
if( element[0].tagName.toLowerCase() == "script" ) {
element.attr("src", element.attr("data-src"));
// remove attribute
if( this.config("removeAttribute") )
element.removeAttr("data-src");
}
else {
// use response function for Zepto
response(false);
}
});
})(window.jQuery || window.Zepto);
/*!
* jQuery & Zepto Lazy - YouTube Plugin - v1.2
* http://jquery.eisbehr.de/lazy/
*
* Copyright 2012 - 2016, Daniel 'Eisbehr' Kern
*
* Dual licensed under the MIT and GPL-2.0 licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl-2.0.html
*/
;(function($) {
// load youtube video iframe, like:
// <iframe data-loader="yt" data-src="1AYGnw6MwFM" width="560" height="315" frameborder="0" allowfullscreen></iframe>
$.lazy(["yt", "youtube"], function(element, response) {
if( element[0].tagName.toLowerCase() == "iframe" ) {
// pass source to iframe
element.attr("src", "https://www.youtube.com/embed/" + element.attr("data-src") + "?rel=0&showinfo=0");
// remove attribute
if( this.config("removeAttribute") )
element.removeAttr("data-src");
}
else {
// pass error state
// use response function for Zepto
response(true);
}
});
})(window.jQuery || window.Zepto);