-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.jsx
117 lines (92 loc) · 2.63 KB
/
index.jsx
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
'use strict';
var isBrowser = (typeof window !== 'undefined');
var React = require('react');
var Picturefill = isBrowser ? require('picturefill') : function () {};
var Picture = React.createClass({
getDefaultProps: function (){
return {
sources: {},
resolutions: [1],
mediaQueryUnit: 'px',
altText: '',
className: '',
defaultImageSource: 'data:image/gif;base64,R0lGODlhAQABAAAAADs='
};
},
componentDidMount: function () {
Picturefill({
elements: [
this.getDOMNode()
]
});
},
getSources: function () {
var sources = [];
var sourceKeys = Object.keys(this.props.sources);
for (var i = 0; i < sourceKeys.length; i++) {
sources.push(this.getSource(i, sourceKeys[i], this.props.sources[sourceKeys[i]]));
}
return sources;
},
getDefaultImageSource: function () {
var sourceKeys = Object.keys(this.props.sources);
for (var i = 0; i < sourceKeys.length; i++) {
var sourceKey = sourceKeys[i];
var data = this.props.sources[sourceKey];
if (data === 0) {
return sourceKey;
}
}
return this.props.defaultImageSource;
},
getSource: function (index, source, data) {
var srcSet = [];
for (var i = 0; i < this.props.resolutions.length; i++) {
var resolution = this.props.resolutions[i];
var s = source.split('.');
var ext = s[s.length - 1];
source = source.replace('.' + ext, resolution.key + '.' + ext);
srcSet.push(source + ' ' + resolution.res + 'x');
}
return (
<source key={index} media={this.getMediaQuery(data)} srcSet={srcSet.join(', ')} />
);
},
buildMediaQuery: function (dimension, value) {
return '(min-' + dimension + ': ' + value + this.props.mediaQueryUnit + ')';
},
getMediaQuery: function (data) {
if (typeof data === 'number') {
return this.buildMediaQuery('width', data);
}
else if (typeof data === 'string') {
return data;
}
else if (typeof data === 'object') {
if (data.width || data.height) {
var dims = Object.keys(data);
var queries = [];
for (var i = 0; i < dims.length; i++) {
var dim = dims[i];
queries.push(this.buildMediaQuery(dim, data[dim]));
}
return queries.join(' and ');
}
console.error('Unimplemented Media Query Object!', data);
}
},
render: function () {
var sources = this.getSources();
return (
<picture className={this.props.className}>
{/*<!--[if IE 9]><video style="display: none;"><![endif]-->*/}
{sources}
{/*<!--[if IE 9]></video><![endif]-->*/}
<img className={this.props.className ? this.props.className + '-image' : ''}
srcSet={this.getDefaultImageSource()}
alt={this.props.alt} />
</picture>
)
}
});
module.exports = Picture;