-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.srcset.js
231 lines (175 loc) · 5.32 KB
/
jquery.srcset.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* ========================================================================
* jquery srcset plugin srcset.js v0.0.1
* URL
* ========================================================================
* Copyright 2011-2015 Andre Zimpel, http://www.andrezimpel.com
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* ======================================================================== */
+function ($) {
'use strict';
// srcset CLASS DEFINITION
// ======================
var srcset = function (element, options) {
this.options = options
this.$element = $(element)
this.init();
// shitty, allow resizing
var my_this = this;
$(window).resize(function(){
my_this.init();
});
}
srcset.VERSION = '0.0.1'
srcset.DEFAULTS = {
ajax: true
}
srcset.prototype.init = function () {
// display densitiy
var density = this.detect_display_densitiy();
var window_width = this.get_current_window_width();
// check if the image has the srcset attribute
if (this.$element.attr("srcset")) {
this.choose_image_and_apply_it();
}
}
srcset.prototype.choose_image_and_apply_it = function () {
var that = this;
var $img = this.$element;
var srcset = this.$element.attr("srcset").trim();
var src_images = srcset.split(",");
var sets = [];
// prepare data for each set
$(src_images).each(function(){
var string = this.trim();
var set = string.split(" ");
// adjust the set to be cool
set = that.parse_set(set);
// push it into the sets
sets.push(set);
});
// filter sets to find the matching set
var matches = sets.filter(this.filter_sets);
// get last match since we have a convetion of sorting by width and resolution
var match = matches[0];
// if there is a match
if (match !== undefined) {
// swap image src
this.prepare_swap_src_with_match(match);
} else {
// use original image
var original_src = $img.attr("data-original-src");
if (original_src !== undefined) {
this.swap_src_with_match($img, original_src);
}
}
}
srcset.prototype.filter_sets = function (set) {
var density = false;
var width = false;
// set data
var set_density = parseInt(set[2]);
var set_width = parseInt(set[1]);
// check for display density
if (window.density == set_density) {
density = true;
}
// check for size
if (window.width <= set_width) {
width = true;
}
// match the set
if (density === true && width === true) {
return set;
}
if (density === true && isNaN(set_width)) {
return set;
}
if (isNaN(set_density) && width === true) {
return set;
}
}
srcset.prototype.prepare_swap_src_with_match = function (match) {
var $img = this.$element;
var match_path = match[0];
if (this.options.ajax) {
// validate image
var img_call = $.ajax(img_src)
.success(function() {
this.swap_src_with_match($img, match_path);
});
} else {
this.swap_src_with_match($img, match_path);
}
}
srcset.prototype.swap_src_with_match = function ($img, url) {
var current_url = $img.attr("src");
var original_src = $img.attr("data-original-src");
// move src to data-original-src
if (original_src === undefined) {
$img.attr("data-original-src", current_url);
}
// edit src with match path
$img.attr("src", url);
}
// ------------------------------------------------
// helper
srcset.prototype.detect_display_densitiy = function () {
var density = window.devicePixelRatio;
window.density = density;
return density;
}
srcset.prototype.get_current_window_width = function () {
var window_width = $(window).outerWidth(true);
window.width = window_width;
return window_width;
}
srcset.prototype.parse_set = function (set) {
var src = set[0];
var width = null;
var density = null;
var conditions = set.slice();
// remove path
conditions.splice(0, 1);
// apply conditions
$(conditions).each(function() {
var condition = this;
// width
if (condition.indexOf("w") > -1) {
width = condition.replace("w", "");
}
// density
if (condition.indexOf("x") > -1) {
density = condition.replace("x", "");
}
});
// apply updated conditions to set
set[1] = width;
set[2] = density;
return set;
}
// :/
srcset.prototype.update = function () {
console.log("coming soon");
}
// srcset PLUGIN DEFINITION
// =======================
function Plugin(option, _relatedTarget) {
return this.each(function () {
var $this = $(this)
var data = $this.data('srcset')
var options = $.extend({}, srcset.DEFAULTS, $this.data(), typeof option == 'object' && option)
if (!data) $this.data('srcset', (data = new srcset(this, options)))
if (typeof option == 'string') data[option](_relatedTarget)
else if (options.update) data.update(_relatedTarget)
})
}
var old = $.fn.srcset
$.fn.srcset = Plugin
$.fn.srcset.Constructor = srcset
// srcset NO CONFLICT
// =================
$.fn.srcset.noConflict = function () {
$.fn.srcset = old
return this
}
}(jQuery);