Debian Codegraph @

inst/rmd/slidy/Slidy2/scripts/img.srcset.js

raw symbols SCIP
1 /* imgsrcset - Img srcset polyfill for resolution responsive images. Authors & copyright (c) 2012: WebLinc, David Knight. */
2
3 // Imgsrcset
4 (function(win) {
5 'use strict';
6
7 var _viewport = win.document.documentElement,
8 _srcsetID = 0,
9 _srcsets = [],
10 _eventPrefix = '',
11 _addEvent = win.addEventListener || (_eventPrefix = 'on') && win.attachEvent,
12 _removeEvent = win.removeEventListener || win.detachEvent,
13 _srcExpr = /[^\s]+/g,
14 _digitExpr = /[0-9\.]+/g,
15 _timer = 0,
16
17 /*
18 _matches
19 */
20 _matches = function(srcset) {
21 var srcList = (srcset.indexOf(',') !== -1 && srcset.split(',')) || [srcset],
22 srcIndex = srcList.length - 1,
23 srcLength = srcIndex,
24
25 list = null,
26 listIndex = 0,
27
28 src = '',
29 media = '';
30
31 if (srcIndex < 0) {
32 return;
33 }
34
35 do {
36 var list = srcList[srcLength - srcIndex].match(_srcExpr) || [],
37 listIndex = list.length;
38
39 while (listIndex--) {
40 var item = list[listIndex],
41 feature = 0,
42 digits = 0;
43
44 if (listIndex > 0) {
45 feature = (item.indexOf('w') !== -1 && (win.innerWidth || _viewport.clientWidth)) ||
46 (item.indexOf('h') !== -1 && (win.innerHeight || _viewport.clientHeight)) ||
47 (item.indexOf('x') !== -1 && (win.devicePixelRatio || 1));
48
49 digits = Number(item.match(_digitExpr));
50
51 if (feature && digits && digits > feature) {
52 break;
53 }
54 } else {
55 src = item;
56 media = srcList[srcIndex];
57 }
58 }
59 } while (srcIndex--);
60
61 return (src && media && {src: src, media: media}) || false;
62 },
63
64 /*
65 watch
66 */
67 _watch = function(evt) {
68 clearTimeout(_timer);
69
70 _timer = setTimeout(function() {
71 var srcset = null,
72 srcsetIndex = _srcsetID - 1,
73 srcsetLength = srcsetIndex,
74 match = false;
75
76 do {
77 srcset = _srcsets[srcsetLength - srcsetIndex];
78
79 // If img element does not have a parent, remove array index to prevent caching
80 if (!srcset.element.parentNode) {
81 _srcsetID--;
82 srcset.splice(srcsetIndex, 1);
83 continue;
84 }
85
86 match = _matches(srcset.media);
87
88 if (match && (srcset.matches !== match.media)) {
89 srcset.matches = match.media;
90
91 srcset.element.src = match.src;
92 } else if (!match) {
93 srcset.matches = false;
94 srcset.src && (srcset.element.src = srcset.src);
95 }
96 } while(srcsetIndex--);
97 }, 10);
98 },
99
100 /*
101 init
102 */
103 _init = function() {
104 _removeEvent(_eventPrefix + 'load', _init);
105
106 win.Imgsrcset.parse();
107 _watch();
108
109 // Processes '_srcsets' array and determines which source to use
110 // '_watch' will clear out any images from the array that do not have parents, which should eliminate element caching
111 _addEvent(_eventPrefix + 'resize', _watch);
112 _addEvent(_eventPrefix + 'orientationchange', _watch);
113 };
114
115 /*
116 imgsrcset
117 */
118 win.Imgsrcset = {
119 /*
120 parse
121
122 Called on '_init' and can also be called if new images are added/removed
123 */
124 parse: function() {
125 _srcsets = [];
126
127 var imgs = win.document.getElementsByTagName('img') || [],
128 imgIndex = imgs.length - 1,
129 imgLength = imgIndex,
130 img = null,
131 srcset = '';
132
133 do {
134 img = imgs[imgLength - imgIndex];
135 srcset = img.getAttribute('srcset') || '';
136
137 if (!srcset) {
138 continue;
139 }
140
141 _srcsetID = _srcsets.push({
142 element : img,
143 media : srcset,
144 matches : false,
145 src : img.getAttribute('src') || ''
146 });
147 } while(imgIndex--);
148 }
149 };
150
151 // Set up listeners
152 _addEvent(_eventPrefix + 'load', _init);
153 })(window);