summaryrefslogtreecommitdiff
path: root/frontend/gamma/js/MochiKit/Format.js
Unidiff
Diffstat (limited to 'frontend/gamma/js/MochiKit/Format.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/gamma/js/MochiKit/Format.js16
1 files changed, 8 insertions, 8 deletions
diff --git a/frontend/gamma/js/MochiKit/Format.js b/frontend/gamma/js/MochiKit/Format.js
index 122845e..58877e7 100644
--- a/frontend/gamma/js/MochiKit/Format.js
+++ b/frontend/gamma/js/MochiKit/Format.js
@@ -1,309 +1,309 @@
1/*** 1/***
2 2
3MochiKit.Format 1.5 3MochiKit.Format 1.5
4 4
5See <http://mochikit.com/> for documentation, downloads, license, etc. 5See <http://mochikit.com/> for documentation, downloads, license, etc.
6 6
7(c) 2005 Bob Ippolito. All rights Reserved. 7(c) 2005 Bob Ippolito. All rights Reserved.
8 8
9***/ 9***/
10 10
11MochiKit.Base._module('Format', '1.5', ['Base']); 11MochiKit.Base.module(MochiKit, 'Format', '1.5', ['Base']);
12 12
13MochiKit.Format._numberFormatter = function (placeholder, header, footer, locale, isPercent, precision, leadingZeros, separatorAt, trailingZeros) { 13MochiKit.Format._numberFormatter = function (placeholder, header, footer, locale, isPercent, precision, leadingZeros, separatorAt, trailingZeros) {
14 return function (num) { 14 return function (num) {
15 num = parseFloat(num); 15 num = parseFloat(num);
16 if (typeof(num) == "undefined" || num === null || isNaN(num)) { 16 if (typeof(num) == "undefined" || num === null || isNaN(num)) {
17 return placeholder; 17 return placeholder;
18 } 18 }
19 var curheader = header; 19 var curheader = header;
20 var curfooter = footer; 20 var curfooter = footer;
21 if (num < 0) { 21 if (num < 0) {
22 num = -num; 22 num = -num;
23 } else { 23 } else {
24 curheader = curheader.replace(/-/, ""); 24 curheader = curheader.replace(/-/, "");
25 } 25 }
26 var me = arguments.callee; 26 var me = arguments.callee;
27 var fmt = MochiKit.Format.formatLocale(locale); 27 var fmt = MochiKit.Format.formatLocale(locale);
28 if (isPercent) { 28 if (isPercent) {
29 num = num * 100.0; 29 num = num * 100.0;
30 curfooter = fmt.percent + curfooter; 30 curfooter = fmt.percent + curfooter;
31 } 31 }
32 num = MochiKit.Format.roundToFixed(num, precision); 32 num = MochiKit.Format.roundToFixed(num, precision);
33 var parts = num.split(/\./); 33 var parts = num.split(/\./);
34 var whole = parts[0]; 34 var whole = parts[0];
35 var frac = (parts.length == 1) ? "" : parts[1]; 35 var frac = (parts.length == 1) ? "" : parts[1];
36 var res = ""; 36 var res = "";
37 while (whole.length < leadingZeros) { 37 while (whole.length < leadingZeros) {
38 whole = "0" + whole; 38 whole = "0" + whole;
39 } 39 }
40 if (separatorAt) { 40 if (separatorAt) {
41 while (whole.length > separatorAt) { 41 while (whole.length > separatorAt) {
42 var i = whole.length - separatorAt; 42 var i = whole.length - separatorAt;
43 //res = res + fmt.separator + whole.substring(i, whole.length); 43 //res = res + fmt.separator + whole.substring(i, whole.length);
44 res = fmt.separator + whole.substring(i, whole.length) + res; 44 res = fmt.separator + whole.substring(i, whole.length) + res;
45 whole = whole.substring(0, i); 45 whole = whole.substring(0, i);
46 } 46 }
47 } 47 }
48 res = whole + res; 48 res = whole + res;
49 if (precision > 0) { 49 if (precision > 0) {
50 while (frac.length < trailingZeros) { 50 while (frac.length < trailingZeros) {
51 frac = frac + "0"; 51 frac = frac + "0";
52 } 52 }
53 res = res + fmt.decimal + frac; 53 res = res + fmt.decimal + frac;
54 } 54 }
55 return curheader + res + curfooter; 55 return curheader + res + curfooter;
56 }; 56 };
57}; 57};
58 58
59/** @id MochiKit.Format.numberFormatter */ 59/** @id MochiKit.Format.numberFormatter */
60MochiKit.Format.numberFormatter = function (pattern, placeholder/* = "" */, locale/* = "default" */) { 60MochiKit.Format.numberFormatter = function (pattern, placeholder/* = "" */, locale/* = "default" */) {
61 // http://java.sun.com/docs/books/tutorial/i18n/format/numberpattern.html 61 // http://java.sun.com/docs/books/tutorial/i18n/format/numberpattern.html
62 // | 0 | leading or trailing zeros 62 // | 0 | leading or trailing zeros
63 // | # | just the number 63 // | # | just the number
64 // | , | separator 64 // | , | separator
65 // | . | decimal separator 65 // | . | decimal separator
66 // | % | Multiply by 100 and format as percent 66 // | % | Multiply by 100 and format as percent
67 if (typeof(placeholder) == "undefined") { 67 if (typeof(placeholder) == "undefined") {
68 placeholder = ""; 68 placeholder = "";
69 } 69 }
70 var match = pattern.match(/((?:[0#]+,)?[0#]+)(?:\.([0#]+))?(%)?/); 70 var match = pattern.match(/((?:[0#]+,)?[0#]+)(?:\.([0#]+))?(%)?/);
71 if (!match) { 71 if (!match) {
72 throw TypeError("Invalid pattern"); 72 throw TypeError("Invalid pattern");
73 } 73 }
74 var header = pattern.substr(0, match.index); 74 var header = pattern.substr(0, match.index);
75 var footer = pattern.substr(match.index + match[0].length); 75 var footer = pattern.substr(match.index + match[0].length);
76 if (header.search(/-/) == -1) { 76 if (header.search(/-/) == -1) {
77 header = header + "-"; 77 header = header + "-";
78 } 78 }
79 var whole = match[1]; 79 var whole = match[1];
80 var frac = (typeof(match[2]) == "string" && match[2] != "") ? match[2] : ""; 80 var frac = (typeof(match[2]) == "string" && match[2] != "") ? match[2] : "";
81 var isPercent = (typeof(match[3]) == "string" && match[3] != ""); 81 var isPercent = (typeof(match[3]) == "string" && match[3] != "");
82 var tmp = whole.split(/,/); 82 var tmp = whole.split(/,/);
83 var separatorAt; 83 var separatorAt;
84 if (typeof(locale) == "undefined") { 84 if (typeof(locale) == "undefined") {
85 locale = "default"; 85 locale = "default";
86 } 86 }
87 if (tmp.length == 1) { 87 if (tmp.length == 1) {
88 separatorAt = null; 88 separatorAt = null;
89 } else { 89 } else {
90 separatorAt = tmp[1].length; 90 separatorAt = tmp[1].length;
91 } 91 }
92 var leadingZeros = whole.length - whole.replace(/0/g, "").length; 92 var leadingZeros = whole.length - whole.replace(/0/g, "").length;
93 var trailingZeros = frac.length - frac.replace(/0/g, "").length; 93 var trailingZeros = frac.length - frac.replace(/0/g, "").length;
94 var precision = frac.length; 94 var precision = frac.length;
95 var rval = MochiKit.Format._numberFormatter( 95 var rval = MochiKit.Format._numberFormatter(
96 placeholder, header, footer, locale, isPercent, precision, 96 placeholder, header, footer, locale, isPercent, precision,
97 leadingZeros, separatorAt, trailingZeros 97 leadingZeros, separatorAt, trailingZeros
98 ); 98 );
99 var m = MochiKit.Base; 99 var m = MochiKit.Base;
100 if (m) { 100 if (m) {
101 var fn = arguments.callee; 101 var fn = arguments.callee;
102 var args = m.concat(arguments); 102 var args = m.concat(arguments);
103 rval.repr = function () { 103 rval.repr = function () {
104 return [ 104 return [
105 self.NAME, 105 self.NAME,
106 "(", 106 "(",
107 map(m.repr, args).join(", "), 107 m.map(m.repr, args).join(", "),
108 ")" 108 ")"
109 ].join(""); 109 ].join("");
110 }; 110 };
111 } 111 }
112 return rval; 112 return rval;
113}; 113};
114 114
115/** @id MochiKit.Format.formatLocale */ 115/** @id MochiKit.Format.formatLocale */
116MochiKit.Format.formatLocale = function (locale) { 116MochiKit.Format.formatLocale = function (locale) {
117 if (typeof(locale) == "undefined" || locale === null) { 117 if (typeof(locale) == "undefined" || locale === null) {
118 locale = "default"; 118 locale = "default";
119 } 119 }
120 if (typeof(locale) == "string") { 120 if (typeof(locale) == "string") {
121 var rval = MochiKit.Format.LOCALE[locale]; 121 var rval = MochiKit.Format.LOCALE[locale];
122 if (typeof(rval) == "string") { 122 if (typeof(rval) == "string") {
123 rval = arguments.callee(rval); 123 rval = arguments.callee(rval);
124 MochiKit.Format.LOCALE[locale] = rval; 124 MochiKit.Format.LOCALE[locale] = rval;
125 } 125 }
126 return rval; 126 return rval;
127 } else { 127 } else {
128 return locale; 128 return locale;
129 } 129 }
130}; 130};
131 131
132/** @id MochiKit.Format.twoDigitAverage */ 132/** @id MochiKit.Format.twoDigitAverage */
133MochiKit.Format.twoDigitAverage = function (numerator, denominator) { 133MochiKit.Format.twoDigitAverage = function (numerator, denominator) {
134 if (denominator) { 134 if (denominator) {
135 var res = numerator / denominator; 135 var res = numerator / denominator;
136 if (!isNaN(res)) { 136 if (!isNaN(res)) {
137 return MochiKit.Format.twoDigitFloat(res); 137 return MochiKit.Format.twoDigitFloat(res);
138 } 138 }
139 } 139 }
140 return "0"; 140 return "0";
141}; 141};
142 142
143/** @id MochiKit.Format.twoDigitFloat */ 143/** @id MochiKit.Format.twoDigitFloat */
144MochiKit.Format.twoDigitFloat = function (aNumber) { 144MochiKit.Format.twoDigitFloat = function (aNumber) {
145 var res = roundToFixed(aNumber, 2); 145 var res = MochiKit.Format.roundToFixed(aNumber, 2);
146 if (res.indexOf(".00") > 0) { 146 if (res.indexOf(".00") > 0) {
147 return res.substring(0, res.length - 3); 147 return res.substring(0, res.length - 3);
148 } else if (res.charAt(res.length - 1) == "0") { 148 } else if (res.charAt(res.length - 1) == "0") {
149 return res.substring(0, res.length - 1); 149 return res.substring(0, res.length - 1);
150 } else { 150 } else {
151 return res; 151 return res;
152 } 152 }
153}; 153};
154 154
155/** @id MochiKit.Format.lstrip */ 155/** @id MochiKit.Format.lstrip */
156MochiKit.Format.lstrip = function (str, /* optional */chars) { 156MochiKit.Format.lstrip = function (str, /* optional */chars) {
157 str = str + ""; 157 str = str + "";
158 if (typeof(str) != "string") { 158 if (typeof(str) != "string") {
159 return null; 159 return null;
160 } 160 }
161 if (!chars) { 161 if (!chars) {
162 return str.replace(/^\s+/, ""); 162 return str.replace(/^\s+/, "");
163 } else { 163 } else {
164 return str.replace(new RegExp("^[" + chars + "]+"), ""); 164 return str.replace(new RegExp("^[" + chars + "]+"), "");
165 } 165 }
166}; 166};
167 167
168/** @id MochiKit.Format.rstrip */ 168/** @id MochiKit.Format.rstrip */
169MochiKit.Format.rstrip = function (str, /* optional */chars) { 169MochiKit.Format.rstrip = function (str, /* optional */chars) {
170 str = str + ""; 170 str = str + "";
171 if (typeof(str) != "string") { 171 if (typeof(str) != "string") {
172 return null; 172 return null;
173 } 173 }
174 if (!chars) { 174 if (!chars) {
175 return str.replace(/\s+$/, ""); 175 return str.replace(/\s+$/, "");
176 } else { 176 } else {
177 return str.replace(new RegExp("[" + chars + "]+$"), ""); 177 return str.replace(new RegExp("[" + chars + "]+$"), "");
178 } 178 }
179}; 179};
180 180
181/** @id MochiKit.Format.strip */ 181/** @id MochiKit.Format.strip */
182MochiKit.Format.strip = function (str, /* optional */chars) { 182MochiKit.Format.strip = function (str, /* optional */chars) {
183 var self = MochiKit.Format; 183 var self = MochiKit.Format;
184 return self.rstrip(self.lstrip(str, chars), chars); 184 return self.rstrip(self.lstrip(str, chars), chars);
185}; 185};
186 186
187/** @id MochiKit.Format.truncToFixed */ 187/** @id MochiKit.Format.truncToFixed */
188MochiKit.Format.truncToFixed = function (aNumber, precision) { 188MochiKit.Format.truncToFixed = function (aNumber, precision) {
189 var fixed = MochiKit.Format._numberToFixed(aNumber, precision); 189 var fixed = MochiKit.Format._numberToFixed(aNumber, precision);
190 var fracPos = fixed.indexOf("."); 190 var fracPos = fixed.indexOf(".");
191 if (fracPos > 0 && fracPos + precision + 1 < fixed.length) { 191 if (fracPos > 0 && fracPos + precision + 1 < fixed.length) {
192 fixed = fixed.substring(0, fracPos + precision + 1); 192 fixed = fixed.substring(0, fracPos + precision + 1);
193 fixed = MochiKit.Format._shiftNumber(fixed, 0); 193 fixed = MochiKit.Format._shiftNumber(fixed, 0);
194 } 194 }
195 return fixed; 195 return fixed;
196} 196};
197 197
198/** @id MochiKit.Format.roundToFixed */ 198/** @id MochiKit.Format.roundToFixed */
199MochiKit.Format.roundToFixed = function (aNumber, precision) { 199MochiKit.Format.roundToFixed = function (aNumber, precision) {
200 var fixed = MochiKit.Format._numberToFixed(aNumber, precision); 200 var fixed = MochiKit.Format._numberToFixed(aNumber, precision);
201 var fracPos = fixed.indexOf("."); 201 var fracPos = fixed.indexOf(".");
202 if (fracPos > 0 && fracPos + precision + 1 < fixed.length) { 202 if (fracPos > 0 && fracPos + precision + 1 < fixed.length) {
203 var str = MochiKit.Format._shiftNumber(fixed, precision); 203 var str = MochiKit.Format._shiftNumber(fixed, precision);
204 str = MochiKit.Format._numberToFixed(Math.round(parseFloat(str)), 0); 204 str = MochiKit.Format._numberToFixed(Math.round(parseFloat(str)), 0);
205 fixed = MochiKit.Format._shiftNumber(str, -precision); 205 fixed = MochiKit.Format._shiftNumber(str, -precision);
206 } 206 }
207 return fixed; 207 return fixed;
208} 208};
209 209
210/** 210/**
211 * Converts a number to a fixed format string. This function handles 211 * Converts a number to a fixed format string. This function handles
212 * conversion of exponents by shifting the decimal point to the left 212 * conversion of exponents by shifting the decimal point to the left
213 * or the right. It also guarantees a specified minimum number of 213 * or the right. It also guarantees a specified minimum number of
214 * fractional digits (but no maximum). 214 * fractional digits (but no maximum).
215 * 215 *
216 * @param {Number} aNumber the number to convert 216 * @param {Number} aNumber the number to convert
217 * @param {Number} precision the minimum number of decimal digits 217 * @param {Number} precision the minimum number of decimal digits
218 * 218 *
219 * @return {String} the fixed format number string 219 * @return {String} the fixed format number string
220 */ 220 */
221MochiKit.Format._numberToFixed = function (aNumber, precision) { 221MochiKit.Format._numberToFixed = function (aNumber, precision) {
222 var str = aNumber.toString(); 222 var str = aNumber.toString();
223 var parts = str.split(/[eE]/); 223 var parts = str.split(/[eE]/);
224 var exp = (parts.length === 1) ? 0 : parseInt(parts[1]) || 0; 224 var exp = (parts.length === 1) ? 0 : parseInt(parts[1], 10) || 0;
225 var fixed = MochiKit.Format._shiftNumber(parts[0], exp); 225 var fixed = MochiKit.Format._shiftNumber(parts[0], exp);
226 parts = fixed.split(/\./); 226 parts = fixed.split(/\./);
227 var whole = parts[0]; 227 var whole = parts[0];
228 var frac = (parts.length === 1) ? "" : parts[1]; 228 var frac = (parts.length === 1) ? "" : parts[1];
229 while (frac.length < precision) { 229 while (frac.length < precision) {
230 frac += "0"; 230 frac += "0";
231 } 231 }
232 if (frac.length > 0) { 232 if (frac.length > 0) {
233 return whole + "." + frac; 233 return whole + "." + frac;
234 } else { 234 } else {
235 return whole; 235 return whole;
236 } 236 }
237} 237};
238 238
239/** 239/**
240 * Shifts the decimal dot location in a fixed format number string. 240 * Shifts the decimal dot location in a fixed format number string.
241 * This function handles negative values and will add and remove 241 * This function handles negative values and will add and remove
242 * leading and trailing zeros as needed. 242 * leading and trailing zeros as needed.
243 * 243 *
244 * @param {String} num the fixed format number string 244 * @param {String} num the fixed format number string
245 * @param {Number} exp the base-10 exponent to apply 245 * @param {Number} exp the base-10 exponent to apply
246 * 246 *
247 * @return {String} the new fixed format number string 247 * @return {String} the new fixed format number string
248 */ 248 */
249MochiKit.Format._shiftNumber = function (num, exp) { 249MochiKit.Format._shiftNumber = function (num, exp) {
250 var pos = num.indexOf("."); 250 var pos = num.indexOf(".");
251 if (pos < 0) { 251 if (pos < 0) {
252 pos = num.length; 252 pos = num.length;
253 } else { 253 } else {
254 num = num.substring(0, pos) + num.substring(pos + 1); 254 num = num.substring(0, pos) + num.substring(pos + 1);
255 } 255 }
256 pos += exp; 256 pos += exp;
257 while (pos <= 0 || (pos <= 1 && num.charAt(0) === "-")) { 257 while (pos <= 0 || (pos <= 1 && num.charAt(0) === "-")) {
258 if (num.charAt(0) === "-") { 258 if (num.charAt(0) === "-") {
259 num = "-0" + num.substring(1); 259 num = "-0" + num.substring(1);
260 } else { 260 } else {
261 num = "0" + num; 261 num = "0" + num;
262 } 262 }
263 pos++; 263 pos++;
264 } 264 }
265 while (pos > num.length) { 265 while (pos > num.length) {
266 num += "0"; 266 num += "0";
267 } 267 }
268 if (pos < num.length) { 268 if (pos < num.length) {
269 num = num.substring(0, pos) + "." + num.substring(pos); 269 num = num.substring(0, pos) + "." + num.substring(pos);
270 } 270 }
271 while (/^0[^.]/.test(num)) { 271 while (/^0[^.]/.test(num)) {
272 num = num.substring(1); 272 num = num.substring(1);
273 } 273 }
274 while (/^-0[^.]/.test(num)) { 274 while (/^-0[^.]/.test(num)) {
275 num = "-" + num.substring(2); 275 num = "-" + num.substring(2);
276 } 276 }
277 return num; 277 return num;
278} 278};
279 279
280/** @id MochiKit.Format.percentFormat */ 280/** @id MochiKit.Format.percentFormat */
281MochiKit.Format.percentFormat = function (aNumber) { 281MochiKit.Format.percentFormat = function (aNumber) {
282 return MochiKit.Format.twoDigitFloat(100 * aNumber) + '%'; 282 return MochiKit.Format.twoDigitFloat(100 * aNumber) + '%';
283}; 283};
284 284
285MochiKit.Format.LOCALE = { 285MochiKit.Format.LOCALE = {
286 en_US: {separator: ",", decimal: ".", percent: "%"}, 286 en_US: {separator: ",", decimal: ".", percent: "%"},
287 de_DE: {separator: ".", decimal: ",", percent: "%"}, 287 de_DE: {separator: ".", decimal: ",", percent: "%"},
288 pt_BR: {separator: ".", decimal: ",", percent: "%"}, 288 pt_BR: {separator: ".", decimal: ",", percent: "%"},
289 fr_FR: {separator: " ", decimal: ",", percent: "%"}, 289 fr_FR: {separator: " ", decimal: ",", percent: "%"},
290 "default": "en_US", 290 "default": "en_US",
291 __export__: false 291 __export__: false
292}; 292};
293 293
294MochiKit.Format.__new__ = function () { 294MochiKit.Format.__new__ = function () {
295 MochiKit.Base.nameFunctions(this); 295 MochiKit.Base.nameFunctions(this);
296 var base = this.NAME + "."; 296 var base = this.NAME + ".";
297 var k, v, o; 297 var k, v, o;
298 for (k in this.LOCALE) { 298 for (k in this.LOCALE) {
299 o = this.LOCALE[k]; 299 o = this.LOCALE[k];
300 if (typeof(o) == "object") { 300 if (typeof(o) == "object") {
301 o.repr = function () { return this.NAME; }; 301 o.repr = function () { return this.NAME; };
302 o.NAME = base + "LOCALE." + k; 302 o.NAME = base + "LOCALE." + k;
303 } 303 }
304 } 304 }
305}; 305};
306 306
307MochiKit.Format.__new__(); 307MochiKit.Format.__new__();
308 308
309MochiKit.Base._exportSymbols(this, MochiKit.Format); 309MochiKit.Base._exportSymbols(this, MochiKit.Format);