forked from stutrek/imageSelector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageSelector.js
executable file
·225 lines (184 loc) · 5.68 KB
/
imageSelector.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
/*
Copyright 2013 Stu Kabakoff
https://github.com/sakabako/imageSelector
MIT Licensed
*/
(function(factory) {
//AMD
if(typeof define === 'function' && define.amd) {
define(['jquery'], factory);
//NODE
} else if(typeof module === 'object' && module.exports) {
module.exports = factory(require('jquery'));
//GLOBAL
} else {
window.imageSelector = factory(jQuery);
}
})(function($) {
"use strict";
var exports = {};
var isRetina = ( window.devicePixelRatio && window.devicePixelRatio > 1.5 );
var responsiveCallbacks = [];
function addResponsiveCallback( callback ) {
if (responsiveCallbacks.length === 0) {
$(window).resize(function() {
for (var i=0; i < responsiveCallbacks.length; i++) {
responsiveCallbacks[i]();
}
})
}
responsiveCallbacks.push(callback);
}
function getStyle( el, styleProp ) {
if (el.currentStyle) {
var y = el.currentStyle[styleProp];
} else if (window.getComputedStyle) {
var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
}
return y;
}
function getWidth( element ) {
var width = element.clientWidth;
if (width === 0) {
var widthStr = getStyle( element, 'width' );
if (widthStr === 'auto') {
width = getWidth( element.parentNode );
} else if (widthStr.charAt(widthStr.length-1) === '%') {
var percent = parseInt( widthStr, 10 );
var parentWidth = getWidth( element.parentNode );
width = parentWidth * (percent / 100);
} else {
width = parseInt( widthStr, 10 );
}
}
return width;
}
function addAt2x( url ) {
var indexOfDot = url.lastIndexOf('.');
return url.substr(0, indexOfDot)+'@2x'+url.substr(indexOfDot);
}
function rateImage( imageWidth, desiredWidth ) {
var ratio = imageWidth / desiredWidth;
if (ratio < 1) {
ratio = ratio / 2;
}
return Math.abs( 1 - ratio );
}
var aspectRatioCache = {};
function createFilterOnAspectRatio( aspectRatio ) {
if (!aspectRatioCache[aspectRatio]) {
aspectRatioCache[aspectRatio] = function( cut ) {
return cut.aspectRatio === aspectRatio;
}
}
return aspectRatioCache[aspectRatio];
}
var heightWidthCache = {};
function createFilterOnWidthAndHeight( width, height ) {
var desiredAspectRatio = width / height;
var ratioString = desiredAspectRatio.toString();
if (!heightWidthCache[ratioString]) {
heightWidthCache[ratioString] = function( cut ) {
// if the cut is exactly the right ratio
if (cut.width / cut.height === desiredAspectRatio) {
return true;
}
// if adding one to the width is larger and subtracting one is smaller than the desired ratio
if ( ((cut.width+1) / cut.height) >= desiredAspectRatio && ((cut.width-1) / cut.height) <= desiredAspectRatio) {
return true;
}
return false;
}
}
return heightWidthCache[ratioString];
}
exports.selectCutWithAspectRatio = function( cuts, desiredWidth, aspectRatio, worstAccepableScore ) {
cuts = cuts.filter( createFilterOnAspectRatio( aspectRatio ) );
return exports.selectCut( cuts, desiredWidth, worstAccepableScore);
};
exports.selectCutWithWidthAndHeight = function( cuts, desiredWidth, desiredHeight, worstAccepableScore ) {
cuts = cuts.filter( createFilterOnWidthAndHeight( desiredWidth, desiredHeight) );
return exports.selectCut( cuts, desiredWidth, worstAccepableScore);
};
exports.selectCut = function( cuts, desiredWidth, worstAccepableScore ) {
if (worstAccepableScore === undefined) {
worstAccepableScore = 0.75;
}
var cutToUse, bestScore = Infinity;
for (var i in cuts) {
var cut = cuts[i];
if (desiredWidth === cut.width) {
return cut;
}
var score = rateImage( cut.width, desiredWidth );
if (score < worstAccepableScore) {
if (score < bestScore) {
cutToUse = cut;
bestScore = score;
}
}
}
return cutToUse;
};
exports.addSource = function( element, srcAttribute, cuts ) {
srcAttribute = srcAttribute || 'src';
cuts = cuts || JSON.parse(element.getAttribute('data-cuts'));
var width = getWidth( element );
var height;
var aspectRatio = element.getAttribute('data-aspect-ratio');
if (element.attributes.height && element.attributes.height.specified) {
height = element.height;
} else {
height = getStyle( element, 'height' );
}
height = parseInt(height, 10);
srcAttribute = element.getAttribute('data-src-attribute') || srcAttribute;
// make sure it's not a missing image icon
if (height < 30) {
height = false;
}
var cut;
if (aspectRatio) {
cut = exports.selectCutWithAspectRatio( cuts, width, aspectRatio );
} else if (height) {
cut = exports.selectCutWithWidthAndHeight( cuts, width, height );
} else {
cut = exports.selectCut( cuts, width );
}
if (cut) {
var src = cut.src;
if (isRetina && cut.at2x && cut.width < width*1.5) {
if (typeof cut.at2x === 'string') {
src = cut.at2x;
} else {
src = addAt2x(src);
}
}
element.setAttribute( srcAttribute, src );
} else {
$(element).addClass('no-cut-found');
element.setAttribute( srcAttribute, '' );
}
};
exports.selectImages = function( container, srcAttribute ) {
$('img[data-cuts]', container).each(function( key, element ) {
try {
var cuts = JSON.parse(element.getAttribute('data-cuts'))
element.removeAttribute('data-cuts');
exports.addSource( element, srcAttribute, cuts );
if (element.getAttribute('data-responsive') === 'true') {
var currentWidth = element.offsetWidth;
addResponsiveCallback(function() {
if (element.offsetWidth !== currentWidth) {
currentWidth = element.offsetWidth;
exports.addSource( element, srcAttribute, cuts );
}
});
}
} catch (e) {
console.error(e);
}
});
};
return exports;
});