-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrespify.js
154 lines (124 loc) · 4.18 KB
/
respify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Respify responsive image library
*
* Parse a responsive image from a set of data attributes trough media queries, depends upon the matchMedia polyfill for older browsers
* @version 0.3.2
* @author Matthisk Heimensen <[email protected]>
*/
(function( w, factory ) {
if( typeof w.define === 'function' && w.define.amd ) {
define( [ 'jquery' ], factory );
} else {
factory( w.jQuery );
}
})( window, function( $ ) {
$.respify = {};
$.respify.DEFAULTS = {
/**
* Set the matched image as background on the parent element
* @type {Boolean}
*/
background : false,
/**
* Dryrun only returns matched pictures but does not actually set them
* @type {Boolean}
*/
dryRun : false,
/**
* Callback function to call once a new image is set
* @type {Function}
*/
callback : undefined,
/**
* If in browser which does not support media queries the following will return false
* Where this should always return true if the browser supports media queries.
* @type {Bool}
*/
mediaQueriesEnabled : matchMedia('(min-width: 1px)').matches
};
var Picture = function( $el, settings ) {
this.$el = $el;
this.types = [];
this.currentMatch = { src : undefined, media : undefined };
this.settings = settings;
};
Picture.prototype.match = function( dry, callback ) {
var match = undefined,
i = this.types.length - 1;
while( ! match && i >= 0 ) {
var possibility = this.types[ i ];
if( ! possibility.media || window.matchMedia( possibility.media || '' ).matches ) {
match = possibility;
}
i--;
}
var newMatch = match.src !== this.currentMatch.src;
if( newMatch ) {
this.currentMatch = match;
}
if( ! dry && newMatch ) {
this.setMatch();
}
if( callback && newMatch ) {
callback( match );
}
return match;
};
Picture.prototype.setImage = function( src, alt ) {
if( ! this.$img ) {
this.$img = $('<img>').appendTo( this.$el );
}
this.$img.attr({
src : src,
alt : alt
});
}
Picture.prototype.setLast = function( ) {
this.currentMatch = this.types[ this.types.length - 1 ];
this.setMatch();
};
Picture.prototype.setMatch = function() {
if( ! this.settings.background ) {
this.setImage( encodeURI( this.currentMatch.src ), this.$el.data( 'alt' ) );
} else {
this.$el.css( 'background-image', 'url(' + encodeURI( this.currentMatch.src ) + ')' );
}
};
$.fn.respify = function( options ) {
var settings = $.extend({}, $.respify.DEFAULTS, options),
$els = $( this ),
dryRunMatches = [];
$els.each(function() {
var $el = $( this ),
picture = new Picture( $el, settings );
$el.find( 'span' ).each(function() {
var $img = $( this );
if( $img.data( 'src' ) ) {
picture.types.push({
src : $img.data( 'src' ),
media : $img.data( 'media' )
});
$img.remove();
}
});
// When matchMedia api is supported set correct image
if( settings.mediaQueriesEnabled ) {
var match = picture.match( settings.dryRun, settings.callback );
// Recalculate image to set on resize
$( window ).resize(function() {
picture.match( settings.dryRun, settings.callback );
});
if( match ) {
dryRunMatches.push({
node : $el,
match : match
});
}
// Else use the last picture from the set
} else {
picture.setLast();
}
});
return dryRunMatches;
}
});