lazy_img_rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 39840ac81c2219c9d99b90c523527fe9d4c0e87f
4
+ data.tar.gz: 93006a07afaf56e8f0cd53a1a70e5e27ae1091d6
5
+ SHA512:
6
+ metadata.gz: ccd26a8a4b2f828ca364354a18cb853b8595a6a813157ca95c64713527b296aef36ed8a1b585d816b12371211b1499989073c2310818ce6136908f211705e395
7
+ data.tar.gz: 62b360069d2b9f6c3dda0776596b3f9ccdc9fefff02dd020dd9cf276cc4dc7fd43e4ed55e895a4bd090ef063cfde0779af0599a5dca6285e66e0571eca7f4f56
@@ -0,0 +1,41 @@
1
+ # LazyImgRails
2
+
3
+ Fork of https://github.com/angelodlfrtr/lazyloadjs
4
+ which use https://github.com/vvo/lazyload/ js lib
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'lazy_img_rails'
11
+
12
+ And then execute:
13
+
14
+ $ bundle install
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install lazy_img_rails
19
+
20
+ ## Usage
21
+
22
+ Add lazyloadjs in Assets Pipeline (e.g. `app/assets/javascripts/application.js`)
23
+
24
+ # Add it after all your others scripts
25
+ //= lazyload
26
+
27
+ Restart your application. Done.
28
+
29
+ This gem provides a helper that allows you to render `<img>` tag
30
+
31
+ # image_tag_lazy 'image_url', *options
32
+ # eg
33
+ = image_tag_lazy image_url("my_logo.png"), style: "height: 200px;", alt: "My logo"
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it ( https://github.com/trrbl/lazy_img_rails/fork )
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create new Pull Request
@@ -0,0 +1,9 @@
1
+ require "lazy_img_rails/version"
2
+ require 'lazy_img_rails/railtie' if defined?(Rails)
3
+
4
+ module LazyImgRails
5
+ module Rails
6
+ class Engine < ::Rails::Engine
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require 'lazy_img_rails/view_helpers'
2
+
3
+ module LazyImgRails
4
+ class Railtie < Rails::Railtie
5
+ initializer "lazy_img_rails.view_helpers" do
6
+ ActionView::Base.send :include, ViewHelpers
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module LazyImgRails
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ module LazyImgRails
2
+ module ViewHelpers
3
+ def image_tag_lazy image_path, args = {}
4
+ options = ''
5
+ args.each{|arg| options += "#{arg[0]}='#{arg[1]}'"}
6
+
7
+ raw "<img data-src='#{image_path}' src=data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== onload=lzld(this) onerror=lzld(this) #{options} />"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,337 @@
1
+ /**
2
+ * @license in-viewport v0.4.1 | github.com/vvo/in-viewport#license
3
+ */
4
+
5
+ (function(win, doc){
6
+ var instances = [];
7
+
8
+ win['inViewport'] = inViewport;
9
+
10
+ function inViewport(elt, params, cb) {
11
+ var opts = {
12
+ container: doc.body,
13
+ offset: 0
14
+ };
15
+
16
+ if (params === undefined || typeof params === 'function') {
17
+ cb = params;
18
+ params = {};
19
+ }
20
+
21
+ var container = opts.container = params['container'] || opts.container;
22
+ var offset = opts.offset = params['offset'] || opts.offset;
23
+
24
+ for (var i = 0; i < instances.length; i++) {
25
+ if (instances[i].container === container) {
26
+ return instances[i].inViewport(elt, offset, cb);
27
+ }
28
+ }
29
+
30
+ return instances[
31
+ instances.push(createInViewport(container)) - 1
32
+ ].inViewport(elt, offset, cb)
33
+ }
34
+
35
+ function addEvent( el, type, fn ) {
36
+ if (el.attachEvent) {
37
+ el.attachEvent( 'on' + type, fn );
38
+ } else {
39
+ el.addEventListener( type, fn, false );
40
+ }
41
+ }
42
+
43
+ function debounce(func, wait, immediate) {
44
+ var timeout;
45
+ return function() {
46
+ var context = this, args = arguments;
47
+ var later = function() {
48
+ timeout = null;
49
+ if (!immediate) func.apply(context, args);
50
+ };
51
+ var callNow = immediate && !timeout;
52
+ clearTimeout(timeout);
53
+ timeout = setTimeout(later, wait);
54
+ if (callNow) func.apply(context, args);
55
+ }
56
+ }
57
+
58
+ // https://github.com/jquery/sizzle/blob/3136f48b90e3edc84cbaaa6f6f7734ef03775a07/sizzle.js#L708
59
+ var contains = document.documentElement.compareDocumentPosition ?
60
+ function( a, b ) {
61
+ return !!(a.compareDocumentPosition( b ) & 16);
62
+ } :
63
+ document.documentElement.contains ?
64
+ function( a, b ) {
65
+ return a !== b && ( a.contains ? a.contains( b ) : false );
66
+ } :
67
+ function( a, b ) {
68
+ while ( (b = b.parentNode) ) {
69
+ if ( b === a ) {
70
+ return true;
71
+ }
72
+ }
73
+ return false;
74
+ };
75
+
76
+ function createInViewport(container) {
77
+ var watches = [];
78
+ var watching = [];
79
+
80
+ var scrollContainer = container === doc.body ? win : container;
81
+ var debouncedCheck = debounce(checkElements, 15);
82
+
83
+ addEvent(scrollContainer, 'scroll', debouncedCheck);
84
+
85
+
86
+ if (scrollContainer === win) {
87
+ addEvent(win, 'resize', debouncedCheck);
88
+ }
89
+
90
+ if (typeof window['MutationObserver'] === 'function') {
91
+ observeDOM(watching, container, debouncedCheck);
92
+ }
93
+
94
+ function inViewport(elt, offset, cb) {
95
+ if (!contains(doc.documentElement, elt) ||
96
+ !contains(doc.documentElement, container)) {
97
+ if (cb) {
98
+ return setTimeout(addWatch(elt, offset, cb), 0);
99
+ } else {
100
+ return false;
101
+ }
102
+ }
103
+
104
+ var eltRect = elt.getBoundingClientRect();
105
+ var containerRect = container.getBoundingClientRect();
106
+
107
+ var pos = {
108
+ left: eltRect.left,
109
+ top: eltRect.top
110
+ };
111
+
112
+ var viewport = {
113
+ width: offset,
114
+ height: offset
115
+ };
116
+
117
+ if (container === doc.body) {
118
+ viewport.width += doc.documentElement.clientWidth;
119
+ viewport.height += doc.documentElement.clientHeight;
120
+
121
+ // We update body rect computing because
122
+ // when you have relative/absolute childs, you get bad compute
123
+ // we need to create a new Object, because it's read only
124
+ containerRect = {
125
+ bottom: container.scrollHeight,
126
+ top: 0,
127
+ left: 0,
128
+ right: container.scrollWidth
129
+ };
130
+ } else {
131
+ pos.left -= containerRect.left;
132
+ pos.top -= containerRect.top;
133
+ viewport.width += container.clientWidth;
134
+ viewport.height += container.clientHeight;
135
+ }
136
+
137
+ var visible =
138
+ // 1. They must overlap
139
+ !(
140
+ eltRect.right < containerRect.left ||
141
+ eltRect.left > containerRect.right ||
142
+ eltRect.bottom < containerRect.top ||
143
+ eltRect.top > containerRect.bottom
144
+ ) && (
145
+ // 2. They must be visible in the viewport
146
+ pos.top <= viewport.height &&
147
+ pos.left <= viewport.width
148
+ );
149
+
150
+ if (visible) {
151
+ if (cb) {
152
+ watching.splice(indexOf.call(watching, elt), 1);
153
+ cb(elt);
154
+ } else {
155
+ return true;
156
+ }
157
+ } else {
158
+ if (cb) {
159
+ setTimeout(addWatch(elt, offset, cb), 0);
160
+ } else {
161
+ return false;
162
+ }
163
+ }
164
+ }
165
+
166
+ function addWatch(elt, offset, cb) {
167
+ if (indexOf.call(watching, elt) === -1) {
168
+ watching.push(elt);
169
+ }
170
+
171
+ return function() {
172
+ watches.push(function() {
173
+ inViewport(elt, offset, cb);
174
+ });
175
+ }
176
+ }
177
+
178
+ function checkElements() {
179
+ var cb;
180
+ while(cb = watches.shift()) {
181
+ cb();
182
+ }
183
+ }
184
+
185
+ return {
186
+ container: container,
187
+ inViewport: inViewport
188
+ }
189
+ }
190
+
191
+ function indexOf(value) {
192
+ for (var i = this.length; i-- && this[i] !== value;) {}
193
+ return i;
194
+ }
195
+
196
+ function observeDOM(elements, container, cb) {
197
+ var observer = new MutationObserver(watch);
198
+ var filter = Array.prototype.filter;
199
+
200
+ observer.observe(container, {
201
+ childList: true,
202
+ subtree: true
203
+ });
204
+
205
+ function watch(mutations) {
206
+ // some new DOM nodes where previously watched
207
+ // we should check their positions
208
+ if (mutations.some(knownNodes) === true) {
209
+ setTimeout(cb, 0);
210
+ }
211
+ }
212
+
213
+ function isWatched(node) {
214
+ return indexOf.call(elements, node) !== -1;
215
+ }
216
+
217
+ function knownNodes(mutation) {
218
+ return filter.call(mutation.addedNodes, isWatched).length > 0
219
+ }
220
+ }
221
+
222
+ })(window, document);
223
+ /**
224
+ * @license lazyload v2.1.1 | github.com/vvo/lazyload#license
225
+ */
226
+
227
+ (function(window, document){
228
+
229
+ var lazyAttrs = ['data-src'];
230
+
231
+ window['lazyload'] = lazyload;
232
+ window['lzld'] = lazyload();
233
+
234
+ // Provide libs using getAttribute early to get the good src
235
+ // and not the fake data-src
236
+ replaceGetAttribute('Image');
237
+ replaceGetAttribute('IFrame');
238
+
239
+ function registerLazyAttr(attr) {
240
+ if (indexOf.call(lazyAttrs, attr) === -1) {
241
+ lazyAttrs.push(attr);
242
+ }
243
+ }
244
+
245
+ function lazyload(opts) {
246
+
247
+ if (arguments.length > 1) {
248
+ return inViewport.apply(undefined, arguments);
249
+ }
250
+
251
+ opts = merge({
252
+ 'offset': 333,
253
+ 'src': 'data-src',
254
+ 'container': false
255
+ }, opts || {});
256
+
257
+ if (typeof opts['src'] === 'string') {
258
+ registerLazyAttr(opts['src']);
259
+ }
260
+
261
+ var elts = [];
262
+
263
+ function show(elt) {
264
+ var src = findRealSrc(elt);
265
+
266
+ if (src) {
267
+ elt.src = src;
268
+ }
269
+
270
+ elt['data-lzled'] = true;
271
+ elts[indexOf.call(elts, elt)] = null;
272
+ }
273
+
274
+ function findRealSrc(elt) {
275
+ if (typeof opts['src'] === 'function') {
276
+ return opts['src'](elt);
277
+ } else {
278
+ return elt.getAttribute(opts['src']);
279
+ }
280
+ }
281
+
282
+ function register(elt) {
283
+ // unsubscribe onload
284
+ // needed by IE < 9, otherwise we get another onload when changing the src
285
+ elt.onload = null;
286
+ elt.removeAttribute('onload');
287
+
288
+ if (indexOf.call(elts, elt) === -1) {
289
+ inViewport(elt, opts, show);
290
+ }
291
+ }
292
+
293
+ return register;
294
+ }
295
+
296
+ function replaceGetAttribute(elementName) {
297
+ var fullname = 'HTML' + elementName + 'Element';
298
+ if (fullname in window === false) {
299
+ return;
300
+ }
301
+
302
+ var original = window[fullname].prototype.getAttribute;
303
+ window[fullname].prototype.getAttribute = function(name) {
304
+ if(name === 'src') {
305
+ var realSrc;
306
+ for (var i = 0, max = lazyAttrs.length; i < max; i++) {
307
+ if (realSrc = original.call(this, lazyAttrs[i])) {
308
+ break;
309
+ }
310
+ }
311
+
312
+ return realSrc || original.call(this, name);
313
+ } else {
314
+ // our own lazyloader will go through theses lines
315
+ // because we use getAttribute(opts.src)
316
+ return original.call(this, name);
317
+ }
318
+ }
319
+ }
320
+
321
+ function merge(defaults, opts) {
322
+ for (var name in defaults) {
323
+ if (opts[name] === undefined) {
324
+ opts[name] = defaults[name];
325
+ }
326
+ }
327
+
328
+ return opts;
329
+ }
330
+
331
+ // http://webreflection.blogspot.fr/2011/06/partial-polyfills.html
332
+ function indexOf(value) {
333
+ for (var i = this.length; i-- && this[i] !== value;) {}
334
+ return i;
335
+ }
336
+
337
+ }(window, document));
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lazy_img_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stanislav Schultz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: railties
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Implementing LazyLoad ( https://github.com/vvo/lazyload/ ) for Ruby on
56
+ Rails
57
+ email:
58
+ - trrblspirit@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - README.md
64
+ - lib/lazy_img_rails.rb
65
+ - lib/lazy_img_rails/railtie.rb
66
+ - lib/lazy_img_rails/version.rb
67
+ - lib/lazy_img_rails/view_helpers.rb
68
+ - vendor/assets/javascripts/lazyload.js
69
+ homepage: http://github.com/trrbl/lazy_img_rails/
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.2.2
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Implementing LazyLoad ( https://github.com/vvo/lazyload/ ) for Ruby on Rails
93
+ test_files: []