responsive_images 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -101,7 +101,29 @@ You can also pass custom version to the helper method with:
101
101
  # custom versions
102
102
  = responsive_background_image @post.image, :sizes => { :'mobile' => :post_small, :tablet => :post_tablet }
103
103
  # => style="url(path/to/image.jpg)" data-desktop-src="path/to/image.jpg" data-tablet-src="path/to/post_tablet_image.jpg" data-mobile-src="path/to/post_mobile_image.jpg"
104
+
105
+
106
+ ### Client-side JavaScript
107
+
108
+ The ResponsiveImages helpers will allow you to construct your responsive image tags and even select the most appropriate size to load initially but if the browser window sizes changes, we'll need some client-side javascript to adjust the image src. The responsive-images.js is included in this gem but needs to be added to your javascript bundle, in application.js add:
109
+
110
+ //= require responsive-images
104
111
 
112
+ The responsive-images.js is based on jQuery, so you'll need jQuery loaded.
113
+
114
+ Now we need to instantiate the plugin. This is quite simple:
115
+
116
+ // setup our responsive images
117
+ $('img.responsive').responsive_images({
118
+ mobile_size: 480,
119
+ tablet_size: 768,
120
+ desktop_size: 980,
121
+ });
122
+
123
+ Unlike the helper methods, with the JS library, you define breakpoints that will tell the plugin to find the appropriate size and resize it to the appropriate data-attribute.
124
+ For example, if you're breakpoint is mobile_size then the plugin will look for a data-mobile-src attribute.
125
+
126
+
105
127
  ### Mobvious
106
128
 
107
129
  ResponsiveImages uses [Mobvious](https://github.com/jistr/mobvious) for device detection. Check out [the gem](https://github.com/jistr/mobvious) for more details. You can use Mobvious to detect the user device by simply calling:
@@ -0,0 +1,6 @@
1
+ module ResponsiveImages
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -3,7 +3,7 @@ require 'responsive_images/view_helpers'
3
3
  require "mobvious"
4
4
 
5
5
  module ResponsiveImages
6
- class Railtie < Rails::Railtie
6
+ class Railtie < ::Rails::Railtie
7
7
 
8
8
  # Add our view helpers
9
9
  initializer "responsive_images.view_helpers" do
@@ -1,14 +1,19 @@
1
+ require "responsive_images/rails/engine"
2
+ require "responsive_images/version"
3
+ require "responsive_images/config"
4
+ require 'responsive_images/railtie' if defined?(Rails)
5
+
1
6
  module ResponsiveImages
2
- module Activator
3
7
 
4
- class << self
5
- def initialize attrs={}
6
- attrs = ResponsiveImages.options.merge(attrs)
7
- Config::VALID_OPTION_KEYS.each do |key|
8
- instance_variable_set("@#{key}".to_sym, attrs[key])
9
- end
8
+ extend Config
9
+
10
+ class << self
11
+ def initialize attrs={}
12
+ attrs = ResponsiveImages.options.merge(attrs)
13
+ Config::VALID_OPTION_KEYS.each do |key|
14
+ instance_variable_set("@#{key}".to_sym, attrs[key])
10
15
  end
11
16
  end
12
-
13
- end
14
- end
17
+ end
18
+
19
+ end
@@ -1,3 +1,3 @@
1
1
  module ResponsiveImages
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,18 +1 @@
1
- require "responsive_images/version"
2
- require "responsive_images/config"
3
- require 'responsive_images/railtie' if defined?(Rails)
4
-
5
- module ResponsiveImages
6
-
7
- extend Config
8
-
9
- class << self
10
- def initialize attrs={}
11
- attrs = ResponsiveImages.options.merge(attrs)
12
- Config::VALID_OPTION_KEYS.each do |key|
13
- instance_variable_set("@#{key}".to_sym, attrs[key])
14
- end
15
- end
16
- end
17
-
18
- end
1
+ require "responsive_images/responsive_images"
@@ -0,0 +1,98 @@
1
+ ;(function($) {
2
+
3
+ $.fn.responsive_images = function(options) {
4
+
5
+ console.log("options:: ", options)
6
+
7
+ // Default settings
8
+ var defaults = {
9
+ default_to_small : true,
10
+ mobile_size: 300,
11
+ tablet_size: 640,
12
+ desktop_size: 980,
13
+ elements : null,
14
+ screen_width : 0,
15
+ screen_height : 0,
16
+ }
17
+
18
+ var responsive_images = this;
19
+
20
+ // Initialize the responsive images plugin
21
+ var init = function() {
22
+
23
+ // merge the default and optional seetings
24
+ responsive_images.settings = $.extend({}, defaults, options);
25
+
26
+ // Bind to resize and let's activate the resize handler immediately on load
27
+ bind_to_resize();
28
+ resize_handler();
29
+
30
+ };
31
+
32
+
33
+
34
+ // Bind to window resize event to trigger our resize handler
35
+ var bind_to_resize = function() {
36
+ $(window).resize(function() {
37
+ responsive_images.settings.screen_width = $(window).width();
38
+ responsive_images.settings.screen_height = $(window).height();
39
+ resize_handler();
40
+ });
41
+ };
42
+
43
+
44
+ var resize_handler = function() {
45
+ // Let's make sure the screen width and height are NOT set to 0. If so, let's set them
46
+ // to the screen resolution
47
+ if (responsive_images.settings.screen_width == 0 && responsive_images.settings.screen_height == 0) {
48
+ responsive_images.settings.screen_width = $(window).width();
49
+ responsive_images.settings.screen_height = $(window).height();
50
+ }
51
+ // Set up our array of sizes and the closest match as null
52
+ var size = null;
53
+ var size_key = null;
54
+ var sizes = {
55
+ mobile_size: defaults.mobile_size,
56
+ tablet_size: defaults.tablet_size,
57
+ desktop_size: defaults.desktop_size,
58
+ };
59
+ // Loop over our size array to figure out which size is the closest to our screen size
60
+ $.each(sizes, function(key, value) {
61
+ if (size == null || Math.abs(value - responsive_images.settings.screen_width) < Math.abs(size - responsive_images.settings.screen_width)) {
62
+ size = value;
63
+ size_key = key;
64
+ };
65
+ });
66
+ responsive_images.update_images(size_key, size);
67
+ };
68
+
69
+
70
+ // Update the actual images
71
+ responsive_images.update_images = function(key, size) {
72
+ // Set up our data attribute
73
+ var data_attribute = "data-" + key.replace("_", "-").replace('size', 'src');
74
+ responsive_images.each(function(index, item) {
75
+ // Let's make sure the data attribute exists
76
+ if ( $(item).attr(data_attribute) != null ) {
77
+ // If the item is a div, let's set the background image
78
+ if ( $(item).is('div') ) {
79
+ $(item).css({
80
+ "background-image" : 'url("' + $(item).attr(data_attribute) + '")',
81
+ "background-size" : "auto",
82
+ });
83
+ }
84
+ // If it's an image, let's just replace the source
85
+ else if ( $(item).is('img') ) {
86
+ $(item).attr('src', $(item).attr(data_attribute));
87
+ };
88
+ };
89
+ });
90
+ };
91
+
92
+ // Initialize the plugin
93
+ init();
94
+
95
+ };
96
+
97
+
98
+ })(jQuery);
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: responsive_images
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -73,11 +73,13 @@ files:
73
73
  - Rakefile
74
74
  - lib/responsive_images.rb
75
75
  - lib/responsive_images/config.rb
76
+ - lib/responsive_images/rails/engine.rb
76
77
  - lib/responsive_images/railtie.rb
77
78
  - lib/responsive_images/responsive_images.rb
78
79
  - lib/responsive_images/version.rb
79
80
  - lib/responsive_images/view_helpers.rb
80
81
  - responsive_images.gemspec
82
+ - vendor/assets/javascripts/responsive-images.js
81
83
  homepage: ''
82
84
  licenses:
83
85
  - MIT