volt-foundation 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,344 +0,0 @@
1
- ;(function ($, window, document, undefined) {
2
- 'use strict';
3
-
4
- Foundation.libs.interchange = {
5
- name : 'interchange',
6
-
7
- version : '5.4.6',
8
-
9
- cache : {},
10
-
11
- images_loaded : false,
12
- nodes_loaded : false,
13
-
14
- settings : {
15
- load_attr : 'interchange',
16
-
17
- named_queries : {
18
- 'default' : 'only screen',
19
- small : Foundation.media_queries.small,
20
- medium : Foundation.media_queries.medium,
21
- large : Foundation.media_queries.large,
22
- xlarge : Foundation.media_queries.xlarge,
23
- xxlarge: Foundation.media_queries.xxlarge,
24
- landscape : 'only screen and (orientation: landscape)',
25
- portrait : 'only screen and (orientation: portrait)',
26
- retina : 'only screen and (-webkit-min-device-pixel-ratio: 2),' +
27
- 'only screen and (min--moz-device-pixel-ratio: 2),' +
28
- 'only screen and (-o-min-device-pixel-ratio: 2/1),' +
29
- 'only screen and (min-device-pixel-ratio: 2),' +
30
- 'only screen and (min-resolution: 192dpi),' +
31
- 'only screen and (min-resolution: 2dppx)'
32
- },
33
-
34
- directives : {
35
- replace: function (el, path, trigger) {
36
- // The trigger argument, if called within the directive, fires
37
- // an event named after the directive on the element, passing
38
- // any parameters along to the event that you pass to trigger.
39
- //
40
- // ex. trigger(), trigger([a, b, c]), or trigger(a, b, c)
41
- //
42
- // This allows you to bind a callback like so:
43
- // $('#interchangeContainer').on('replace', function (e, a, b, c) {
44
- // console.log($(this).html(), a, b, c);
45
- // });
46
-
47
- if (/IMG/.test(el[0].nodeName)) {
48
- var orig_path = el[0].src;
49
-
50
- if (new RegExp(path, 'i').test(orig_path)) return;
51
-
52
- el[0].src = path;
53
-
54
- return trigger(el[0].src);
55
- }
56
- var last_path = el.data(this.data_attr + '-last-path'),
57
- self = this;
58
-
59
- if (last_path == path) return;
60
-
61
- if (/\.(gif|jpg|jpeg|tiff|png)([?#].*)?/i.test(path)) {
62
- $(el).css('background-image', 'url('+path+')');
63
- el.data('interchange-last-path', path);
64
- return trigger(path);
65
- }
66
-
67
- return $.get(path, function (response) {
68
- el.html(response);
69
- el.data(self.data_attr + '-last-path', path);
70
- trigger();
71
- });
72
-
73
- }
74
- }
75
- },
76
-
77
- init : function (scope, method, options) {
78
- Foundation.inherit(this, 'throttle random_str');
79
-
80
- this.data_attr = this.set_data_attr();
81
- $.extend(true, this.settings, method, options);
82
- this.bindings(method, options);
83
- this.load('images');
84
- this.load('nodes');
85
- },
86
-
87
- get_media_hash : function() {
88
- var mediaHash='';
89
- for (var queryName in this.settings.named_queries ) {
90
- mediaHash += matchMedia(this.settings.named_queries[queryName]).matches.toString();
91
- }
92
- return mediaHash;
93
- },
94
-
95
- events : function () {
96
- var self = this, prevMediaHash;
97
-
98
- $(window)
99
- .off('.interchange')
100
- .on('resize.fndtn.interchange', self.throttle(function () {
101
- var currMediaHash = self.get_media_hash();
102
- if (currMediaHash !== prevMediaHash) {
103
- self.resize();
104
- }
105
- prevMediaHash = currMediaHash;
106
- }, 50));
107
-
108
- return this;
109
- },
110
-
111
- resize : function () {
112
- var cache = this.cache;
113
-
114
- if(!this.images_loaded || !this.nodes_loaded) {
115
- setTimeout($.proxy(this.resize, this), 50);
116
- return;
117
- }
118
-
119
- for (var uuid in cache) {
120
- if (cache.hasOwnProperty(uuid)) {
121
- var passed = this.results(uuid, cache[uuid]);
122
-
123
- if (passed) {
124
- this.settings.directives[passed
125
- .scenario[1]].call(this, passed.el, passed.scenario[0], function () {
126
- if (arguments[0] instanceof Array) {
127
- var args = arguments[0];
128
- } else {
129
- var args = Array.prototype.slice.call(arguments, 0);
130
- }
131
-
132
- passed.el.trigger(passed.scenario[1], args);
133
- });
134
- }
135
- }
136
- }
137
-
138
- },
139
-
140
- results : function (uuid, scenarios) {
141
- var count = scenarios.length;
142
-
143
- if (count > 0) {
144
- var el = this.S('[' + this.add_namespace('data-uuid') + '="' + uuid + '"]');
145
-
146
- while (count--) {
147
- var mq, rule = scenarios[count][2];
148
- if (this.settings.named_queries.hasOwnProperty(rule)) {
149
- mq = matchMedia(this.settings.named_queries[rule]);
150
- } else {
151
- mq = matchMedia(rule);
152
- }
153
- if (mq.matches) {
154
- return {el: el, scenario: scenarios[count]};
155
- }
156
- }
157
- }
158
-
159
- return false;
160
- },
161
-
162
- load : function (type, force_update) {
163
- if (typeof this['cached_' + type] === 'undefined' || force_update) {
164
- this['update_' + type]();
165
- }
166
-
167
- return this['cached_' + type];
168
- },
169
-
170
- update_images : function () {
171
- var images = this.S('img[' + this.data_attr + ']'),
172
- count = images.length,
173
- i = count,
174
- loaded_count = 0,
175
- data_attr = this.data_attr;
176
-
177
- this.cache = {};
178
- this.cached_images = [];
179
- this.images_loaded = (count === 0);
180
-
181
- while (i--) {
182
- loaded_count++;
183
- if (images[i]) {
184
- var str = images[i].getAttribute(data_attr) || '';
185
-
186
- if (str.length > 0) {
187
- this.cached_images.push(images[i]);
188
- }
189
- }
190
-
191
- if (loaded_count === count) {
192
- this.images_loaded = true;
193
- this.enhance('images');
194
- }
195
- }
196
-
197
- return this;
198
- },
199
-
200
- update_nodes : function () {
201
- var nodes = this.S('[' + this.data_attr + ']').not('img'),
202
- count = nodes.length,
203
- i = count,
204
- loaded_count = 0,
205
- data_attr = this.data_attr;
206
-
207
- this.cached_nodes = [];
208
- this.nodes_loaded = (count === 0);
209
-
210
-
211
- while (i--) {
212
- loaded_count++;
213
- var str = nodes[i].getAttribute(data_attr) || '';
214
-
215
- if (str.length > 0) {
216
- this.cached_nodes.push(nodes[i]);
217
- }
218
-
219
- if(loaded_count === count) {
220
- this.nodes_loaded = true;
221
- this.enhance('nodes');
222
- }
223
- }
224
-
225
- return this;
226
- },
227
-
228
- enhance : function (type) {
229
- var i = this['cached_' + type].length;
230
-
231
- while (i--) {
232
- this.object($(this['cached_' + type][i]));
233
- }
234
-
235
- return $(window).trigger('resize').trigger('resize.fndtn.interchange');
236
- },
237
-
238
- convert_directive : function (directive) {
239
-
240
- var trimmed = this.trim(directive);
241
-
242
- if (trimmed.length > 0) {
243
- return trimmed;
244
- }
245
-
246
- return 'replace';
247
- },
248
-
249
- parse_scenario : function (scenario) {
250
- // This logic had to be made more complex since some users were using commas in the url path
251
- // So we cannot simply just split on a comma
252
- var directive_match = scenario[0].match(/(.+),\s*(\w+)\s*$/),
253
- media_query = scenario[1];
254
-
255
- if (directive_match) {
256
- var path = directive_match[1],
257
- directive = directive_match[2];
258
- }
259
- else {
260
- var cached_split = scenario[0].split(/,\s*$/),
261
- path = cached_split[0],
262
- directive = '';
263
- }
264
-
265
- return [this.trim(path), this.convert_directive(directive), this.trim(media_query)];
266
- },
267
-
268
- object : function(el) {
269
- var raw_arr = this.parse_data_attr(el),
270
- scenarios = [],
271
- i = raw_arr.length;
272
-
273
- if (i > 0) {
274
- while (i--) {
275
- var split = raw_arr[i].split(/\((.*?)(\))$/);
276
-
277
- if (split.length > 1) {
278
- var params = this.parse_scenario(split);
279
- scenarios.push(params);
280
- }
281
- }
282
- }
283
-
284
- return this.store(el, scenarios);
285
- },
286
-
287
- store : function (el, scenarios) {
288
- var uuid = this.random_str(),
289
- current_uuid = el.data(this.add_namespace('uuid', true));
290
-
291
- if (this.cache[current_uuid]) return this.cache[current_uuid];
292
-
293
- el.attr(this.add_namespace('data-uuid'), uuid);
294
-
295
- return this.cache[uuid] = scenarios;
296
- },
297
-
298
- trim : function(str) {
299
-
300
- if (typeof str === 'string') {
301
- return $.trim(str);
302
- }
303
-
304
- return str;
305
- },
306
-
307
- set_data_attr: function (init) {
308
- if (init) {
309
- if (this.namespace.length > 0) {
310
- return this.namespace + '-' + this.settings.load_attr;
311
- }
312
-
313
- return this.settings.load_attr;
314
- }
315
-
316
- if (this.namespace.length > 0) {
317
- return 'data-' + this.namespace + '-' + this.settings.load_attr;
318
- }
319
-
320
- return 'data-' + this.settings.load_attr;
321
- },
322
-
323
- parse_data_attr : function (el) {
324
- var raw = el.attr(this.attr_name()).split(/\[(.*?)\]/),
325
- i = raw.length,
326
- output = [];
327
-
328
- while (i--) {
329
- if (raw[i].replace(/[\W\d]+/, '').length > 4) {
330
- output.push(raw[i]);
331
- }
332
- }
333
-
334
- return output;
335
- },
336
-
337
- reflow : function () {
338
- this.load('images', true);
339
- this.load('nodes', true);
340
- }
341
-
342
- };
343
-
344
- }(jQuery, window, window.document));