ElementQueries.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /**
  2. * Copyright Marc J. Schmidt. See the LICENSE file at the top-level
  3. * directory of this distribution and at
  4. * https://github.com/marcj/css-element-queries/blob/master/LICENSE.
  5. */
  6. ;
  7. (function (root, factory) {
  8. if (typeof define === "function" && define.amd) {
  9. define(['./ResizeSensor.js'], factory);
  10. } else if (typeof exports === "object") {
  11. module.exports = factory(require('./ResizeSensor.js'));
  12. } else {
  13. root.ElementQueries = factory(root.ResizeSensor);
  14. }
  15. }(this, function (ResizeSensor) {
  16. /**
  17. *
  18. * @type {Function}
  19. * @constructor
  20. */
  21. var ElementQueries = function() {
  22. var trackingActive = false;
  23. var elements = [];
  24. /**
  25. *
  26. * @param element
  27. * @returns {Number}
  28. */
  29. function getEmSize(element) {
  30. if (!element) {
  31. element = document.documentElement;
  32. }
  33. var fontSize = window.getComputedStyle(element, null).fontSize;
  34. return parseFloat(fontSize) || 16;
  35. }
  36. /**
  37. *
  38. * @copyright https://github.com/Mr0grog/element-query/blob/master/LICENSE
  39. *
  40. * @param {HTMLElement} element
  41. * @param {*} value
  42. * @returns {*}
  43. */
  44. function convertToPx(element, value) {
  45. var numbers = value.split(/\d/);
  46. var units = numbers[numbers.length-1];
  47. value = parseFloat(value);
  48. switch (units) {
  49. case "px":
  50. return value;
  51. case "em":
  52. return value * getEmSize(element);
  53. case "rem":
  54. return value * getEmSize();
  55. // Viewport units!
  56. // According to http://quirksmode.org/mobile/tableViewport.html
  57. // documentElement.clientWidth/Height gets us the most reliable info
  58. case "vw":
  59. return value * document.documentElement.clientWidth / 100;
  60. case "vh":
  61. return value * document.documentElement.clientHeight / 100;
  62. case "vmin":
  63. case "vmax":
  64. var vw = document.documentElement.clientWidth / 100;
  65. var vh = document.documentElement.clientHeight / 100;
  66. var chooser = Math[units === "vmin" ? "min" : "max"];
  67. return value * chooser(vw, vh);
  68. default:
  69. return value;
  70. // for now, not supporting physical units (since they are just a set number of px)
  71. // or ex/ch (getting accurate measurements is hard)
  72. }
  73. }
  74. /**
  75. *
  76. * @param {HTMLElement} element
  77. * @constructor
  78. */
  79. function SetupInformation(element) {
  80. this.element = element;
  81. this.options = {};
  82. var key, option, width = 0, height = 0, value, actualValue, attrValues, attrValue, attrName;
  83. /**
  84. * @param {Object} option {mode: 'min|max', property: 'width|height', value: '123px'}
  85. */
  86. this.addOption = function(option) {
  87. var idx = [option.mode, option.property, option.value].join(',');
  88. this.options[idx] = option;
  89. };
  90. var attributes = ['min-width', 'min-height', 'max-width', 'max-height'];
  91. /**
  92. * Extracts the computed width/height and sets to min/max- attribute.
  93. */
  94. this.call = function() {
  95. // extract current dimensions
  96. width = this.element.offsetWidth;
  97. height = this.element.offsetHeight;
  98. attrValues = {};
  99. for (key in this.options) {
  100. if (!this.options.hasOwnProperty(key)){
  101. continue;
  102. }
  103. option = this.options[key];
  104. value = convertToPx(this.element, option.value);
  105. actualValue = option.property == 'width' ? width : height;
  106. attrName = option.mode + '-' + option.property;
  107. attrValue = '';
  108. if (option.mode == 'min' && actualValue >= value) {
  109. attrValue += option.value;
  110. }
  111. if (option.mode == 'max' && actualValue <= value) {
  112. attrValue += option.value;
  113. }
  114. if (!attrValues[attrName]) attrValues[attrName] = '';
  115. if (attrValue && -1 === (' '+attrValues[attrName]+' ').indexOf(' ' + attrValue + ' ')) {
  116. attrValues[attrName] += ' ' + attrValue;
  117. }
  118. }
  119. for (var k in attributes) {
  120. if(!attributes.hasOwnProperty(k)) continue;
  121. if (attrValues[attributes[k]]) {
  122. this.element.setAttribute(attributes[k], attrValues[attributes[k]].substr(1));
  123. } else {
  124. this.element.removeAttribute(attributes[k]);
  125. }
  126. }
  127. };
  128. }
  129. /**
  130. * @param {HTMLElement} element
  131. * @param {Object} options
  132. */
  133. function setupElement(element, options) {
  134. if (element.elementQueriesSetupInformation) {
  135. element.elementQueriesSetupInformation.addOption(options);
  136. } else {
  137. element.elementQueriesSetupInformation = new SetupInformation(element);
  138. element.elementQueriesSetupInformation.addOption(options);
  139. element.elementQueriesSensor = new ResizeSensor(element, function() {
  140. element.elementQueriesSetupInformation.call();
  141. });
  142. }
  143. element.elementQueriesSetupInformation.call();
  144. if (trackingActive && elements.indexOf(element) < 0) {
  145. elements.push(element);
  146. }
  147. }
  148. /**
  149. * @param {String} selector
  150. * @param {String} mode min|max
  151. * @param {String} property width|height
  152. * @param {String} value
  153. */
  154. var allQueries = {};
  155. function queueQuery(selector, mode, property, value) {
  156. if (typeof(allQueries[mode]) == 'undefined') allQueries[mode] = {};
  157. if (typeof(allQueries[mode][property]) == 'undefined') allQueries[mode][property] = {};
  158. if (typeof(allQueries[mode][property][value]) == 'undefined') allQueries[mode][property][value] = selector;
  159. else allQueries[mode][property][value] += ','+selector;
  160. }
  161. function getQuery() {
  162. var query;
  163. if (document.querySelectorAll) query = document.querySelectorAll.bind(document);
  164. if (!query && 'undefined' !== typeof $$) query = $$;
  165. if (!query && 'undefined' !== typeof jQuery) query = jQuery;
  166. if (!query) {
  167. throw 'No document.querySelectorAll, jQuery or Mootools\'s $$ found.';
  168. }
  169. return query;
  170. }
  171. /**
  172. * Start the magic. Go through all collected rules (readRules()) and attach the resize-listener.
  173. */
  174. function findElementQueriesElements() {
  175. var query = getQuery();
  176. for (var mode in allQueries) if (allQueries.hasOwnProperty(mode)) {
  177. for (var property in allQueries[mode]) if (allQueries[mode].hasOwnProperty(property)) {
  178. for (var value in allQueries[mode][property]) if (allQueries[mode][property].hasOwnProperty(value)) {
  179. var elements = query(allQueries[mode][property][value]);
  180. for (var i = 0, j = elements.length; i < j; i++) {
  181. setupElement(elements[i], {
  182. mode: mode,
  183. property: property,
  184. value: value
  185. });
  186. }
  187. }
  188. }
  189. }
  190. }
  191. /**
  192. *
  193. * @param {HTMLElement} element
  194. */
  195. function attachResponsiveImage(element) {
  196. var children = [];
  197. var rules = [];
  198. var sources = [];
  199. var defaultImageId = 0;
  200. var lastActiveImage = -1;
  201. var loadedImages = [];
  202. for (var i in element.children) {
  203. if(!element.children.hasOwnProperty(i)) continue;
  204. if (element.children[i].tagName && element.children[i].tagName.toLowerCase() === 'img') {
  205. children.push(element.children[i]);
  206. var minWidth = element.children[i].getAttribute('min-width') || element.children[i].getAttribute('data-min-width');
  207. //var minHeight = element.children[i].getAttribute('min-height') || element.children[i].getAttribute('data-min-height');
  208. var src = element.children[i].getAttribute('data-src') || element.children[i].getAttribute('url');
  209. sources.push(src);
  210. var rule = {
  211. minWidth: minWidth
  212. };
  213. rules.push(rule);
  214. if (!minWidth) {
  215. defaultImageId = children.length - 1;
  216. element.children[i].style.display = 'block';
  217. } else {
  218. element.children[i].style.display = 'none';
  219. }
  220. }
  221. }
  222. lastActiveImage = defaultImageId;
  223. function check() {
  224. var imageToDisplay = false, i;
  225. for (i in children){
  226. if(!children.hasOwnProperty(i)) continue;
  227. if (rules[i].minWidth) {
  228. if (element.offsetWidth > rules[i].minWidth) {
  229. imageToDisplay = i;
  230. }
  231. }
  232. }
  233. if (!imageToDisplay) {
  234. //no rule matched, show default
  235. imageToDisplay = defaultImageId;
  236. }
  237. if (lastActiveImage != imageToDisplay) {
  238. //image change
  239. if (!loadedImages[imageToDisplay]){
  240. //image has not been loaded yet, we need to load the image first in memory to prevent flash of
  241. //no content
  242. var image = new Image();
  243. image.onload = function() {
  244. children[imageToDisplay].src = sources[imageToDisplay];
  245. children[lastActiveImage].style.display = 'none';
  246. children[imageToDisplay].style.display = 'block';
  247. loadedImages[imageToDisplay] = true;
  248. lastActiveImage = imageToDisplay;
  249. };
  250. image.src = sources[imageToDisplay];
  251. } else {
  252. children[lastActiveImage].style.display = 'none';
  253. children[imageToDisplay].style.display = 'block';
  254. lastActiveImage = imageToDisplay;
  255. }
  256. } else {
  257. //make sure for initial check call the .src is set correctly
  258. children[imageToDisplay].src = sources[imageToDisplay];
  259. }
  260. }
  261. element.resizeSensor = new ResizeSensor(element, check);
  262. check();
  263. if (trackingActive) {
  264. elements.push(element);
  265. }
  266. }
  267. function findResponsiveImages(){
  268. var query = getQuery();
  269. var elements = query('[data-responsive-image],[responsive-image]');
  270. for (var i = 0, j = elements.length; i < j; i++) {
  271. attachResponsiveImage(elements[i]);
  272. }
  273. }
  274. var regex = /,?[\s\t]*([^,\n]*?)((?:\[[\s\t]*?(?:min|max)-(?:width|height)[\s\t]*?[~$\^]?=[\s\t]*?"[^"]*?"[\s\t]*?])+)([^,\n\s\{]*)/mgi;
  275. var attrRegex = /\[[\s\t]*?(min|max)-(width|height)[\s\t]*?[~$\^]?=[\s\t]*?"([^"]*?)"[\s\t]*?]/mgi;
  276. /**
  277. * @param {String} css
  278. */
  279. function extractQuery(css) {
  280. var match;
  281. var smatch;
  282. css = css.replace(/'/g, '"');
  283. while (null !== (match = regex.exec(css))) {
  284. smatch = match[1] + match[3];
  285. attrs = match[2];
  286. while (null !== (attrMatch = attrRegex.exec(attrs))) {
  287. queueQuery(smatch, attrMatch[1], attrMatch[2], attrMatch[3]);
  288. }
  289. }
  290. }
  291. /**
  292. * @param {CssRule[]|String} rules
  293. */
  294. function readRules(rules) {
  295. var selector = '';
  296. if (!rules) {
  297. return;
  298. }
  299. if ('string' === typeof rules) {
  300. rules = rules.toLowerCase();
  301. if (-1 !== rules.indexOf('min-width') || -1 !== rules.indexOf('max-width')) {
  302. extractQuery(rules);
  303. }
  304. } else {
  305. for (var i = 0, j = rules.length; i < j; i++) {
  306. if (1 === rules[i].type) {
  307. selector = rules[i].selectorText || rules[i].cssText;
  308. if (-1 !== selector.indexOf('min-height') || -1 !== selector.indexOf('max-height')) {
  309. extractQuery(selector);
  310. }else if(-1 !== selector.indexOf('min-width') || -1 !== selector.indexOf('max-width')) {
  311. extractQuery(selector);
  312. }
  313. } else if (4 === rules[i].type) {
  314. readRules(rules[i].cssRules || rules[i].rules);
  315. }
  316. }
  317. }
  318. }
  319. var defaultCssInjected = false;
  320. /**
  321. * Searches all css rules and setups the event listener to all elements with element query rules..
  322. *
  323. * @param {Boolean} withTracking allows and requires you to use detach, since we store internally all used elements
  324. * (no garbage collection possible if you don not call .detach() first)
  325. */
  326. this.init = function(withTracking) {
  327. trackingActive = typeof withTracking === 'undefined' ? false : withTracking;
  328. for (var i = 0, j = document.styleSheets.length; i < j; i++) {
  329. try {
  330. readRules(document.styleSheets[i].cssRules || document.styleSheets[i].rules || document.styleSheets[i].cssText);
  331. } catch(e) {
  332. if (e.name !== 'SecurityError') {
  333. throw e;
  334. }
  335. }
  336. }
  337. if (!defaultCssInjected) {
  338. var style = document.createElement('style');
  339. style.type = 'text/css';
  340. style.innerHTML = '[responsive-image] > img, [data-responsive-image] {overflow: hidden; padding: 0; } [responsive-image] > img, [data-responsive-image] > img { width: 100%;}';
  341. document.getElementsByTagName('head')[0].appendChild(style);
  342. defaultCssInjected = true;
  343. }
  344. findElementQueriesElements();
  345. findResponsiveImages();
  346. };
  347. /**
  348. *
  349. * @param {Boolean} withTracking allows and requires you to use detach, since we store internally all used elements
  350. * (no garbage collection possible if you don not call .detach() first)
  351. */
  352. this.update = function(withTracking) {
  353. this.init(withTracking);
  354. };
  355. this.detach = function() {
  356. if (!this.withTracking) {
  357. throw 'withTracking is not enabled. We can not detach elements since we don not store it.' +
  358. 'Use ElementQueries.withTracking = true; before domready or call ElementQueryes.update(true).';
  359. }
  360. var element;
  361. while (element = elements.pop()) {
  362. ElementQueries.detach(element);
  363. }
  364. elements = [];
  365. };
  366. };
  367. /**
  368. *
  369. * @param {Boolean} withTracking allows and requires you to use detach, since we store internally all used elements
  370. * (no garbage collection possible if you don not call .detach() first)
  371. */
  372. ElementQueries.update = function(withTracking) {
  373. ElementQueries.instance.update(withTracking);
  374. };
  375. /**
  376. * Removes all sensor and elementquery information from the element.
  377. *
  378. * @param {HTMLElement} element
  379. */
  380. ElementQueries.detach = function(element) {
  381. if (element.elementQueriesSetupInformation) {
  382. //element queries
  383. element.elementQueriesSensor.detach();
  384. delete element.elementQueriesSetupInformation;
  385. delete element.elementQueriesSensor;
  386. } else if (element.resizeSensor) {
  387. //responsive image
  388. element.resizeSensor.detach();
  389. delete element.resizeSensor;
  390. } else {
  391. //console.log('detached already', element);
  392. }
  393. };
  394. ElementQueries.withTracking = false;
  395. ElementQueries.init = function() {
  396. if (!ElementQueries.instance) {
  397. ElementQueries.instance = new ElementQueries();
  398. }
  399. ElementQueries.instance.init(ElementQueries.withTracking);
  400. };
  401. var domLoaded = function (callback) {
  402. /* Internet Explorer */
  403. /*@cc_on
  404. @if (@_win32 || @_win64)
  405. document.write('<script id="ieScriptLoad" defer src="//:"><\/script>');
  406. document.getElementById('ieScriptLoad').onreadystatechange = function() {
  407. if (this.readyState == 'complete') {
  408. callback();
  409. }
  410. };
  411. @end @*/
  412. /* Mozilla, Chrome, Opera */
  413. if (document.addEventListener) {
  414. document.addEventListener('DOMContentLoaded', callback, false);
  415. }
  416. /* Safari, iCab, Konqueror */
  417. else if (/KHTML|WebKit|iCab/i.test(navigator.userAgent)) {
  418. var DOMLoadTimer = setInterval(function () {
  419. if (/loaded|complete/i.test(document.readyState)) {
  420. callback();
  421. clearInterval(DOMLoadTimer);
  422. }
  423. }, 10);
  424. }
  425. /* Other web browsers */
  426. else window.onload = callback;
  427. };
  428. ElementQueries.listen = function() {
  429. domLoaded(ElementQueries.init);
  430. };
  431. // make available to common module loader
  432. if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
  433. module.exports = ElementQueries;
  434. }
  435. else {
  436. window.ElementQueries = ElementQueries;
  437. ElementQueries.listen();
  438. }
  439. return ElementQueries;
  440. }));