foundation.interchange.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. ;(function ($, window, document, undefined) {
  2. 'use strict';
  3. Foundation.libs.interchange = {
  4. name : 'interchange',
  5. version : '5.2.2',
  6. cache : {},
  7. images_loaded : false,
  8. nodes_loaded : false,
  9. settings : {
  10. load_attr : 'interchange',
  11. named_queries : {
  12. 'default' : 'only screen',
  13. small : Foundation.media_queries.small,
  14. medium : Foundation.media_queries.medium,
  15. large : Foundation.media_queries.large,
  16. xlarge : Foundation.media_queries.xlarge,
  17. xxlarge: Foundation.media_queries.xxlarge,
  18. landscape : 'only screen and (orientation: landscape)',
  19. portrait : 'only screen and (orientation: portrait)',
  20. retina : 'only screen and (-webkit-min-device-pixel-ratio: 2),' +
  21. 'only screen and (min--moz-device-pixel-ratio: 2),' +
  22. 'only screen and (-o-min-device-pixel-ratio: 2/1),' +
  23. 'only screen and (min-device-pixel-ratio: 2),' +
  24. 'only screen and (min-resolution: 192dpi),' +
  25. 'only screen and (min-resolution: 2dppx)'
  26. },
  27. directives : {
  28. replace: function (el, path, trigger) {
  29. // The trigger argument, if called within the directive, fires
  30. // an event named after the directive on the element, passing
  31. // any parameters along to the event that you pass to trigger.
  32. //
  33. // ex. trigger(), trigger([a, b, c]), or trigger(a, b, c)
  34. //
  35. // This allows you to bind a callback like so:
  36. // $('#interchangeContainer').on('replace', function (e, a, b, c) {
  37. // console.log($(this).html(), a, b, c);
  38. // });
  39. if (/IMG/.test(el[0].nodeName)) {
  40. var orig_path = el[0].src;
  41. if (new RegExp(path, 'i').test(orig_path)) return;
  42. el[0].src = path;
  43. return trigger(el[0].src);
  44. }
  45. var last_path = el.data(this.data_attr + '-last-path');
  46. if (last_path == path) return;
  47. if (/\.(gif|jpg|jpeg|tiff|png)([?#].*)?/i.test(path)) {
  48. $(el).css('background-image', 'url('+path+')');
  49. el.data('interchange-last-path', path);
  50. return trigger(path);
  51. }
  52. return $.get(path, function (response) {
  53. el.html(response);
  54. el.data(this.data_attr + '-last-path', path);
  55. trigger();
  56. });
  57. }
  58. }
  59. },
  60. init : function (scope, method, options) {
  61. Foundation.inherit(this, 'throttle random_str');
  62. this.data_attr = this.set_data_attr();
  63. $.extend(true, this.settings, method, options);
  64. this.bindings(method, options);
  65. this.load('images');
  66. this.load('nodes');
  67. },
  68. get_media_hash : function() {
  69. var mediaHash='';
  70. for (var queryName in this.settings.named_queries ) {
  71. mediaHash += matchMedia(this.settings.named_queries[queryName]).matches.toString();
  72. }
  73. return mediaHash;
  74. },
  75. events : function () {
  76. var self = this, prevMediaHash;
  77. $(window)
  78. .off('.interchange')
  79. .on('resize.fndtn.interchange', self.throttle(function () {
  80. var currMediaHash = self.get_media_hash();
  81. if (currMediaHash !== prevMediaHash) {
  82. self.resize();
  83. }
  84. prevMediaHash = currMediaHash;
  85. }, 50));
  86. return this;
  87. },
  88. resize : function () {
  89. var cache = this.cache;
  90. if(!this.images_loaded || !this.nodes_loaded) {
  91. setTimeout($.proxy(this.resize, this), 50);
  92. return;
  93. }
  94. for (var uuid in cache) {
  95. if (cache.hasOwnProperty(uuid)) {
  96. var passed = this.results(uuid, cache[uuid]);
  97. if (passed) {
  98. this.settings.directives[passed
  99. .scenario[1]].call(this, passed.el, passed.scenario[0], function () {
  100. if (arguments[0] instanceof Array) {
  101. var args = arguments[0];
  102. } else {
  103. var args = Array.prototype.slice.call(arguments, 0);
  104. }
  105. passed.el.trigger(passed.scenario[1], args);
  106. });
  107. }
  108. }
  109. }
  110. },
  111. results : function (uuid, scenarios) {
  112. var count = scenarios.length;
  113. if (count > 0) {
  114. var el = this.S('[' + this.add_namespace('data-uuid') + '="' + uuid + '"]');
  115. while (count--) {
  116. var mq, rule = scenarios[count][2];
  117. if (this.settings.named_queries.hasOwnProperty(rule)) {
  118. mq = matchMedia(this.settings.named_queries[rule]);
  119. } else {
  120. mq = matchMedia(rule);
  121. }
  122. if (mq.matches) {
  123. return {el: el, scenario: scenarios[count]};
  124. }
  125. }
  126. }
  127. return false;
  128. },
  129. load : function (type, force_update) {
  130. if (typeof this['cached_' + type] === 'undefined' || force_update) {
  131. this['update_' + type]();
  132. }
  133. return this['cached_' + type];
  134. },
  135. update_images : function () {
  136. var images = this.S('img[' + this.data_attr + ']'),
  137. count = images.length,
  138. i = count,
  139. loaded_count = 0,
  140. data_attr = this.data_attr;
  141. this.cache = {};
  142. this.cached_images = [];
  143. this.images_loaded = (count === 0);
  144. while (i--) {
  145. loaded_count++;
  146. if (images[i]) {
  147. var str = images[i].getAttribute(data_attr) || '';
  148. if (str.length > 0) {
  149. this.cached_images.push(images[i]);
  150. }
  151. }
  152. if (loaded_count === count) {
  153. this.images_loaded = true;
  154. this.enhance('images');
  155. }
  156. }
  157. return this;
  158. },
  159. update_nodes : function () {
  160. var nodes = this.S('[' + this.data_attr + ']').not('img'),
  161. count = nodes.length,
  162. i = count,
  163. loaded_count = 0,
  164. data_attr = this.data_attr;
  165. this.cached_nodes = [];
  166. this.nodes_loaded = (count === 0);
  167. while (i--) {
  168. loaded_count++;
  169. var str = nodes[i].getAttribute(data_attr) || '';
  170. if (str.length > 0) {
  171. this.cached_nodes.push(nodes[i]);
  172. }
  173. if(loaded_count === count) {
  174. this.nodes_loaded = true;
  175. this.enhance('nodes');
  176. }
  177. }
  178. return this;
  179. },
  180. enhance : function (type) {
  181. var i = this['cached_' + type].length;
  182. while (i--) {
  183. this.object($(this['cached_' + type][i]));
  184. }
  185. return $(window).trigger('resize');
  186. },
  187. parse_params : function (path, directive, mq) {
  188. return [this.trim(path), this.convert_directive(directive), this.trim(mq)];
  189. },
  190. convert_directive : function (directive) {
  191. var trimmed = this.trim(directive);
  192. if (trimmed.length > 0) {
  193. return trimmed;
  194. }
  195. return 'replace';
  196. },
  197. object : function(el) {
  198. var raw_arr = this.parse_data_attr(el),
  199. scenarios = [],
  200. i = raw_arr.length;
  201. if (i > 0) {
  202. while (i--) {
  203. var split = raw_arr[i].split(/\((.*?)(\))$/);
  204. if (split.length > 1) {
  205. var cached_split = split[0].split(','),
  206. params = this.parse_params(cached_split[0],
  207. cached_split[1], split[1]);
  208. scenarios.push(params);
  209. }
  210. }
  211. }
  212. return this.store(el, scenarios);
  213. },
  214. store : function (el, scenarios) {
  215. var uuid = this.random_str(),
  216. current_uuid = el.data(this.add_namespace('uuid', true));
  217. if (this.cache[current_uuid]) return this.cache[current_uuid];
  218. el.attr(this.add_namespace('data-uuid'), uuid);
  219. return this.cache[uuid] = scenarios;
  220. },
  221. trim : function(str) {
  222. if (typeof str === 'string') {
  223. return $.trim(str);
  224. }
  225. return str;
  226. },
  227. set_data_attr: function (init) {
  228. if (init) {
  229. if (this.namespace.length > 0) {
  230. return this.namespace + '-' + this.settings.load_attr;
  231. }
  232. return this.settings.load_attr;
  233. }
  234. if (this.namespace.length > 0) {
  235. return 'data-' + this.namespace + '-' + this.settings.load_attr;
  236. }
  237. return 'data-' + this.settings.load_attr;
  238. },
  239. parse_data_attr : function (el) {
  240. var raw = el.attr(this.attr_name()).split(/\[(.*?)\]/),
  241. i = raw.length,
  242. output = [];
  243. while (i--) {
  244. if (raw[i].replace(/[\W\d]+/, '').length > 4) {
  245. output.push(raw[i]);
  246. }
  247. }
  248. return output;
  249. },
  250. reflow : function () {
  251. this.load('images', true);
  252. this.load('nodes', true);
  253. }
  254. };
  255. }(jQuery, this, this.document));