Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / new_ui / ui / javascripts / foundation / foundation.js @ e4a6c3b8

History | View | Annotate | Download (9.7 kB)

1
/*
2
 * Foundation Responsive Library
3
 * http://foundation.zurb.com
4
 * Copyright 2013, ZURB
5
 * Free to use under the MIT license.
6
 * http://www.opensource.org/licenses/mit-license.php
7
*/
8

    
9
/*jslint unparam: true, browser: true, indent: 2 */
10

    
11
// Accommodate running jQuery or Zepto in noConflict() mode by
12
// using an anonymous function to redefine the $ shorthand name.
13
// See http://docs.jquery.com/Using_jQuery_with_Other_Libraries
14
// and http://zeptojs.com/
15
var libFuncName = null;
16
if (typeof jQuery === "undefined" &&
17
    typeof Zepto === "undefined" &&
18
    typeof $ === "function") {
19
    libFuncName = $;
20
} else if (typeof jQuery === "function") {
21
    libFuncName = jQuery;
22
} else if (typeof Zepto === "function") {
23
    libFuncName = Zepto;
24
} else {
25
    throw new TypeError();
26
}
27

    
28
(function ($) {
29

    
30
(function () {
31
  // add dusty browser stuff
32
  if (!Array.prototype.filter) {
33
    Array.prototype.filter = function(fun /*, thisp */) {
34
      "use strict";
35
   
36
      if (this == null) {
37
        throw new TypeError();
38
      }
39
   
40
      var t = Object(this),
41
          len = t.length >>> 0;
42
      if (typeof fun != "function") {
43
        try {
44
          throw new TypeError();
45
        } catch (e) {
46
          return;
47
        }
48
      }
49
   
50
      var res = [],
51
          thisp = arguments[1];
52
      for (var i = 0; i < len; i++) {
53
        if (i in t) {
54
          var val = t[i]; // in case fun mutates this
55
          if (fun && fun.call(thisp, val, i, t)) {
56
            res.push(val);
57
          }
58
        }
59
      }
60
   
61
      return res;
62
    };
63

    
64
    if (!Function.prototype.bind) {
65
      Function.prototype.bind = function (oThis) {
66
        if (typeof this !== "function") {
67
          // closest thing possible to the ECMAScript 5 internal IsCallable function
68
          throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
69
        }
70
     
71
        var aArgs = Array.prototype.slice.call(arguments, 1), 
72
            fToBind = this, 
73
            fNOP = function () {},
74
            fBound = function () {
75
              return fToBind.apply(this instanceof fNOP && oThis
76
                 ? this
77
                 : oThis,
78
               aArgs.concat(Array.prototype.slice.call(arguments)));
79
            };
80
     
81
        fNOP.prototype = this.prototype;
82
        fBound.prototype = new fNOP();
83
     
84
        return fBound;
85
      };
86
    }
87
  }
88

    
89
  // fake stop() for zepto.
90
  $.fn.stop = $.fn.stop || function() {
91
    return this;
92
  };
93
}());
94

    
95
;(function (window, document, undefined) {
96
  'use strict';
97

    
98
  window.Foundation = {
99
    name : 'Foundation',
100

    
101
    version : '4.0.8',
102

    
103
    // global Foundation cache object
104
    cache : {},
105

    
106
    init : function (scope, libraries, method, options, response, /* internal */ nc) {
107
      var library_arr,
108
          args = [scope, method, options, response],
109
          responses = [],
110
          nc = nc || false;
111
          
112
      // disable library error catching,
113
      // used for development only
114
      if (nc) this.nc = nc;
115

    
116
      // set foundation global scope
117
      this.scope = scope || this.scope;
118

    
119
      if (libraries && typeof libraries === 'string') {
120
        if (/off/i.test(libraries)) return this.off();
121

    
122
        library_arr = libraries.split(' ');
123

    
124
        if (library_arr.length > 0) {
125
          for (var i = library_arr.length - 1; i >= 0; i--) {
126
            responses.push(this.init_lib(library_arr[i], args));
127
          }
128
        }
129
      } else {
130
        for (var lib in this.libs) {
131
          responses.push(this.init_lib(lib, args));
132
        }
133
      }
134

    
135
      // if first argument is callback, add to args
136
      if (typeof libraries === 'function') {
137
        args.unshift(libraries);
138
      }
139

    
140
      return this.response_obj(responses, args);
141
    },
142

    
143
    response_obj : function (response_arr, args) {
144
      for (var callback in args) {
145
        if (typeof args[callback] === 'function') {
146
          return args[callback]({
147
            errors: response_arr.filter(function (s) {
148
              if (typeof s === 'string') return s;
149
            })
150
          });
151
        }
152
      }
153

    
154
      return response_arr;
155
    },
156

    
157
    init_lib : function (lib, args) {
158
      return this.trap(function () {
159
        if (this.libs.hasOwnProperty(lib)) {
160
          this.patch(this.libs[lib]);
161
          return this.libs[lib].init.apply(this.libs[lib], args);
162
        }
163
      }.bind(this), lib);
164
    },
165

    
166
    trap : function (fun, lib) {
167
      if (!this.nc) {
168
        try {
169
          return fun();
170
        } catch (e) {
171
          return this.error({name: lib, message: 'could not be initialized', more: e.name + ' ' + e.message});
172
        }
173
      }
174

    
175
      return fun();
176
    },
177

    
178
    patch : function (lib) {
179
      this.fix_outer(lib);
180
    },
181

    
182
    inherit : function (scope, methods) {
183
      var methods_arr = methods.split(' ');
184

    
185
      for (var i = methods_arr.length - 1; i >= 0; i--) {
186
        if (this.lib_methods.hasOwnProperty(methods_arr[i])) {
187
          this.libs[scope.name][methods_arr[i]] = this.lib_methods[methods_arr[i]];
188
        }
189
      }
190
    },
191

    
192
    random_str : function (length) {
193
      var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split('');
194
      
195
      if (!length) {
196
          length = Math.floor(Math.random() * chars.length);
197
      }
198
      
199
      var str = '';
200
      for (var i = 0; i < length; i++) {
201
          str += chars[Math.floor(Math.random() * chars.length)];
202
      }
203
      return str;
204
    },
205

    
206
    libs : {},
207

    
208
    // methods that can be inherited in libraries
209
    lib_methods : {
210
      set_data : function (node, data) {
211
        // this.name references the name of the library calling this method
212
        var id = [this.name,+new Date(),Foundation.random_str(5)].join('-');
213

    
214
        Foundation.cache[id] = data;
215
        node.attr('data-' + this.name + '-id', id);
216
        return data;
217
      },
218

    
219
      get_data : function (node) {
220
        return Foundation.cache[node.attr('data-' + this.name + '-id')];
221
      },
222

    
223
      remove_data : function (node) {
224
        if (node) {
225
          delete Foundation.cache[node.attr('data-' + this.name + '-id')];
226
          node.attr('data-' + this.name + '-id', '');
227
        } else {
228
          $('[data-' + this.name + '-id]').each(function () {
229
            delete Foundation.cache[$(this).attr('data-' + this.name + '-id')];
230
            $(this).attr('data-' + this.name + '-id', '');
231
          });
232
        }
233
      },
234

    
235
      throttle : function(fun, delay) {
236
        var timer = null;
237
        return function () {
238
          var context = this, args = arguments;
239
          clearTimeout(timer);
240
          timer = setTimeout(function () {
241
            fun.apply(context, args);
242
          }, delay);
243
        };
244
      },
245

    
246
      // parses data-options attribute on nodes and turns
247
      // them into an object
248
      data_options : function (el) {
249
        var opts = {}, ii, p,
250
            opts_arr = (el.attr('data-options') || ':').split(';'),
251
            opts_len = opts_arr.length;
252

    
253
        function isNumber (o) {
254
          return ! isNaN (o-0) && o !== null && o !== "" && o !== false && o !== true;
255
        }
256

    
257
        function trim(str) {
258
          if (typeof str === 'string') return $.trim(str);
259
          return str;
260
        }
261

    
262
        // parse options
263
        for (ii = opts_len - 1; ii >= 0; ii--) {
264
          p = opts_arr[ii].split(':');
265

    
266
          if (/true/i.test(p[1])) p[1] = true;
267
          if (/false/i.test(p[1])) p[1] = false;
268
          if (isNumber(p[1])) p[1] = parseInt(p[1], 10);
269

    
270
          if (p.length === 2 && p[0].length > 0) {
271
            opts[trim(p[0])] = trim(p[1]);
272
          }
273
        }
274

    
275
        return opts;
276
      },
277

    
278
      delay : function (fun, delay) {
279
        return setTimeout(fun, delay);
280
      },
281

    
282
      // animated scrolling
283
      scrollTo : function (el, to, duration) {
284
        if (duration < 0) return;
285
        var difference = to - $(window).scrollTop();
286
        var perTick = difference / duration * 10;
287

    
288
        this.scrollToTimerCache = setTimeout(function() {
289
          if (!isNaN(parseInt(perTick, 10))) {
290
            window.scrollTo(0, $(window).scrollTop() + perTick);
291
            this.scrollTo(el, to, duration - 10);
292
          }
293
        }.bind(this), 10);
294
      },
295

    
296
      // not supported in core Zepto
297
      scrollLeft : function (el) {
298
        if (!el.length) return;
299
        return ('scrollLeft' in el[0]) ? el[0].scrollLeft : el[0].pageXOffset;
300
      },
301

    
302
      // test for empty object or array
303
      empty : function (obj) {
304
        if (obj.length && obj.length > 0)    return false;
305
        if (obj.length && obj.length === 0)  return true;
306

    
307
        for (var key in obj) {
308
          if (hasOwnProperty.call(obj, key))    return false;
309
        }
310

    
311
        return true;
312
      }
313
    },
314

    
315
    fix_outer : function (lib) {
316
      lib.outerHeight = function (el, bool) {
317
        if (typeof Zepto === 'function') {
318
          return el.height();
319
        }
320

    
321
        if (typeof bool !== 'undefined') {
322
          return el.outerHeight(bool);
323
        }
324

    
325
        return el.outerHeight();
326
      };
327

    
328
      lib.outerWidth = function (el) {
329
        if (typeof Zepto === 'function') {
330
          return el.width();
331
        }
332

    
333
        if (typeof bool !== 'undefined') {
334
          return el.outerWidth(bool);
335
        }
336

    
337
        return el.outerWidth();
338
      };
339
    },
340

    
341
    error : function (error) {
342
      return error.name + ' ' + error.message + '; ' + error.more;
343
    },
344

    
345
    // remove all foundation events.
346
    off: function () {
347
      $(this.scope).off('.fndtn');
348
      $(window).off('.fndtn');
349
      return true;
350
    },
351

    
352
    zj : function () {
353
      try {
354
        return Zepto;
355
      } catch (e) {
356
        return jQuery;
357
      }
358
    }()
359
  },
360

    
361
  $.fn.foundation = function () {
362
    var args = Array.prototype.slice.call(arguments, 0);
363

    
364
    return this.each(function () {
365
      Foundation.init.apply(Foundation, [this].concat(args));
366
      return this;
367
    });
368
  };
369

    
370
}(this, this.document));
371

    
372
})(libFuncName);