author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Sat, 09 Oct 2010 00:05:52 +0200 | |
changeset 6427 | c8a5ac2d1eaa |
parent 5727 | 29afb9e715bb |
child 6880 | 4be32427b2b9 |
permissions | -rw-r--r-- |
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
1 |
/** |
0 | 2 |
* This file contains extensions for standard javascript types |
3 |
* |
|
4 |
*/ |
|
5 |
||
6 |
ONE_DAY = 86400000; // (in milliseconds) |
|
7 |
// ========== DATE EXTENSIONS ========== /// |
|
8 |
Date.prototype.equals = function(other) { |
|
9 |
/* compare with other date ignoring time differences */ |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
10 |
if (this.getYear() == other.getYear() && this.getMonth() == other.getMonth() && this.getDate() == other.getDate()) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
11 |
return true; |
0 | 12 |
} |
13 |
return false; |
|
2363 | 14 |
}; |
0 | 15 |
|
16 |
Date.prototype.add = function(days) { |
|
2363 | 17 |
var res = new Date(); |
18 |
res.setTime(this.getTime() + (days * ONE_DAY)); |
|
19 |
return res; |
|
2008
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
20 |
}; |
0 | 21 |
|
22 |
Date.prototype.sub = function(days) { |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
23 |
return this.add( - days); |
2008
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
24 |
}; |
0 | 25 |
|
26 |
Date.prototype.iadd = function(days) { |
|
27 |
// in-place add |
|
2363 | 28 |
this.setTime(this.getTime() + (days * ONE_DAY)); |
0 | 29 |
// avoid strange rounding problems !! |
30 |
this.setHours(12); |
|
2008
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
31 |
}; |
0 | 32 |
|
33 |
Date.prototype.isub = function(days) { |
|
34 |
// in-place sub |
|
2363 | 35 |
this.setTime(this.getTime() - (days * ONE_DAY)); |
2008
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
36 |
}; |
0 | 37 |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
38 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
39 |
* .. function:: Date.prototype.nextMonth() |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
40 |
* |
0 | 41 |
* returns the first day of the next month |
42 |
*/ |
|
43 |
Date.prototype.nextMonth = function() { |
|
44 |
if (this.getMonth() == 11) { |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
45 |
var d = new Date(this.getFullYear() + 1, 0, 1); |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
46 |
return d; |
0 | 47 |
} else { |
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
48 |
var d2 = new Date(this.getFullYear(), this.getMonth() + 1, 1); |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
49 |
return d2; |
0 | 50 |
} |
2008
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
51 |
}; |
0 | 52 |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
53 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
54 |
* .. function:: Date.prototype.getRealDay() |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
55 |
* |
0 | 56 |
* returns the day of week, 0 being monday, 6 being sunday |
57 |
*/ |
|
58 |
Date.prototype.getRealDay = function() { |
|
59 |
// getDay() returns 0 for Sunday ==> 6 for Saturday |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
60 |
return (this.getDay() + 6) % 7; |
2008
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
61 |
}; |
0 | 62 |
|
2008
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
63 |
Date.prototype.strftime = function(fmt) { |
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
64 |
if (this.toLocaleFormat !== undefined) { // browser dependent |
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
65 |
return this.toLocaleFormat(fmt); |
2008
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
66 |
} |
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
67 |
// XXX implement at least a decent fallback implementation |
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
68 |
return this.getFullYear() + '/' + (this.getMonth() + 1) + '/' + this.getDate(); |
2008
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
69 |
}; |
0 | 70 |
|
71 |
var _DATE_FORMAT_REGXES = { |
|
72 |
'Y': new RegExp('^-?[0-9]+'), |
|
73 |
'd': new RegExp('^[0-9]{1,2}'), |
|
74 |
'm': new RegExp('^[0-9]{1,2}'), |
|
75 |
'H': new RegExp('^[0-9]{1,2}'), |
|
76 |
'M': new RegExp('^[0-9]{1,2}') |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
77 |
}; |
0 | 78 |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
79 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
80 |
* .. function:: _parseDate(datestring, format) |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
81 |
* |
0 | 82 |
* _parseData does the actual parsing job needed by `strptime` |
83 |
*/ |
|
84 |
function _parseDate(datestring, format) { |
|
85 |
var skip0 = new RegExp('^0*[0-9]+'); |
|
86 |
var parsed = {}; |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
87 |
for (var i1 = 0, i2 = 0; i1 < format.length; i1++, i2++) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
88 |
var c1 = format.charAt(i1); |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
89 |
var c2 = datestring.charAt(i2); |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
90 |
if (c1 == '%') { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
91 |
c1 = format.charAt(++i1); |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
92 |
var data = _DATE_FORMAT_REGXES[c1].exec(datestring.substring(i2)); |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
93 |
if (!data.length) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
94 |
return null; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
95 |
} |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
96 |
data = data[0]; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
97 |
i2 += data.length - 1; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
98 |
var value = parseInt(data, 10); |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
99 |
if (isNaN(value)) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
100 |
return null; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
101 |
} |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
102 |
parsed[c1] = value; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
103 |
continue; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
104 |
} |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
105 |
if (c1 != c2) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
106 |
return null; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
107 |
} |
0 | 108 |
} |
109 |
return parsed; |
|
110 |
} |
|
111 |
||
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
112 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
113 |
* .. function:: strptime(datestring, format) |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
114 |
* |
0 | 115 |
* basic implementation of strptime. The only recognized formats |
116 |
* defined in _DATE_FORMAT_REGEXES (i.e. %Y, %d, %m, %H, %M) |
|
117 |
*/ |
|
118 |
function strptime(datestring, format) { |
|
119 |
var parsed = _parseDate(datestring, format); |
|
120 |
if (!parsed) { |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
121 |
return null; |
0 | 122 |
} |
123 |
// create initial date (!!! year=0 means 1900 !!!) |
|
156 | 124 |
var date = new Date(0, 0, 1, 0, 0); |
0 | 125 |
date.setFullYear(0); // reset to year 0 |
126 |
if (parsed.Y) { |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
127 |
date.setFullYear(parsed.Y); |
0 | 128 |
} |
129 |
if (parsed.m) { |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
130 |
if (parsed.m < 1 || parsed.m > 12) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
131 |
return null; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
132 |
} |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
133 |
// !!! month indexes start at 0 in javascript !!! |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
134 |
date.setMonth(parsed.m - 1); |
0 | 135 |
} |
136 |
if (parsed.d) { |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
137 |
if (parsed.m < 1 || parsed.m > 31) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
138 |
return null; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
139 |
} |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
140 |
date.setDate(parsed.d); |
0 | 141 |
} |
142 |
if (parsed.H) { |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
143 |
if (parsed.H < 0 || parsed.H > 23) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
144 |
return null; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
145 |
} |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
146 |
date.setHours(parsed.H); |
0 | 147 |
} |
148 |
if (parsed.M) { |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
149 |
if (parsed.M < 0 || parsed.M > 59) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
150 |
return null; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
151 |
} |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
152 |
date.setMinutes(parsed.M); |
0 | 153 |
} |
154 |
return date; |
|
155 |
} |
|
156 |
||
157 |
// ========== END OF DATE EXTENSIONS ========== /// |
|
158 |
// ========== STRING EXTENSIONS ========== // |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
159 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
160 |
* .. function:: String.prototype.startswith(prefix) |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
161 |
* |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
162 |
* python-like startsWith method for js strings |
0 | 163 |
* >>> |
164 |
*/ |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
165 |
String.prototype.startswith = function(prefix) { |
0 | 166 |
return this.indexOf(prefix) == 0; |
2363 | 167 |
}; |
0 | 168 |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
169 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
170 |
* .. function:: String.prototype.endswith(suffix) |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
171 |
* |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
172 |
* python-like endsWith method for js strings |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
173 |
*/ |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
174 |
String.prototype.endswith = function(suffix) { |
0 | 175 |
var startPos = this.length - suffix.length; |
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
176 |
if (startPos < 0) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
177 |
return false; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
178 |
} |
0 | 179 |
return this.lastIndexOf(suffix, startPos) == startPos; |
2363 | 180 |
}; |
0 | 181 |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
182 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
183 |
* .. function:: String.prototype.strip() |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
184 |
* |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
185 |
* python-like strip method for js strings |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
186 |
*/ |
0 | 187 |
String.prototype.strip = function() { |
188 |
return this.replace(/^\s*(.*?)\s*$/, "$1"); |
|
189 |
}; |
|
190 |
||
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
191 |
// ========= class factories ========= // |
0 | 192 |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
193 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
194 |
* .. function:: makeUnboundMethod(meth) |
0 | 195 |
* |
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
196 |
* transforms a function into an unbound method |
0 | 197 |
*/ |
198 |
function makeUnboundMethod(meth) { |
|
199 |
function unboundMeth(self) { |
|
5727
29afb9e715bb
[3.9] api update (mostly js) + add support for FCKEditor in formContents js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5658
diff
changeset
|
200 |
var newargs = cw.utils.sliceList(arguments, 1); |
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
201 |
return meth.apply(self, newargs); |
0 | 202 |
} |
203 |
unboundMeth.__name__ = meth.__name__; |
|
204 |
return unboundMeth; |
|
205 |
} |
|
206 |
||
207 |
function attachMethodToClass(cls, methname, meth) { |
|
208 |
meth.__name__ = methname; |
|
209 |
// XXX : this is probably bad for memory usage |
|
210 |
cls.__dict__[methname] = meth; |
|
211 |
cls[methname] = makeUnboundMethod(meth); // for the class itself |
|
212 |
cls.prototype[methname] = meth; // for the instance |
|
213 |
} |
|
214 |
||
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
215 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
216 |
* .. function:: _isAttrSkipped(attrname) |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
217 |
* |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
218 |
* simple internal function that tells if the attribute should |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
219 |
* be copied from baseclasses or not |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
220 |
*/ |
0 | 221 |
function _isAttrSkipped(attrname) { |
222 |
var skipped = ['__class__', '__dict__', '__bases__', 'prototype']; |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
223 |
for (var i = 0; i < skipped.length; i++) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
224 |
if (skipped[i] == attrname) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
225 |
return true; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
226 |
} |
0 | 227 |
} |
228 |
return false; |
|
229 |
} |
|
230 |
||
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
231 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
232 |
* .. function:: makeConstructor(userctor) |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
233 |
* |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
234 |
* internal function used to build the class constructor |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
235 |
*/ |
0 | 236 |
function makeConstructor(userctor) { |
237 |
return function() { |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
238 |
// this is a proxy to user's __init__ |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
239 |
if (userctor) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
240 |
userctor.apply(this, arguments); |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
241 |
} |
0 | 242 |
}; |
243 |
} |
|
244 |
||
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
245 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
246 |
* .. function:: defclass(name, bases, classdict) |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
247 |
* |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
248 |
* this is a js class factory. objects returned by this function behave |
0 | 249 |
* more or less like a python class. The `class` function prototype is |
250 |
* inspired by the python `type` builtin |
|
251 |
* Important notes : |
|
252 |
* -> methods are _STATICALLY_ attached when the class it created |
|
253 |
* -> multiple inheritance was never tested, which means it doesn't work ;-) |
|
254 |
*/ |
|
255 |
function defclass(name, bases, classdict) { |
|
256 |
var baseclasses = bases || []; |
|
257 |
||
258 |
// this is the static inheritance approach (<=> differs from python) |
|
259 |
var basemeths = {}; |
|
260 |
var reverseLookup = []; |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
261 |
for (var i = baseclasses.length - 1; i >= 0; i--) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
262 |
reverseLookup.push(baseclasses[i]); |
0 | 263 |
} |
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
264 |
reverseLookup.push({ |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
265 |
'__dict__': classdict |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
266 |
}); |
0 | 267 |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
268 |
for (var i = 0; i < reverseLookup.length; i++) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
269 |
var cls = reverseLookup[i]; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
270 |
for (prop in cls.__dict__) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
271 |
// XXX hack to avoid __init__, __bases__... |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
272 |
if (!_isAttrSkipped(prop)) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
273 |
basemeths[prop] = cls.__dict__[prop]; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
274 |
} |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
275 |
} |
0 | 276 |
} |
277 |
var userctor = basemeths['__init__']; |
|
2385
8fa16dc4b8c8
[js] shrink htmlhelpers, extract massmailing functionality, jslint
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2363
diff
changeset
|
278 |
var constructor = makeConstructor(userctor); |
0 | 279 |
|
280 |
// python-like interface |
|
281 |
constructor.__name__ = name; |
|
282 |
constructor.__bases__ = baseclasses; |
|
283 |
constructor.__dict__ = {}; |
|
284 |
constructor.prototype.__class__ = constructor; |
|
285 |
// make bound / unbound methods |
|
286 |
for (methname in basemeths) { |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
287 |
attachMethodToClass(constructor, methname, basemeths[methname]); |
0 | 288 |
} |
289 |
||
290 |
return constructor; |
|
291 |
} |