match_media_js 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -1,18 +1,46 @@
|
|
1
1
|
# match_media_js
|
2
2
|
a ruby gem for use in rails 3.2
|
3
3
|
|
4
|
+
It's a hack-gem with css3 polyfills and javascript libraries to get a css3 responsive site behaving with IE8.
|
5
|
+
|
4
6
|
Gemfile
|
7
|
+
|
5
8
|
gem 'match_media_js'
|
6
9
|
|
7
|
-
application.js
|
10
|
+
application.js (this is all the libraries in the right order-- match media needs to be before enquire)
|
11
|
+
|
12
|
+
//= require match_media_js
|
13
|
+
|
14
|
+
Individual Libaraies
|
15
|
+
|
8
16
|
//= require match_media
|
17
|
+
//= require respond
|
18
|
+
//= require enquire
|
19
|
+
|
9
20
|
|
10
21
|
Individual Files
|
22
|
+
|
11
23
|
//= require match_media/matchMedia
|
12
24
|
//= require match_media/matchMedia.addListener
|
13
25
|
|
26
|
+
Background Size Polyfill (IE8)
|
27
|
+
|
28
|
+
Source is available at /assets/background_size/backgroundsize.src.htc and minified prefered as default
|
29
|
+
|
30
|
+
.selector {
|
31
|
+
background-size: cover;
|
32
|
+
/* absolute url */
|
33
|
+
-ms-behavior: url(/assets/background_size/backgroundsize.htc);
|
34
|
+
}
|
35
|
+
|
36
|
+
|
14
37
|
License: MIT (http://www.opensource.org/licenses/mit-license.php)
|
15
38
|
|
16
39
|
|
17
|
-
Original
|
40
|
+
Original repositories:
|
41
|
+
[github.com/paulirish/matchMedia.js](https://github.com/paulirish/matchMedia.js)
|
42
|
+
[github.com/scottjehl/Respond](https://github.com/scottjehl/Respond)
|
43
|
+
[github.com/WickyNilliams/enquire.js/](https://github.com/WickyNilliams/enquire.js/)
|
44
|
+
|
45
|
+
[github.com/louisremi/background-size-polyfill](https://github.com/louisremi/background-size-polyfill)
|
18
46
|
|
@@ -0,0 +1,7 @@
|
|
1
|
+
<component lightWeight="true">
|
2
|
+
<attach event="onpropertychange" onevent="handlePropertychange()" />
|
3
|
+
<attach event="ondetach" onevent="restore()" />
|
4
|
+
<attach event="onresize" for="window" onevent="handleResize()" />
|
5
|
+
<script type="text/javascript">
|
6
|
+
var rsrc=/url\(["']?(.*?)["']?\)/,positions={top:0,left:0,bottom:1,right:1,center:0.5},doc=element.document;init(); function init(){var b=doc.createElement("div"),a=doc.createElement("img"),c,d;b.style.position="absolute";b.style.zIndex=-1;b.style.top=0;b.style.right=0;b.style.left=0;b.style.bottom=0;b.style.overflow="hidden";a.style.position="absolute";a.style.width=a.style.width="auto";b.appendChild(a);element.insertBefore(b,element.firstChild);d=[element.currentStyle.backgroundPositionX,element.currentStyle.backgroundPositionY];element.bgsExpando=c={wrapper:b,img:a,backgroundSize:element.currentStyle["background-size"], backgroundPositionX:positions[d[0]]||parseFloat(d[0])/100,backgroundPositionY:positions[d[1]]||parseFloat(d[1])/100};"auto"==element.currentStyle.zIndex&&(element.style.zIndex=0);"static"==element.currentStyle.position&&(element.style.position="relative");refreshDisplay(element,c)&&(refreshDimensions(element,c),refreshBackgroundImage(element,c,function(){updateBackground(element,c)}))} function refreshDisplay(b,a){var c=b.currentStyle.display;c!=a.display&&(a.display=c,a.somethingChanged=!0);return"none"!=c}function refreshDimensions(b,a){var c=b.offsetWidth-(parseFloat(b.currentStyle.borderLeftWidth)||0)-(parseFloat(b.currentStyle.borderRightWidth)||0),d=b.offsetHeight-(parseFloat(b.currentStyle.borderTopWidth)||0)-(parseFloat(b.currentStyle.borderBottomWidth)||0);if(c!=a.innerWidth||d!=a.innerHeight)a.innerWidth=c,a.innerHeight=d,a.somethingChanged=!0} function refreshBackgroundImage(b,a,c){var d=a.img,e=(rsrc.exec(b.currentStyle.backgroundImage)||[])[1];if(e&&e!=a.backgroundSrc){a.backgroundSrc=e;a.somethingChanged=!0;d.onload=function(){var b=d.width,e=d.height;1==b&&1==e||(a.imgWidth=b,a.imgHeight=e,a.constrain=!1,c(),d.style.visibility="visible",d.onload=null)};d.style.visibility="hidden";d.src=a.backgroundSrc;if(d.readyState||d.complete)d.src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==",d.src=a.backgroundSrc;a.ignoreNextPropertyChange= !0;b.style.backgroundImage="none"}else c()} function updateBackground(b,a){if(a.somethingChanged){var c=a.img,d=a.innerWidth/a.innerHeight,e=a.imgWidth/a.imgHeight,f=a.constrain;"contain"==a.backgroundSize?e>d?(a.constrain=d="width",e=Math.floor((a.innerHeight-a.innerWidth/e)*a.backgroundPositionY),c.style.top=e+"px",d!=f&&(c.style.width="100%",c.style.height="auto",c.style.left=0)):(a.constrain=d="height",e=Math.floor((a.innerWidth-a.innerHeight*e)*a.backgroundPositionX),c.style.left=e+"px",d!=f&&(c.style.width="auto",c.style.height="100%", c.style.top=0)):"cover"==a.backgroundSize&&(e>d?(a.constrain=d="height",e=Math.floor((a.innerHeight*e-a.innerWidth)*a.backgroundPositionX),c.style.left=-e+"px",d!=f&&(c.style.width="auto",c.style.height="100%",c.style.top=0)):(a.constrain=d="width",e=Math.floor((a.innerWidth/e-a.innerHeight)*a.backgroundPositionY),c.style.top=-e+"px",d!=f&&(c.style.width="100%",c.style.height="auto",c.style.left=0)));a.somethingChanged=!1}} function handlePropertychange(){var b=element.bgsExpando;b.ignoreNextPropertyChange?b.ignoreNextPropertyChange=!1:refreshDisplay(element,b)&&(refreshDimensions(element,b),refreshBackgroundImage(element,b,function(){updateBackground(element,b)}))}function handleResize(){var b=element.bgsExpando;"none"!=b.display&&(refreshDimensions(element,b),updateBackground(element,b))} function restore(){var b=element.bgsExpando;try{element.style.backgroundImage="url('"+b.backgroundSrc+"')",element.removeChild(b.wrapper),element.bgsExpando=null}catch(a){}};
|
7
|
+
</script>
|
@@ -0,0 +1,271 @@
|
|
1
|
+
<component lightWeight="true">
|
2
|
+
<attach event="onpropertychange" onevent="handlePropertychange()" />
|
3
|
+
<attach event="ondetach" onevent="restore()" />
|
4
|
+
<attach event="onresize" for="window" onevent="handleResize()" />
|
5
|
+
<script type="text/javascript">
|
6
|
+
|
7
|
+
var rsrc = /url\(["']?(.*?)["']?\)/,
|
8
|
+
positions = {
|
9
|
+
top: 0,
|
10
|
+
left: 0,
|
11
|
+
bottom: 1,
|
12
|
+
right: 1,
|
13
|
+
center: .5
|
14
|
+
},
|
15
|
+
doc = element.document;
|
16
|
+
|
17
|
+
init();
|
18
|
+
|
19
|
+
// remove the background-image and emulate it with a wrapped <img/>
|
20
|
+
function init() {
|
21
|
+
var wrapper = doc.createElement( "div" ),
|
22
|
+
img = doc.createElement( "img" ),
|
23
|
+
expando,
|
24
|
+
pos;
|
25
|
+
|
26
|
+
wrapper.style.position = "absolute";
|
27
|
+
wrapper.style.zIndex = -1;
|
28
|
+
wrapper.style.top = 0;
|
29
|
+
wrapper.style.right = 0;
|
30
|
+
wrapper.style.left = 0;
|
31
|
+
wrapper.style.bottom = 0;
|
32
|
+
wrapper.style.overflow = "hidden";
|
33
|
+
|
34
|
+
img.alt = "";
|
35
|
+
img.style.position = "absolute";
|
36
|
+
img.style.width = img.style.width = "auto";
|
37
|
+
|
38
|
+
wrapper.appendChild( img );
|
39
|
+
|
40
|
+
element.insertBefore( wrapper, element.firstChild );
|
41
|
+
|
42
|
+
pos = [
|
43
|
+
element.currentStyle.backgroundPositionX,
|
44
|
+
element.currentStyle.backgroundPositionY
|
45
|
+
];
|
46
|
+
|
47
|
+
// save useful data for quick access
|
48
|
+
element.bgsExpando = expando = {
|
49
|
+
wrapper: wrapper,
|
50
|
+
img: img,
|
51
|
+
backgroundSize: element.currentStyle['background-size'],
|
52
|
+
// Only keywords or percentage values are supported
|
53
|
+
backgroundPositionX: positions[ pos[0] ] || parseFloat( pos[0] ) / 100,
|
54
|
+
backgroundPositionY: positions[ pos[1] ] || parseFloat( pos[1] ) / 100
|
55
|
+
};
|
56
|
+
|
57
|
+
// This is the part where we mess with the existing DOM
|
58
|
+
// to make sure that the background image is correctly zIndexed
|
59
|
+
if ( element.currentStyle.zIndex == "auto" ) {
|
60
|
+
element.style.zIndex = 0;
|
61
|
+
}
|
62
|
+
if ( element.currentStyle.position == "static" ) {
|
63
|
+
element.style.position = "relative";
|
64
|
+
}
|
65
|
+
|
66
|
+
if ( refreshDisplay( element, expando ) ) {
|
67
|
+
refreshDimensions( element, expando );
|
68
|
+
refreshBackgroundImage( element, expando, function() {
|
69
|
+
updateBackground( element, expando );
|
70
|
+
});
|
71
|
+
}
|
72
|
+
}
|
73
|
+
|
74
|
+
function refreshDisplay( element, expando ) {
|
75
|
+
var display = element.currentStyle.display;
|
76
|
+
|
77
|
+
if ( display != expando.display ) {
|
78
|
+
expando.display = display;
|
79
|
+
expando.somethingChanged = true;
|
80
|
+
}
|
81
|
+
|
82
|
+
return display != "none";
|
83
|
+
}
|
84
|
+
|
85
|
+
function refreshDimensions( element, expando ) {
|
86
|
+
var innerWidth = element.offsetWidth
|
87
|
+
- ( parseFloat( element.currentStyle.borderLeftWidth ) || 0 )
|
88
|
+
- ( parseFloat( element.currentStyle.borderRightWidth ) || 0 ),
|
89
|
+
innerHeight = element.offsetHeight
|
90
|
+
- ( parseFloat( element.currentStyle.borderTopWidth ) || 0 )
|
91
|
+
- ( parseFloat( element.currentStyle.borderBottomWidth ) || 0 );
|
92
|
+
|
93
|
+
if ( innerWidth != expando.innerWidth || innerHeight != expando.innerHeight ) {
|
94
|
+
expando.innerWidth = innerWidth;
|
95
|
+
expando.innerHeight = innerHeight;
|
96
|
+
expando.somethingChanged = true;
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
function refreshBackgroundImage( element, expando, callback ) {
|
101
|
+
var img = expando.img,
|
102
|
+
src = ( rsrc.exec( element.currentStyle.backgroundImage ) || [] )[1];
|
103
|
+
|
104
|
+
if ( src && src != expando.backgroundSrc ) {
|
105
|
+
expando.backgroundSrc = src;
|
106
|
+
expando.somethingChanged = true;
|
107
|
+
|
108
|
+
img.onload = function() {
|
109
|
+
var width = img.width,
|
110
|
+
height = img.height;
|
111
|
+
|
112
|
+
// ignore onload on the proxy image
|
113
|
+
if ( width == 1 && height == 1 ) { return; }
|
114
|
+
|
115
|
+
expando.imgWidth = width;
|
116
|
+
expando.imgHeight = height;
|
117
|
+
expando.constrain = false;
|
118
|
+
|
119
|
+
callback();
|
120
|
+
|
121
|
+
img.style.visibility = "visible";
|
122
|
+
img.onload = null;
|
123
|
+
};
|
124
|
+
|
125
|
+
img.style.visibility = "hidden";
|
126
|
+
img.src = expando.backgroundSrc;
|
127
|
+
|
128
|
+
// force onload execution
|
129
|
+
if ( img.readyState || img.complete ) {
|
130
|
+
img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
|
131
|
+
img.src = expando.backgroundSrc;
|
132
|
+
}
|
133
|
+
|
134
|
+
expando.ignoreNextPropertyChange = true;
|
135
|
+
element.style.backgroundImage = "none";
|
136
|
+
|
137
|
+
} else {
|
138
|
+
// the callback should be exacuted in all cases
|
139
|
+
callback();
|
140
|
+
}
|
141
|
+
}
|
142
|
+
|
143
|
+
function updateBackground( element, expando ) {
|
144
|
+
if ( !expando.somethingChanged ) { return; }
|
145
|
+
|
146
|
+
var img = expando.img,
|
147
|
+
elemRatio = expando.innerWidth / expando.innerHeight,
|
148
|
+
imgRatio = expando.imgWidth / expando.imgHeight,
|
149
|
+
prevConstrain = expando.constrain,
|
150
|
+
curConstrain,
|
151
|
+
delta;
|
152
|
+
|
153
|
+
if ( expando.backgroundSize == "contain" ) {
|
154
|
+
if ( imgRatio > elemRatio ) {
|
155
|
+
expando.constrain = curConstrain = "width";
|
156
|
+
|
157
|
+
delta = Math.floor(
|
158
|
+
( expando.innerHeight - expando.innerWidth / imgRatio )
|
159
|
+
* expando.backgroundPositionY
|
160
|
+
);
|
161
|
+
|
162
|
+
img.style.top = delta + "px";
|
163
|
+
|
164
|
+
// when switching from height to width constraint,
|
165
|
+
// make sure to release constraint on height and reset left
|
166
|
+
if ( curConstrain != prevConstrain ) {
|
167
|
+
img.style.width = "100%";
|
168
|
+
img.style.height = "auto";
|
169
|
+
img.style.left = 0;
|
170
|
+
}
|
171
|
+
|
172
|
+
// elemRatio > imgRatio
|
173
|
+
} else {
|
174
|
+
expando.constrain = curConstrain = "height";
|
175
|
+
|
176
|
+
delta = Math.floor(
|
177
|
+
( expando.innerWidth - expando.innerHeight * imgRatio )
|
178
|
+
* expando.backgroundPositionX
|
179
|
+
);
|
180
|
+
|
181
|
+
img.style.left = delta + "px";
|
182
|
+
|
183
|
+
if ( curConstrain != prevConstrain ) {
|
184
|
+
img.style.width = "auto";
|
185
|
+
img.style.height = "100%";
|
186
|
+
img.style.top = 0;
|
187
|
+
}
|
188
|
+
}
|
189
|
+
|
190
|
+
} else if ( expando.backgroundSize == "cover" ) {
|
191
|
+
if ( imgRatio > elemRatio ) {
|
192
|
+
expando.constrain = curConstrain = "height";
|
193
|
+
|
194
|
+
delta = Math.floor(
|
195
|
+
( expando.innerHeight * imgRatio - expando.innerWidth )
|
196
|
+
* expando.backgroundPositionX
|
197
|
+
);
|
198
|
+
|
199
|
+
img.style.left = -delta + "px";
|
200
|
+
|
201
|
+
// when switching from height to width constraint,
|
202
|
+
// make sure to release constraint on height and reset left
|
203
|
+
if ( curConstrain != prevConstrain ) {
|
204
|
+
img.style.width = "auto";
|
205
|
+
img.style.height = "100%";
|
206
|
+
img.style.top = 0;
|
207
|
+
}
|
208
|
+
|
209
|
+
// elemRatio > imgRatio
|
210
|
+
} else {
|
211
|
+
expando.constrain = curConstrain = "width";
|
212
|
+
|
213
|
+
delta = Math.floor(
|
214
|
+
( expando.innerWidth / imgRatio - expando.innerHeight )
|
215
|
+
* expando.backgroundPositionY
|
216
|
+
);
|
217
|
+
|
218
|
+
img.style.top = -delta + "px";
|
219
|
+
|
220
|
+
if ( curConstrain != prevConstrain ) {
|
221
|
+
img.style.width = "100%";
|
222
|
+
img.style.height = "auto";
|
223
|
+
img.style.left = 0;
|
224
|
+
}
|
225
|
+
}
|
226
|
+
}
|
227
|
+
|
228
|
+
expando.somethingChanged = false;
|
229
|
+
}
|
230
|
+
|
231
|
+
// handle different style changes
|
232
|
+
function handlePropertychange() {
|
233
|
+
var expando = element.bgsExpando;
|
234
|
+
|
235
|
+
// this prevents handling propertychange events caused by this script
|
236
|
+
// TODO: make it reliable
|
237
|
+
if ( expando.ignoreNextPropertyChange ) {
|
238
|
+
expando.ignoreNextPropertyChange = false;
|
239
|
+
return;
|
240
|
+
}
|
241
|
+
|
242
|
+
if ( refreshDisplay( element, expando ) ) {
|
243
|
+
refreshDimensions( element, expando );
|
244
|
+
refreshBackgroundImage( element, expando, function() {
|
245
|
+
updateBackground( element, expando );
|
246
|
+
});
|
247
|
+
}
|
248
|
+
}
|
249
|
+
|
250
|
+
function handleResize() {
|
251
|
+
// TODO: throttle resize events
|
252
|
+
|
253
|
+
var expando = element.bgsExpando;
|
254
|
+
|
255
|
+
if ( expando.display != "none" ) {
|
256
|
+
refreshDimensions( element, expando );
|
257
|
+
updateBackground( element, expando );
|
258
|
+
}
|
259
|
+
}
|
260
|
+
|
261
|
+
function restore() {
|
262
|
+
var expando = element.bgsExpando;
|
263
|
+
|
264
|
+
try {
|
265
|
+
element.style.backgroundImage = "url('" + expando.backgroundSrc + "')";
|
266
|
+
element.removeChild( expando.wrapper );
|
267
|
+
element.bgsExpando = null;
|
268
|
+
} catch(e) {}
|
269
|
+
}
|
270
|
+
|
271
|
+
</script>
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- rheaton
|
@@ -42,6 +42,8 @@ extra_rdoc_files: []
|
|
42
42
|
files:
|
43
43
|
- lib/match_media_js/version.rb
|
44
44
|
- lib/match_media_js.rb
|
45
|
+
- vendor/assets/javascripts/background_size/backgroundsize.htc
|
46
|
+
- vendor/assets/javascripts/background_size/backgroundsize.src.htc
|
45
47
|
- vendor/assets/javascripts/enquire/enquire.js
|
46
48
|
- vendor/assets/javascripts/enquire.js
|
47
49
|
- vendor/assets/javascripts/match_media/matchMedia.addListener.js
|