foundation.interchange.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. ;(function ($, window, document, undefined) {
  2. 'use strict';
  3. Foundation.libs.interchange = {
  4. name : 'interchange',
  5. version : '5.1.1',
  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. return $.get(path, function (response) {
  48. el.html(response);
  49. el.data(this.data_attr + '-last-path', path);
  50. trigger();
  51. });
  52. }
  53. }
  54. },
  55. init : function (scope, method, options) {
  56. Foundation.inherit(this, 'throttle random_str');
  57. this.data_attr = this.set_data_attr();
  58. $.extend(true, this.settings, method, options);
  59. this.bindings(method, options);
  60. this.load('images');
  61. this.load('nodes');
  62. },
  63. events : function () {
  64. var self = this;
  65. $(window)
  66. .off('.interchange')
  67. .on('resize.fndtn.interchange', self.throttle(function () {
  68. self.resize();
  69. }, 50));
  70. return this;
  71. },
  72. resize : function () {
  73. var cache = this.cache;
  74. if(!this.images_loaded || !this.nodes_loaded) {
  75. setTimeout($.proxy(this.resize, this), 50);
  76. return;
  77. }
  78. for (var uuid in cache) {
  79. if (cache.hasOwnProperty(uuid)) {
  80. var passed = this.results(uuid, cache[uuid]);
  81. if (passed) {
  82. this.settings.directives[passed
  83. .scenario[1]].call(this, passed.el, passed.scenario[0], function () {
  84. if (arguments[0] instanceof Array) {
  85. var args = arguments[0];
  86. } else {
  87. var args = Array.prototype.slice.call(arguments, 0);
  88. }
  89. passed.el.trigger(passed.scenario[1], args);
  90. });
  91. }
  92. }
  93. }
  94. },
  95. results : function (uuid, scenarios) {
  96. var count = scenarios.length;
  97. if (count > 0) {
  98. var el = this.S('[' + this.add_namespace('data-uuid') + '="' + uuid + '"]');
  99. while (count--) {
  100. var mq, rule = scenarios[count][2];
  101. if (this.settings.named_queries.hasOwnProperty(rule)) {
  102. mq = matchMedia(this.settings.named_queries[rule]);
  103. } else {
  104. mq = matchMedia(rule);
  105. }
  106. if (mq.matches) {
  107. return {el: el, scenario: scenarios[count]};
  108. }
  109. }
  110. }
  111. return false;
  112. },
  113. load : function (type, force_update) {
  114. if (typeof this['cached_' + type] === 'undefined' || force_update) {
  115. this['update_' + type]();
  116. }
  117. return this['cached_' + type];
  118. },
  119. update_images : function () {
  120. var images = this.S('img[' + this.data_attr + ']'),
  121. count = images.length,
  122. i = count,
  123. loaded_count = 0,
  124. data_attr = this.data_attr;
  125. this.cache = {};
  126. this.cached_images = [];
  127. this.images_loaded = (count === 0);
  128. while (i--) {
  129. loaded_count++;
  130. if (images[i]) {
  131. var str = images[i].getAttribute(data_attr) || '';
  132. if (str.length > 0) {
  133. this.cached_images.push(images[i]);
  134. }
  135. }
  136. if (loaded_count === count) {
  137. this.images_loaded = true;
  138. this.enhance('images');
  139. }
  140. }
  141. return this;
  142. },
  143. update_nodes : function () {
  144. var nodes = this.S('[' + this.data_attr + ']').not('img'),
  145. count = nodes.length,
  146. i = count,
  147. loaded_count = 0,
  148. data_attr = this.data_attr;
  149. this.cached_nodes = [];
  150. // Set nodes_loaded to true if there are no nodes
  151. // this.nodes_loaded = false;
  152. this.nodes_loaded = (count === 0);
  153. while (i--) {
  154. loaded_count++;
  155. var str = nodes[i].getAttribute(data_attr) || '';
  156. if (str.length > 0) {
  157. this.cached_nodes.push(nodes[i]);
  158. }
  159. if(loaded_count === count) {
  160. this.nodes_loaded = true;
  161. this.enhance('nodes');
  162. }
  163. }
  164. return this;
  165. },
  166. enhance : function (type) {
  167. var i = this['cached_' + type].length;
  168. while (i--) {
  169. this.object($(this['cached_' + type][i]));
  170. }
  171. return $(window).trigger('resize');
  172. },
  173. parse_params : function (path, directive, mq) {
  174. return [this.trim(path), this.convert_directive(directive), this.trim(mq)];
  175. },
  176. convert_directive : function (directive) {
  177. var trimmed = this.trim(directive);
  178. if (trimmed.length > 0) {
  179. return trimmed;
  180. }
  181. return 'replace';
  182. },
  183. object : function(el) {
  184. var raw_arr = this.parse_data_attr(el),
  185. scenarios = [],
  186. i = raw_arr.length;
  187. if (i > 0) {
  188. while (i--) {
  189. var split = raw_arr[i].split(/\((.*?)(\))$/);
  190. if (split.length > 1) {
  191. var cached_split = split[0].split(','),
  192. params = this.parse_params(cached_split[0],
  193. cached_split[1], split[1]);
  194. scenarios.push(params);
  195. }
  196. }
  197. }
  198. return this.store(el, scenarios);
  199. },
  200. uuid : function (separator) {
  201. var delim = separator || "-",
  202. self = this;
  203. function S4() {
  204. return self.random_str(6);
  205. }
  206. return (S4() + S4() + delim + S4() + delim + S4()
  207. + delim + S4() + delim + S4() + S4() + S4());
  208. },
  209. store : function (el, scenarios) {
  210. var uuid = this.uuid(),
  211. current_uuid = el.data(this.add_namespace('uuid', true));
  212. if (this.cache[current_uuid]) return this.cache[current_uuid];
  213. el.attr(this.add_namespace('data-uuid'), uuid);
  214. return this.cache[uuid] = scenarios;
  215. },
  216. trim : function(str) {
  217. if (typeof str === 'string') {
  218. return $.trim(str);
  219. }
  220. return str;
  221. },
  222. set_data_attr: function (init) {
  223. if (init) {
  224. if (this.namespace.length > 0) {
  225. return this.namespace + '-' + this.settings.load_attr;
  226. }
  227. return this.settings.load_attr;
  228. }
  229. if (this.namespace.length > 0) {
  230. return 'data-' + this.namespace + '-' + this.settings.load_attr;
  231. }
  232. return 'data-' + this.settings.load_attr;
  233. },
  234. parse_data_attr : function (el) {
  235. var raw = el.attr(this.attr_name()).split(/\[(.*?)\]/),
  236. i = raw.length,
  237. output = [];
  238. while (i--) {
  239. if (raw[i].replace(/[\W\d]+/, '').length > 4) {
  240. output.push(raw[i]);
  241. }
  242. }
  243. return output;
  244. },
  245. reflow : function () {
  246. this.load('images', true);
  247. this.load('nodes', true);
  248. }
  249. };
  250. }(jQuery, this, this.document));