This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
resize.js
137 lines (120 loc) · 3.05 KB
/
resize.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
/*
Resize plugin.
Grabs appropriately sized image from the server, with noscript fallback and error handling
Author: Gavyn McKenzie @ Etch Apps
Requires: jQuery, smartresize
- Find the images in the dom
- Get their size and src
- Post them to the server
- Populate the image tags on reply
*/
(function() {
var crispy;
crispy = {
els: ".js-crispy",
images: [],
breakpoints: [
"32em",
"48em",
"62em",
"76em"
],
init: function() {
this.currentBreakpoint = this.getCurrentBreakpoint();
this.gather();
this.bind();
},
bind: function() {
var _this = this;
$(window).smartresize(function() {
_this.refreshImages();
});
},
windowWidth: function() {
return $(window).width() / Number($("body").css("font-size"));
},
getCurrentBreakpoint: function() {
var bp, breakpoint, _fn, _i, _len, _ref,
_this = this;
bp = this.breakpoints[0];
_ref = this.breakpoints;
_fn = function(breakpoint) {
if (window.matchMedia && window.matchMedia("all and (min-width: " + breakpoint + ")").matches) {
return bp = breakpoint;
}
};
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
breakpoint = _ref[_i];
_fn(breakpoint);
}
return bp;
},
testBreakpointChange: function() {
var bp;
bp = this.getCurrentBreakpoint();
if (this.currentBreakpoint === bp) {
return false;
} else {
this.currentBreakpoint = bp;
return true;
}
},
refreshImages: function() {
if (this.testBreakpointChange()) {
this.gather();
}
},
gather: function() {
var el, els, _i, _len;
els = $(this.els);
this.images = [];
for (_i = 0, _len = els.length; _i < _len; _i++) {
el = els[_i];
this.add(el);
}
this.grabFromServer();
},
add: function(image) {
image = $(image);
this.images.push({
src: image.attr("data-src"),
width: image.width()
});
},
buildQuery: function() {
var image = { image: this.images }
return $.param(image);
},
grabFromServer: function() {
var data,
_this = this;
data = this.buildQuery();
$.get("resize.php", data, function(data) {
var image, _i, _len;
for (_i = 0, _len = data.length; _i < _len; _i++) {
image = data[_i];
_this.loadImage(image);
}
}
);
},
loadImage: function(image) {
var el, img,
_this = this;
el = $("[data-src='" + image.og_src + "']");
img = $("<img />");
img.attr("src", image.src).attr("alt", el.attr("data-alt"));
if (el.children("img").length) {
el.children("img").attr("src", image.src);
} else {
img.load(function() {
el.append(img);
el.addClass('img-loaded');
});
}
}
};
$(document).ready(function() {
crispy.init();
});
}).call(this);