/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
define(["jquery", "jqueryPlugins"], function ($) {
var util = {};
util.Deferred = $.Deferred;
TogetherJS.$ = $;
/* A simple class pattern, use like:
var Foo = util.Class({
constructor: function (a, b) {
init the class
},
otherMethod: ...
});
You can also give a superclass as the optional first argument.
Instantiation does not require "new"
*/
util.Class = function (superClass, prototype) {
var a;
if (prototype === undefined) {
prototype = superClass;
} else {
if (superClass.prototype) {
superClass = superClass.prototype;
}
var newPrototype = Object.create(superClass);
for (a in prototype) {
if (prototype.hasOwnProperty(a)) {
newPrototype[a] = prototype[a];
}
}
prototype = newPrototype;
}
var ClassObject = function () {
var obj = Object.create(prototype);
obj.constructor.apply(obj, arguments);
obj.constructor = ClassObject;
return obj;
};
ClassObject.prototype = prototype;
if (prototype.constructor.name) {
ClassObject.className = prototype.constructor.name;
ClassObject.toString = function () {
return '[Class ' + this.className + ']';
};
}
if (prototype.classMethods) {
for (a in prototype.classMethods) {
if (prototype.classMethods.hasOwnProperty(a)) {
ClassObject[a] = prototype.classMethods[a];
}
}
}
return ClassObject;
};
/* Extends obj with other, or copies obj if no other is given. */
util.extend = TogetherJS._extend;
util.forEachAttr = function (obj, callback, context) {
context = context || obj;
for (var a in obj) {
if (obj.hasOwnProperty(a)) {
callback.call(context, obj[a], a);
}
}
};
/* Trim whitespace from a string */
util.trim = function trim(s) {
return s.replace(/^\s+/, "").replace(/\s+$/, "");
};
/* Convert a string into something safe to use as an HTML class name */
util.safeClassName = function safeClassName(name) {
return name.replace(/[^a-zA-Z0-9_\-]/g, "_") || "class";
};
util.AssertionError = function (message) {
if (! this instanceof util.AssertionError) {
return new util.AssertionError(message);
}
this.message = message;
this.name = "AssertionError";
};
util.AssertionError.prototype = Error.prototype;
util.assert = function (cond) {
if (! cond) {
var args = ["Assertion error:"].concat(Array.prototype.slice.call(arguments, 1));
console.error.apply(console, args);
if (console.trace) {
console.trace();
}
throw new util.AssertionError(args.join(" "));
}
};
/* Generates a random ID */
util.generateId = function (length) {
length = length || 10;
var letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV0123456789';
var s = '';
for (var i=0; i<length; i++) {
s += letters.charAt(Math.floor(Math.random() * letters.length));
}
return s;
};
util.pickRandom = function (array) {
return array[Math.floor(Math.random() * array.length)];
};
util.mixinEvents = TogetherJS._mixinEvents;
util.Module = util.Class({
constructor: function (name) {
this._name = name;
},
toString: function () {
return '[Module ' + this._name + ']';
}
});
util.blobToBase64 = function (blob) {