nsedit/jtable/dev/jquery.jtable.utils.js

159 lines
5.1 KiB
JavaScript

/************************************************************************
* Some UTULITY methods used by jTable *
*************************************************************************/
(function ($) {
$.extend(true, $.hik.jtable.prototype, {
/* Gets property value of an object recursively.
*************************************************************************/
_getPropertyOfObject: function (obj, propName) {
if (propName.indexOf('.') < 0) {
return obj[propName];
} else {
var preDot = propName.substring(0, propName.indexOf('.'));
var postDot = propName.substring(propName.indexOf('.') + 1);
return this._getPropertyOfObject(obj[preDot], postDot);
}
},
/* Sets property value of an object recursively.
*************************************************************************/
_setPropertyOfObject: function (obj, propName, value) {
if (propName.indexOf('.') < 0) {
obj[propName] = value;
} else {
var preDot = propName.substring(0, propName.indexOf('.'));
var postDot = propName.substring(propName.indexOf('.') + 1);
this._setPropertyOfObject(obj[preDot], postDot, value);
}
},
/* Inserts a value to an array if it does not exists in the array.
*************************************************************************/
_insertToArrayIfDoesNotExists: function (array, value) {
if ($.inArray(value, array) < 0) {
array.push(value);
}
},
/* Finds index of an element in an array according to given comparision function
*************************************************************************/
_findIndexInArray: function (value, array, compareFunc) {
//If not defined, use default comparision
if (!compareFunc) {
compareFunc = function (a, b) {
return a == b;
};
}
for (var i = 0; i < array.length; i++) {
if (compareFunc(value, array[i])) {
return i;
}
}
return -1;
},
/* Normalizes a number between given bounds or sets to a defaultValue
* if it is undefined
*************************************************************************/
_normalizeNumber: function (number, min, max, defaultValue) {
if (number == undefined || number == null || isNaN(number)) {
return defaultValue;
}
if (number < min) {
return min;
}
if (number > max) {
return max;
}
return number;
},
/* Formats a string just like string.format in c#.
* Example:
* _formatString('Hello {0}','Halil') = 'Hello Halil'
*************************************************************************/
_formatString: function () {
if (arguments.length == 0) {
return null;
}
var str = arguments[0];
for (var i = 1; i < arguments.length; i++) {
var placeHolder = '{' + (i - 1) + '}';
str = str.replace(placeHolder, arguments[i]);
}
return str;
},
/* Checks if given object is a jQuery Deferred object.
*/
_isDeferredObject: function (obj) {
return obj.then && obj.done && obj.fail;
},
//Logging methods ////////////////////////////////////////////////////////
_logDebug: function (text) {
if (!window.console) {
return;
}
console.log('jTable DEBUG: ' + text);
},
_logInfo: function (text) {
if (!window.console) {
return;
}
console.log('jTable INFO: ' + text);
},
_logWarn: function (text) {
if (!window.console) {
return;
}
console.log('jTable WARNING: ' + text);
},
_logError: function (text) {
if (!window.console) {
return;
}
console.log('jTable ERROR: ' + text);
}
});
/* Fix for array.indexOf method in IE7.
* This code is taken from http://www.tutorialspoint.com/javascript/array_indexof.htm */
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (elt) {
var len = this.length;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++) {
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
}
})(jQuery);