hisrc-rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +3 -0
- data/README.md +86 -0
- data/Rakefile +8 -0
- data/hisrc-rails.gemspec +18 -0
- data/lib/hisrc-rails.rb +3 -0
- data/lib/hisrc-rails/railtie.rb +15 -0
- data/lib/hisrc-rails/responsive_image_tag_helper.rb +30 -0
- data/spec/integration_spec.rb +22 -0
- data/spec/responsive_image_tag_helper_spec.rb +58 -0
- data/spec/spec_helper.rb +5 -0
- data/vendor/assets/javascripts/hisrc.js +252 -0
- metadata +123 -0
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# hisrc-rails
|
2
|
+
|
3
|
+
Retina displays are coming! Be sure to make your Rails app ready today.
|
4
|
+
|
5
|
+
How? Well, there are a lot of different approaches:
|
6
|
+
* http://css-tricks.com/which-responsive-images-solution-should-you-use/
|
7
|
+
* http://www.alistapart.com/articles/responsive-images-how-they-almost-worked-and-what-we-need/
|
8
|
+
|
9
|
+
The one I like the most is [HiSRC](https://github.com/teleject/hisrc). This gem makes your live easier through two steps:
|
10
|
+
|
11
|
+
* It adds HiSRC to the Rails asset pipeline – no more messing around with javascript files.
|
12
|
+
* It provides a helper method to easy include HiSRC in your Rails views.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
In your `Gemfile`:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'hisrc-rails'
|
20
|
+
```
|
21
|
+
|
22
|
+
In your `application.js`:
|
23
|
+
|
24
|
+
```js
|
25
|
+
//= require hisrc
|
26
|
+
|
27
|
+
$(function() {
|
28
|
+
$('img').hisrc();
|
29
|
+
});
|
30
|
+
```
|
31
|
+
|
32
|
+
For detailed information about available configuration options be sure to checkout the [HiSRC readme](https://github.com/teleject/hisrc#setting-up).
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
This gem provides a nice little helper method to optimize your views: `responsive_image_tag`
|
37
|
+
|
38
|
+
`responsive_image_tag` accepts the same options as `image_tag`, and two additional options as well:
|
39
|
+
|
40
|
+
* `:'1x'` - If no 1x option is provided, the `src` is used.
|
41
|
+
* `:'2x'` - If no 2x options is provided, "@2x" is added to the `src`. So "rails.png" becomes "rails@2x.png".
|
42
|
+
|
43
|
+
## Examples
|
44
|
+
```ruby
|
45
|
+
responsive_image_tag("rails.png")
|
46
|
+
# => <img src="/assets/rails.png" data-1x="/assets/rails.png" data-2x="/assets/rails@2x.png" />
|
47
|
+
|
48
|
+
responsive_image_tag("http://placehold.it/100x100", :'1x' => "http://placehold.it/200x200", :'2x' => "http://placehold.it/400x400")
|
49
|
+
# => <img src="http://placehold.it/100x100" data-1x="http://placehold.it/200x200" data-2x="http://placehold.it/200x200" />
|
50
|
+
```
|
51
|
+
|
52
|
+
## Acknowledgements
|
53
|
+
Many thanks to [1Mark](https://github.com/1Marc) for [his first version](https://github.com/1Marc/hisrc) of HiSRC, and to [teleject](https://github.com/teleject) for maintaining the [current version](https://github.com/teleject/hisrc) of this really great jQuery plugin. :)
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
1. Fork it
|
58
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
59
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
60
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
61
|
+
5. Create new Pull Request
|
62
|
+
|
63
|
+
## Copyright
|
64
|
+
|
65
|
+
(The MIT license)
|
66
|
+
|
67
|
+
Copyright (c) 2012 Mario Uher
|
68
|
+
|
69
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
70
|
+
a copy of this software and associated documentation files (the
|
71
|
+
"Software"), to deal in the Software without restriction, including
|
72
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
73
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
74
|
+
permit persons to whom the Software is furnished to do so, subject to
|
75
|
+
the following conditions:
|
76
|
+
|
77
|
+
The above copyright notice and this permission notice shall be
|
78
|
+
included in all copies or substantial portions of the Software.
|
79
|
+
|
80
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
81
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
82
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
83
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
84
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
85
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
86
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/hisrc-rails.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Gem::Specification.new do |gem|
|
2
|
+
gem.name = 'hisrc-rails'
|
3
|
+
gem.version = '0.1.0'
|
4
|
+
gem.authors = 'Mario Uher'
|
5
|
+
gem.email = 'uher.mario@gmail.com'
|
6
|
+
gem.description = 'Make owners of the MacBook Pro with Retina Display happy and provide high-res images within your Rails app.'
|
7
|
+
gem.summary = 'Use hisrc-rails with Rails 3.1+.'
|
8
|
+
gem.homepage = 'http://haihappen.github.com/hisrc-rails'
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split("\n")
|
11
|
+
gem.require_path = 'lib'
|
12
|
+
|
13
|
+
gem.add_dependency 'activesupport', '~> 3.1'
|
14
|
+
gem.add_dependency 'jquery-rails', '~> 2.0'
|
15
|
+
|
16
|
+
gem.add_development_dependency 'minitest'
|
17
|
+
gem.add_development_dependency 'rails', '~> 3.1'
|
18
|
+
end
|
data/lib/hisrc-rails.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rails/railtie'
|
2
|
+
require 'rails/engine'
|
3
|
+
|
4
|
+
module HisrcRails
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
initializer 'hisrc-rails.initialize' do
|
7
|
+
ActiveSupport.on_load :action_view do
|
8
|
+
require 'hisrc-rails/responsive_image_tag_helper'
|
9
|
+
include HisrcRails::ResponsiveImageTagHelper
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Engine < ::Rails::Engine; end
|
15
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'active_support/core_ext/object/blank'
|
2
|
+
|
3
|
+
module HisrcRails
|
4
|
+
module ResponsiveImageTagHelper
|
5
|
+
# Returns a hisrc-ready html image tag for the +src+.
|
6
|
+
# If not otherwise specified, it will add two data attributes
|
7
|
+
# which are required for hisrc to work.
|
8
|
+
#
|
9
|
+
# ==== Options
|
10
|
+
# +responsive_image_tag+ accepts the same options as +image_tag+,
|
11
|
+
# and two additional options as well:
|
12
|
+
#
|
13
|
+
# * <tt>:'1x'</tt> - If no 1x option is provided, the +src+ is used.
|
14
|
+
# * <tt>:'2x'</tt> - If no 2x options is provided, "@2x" is added to
|
15
|
+
# the +src+. So "rails.png" becomes "rails@2x.png".
|
16
|
+
#
|
17
|
+
# ==== Examples
|
18
|
+
# responsive_image_tag("rails.png") # =>
|
19
|
+
# <img src="/assets/rails.png" data-1x="/assets/rails.png" data-2x="/assets/rails@2x.png" />
|
20
|
+
# responsive_image_tag("http://placehold.it/100x100", :'1x' => "http://placehold.it/200x200", :'2x' => "http://placehold.it/400x400") # =>
|
21
|
+
# <img src="http://placehold.it/100x100" data-1x="http://placehold.it/200x200" data-2x="http://placehold.it/200x200" />
|
22
|
+
def responsive_image_tag(src, options = {})
|
23
|
+
options[:data] ||= {}
|
24
|
+
options[:data][:'1x'] ||= path_to_image(options.delete(:'1x').presence || options.delete('1x').presence || src)
|
25
|
+
options[:data][:'2x'] ||= path_to_image(options.delete(:'2x').presence || options.delete('2x').presence || src.gsub(/([\w\/]+).(\w+)/, '\1@2x.\2'))
|
26
|
+
|
27
|
+
image_tag(src, options)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.expand_path('../spec_helper', __FILE__)
|
2
|
+
require 'rails/railtie'
|
3
|
+
require 'hisrc-rails/railtie'
|
4
|
+
require 'action_controller/railtie'
|
5
|
+
|
6
|
+
class App < Rails::Application
|
7
|
+
config.active_support.deprecation = :log
|
8
|
+
new.initialize!
|
9
|
+
end
|
10
|
+
|
11
|
+
describe ActionView::Base do
|
12
|
+
it 'haz method' do
|
13
|
+
ActionView::Base.instance_methods.must_include(:responsive_image_tag)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
describe ActionController::Base do
|
19
|
+
it 'haz helper' do
|
20
|
+
ActionController::Base.helpers.methods.must_include(:responsive_image_tag)
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require File.expand_path('../spec_helper', __FILE__)
|
2
|
+
require 'hisrc-rails/responsive_image_tag_helper'
|
3
|
+
|
4
|
+
describe HisrcRails::ResponsiveImageTagHelper do
|
5
|
+
let(:mock) do
|
6
|
+
Class.new(MiniTest::Mock) do
|
7
|
+
include HisrcRails::ResponsiveImageTagHelper
|
8
|
+
|
9
|
+
def path_to_image(_) _ end
|
10
|
+
end.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def data(_1x, _2x)
|
14
|
+
{ data: { :'1x' => _1x, :'2x' => _2x }}
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
describe :responsive_image_tag do
|
19
|
+
it 'sets the data-1x and data-2x attributes' do
|
20
|
+
mock.expect(:image_tag, nil, ['rails.png', data('rails.png', 'rails@2x.png')])
|
21
|
+
|
22
|
+
mock.responsive_image_tag('rails.png')
|
23
|
+
mock.verify
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
it 'accepts a long +src+ path too' do
|
28
|
+
mock.expect(:image_tag, nil, ['long/path/to/image.png', data('long/path/to/image.png', 'long/path/to/image@2x.png')])
|
29
|
+
|
30
|
+
mock.responsive_image_tag('long/path/to/image.png')
|
31
|
+
mock.verify
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
it 'allows to explicity set the +x+ option' do
|
36
|
+
mock.expect(:image_tag, nil, ['http://placehold.it/100x100', data('http://placehold.it/200x200', 'http://placehold.it/400x400')])
|
37
|
+
|
38
|
+
mock.responsive_image_tag("http://placehold.it/100x100", :'1x' => "http://placehold.it/200x200", :'2x' => "http://placehold.it/400x400")
|
39
|
+
mock.verify
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
it 'allows to explicity set the +x+ option using strings' do
|
44
|
+
mock.expect(:image_tag, nil, ['http://placehold.it/100x100', data('http://placehold.it/200x200', 'http://placehold.it/400x400')])
|
45
|
+
|
46
|
+
mock.responsive_image_tag("http://placehold.it/100x100", '1x' => "http://placehold.it/200x200", '2x' => "http://placehold.it/400x400")
|
47
|
+
mock.verify
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
it 'allows to explicity set the +data+ hash' do
|
52
|
+
mock.expect(:image_tag, nil, ['http://placehold.it/100x100', data('http://placehold.it/200x200', 'http://placehold.it/400x400')])
|
53
|
+
|
54
|
+
mock.responsive_image_tag("http://placehold.it/100x100", data: { :'1x' => "http://placehold.it/200x200", :'2x' => "http://placehold.it/400x400" })
|
55
|
+
mock.verify
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,252 @@
|
|
1
|
+
/*
|
2
|
+
* Hisrc jQuery Plugin
|
3
|
+
*
|
4
|
+
* Copyright (c) 2012
|
5
|
+
* Licensed under the MIT license.
|
6
|
+
*
|
7
|
+
*/
|
8
|
+
|
9
|
+
(function($){
|
10
|
+
$.hisrc = {
|
11
|
+
bandwidth: null,
|
12
|
+
connectionTestResult: null,
|
13
|
+
connectionKbps: null,
|
14
|
+
connectionType: null,
|
15
|
+
devicePixelRatio: null
|
16
|
+
};
|
17
|
+
|
18
|
+
$.hisrc.defaults = {
|
19
|
+
useTransparentGif: false,
|
20
|
+
transparentGifSrc: 'data:image/gif;base64,R0lGODlhAQABAIAAAMz/AAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==',
|
21
|
+
minKbpsForHighBandwidth: 300,
|
22
|
+
speedTestUri: 'https://s3.amazonaws.com/cdeutsch/50K',
|
23
|
+
speedTestKB: 50,
|
24
|
+
speedTestExpireMinutes: 30,
|
25
|
+
forcedBandwidth: false
|
26
|
+
};
|
27
|
+
|
28
|
+
// for performance, run this right away (requires jQuery, but no need to wait for DOM to be ready)
|
29
|
+
$.hisrc.speedTest = function() {
|
30
|
+
$(window).hisrc();
|
31
|
+
};
|
32
|
+
|
33
|
+
$.fn.hisrc = function(options) {
|
34
|
+
var settings = $.extend({}, $.hisrc.defaults, options),
|
35
|
+
|
36
|
+
$els = $(this),
|
37
|
+
|
38
|
+
// check bandwidth via @Modernizr's network-connection.js
|
39
|
+
connection = navigator.connection || { type: 0 }, // polyfill
|
40
|
+
|
41
|
+
isSlowConnection = connection.type == 3
|
42
|
+
|| connection.type == 4
|
43
|
+
|| /^[23]g$/.test(connection.type);
|
44
|
+
|
45
|
+
|
46
|
+
// get pixel ratio
|
47
|
+
$.hisrc.devicePixelRatio = 1;
|
48
|
+
if(window.devicePixelRatio !== undefined) { $.hisrc.devicePixelRatio = window.devicePixelRatio; }
|
49
|
+
|
50
|
+
|
51
|
+
// variables/functions below for speed test are taken from Foresight.js
|
52
|
+
// Copyright (c) 2012 Adam Bradley
|
53
|
+
// Licensed under the MIT license.
|
54
|
+
// https://github.com/adamdbradley/foresight.js
|
55
|
+
// Modified by Christopher Deutsch for hisrc.js
|
56
|
+
var speedTestUri = settings.speedTestUri,
|
57
|
+
STATUS_LOADING = 'loading',
|
58
|
+
STATUS_COMPLETE = 'complete',
|
59
|
+
LOCAL_STORAGE_KEY = 'fsjs', // may as well piggy-back on Forsight localstorage key since we're doing the same thing.
|
60
|
+
speedConnectionStatus,
|
61
|
+
|
62
|
+
initSpeedTest = function () {
|
63
|
+
|
64
|
+
// only check the connection speed once, if there is a status then we've
|
65
|
+
// already got info or it already started
|
66
|
+
if ( speedConnectionStatus ) {
|
67
|
+
return;
|
68
|
+
}
|
69
|
+
|
70
|
+
// force that this device has a low or high bandwidth, used more so for debugging purposes
|
71
|
+
if ( settings.forcedBandwidth ) {
|
72
|
+
$.hisrc.bandwidth = settings.forcedBandwidth;
|
73
|
+
$.hisrc.connectionTestResult = 'forced';
|
74
|
+
speedConnectionStatus = STATUS_COMPLETE;
|
75
|
+
$els.trigger('speedTestComplete.hisrc');
|
76
|
+
return;
|
77
|
+
}
|
78
|
+
|
79
|
+
// if the device pixel ratio is 1, then no need to do a network connection
|
80
|
+
// speed test since it can't show hi-res anyways
|
81
|
+
if ( $.hisrc.devicePixelRatio === 1 ) {
|
82
|
+
$.hisrc.connectionTestResult = 'skip';
|
83
|
+
speedConnectionStatus = STATUS_COMPLETE;
|
84
|
+
$els.trigger('speedTestComplete.hisrc');
|
85
|
+
return;
|
86
|
+
}
|
87
|
+
|
88
|
+
// if we know the connection is 2g or 3g
|
89
|
+
// don't even bother with the speed test, cuz its slow
|
90
|
+
// Copyright (c) Faruk Ates, Paul Irish, Alex Sexton
|
91
|
+
// Available under the BSD and MIT licenses: www.modernizr.com/license/
|
92
|
+
// https://github.com/Modernizr/Modernizr/blob/master/feature-detects/network-connection.js
|
93
|
+
// Modified by Adam Bradley for Foresight.js
|
94
|
+
$.hisrc.connectionType = connection.type;
|
95
|
+
if ( isSlowConnection ) {
|
96
|
+
// we know this connection is slow, don't bother even doing a speed test
|
97
|
+
$.hisrc.connectionTestResult = 'connTypeSlow';
|
98
|
+
speedConnectionStatus = STATUS_COMPLETE;
|
99
|
+
$els.trigger('speedTestComplete.hisrc');
|
100
|
+
return;
|
101
|
+
}
|
102
|
+
|
103
|
+
// check if a speed test has recently been completed and its
|
104
|
+
// results are saved in the local storage
|
105
|
+
try {
|
106
|
+
var fsData = JSON.parse( localStorage.getItem( LOCAL_STORAGE_KEY ) );
|
107
|
+
if ( fsData !== null ) {
|
108
|
+
if ( ( new Date() ).getTime() < fsData.exp ) {
|
109
|
+
// already have connection data within our desired timeframe
|
110
|
+
// use this recent data instead of starting another test
|
111
|
+
$.hisrc.bandwidth = fsData.bw;
|
112
|
+
$.hisrc.connectionKbps = fsData.kbps;
|
113
|
+
$.hisrc.connectionTestResult = 'localStorage';
|
114
|
+
speedConnectionStatus = STATUS_COMPLETE;
|
115
|
+
$els.trigger('speedTestComplete.hisrc');
|
116
|
+
return;
|
117
|
+
}
|
118
|
+
}
|
119
|
+
} catch( e ) { }
|
120
|
+
|
121
|
+
var
|
122
|
+
speedTestImg = document.createElement( 'img' ),
|
123
|
+
endTime,
|
124
|
+
startTime,
|
125
|
+
speedTestTimeoutMS;
|
126
|
+
|
127
|
+
speedTestImg.onload = function () {
|
128
|
+
// speed test image download completed
|
129
|
+
// figure out how long it took and an estimated connection speed
|
130
|
+
endTime = ( new Date() ).getTime();
|
131
|
+
|
132
|
+
var duration = ( endTime - startTime ) / 1000;
|
133
|
+
duration = ( duration > 1 ? duration : 1 ); // just to ensure we don't divide by 0
|
134
|
+
|
135
|
+
$.hisrc.connectionKbps = ( ( settings.speedTestKB * 1024 * 8 ) / duration ) / 1024;
|
136
|
+
$.hisrc.bandwidth = ( $.hisrc.connectionKbps >= settings.minKbpsForHighBandwidth ? 'high' : 'low' );
|
137
|
+
|
138
|
+
speedTestComplete( 'networkSuccess' );
|
139
|
+
};
|
140
|
+
|
141
|
+
speedTestImg.onerror = function () {
|
142
|
+
// fallback incase there was an error downloading the speed test image
|
143
|
+
speedTestComplete( 'networkError', 5 );
|
144
|
+
};
|
145
|
+
|
146
|
+
speedTestImg.onabort = function () {
|
147
|
+
// fallback incase there was an abort during the speed test image
|
148
|
+
speedTestComplete( 'networkAbort', 5 );
|
149
|
+
};
|
150
|
+
|
151
|
+
// begin the network connection speed test image download
|
152
|
+
startTime = ( new Date() ).getTime();
|
153
|
+
speedConnectionStatus = STATUS_LOADING;
|
154
|
+
if ( document.location.protocol === 'https:' ) {
|
155
|
+
// if this current document is SSL, make sure this speed test request
|
156
|
+
// uses https so there are no ugly security warnings from the browser
|
157
|
+
speedTestUri = speedTestUri.replace( 'http:', 'https:' );
|
158
|
+
}
|
159
|
+
speedTestImg.src = speedTestUri + "?r=" + Math.random();
|
160
|
+
|
161
|
+
// calculate the maximum number of milliseconds it 'should' take to download an XX Kbps file
|
162
|
+
// set a timeout so that if the speed test download takes too long
|
163
|
+
// than it isn't a 'high-bandwidth' and ignore what the test image .onload has to say
|
164
|
+
// this is used so we don't wait too long on a speed test response
|
165
|
+
// Adding 350ms to account for TCP slow start, quickAndDirty === TRUE
|
166
|
+
speedTestTimeoutMS = ( ( ( settings.speedTestKB * 8 ) / settings.minKbpsForHighBandwidth ) * 1000 ) + 350;
|
167
|
+
setTimeout( function () {
|
168
|
+
speedTestComplete( 'networkSlow' );
|
169
|
+
}, speedTestTimeoutMS );
|
170
|
+
},
|
171
|
+
|
172
|
+
speedTestComplete = function ( connTestResult, expireMinutes ) {
|
173
|
+
// if we haven't already gotten a speed connection status then save the info
|
174
|
+
if (speedConnectionStatus === STATUS_COMPLETE) { return; }
|
175
|
+
|
176
|
+
// first one with an answer wins
|
177
|
+
speedConnectionStatus = STATUS_COMPLETE;
|
178
|
+
$.hisrc.connectionTestResult = connTestResult;
|
179
|
+
|
180
|
+
try {
|
181
|
+
if ( !expireMinutes ) {
|
182
|
+
expireMinutes = settings.speedTestExpireMinutes;
|
183
|
+
}
|
184
|
+
var fsDataToSet = {
|
185
|
+
kbps: $.hisrc.connectionKbps,
|
186
|
+
bw: $.hisrc.bandwidth,
|
187
|
+
exp: ( new Date() ).getTime() + (expireMinutes * 60000)
|
188
|
+
};
|
189
|
+
localStorage.setItem( LOCAL_STORAGE_KEY, JSON.stringify( fsDataToSet ) );
|
190
|
+
} catch( e ) { }
|
191
|
+
|
192
|
+
// trigger swap once speedtest is complete.
|
193
|
+
$els.trigger('speedTestComplete.hisrc');
|
194
|
+
},
|
195
|
+
|
196
|
+
setImageSource = function ( $el, src ) {
|
197
|
+
if ( settings.useTransparentGif ) {
|
198
|
+
$el.attr('src', settings.transparentGifSrc)
|
199
|
+
.css('max-height', '100%')
|
200
|
+
.css('max-width', '100%')
|
201
|
+
.css('background', 'url("' + src + '") no-repeat 0 0')
|
202
|
+
.css('background-size', 'contain');
|
203
|
+
} else {
|
204
|
+
$el.attr( 'src', src );
|
205
|
+
}
|
206
|
+
};
|
207
|
+
|
208
|
+
$els.each(function(){
|
209
|
+
var $el = $(this);
|
210
|
+
|
211
|
+
if (!$el.data('m1src')) {
|
212
|
+
$el.data('m1src', $el.attr('src'));
|
213
|
+
}
|
214
|
+
|
215
|
+
// check for zero which often happens in safari.
|
216
|
+
if (!$el.attr('width') && $el.width() > 0) {
|
217
|
+
$el.attr('width', $el.width());
|
218
|
+
}
|
219
|
+
if (!$el.attr('height') && $el.height() > 0) {
|
220
|
+
$el.attr('height', $el.height());
|
221
|
+
}
|
222
|
+
|
223
|
+
$el.on('speedTestComplete.hisrc', function(){
|
224
|
+
|
225
|
+
if (speedConnectionStatus === STATUS_COMPLETE) {
|
226
|
+
|
227
|
+
if (isSlowConnection) {
|
228
|
+
$el.attr( 'src', $el.data('m1src') );
|
229
|
+
} else {
|
230
|
+
|
231
|
+
// check if client can get high res image
|
232
|
+
if ($.hisrc.devicePixelRatio > 1 && $.hisrc.bandwidth === 'high') {
|
233
|
+
setImageSource( $el, $el.data('2x') );
|
234
|
+
} else {
|
235
|
+
setImageSource( $el, $el.data('1x') );
|
236
|
+
}
|
237
|
+
}
|
238
|
+
// turn off so hisrc() can be called many times on same element.
|
239
|
+
$el.off('speedTestComplete.hisrc');
|
240
|
+
}
|
241
|
+
|
242
|
+
});
|
243
|
+
});
|
244
|
+
|
245
|
+
initSpeedTest();
|
246
|
+
|
247
|
+
return $els;
|
248
|
+
};
|
249
|
+
|
250
|
+
})(jQuery);
|
251
|
+
|
252
|
+
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hisrc-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mario Uher
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.1'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.1'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: jquery-rails
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '2.0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '2.0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: minitest
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rails
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '3.1'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '3.1'
|
78
|
+
description: Make owners of the MacBook Pro with Retina Display happy and provide
|
79
|
+
high-res images within your Rails app.
|
80
|
+
email: uher.mario@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- CHANGELOG.md
|
87
|
+
- Gemfile
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- hisrc-rails.gemspec
|
91
|
+
- lib/hisrc-rails.rb
|
92
|
+
- lib/hisrc-rails/railtie.rb
|
93
|
+
- lib/hisrc-rails/responsive_image_tag_helper.rb
|
94
|
+
- spec/integration_spec.rb
|
95
|
+
- spec/responsive_image_tag_helper_spec.rb
|
96
|
+
- spec/spec_helper.rb
|
97
|
+
- vendor/assets/javascripts/hisrc.js
|
98
|
+
homepage: http://haihappen.github.com/hisrc-rails
|
99
|
+
licenses: []
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ! '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 1.8.23
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: Use hisrc-rails with Rails 3.1+.
|
122
|
+
test_files: []
|
123
|
+
has_rdoc:
|