layzr-rails 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +64 -0
- data/lib/layzr-rails.rb +77 -0
- data/lib/layzr-rails/configuration.rb +23 -0
- data/lib/layzr-rails/version.rb +5 -0
- metadata +76 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1fec3097e57da47bc33415bd484eb8943e1e9be2
|
4
|
+
data.tar.gz: 213ee8ea7245201e1063b0fcc70132c0d25ade84
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dd425ff8689ae7fbdc14e57d3daf9f3d7a0a050505108aa8e1e5f2a568ad19b4dd0f50a93eeacd87b1f4591e93b79adc6677f6b45b0bdcc6c5fae22e33ec8e87
|
7
|
+
data.tar.gz: d45821d79369ba7d94f7b7a7efb406e57e3661995853fa37b289e94f017b849ce540f2ea176ffb72c6d4c55e9292a6743e1c840d397e542aa6c5aa81969bcc53
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2013 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
## Layzr-Rails
|
2
|
+
|
3
|
+
This project integrates the jQuery [Layzr Plugin](https://github.com/callmecavs/layzr.js)
|
4
|
+
for Rails `image_tag` helpers
|
5
|
+
|
6
|
+
### What's jQuery Layzr Plugin?
|
7
|
+
|
8
|
+
From the project page:
|
9
|
+
|
10
|
+
*A small, fast, and modern library for lazy loading images.*
|
11
|
+
|
12
|
+
See [live demo](http://callmecavs.com/layzr.js/)
|
13
|
+
|
14
|
+
## Documentation
|
15
|
+
|
16
|
+
### Installation
|
17
|
+
|
18
|
+
Add it in your gemfile
|
19
|
+
|
20
|
+
gem 'layzr-rails'
|
21
|
+
|
22
|
+
Create a configuration file and put it in your initializers `config/initializers/layzr.rb`
|
23
|
+
|
24
|
+
|
25
|
+
Layzr::Rails.configure do |config|
|
26
|
+
config.placeholder = "/assets/some-default-image.png"
|
27
|
+
end
|
28
|
+
|
29
|
+
Require layzr library in application.js ie:
|
30
|
+
|
31
|
+
//= require layzr
|
32
|
+
|
33
|
+
And place the jvascript code:
|
34
|
+
|
35
|
+
$(document).ready(function() {
|
36
|
+
const instance = Layzr()
|
37
|
+
});
|
38
|
+
|
39
|
+
### Features
|
40
|
+
|
41
|
+
* Add `lazy: true` option to Rails `image_tag` helpers to render layzr-friendly img tags.
|
42
|
+
* Simple (really). That's pretty much it.
|
43
|
+
|
44
|
+
### Example
|
45
|
+
|
46
|
+
<%= image_tag "kittenz.png", alt: "OMG a cat!", lazy: true %>
|
47
|
+
|
48
|
+
Equals:
|
49
|
+
|
50
|
+
<img alt="OMG a cat!" data-normal="/images/kittenz.png" src="/assets/some-default-image.png">
|
51
|
+
|
52
|
+
### More options
|
53
|
+
|
54
|
+
|
55
|
+
<%= image_tag "normal.jpg", lazy: true, layzr: { retina: "retina.jpg", srcset: "small.jpg 320w, medium.jpg 768w, large.jpg 1024w"} %>
|
56
|
+
|
57
|
+
Equals:
|
58
|
+
|
59
|
+
<img src="/assets/some-default-image.png" data-normal="normal.jpg" data-retina="retina.jpg" data-srcset="small.jpg 320w, medium.jpg 768w, large.jpg 1024w">
|
60
|
+
|
61
|
+
|
62
|
+
## License
|
63
|
+
|
64
|
+
Layzr-Rails is released under the [MIT License](http://www.opensource.org/licenses/MIT).
|
data/lib/layzr-rails.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require "nokogiri"
|
2
|
+
require "action_view"
|
3
|
+
|
4
|
+
require "layzr-rails/version"
|
5
|
+
require "layzr-rails/configuration"
|
6
|
+
|
7
|
+
module Layzr
|
8
|
+
module Rails
|
9
|
+
|
10
|
+
def self.configuration
|
11
|
+
@configuration ||= Layzr::Rails::Configuration.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.configuration=(new_configuration)
|
15
|
+
@configuration = new_configuration
|
16
|
+
end
|
17
|
+
|
18
|
+
# Yields the global configuration to a block.
|
19
|
+
#
|
20
|
+
# Example:
|
21
|
+
# Layzr::Rails.configure do |config|
|
22
|
+
# config.placeholder = '/public/images/foo.gif'
|
23
|
+
# end
|
24
|
+
def self.configure
|
25
|
+
yield configuration if block_given?
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.reset
|
29
|
+
@configuration = nil
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
ActionView::Helpers::AssetTagHelper.module_eval do
|
35
|
+
alias :rails_image_tag :image_tag
|
36
|
+
|
37
|
+
def image_tag(*attrs)
|
38
|
+
options, args, layzr_options = extract_options_and_args(*attrs)
|
39
|
+
image_html = rails_image_tag(*args)
|
40
|
+
|
41
|
+
if options[:lazy]
|
42
|
+
to_lazy_image(image_html, layzr_options)
|
43
|
+
else
|
44
|
+
image_html
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def to_lazy_image(image_html, layzr_options)
|
51
|
+
img = Nokogiri::HTML::DocumentFragment.parse(image_html).at_css("img")
|
52
|
+
|
53
|
+
img["data-normal"] = img["src"]
|
54
|
+
unless layzr_options.nil?
|
55
|
+
img['data-retina'] = layzr_options[:retina] unless layzr_options[:retina].blank?
|
56
|
+
img['data-srcset'] = layzr_options[:srcset] unless layzr_options[:srcset].blank?
|
57
|
+
end
|
58
|
+
img["src"] = Layzr::Rails.configuration.placeholder
|
59
|
+
|
60
|
+
img.to_s.html_safe
|
61
|
+
end
|
62
|
+
|
63
|
+
def extract_options_and_args(*attrs)
|
64
|
+
args = attrs
|
65
|
+
if args.size > 1
|
66
|
+
options = attrs.last.dup
|
67
|
+
args.last.delete(:lazy)
|
68
|
+
layzr_options = args.last.delete(:layzr) || {}
|
69
|
+
else
|
70
|
+
layzr_options = {}
|
71
|
+
options = {}
|
72
|
+
end
|
73
|
+
|
74
|
+
[options, args, layzr_options]
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
module Layzr
|
3
|
+
module Rails
|
4
|
+
|
5
|
+
# Stores runtime configuration information.
|
6
|
+
#
|
7
|
+
# Example settings
|
8
|
+
# Layzr::Rails.configure do |c|
|
9
|
+
# c.placeholder = "/public/img/grey.gif"
|
10
|
+
# end
|
11
|
+
class Configuration
|
12
|
+
|
13
|
+
def placeholder
|
14
|
+
@placeholder
|
15
|
+
end
|
16
|
+
|
17
|
+
def placeholder=(new_placeholder)
|
18
|
+
@placeholder = new_placeholder
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: layzr-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mohit Jain
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nokogiri
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :runtime
|
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: actionpack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.1'
|
41
|
+
description: layzr-rails project integrates Layzr JS for Rails image_tag helpers
|
42
|
+
email: jain.mohit27@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- MIT-LICENSE
|
48
|
+
- README.md
|
49
|
+
- lib/layzr-rails.rb
|
50
|
+
- lib/layzr-rails/configuration.rb
|
51
|
+
- lib/layzr-rails/version.rb
|
52
|
+
homepage: https://github.com/mohitjain/layzr-rails
|
53
|
+
licenses:
|
54
|
+
- MIT
|
55
|
+
metadata: {}
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 2.4.3
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: Layzr JS Wrapper for Rails image_tag helpers
|
76
|
+
test_files: []
|