* mOxie - multi-runtime File API & XMLHttpRequest L2 Polyfill
* Copyright 2013, Moxiecode Systems AB
* Released under GPL License.
* License: http://www.plupload.com/license
* Contributing: http://www.plupload.com/contributing
* Compiled inline version. (Library mode)
* Modified for WordPress, Silverlight and Flash runtimes support was removed.
* See https://core.trac.wordpress.org/ticket/41755.
/*jshint smarttabs:true, undef:true, latedef:true, curly:true, bitwise:true, camelcase:true */
(function(exports, undefined) {
function require(ids, callback) {
for (var i = 0; i < ids.length; ++i) {
module = modules[ids[i]] || resolve(ids[i]);
throw 'module definition dependecy not found: ' + ids[i];
callback.apply(null, defs);
function define(id, dependencies, definition) {
if (typeof id !== 'string') {
throw 'invalid module definition, module id must be defined and be a string';
if (dependencies === undefined) {
throw 'invalid module definition, dependencies must be specified';
if (definition === undefined) {
throw 'invalid module definition, definition function must be specified';
require(dependencies, function() {
modules[id] = definition.apply(null, arguments);
var fragments = id.split(/[.\/]/);
for (var fi = 0; fi < fragments.length; ++fi) {
if (!target[fragments[fi]]) {
target = target[fragments[fi]];
for (var i = 0; i < ids.length; i++) {
var fragments = id.split(/[.\/]/);
for (var fi = 0; fi < fragments.length - 1; ++fi) {
if (target[fragments[fi]] === undefined) {
target[fragments[fi]] = {};
target = target[fragments[fi]];
target[fragments[fragments.length - 1]] = modules[id];
// Included from: src/javascript/core/utils/Basic.js
* Copyright 2013, Moxiecode Systems AB
* Released under GPL License.
* License: http://www.plupload.com/license
* Contributing: http://www.plupload.com/contributing
define('moxie/core/utils/Basic', [], function() {
Gets the true type of the built-in object (better version of typeof).
@author Angus Croll (http://javascriptweblog.wordpress.com/)
@param {Object} o Object to check.
@return {String} Object [[Class]]
var typeOf = function(o) {
// the snippet below is awesome, however it fails to detect null, undefined and arguments types in IE lte 8
return ({}).toString.call(o).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
Extends the specified object with another object.
@param {Object} target Object to extend.
@param {Object} [obj]* Multiple objects to extend with.
@return {Object} Same as target, the extended object.
var extend = function(target) {
each(arguments, function(arg, i) {
each(arg, function(value, key) {
if (typeOf(target[key]) === typeOf(value) && !!~inArray(typeOf(value), ['array', 'object'])) {
extend(target[key], value);
Executes the callback function for each item in array/object. If you return false in the
callback it will break the loop.
@param {Object} obj Object to iterate.
@param {function} callback Callback function to execute for each item.
var each = function(obj, callback) {
var length, key, i, undef;
if (typeOf(obj.length) === 'number') { // it might be Array, FileList or even arguments object
for (i = 0, length = obj.length; i < length; i++) {
if (callback(obj[i], i) === false) {
} else if (typeOf(obj) === 'object') {
if (obj.hasOwnProperty(key)) {
if (callback(obj[key], key) === false) {
Checks if object is empty.
@param {Object} o Object to check.
var isEmptyObj = function(obj) {
if (!obj || typeOf(obj) !== 'object') {
Recieve an array of functions (usually async) to call in sequence, each function
receives a callback as first argument that it should call, when it completes. Finally,
after everything is complete, main callback is called. Passing truthy value to the
callback as a first argument will interrupt the sequence and invoke main callback
@param {Array} queue Array of functions to call in sequence
@param {Function} cb Main callback that is called in the end, or in case of error
var inSeries = function(queue, cb) {
var i = 0, length = queue.length;
if (typeOf(cb) !== 'function') {
if (!queue || !queue.length) {
if (typeOf(queue[i]) === 'function') {
queue[i](function(error) {
++i < length && !error ? callNext(i) : cb(error);
Recieve an array of functions (usually async) to call in parallel, each function
receives a callback as first argument that it should call, when it completes. After
everything is complete, main callback is called. Passing truthy value to the
callback as a first argument will interrupt the process and invoke main callback
@param {Array} queue Array of functions to call in sequence
@param {Function} cb Main callback that is called in the end, or in case of error
var inParallel = function(queue, cb) {
var count = 0, num = queue.length, cbArgs = new Array(num);
each(queue, function(fn, i) {
var args = [].slice.call(arguments);
args.shift(); // strip error - undefined or not
Find an element in array and return it's index if present, otherwise return -1.
@param {Mixed} needle Element to find
@return {Int} Index of the element, or -1 if not found
var inArray = function(needle, array) {
if (Array.prototype.indexOf) {
return Array.prototype.indexOf.call(array, needle);
for (var i = 0, length = array.length; i < length; i++) {
if (array[i] === needle) {
Returns elements of first array if they are not present in second. And false - otherwise.
var arrayDiff = function(needles, array) {
if (typeOf(needles) !== 'array') {
if (typeOf(array) !== 'array') {
if (inArray(needles[i], array) === -1) {
return diff.length ? diff : false;
Find intersection of two arrays.
@return {Array} Intersection of two arrays or null if there is none
var arrayIntersect = function(array1, array2) {
each(array1, function(item) {
if (inArray(item, array2) !== -1) {
return result.length ? result : null;
Forces anything into an array.
@param {Object} obj Object with length field.
@return {Array} Array object containing all items.
var toArray = function(obj) {
for (i = 0; i < obj.length; i++) {
Generates an unique ID. The only way a user would be able to get the same ID is if the two persons
at the same exact millisecond manage to get the same 5 random numbers between 0-65535; it also uses
a counter so each ID is guaranteed to be unique for the given page. It is more probable for the earth
to be hit with an asteroid.
@param {String} prefix to prepend (by default 'o' will be prepended).
@return {String} Virtually unique id.
return function(prefix) {
var guid = new Date().getTime().toString(32), i;
for (i = 0; i < 5; i++) {
guid += Math.floor(Math.random() * 65535).toString(32);
return (prefix || 'o_') + guid + (counter++).toString(32);
Trims white spaces around the string
var trim = function(str) {
return String.prototype.trim ? String.prototype.trim.call(str) : str.toString().replace(/^\s*/, '').replace(/\s*$/, '');
Parses the specified size string into a byte value. For example 10kb becomes 10240.
@param {String/Number} size String to parse or number to just pass through.
@return {Number} Size in bytes.
var parseSizeStr = function(size) {
if (typeof(size) !== 'string') {
size = /^([0-9\.]+)([tmgk]?)$/.exec(size.toLowerCase().replace(/[^0-9\.tmkg]/g, ''));
if (muls.hasOwnProperty(mul)) {
* Pseudo sprintf implementation - simple way to replace tokens with specified values.
* @param {String} str String with tokens
* @return {String} String with replaced tokens
var sprintf = function(str) {
var args = [].slice.call(arguments, 1);
return str.replace(/%[a-z]/g, function() {
var value = args.shift();
return typeOf(value) !== 'undefined' ? value : '';
arrayIntersect: arrayIntersect,
parseSizeStr: parseSizeStr
// Included from: src/javascript/core/utils/Env.js