1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
/************************************************************************
* 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);
|