This repository has been archived by the owner on Oct 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pictureTime.js
110 lines (102 loc) · 3.44 KB
/
pictureTime.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
(function() {
var pictureTime, stringifyPictures, watchPictures;
pictureTime = function() {
var correctSrc, deviceRatio, frag, img, matches, media, pic, picImg, picText, pictures, resMatch, sAttr, sources, src, srcSets, srcs, _i, _j, _k, _len, _len1, _len2;
pictures = document.getElementsByTagName("picture");
deviceRatio = window.devicePixelRatio ? Math.round(window.devicePixelRatio) : 1;
if (deviceRatio > 2) {
deviceRatio = 2;
}
for (_i = 0, _len = pictures.length; _i < _len; _i++) {
pic = pictures[_i];
matches = [];
sources = pic.getElementsByTagName("source");
if (pic.innerHTML === "") {
if ((img = pic.getElementsByTagName("img")[0]) && (src = img.getAttribute("data-src"))) {
img.setAttribute("src", src);
}
return null;
}
if (!sources.length) {
picText = pic.innerHTML;
frag = document.createElement("div");
srcs = picText.replace(/(<)source([^>]+>)/gmi, "$1div$2").match(/<div[^>]+>/gmi);
frag.innerHTML = srcs.join("");
sources = frag.getElementsByTagName("div");
}
for (_j = 0, _len1 = sources.length; _j < _len1; _j++) {
sAttr = sources[_j];
media = sAttr.getAttribute("media");
if (!media || window.matchMedia && window.matchMedia(media).matches) {
matches.push(sAttr);
break;
}
}
picImg = pic.getElementsByTagName("img")[0];
if (matches.length !== 0) {
if (!picImg) {
picImg = document.createElement("img");
picImg.alt = pic.getAttribute("alt");
pic.appendChild(picImg);
}
srcSets = matches.pop().getAttribute("srcset");
if (srcSets && srcSets.length) {
if (deviceRatio && srcSets.indexOf(" 2x") !== -1) {
srcSets = srcSets.split(",");
for (_k = 0, _len2 = srcSets.length; _k < _len2; _k++) {
src = srcSets[_k];
src = src.replace(/^\s*/, '').replace(/\s*$/, '').split(" ");
resMatch = parseFloat(src[1], 10);
if (deviceRatio === resMatch) {
correctSrc = src[0];
break;
}
}
} else {
correctSrc = srcSets;
}
picImg.src = correctSrc;
}
} else if (picImg) {
pic.removeChild(picImg);
}
}
};
watchPictures = function(lastPictures) {
var pictures;
pictures = stringifyPictures();
if (lastPictures && lastPictures !== pictures) {
pictureTime();
}
return setTimeout(function() {
return watchPictures(pictures);
}, 500);
};
stringifyPictures = function() {
var picture, pictures, _i, _len, _ref;
pictures = "";
_ref = document.getElementsByTagName("picture");
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
picture = _ref[_i];
pictures += picture.outerHTML;
}
return pictures;
};
if (window.addEventListener) {
window.addEventListener("resize", pictureTime, false);
window.addEventListener("DOMContentLoaded", function() {
pictureTime();
return window.removeEventListener("load", pictureTime, false);
}, false);
window.addEventListener("load", pictureTime, false);
} else {
window.attachEvent("onload", pictureTime);
}
if (typeof define === 'function') {
define(function() {
return pictureTime;
});
}
watchPictures();
pictureTime();
}).call(this);