diff options
Diffstat (limited to 'lib/scripts/helpers.js')
-rw-r--r-- | lib/scripts/helpers.js | 184 |
1 files changed, 54 insertions, 130 deletions
diff --git a/lib/scripts/helpers.js b/lib/scripts/helpers.js index 129964d29..d6f36967d 100644 --- a/lib/scripts/helpers.js +++ b/lib/scripts/helpers.js @@ -1,134 +1,11 @@ /** - * Differrent helper functions - * - * @author Ilya Lebedev <ilya@lebedev.net> - * @license LGPL - */ -//----------------------------------------------------------------------------- -// Variable/property checks -//----------------------------------------------------------------------------- -/** - * Checks if property is undefined - * - * @param {Object} prop value to check - * @return {Boolean} true if matched - * @scope public - */ -function isUndefined (prop /* :Object */) /* :Boolean */ { - return (typeof prop == 'undefined'); -} -/** - * Checks if property is function - * - * @param {Object} prop value to check - * @return {Boolean} true if matched - * @scope public - */ -function isFunction (prop /* :Object */) /* :Boolean */ { - return (typeof prop == 'function'); -} -/** - * Checks if property is string - * - * @param {Object} prop value to check - * @return {Boolean} true if matched - * @scope public - */ -function isString (prop /* :Object */) /* :Boolean */ { - return (typeof prop == 'string'); -} -/** - * Checks if property is number - * - * @param {Object} prop value to check - * @return {Boolean} true if matched - * @scope public - */ -function isNumber (prop /* :Object */) /* :Boolean */ { - return (typeof prop == 'number'); -} -/** - * Checks if property is the calculable number - * - * @param {Object} prop value to check - * @return {Boolean} true if matched - * @scope public + * Various helper functions */ -function isNumeric (prop /* :Object */) /* :Boolean */ { - return isNumber(prop)&&!isNaN(prop)&&isFinite(prop); -} -/** - * Checks if property is array - * - * @param {Object} prop value to check - * @return {Boolean} true if matched - * @scope public - */ -function isArray (prop /* :Object */) /* :Boolean */ { - return (prop instanceof Array); -} -/** - * Checks if property is regexp - * - * @param {Object} prop value to check - * @return {Boolean} true if matched - * @scope public - */ -function isRegExp (prop /* :Object */) /* :Boolean */ { - return (prop instanceof RegExp); -} -/** - * Checks if property is a boolean value - * - * @param {Object} prop value to check - * @return {Boolean} true if matched - * @scope public - */ -function isBoolean (prop /* :Object */) /* :Boolean */ { - return ('boolean' == typeof prop); -} -/** - * Checks if property is a scalar value (value that could be used as the hash key) - * - * @param {Object} prop value to check - * @return {Boolean} true if matched - * @scope public - */ -function isScalar (prop /* :Object */) /* :Boolean */ { - return isNumeric(prop)||isString(prop); -} -/** - * Checks if property is empty - * - * @param {Object} prop value to check - * @return {Boolean} true if matched - * @scope public - */ -function isEmpty (prop /* :Object */) /* :Boolean */ { - if (isBoolean(prop)) return false; - if (isRegExp(prop) && new RegExp("").toString() == prop.toString()) return true; - if (isString(prop) || isNumber(prop)) return !prop; - if (Boolean(prop)&&false != prop) { - for (var i in prop) if(prop.hasOwnProperty(i)) return false; - } - return true; -} - -/** - * Checks if property is derived from prototype, applies method if it is not exists - * - * @param string property name - * @return bool true if prototyped - * @access public - */ -if ('undefined' == typeof Object.hasOwnProperty) { - Object.prototype.hasOwnProperty = function (prop) { - return !('undefined' == typeof this[prop] || this.constructor && this.constructor.prototype[prop] && this[prop] === this.constructor.prototype[prop]); - }; -} /** * Very simplistic Flash plugin check, probably works for Flash 8 and higher only + * + * @author Andreas Gohr <andi@splitbrain.org> */ function hasFlash(version){ var ver = 0; @@ -136,11 +13,58 @@ function hasFlash(version){ if(navigator.plugins != null && navigator.plugins.length > 0){ ver = navigator.plugins["Shockwave Flash"].description.split(' ')[2].split('.')[0]; }else{ - var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash"); - ver = axo.GetVariable("$version").split(' ')[1].split(',')[0]; + ver = (new ActiveXObject("ShockwaveFlash.ShockwaveFlash")) + .GetVariable("$version").split(' ')[1].split(',')[0]; } }catch(e){ } - if(ver >= version) return true; - return false; + return ver >= version; +} + +/** + * A PHP-style substr_replace + * + * Supports negative start and length and omitting length, but not + * str and replace arrays. + * See http://php.net/substr-replace for further documentation. + */ +function substr_replace(str, replace, start, length) { + var a2, b1; + a2 = (start < 0 ? str.length : 0) + start; + if (typeof length === 'undefined') { + length = str.length - a2; + } else if (length < 0 && start < 0 && length <= start) { + length = 0; + } + b1 = (length < 0 ? str.length : a2) + length; + return str.substring(0, a2) + replace + str.substring(b1); +} + +/** + * Bind variables to a function call creating a closure + * + * Use this to circumvent variable scope problems when creating closures + * inside a loop + * + * @author Adrian Lang <lang@cosmocode.de> + * @link http://www.cosmocode.de/en/blog/gohr/2009-10/15-javascript-fixing-the-closure-scope-in-loops + * @param functionref fnc - the function to be called + * @param mixed - any arguments to be passed to the function + * @returns functionref + */ +function bind(fnc/*, ... */) { + var Aps = Array.prototype.slice, + // Store passed arguments in this scope. + // Since arguments is no Array nor has an own slice method, + // we have to apply the slice method from the Array.prototype + static_args = Aps.call(arguments, 1); + + // Return a function evaluating the passed function with the + // given args and optional arguments passed on invocation. + return function (/* ... */) { + // Same here, but we use Array.prototype.slice solely for + // converting arguments to an Array. + return fnc.apply(this, + static_args.concat(Aps.call(arguments, 0))); + }; } |