/*jslint devel: false, browser: true, node: true, eqeq: true, white: true, passfail: true, plusplus: false, regexp: true, maxerr: 50, indent: 4 */ // // Pre.js 1.0.0 // // (c) 20011-2012 David Hong, @hongymagic. // Underscore is freely distributable under the MIT license. // Portions of Pre.js are inspired or borrowed from Prototype, // Oliver Steele's Functional, and DocumentCloud's Underscore.js. // For all details and documentation: // http://hongymagic.github.com/pre.js // // ## Global namespace, Pre var Pre = (function () { 'use strict'; var Pre, prototype, // Regular expressions to help determine file type. Pre.js supports the // following file types: // // 1. Images: jpg, jpeg, gif, bmp, gif, png // 2. Audio: wav, weba, ogg, mp3, m4a, aiff // // #### TODO // // 1. JSON // 2. HTML // 3. SVG types = { "image": { regex: /^.*\.(jpe?g|png|bmp|gif)$/i, get: function (resource) { var image = new Image(); image.src = resource; return image; } }, "audio": { regex: /^.*\.(wav|weba|ogg|mp3|m4a|aiff)$/i, get: function (resource) { var audio = new Audio(); audio.preload = "auto"; audio.src = resource; return audio; } } }, // Returns the type of resource based on file types defined above. type = function (resource) { var rtype, matches; for (rtype in types) { if (types.hasOwnProperty(rtype)) { matches = types[rtype].regex.exec(resource); if (matches && matches.length > 1) { return rtype; } } } throw new Error('Unable to determine extension type: ' + resource); }, // Cache inline functions for faster! performance! Woo yeah! ArrayProto = Array.prototype, ObjectProto = Object.prototype, FuncProto = Function.prototype, isArray = Array.isArray, reduce = ArrayProto.reduce, each = ArrayProto.each, map = ArrayProto.map, slice = ArrayProto.slice, bind = FuncProto.bind, toString = ObjectProto.toString, // ### Custom inline functions // Returns a flattened array, **usage**: // // flatten([1, 2, 3], [4, 5, 6]); // => [1, 2, 3, 4, 5, 6]; flatten = function (array, shallow) { return array.reduce(function (memo, value) { if (isArray(value)) { return memo.concat(shallow ? value : flatten(value)); } memo[memo.length] = value; return memo; }, []); }, isFunction = function (func) { return toString.call(func) == '[object Function]'; }, // Create an asset data for internal use createAsset = function (resource, asset) { return { url: resource, resource: resource, asset: asset }; }, // Create a wrapped Callback object for internal use createCallback = function (callback, context) { return { context: context, callback: callback }; }, // Store downloaded assets locally, main advantage is for audio assets // we can cheat and play it over and over again without recreating the //