Skip to content

Commit ec24113

Browse files
committed
Fucking Apple does not support javascript toLocaleString on iOS. Stupid!
1 parent 75769a0 commit ec24113

File tree

1 file changed

+287
-5
lines changed

1 file changed

+287
-5
lines changed

static/js/terrariumpi.js

Lines changed: 287 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,290 @@ function load_page(url) {
200200
return false;
201201
}
202202

203+
(function() {
204+
'use strict';
205+
206+
// Got this from MDN:
207+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString#Example:_Checking_for_support_for_locales_and_options_arguments
208+
function toLocaleStringSupportsLocales() {
209+
var number = 0;
210+
try {
211+
number.toLocaleString("i");
212+
} catch (e) {
213+
return e.name === "RangeError";
214+
}
215+
return false;
216+
}
217+
218+
if (!toLocaleStringSupportsLocales()) {
219+
var replaceSeparators = function(sNum, separators) {
220+
var sNumParts = sNum.split('.');
221+
if (separators && separators.thousands) {
222+
sNumParts[0] = sNumParts[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1" + separators.thousands);
223+
}
224+
sNum = sNumParts.join(separators.decimal);
225+
226+
return sNum;
227+
};
228+
229+
var renderFormat = function(template, props) {
230+
for (var prop in props) {
231+
template = template.replace("[[" + prop + "]]", props[prop]);
232+
}
233+
234+
return template;
235+
};
236+
237+
var mapMatch = function(map, locale) {
238+
var match = locale;
239+
var language = locale && locale.toLowerCase().match(/^\w+/);
240+
241+
if (!map.hasOwnProperty(locale)) {
242+
if (map.hasOwnProperty(language)) {
243+
match = language;
244+
} else {
245+
match = "en";
246+
}
247+
}
248+
249+
return map[match];
250+
};
251+
252+
var dotThousCommaDec = function(sNum) {
253+
var separators = {
254+
decimal: ',',
255+
thousands: '.'
256+
};
257+
258+
return replaceSeparators(sNum, separators);
259+
};
260+
261+
var commaThousDotDec = function(sNum) {
262+
var separators = {
263+
decimal: '.',
264+
thousands: ','
265+
};
266+
267+
return replaceSeparators(sNum, separators);
268+
};
269+
270+
var spaceThousCommaDec = function(sNum) {
271+
var seperators = {
272+
decimal: ',',
273+
thousands: '\u00A0'
274+
};
275+
276+
return replaceSeparators(sNum, seperators);
277+
};
278+
279+
var apostrophThousDotDec = function(sNum) {
280+
var seperators = {
281+
decimal: '.',
282+
thousands: '\u0027'
283+
};
284+
285+
return replaceSeparators(sNum, seperators);
286+
};
287+
288+
var transformForLocale = {
289+
en: commaThousDotDec,
290+
it: dotThousCommaDec,
291+
fr: spaceThousCommaDec,
292+
de: dotThousCommaDec,
293+
nl: dotThousCommaDec,
294+
"de-DE": dotThousCommaDec,
295+
"de-AT": dotThousCommaDec,
296+
"de-CH": apostrophThousDotDec,
297+
"de-LI": apostrophThousDotDec,
298+
"de-BE": dotThousCommaDec,
299+
ro: dotThousCommaDec,
300+
"ro-RO": dotThousCommaDec,
301+
hu: spaceThousCommaDec,
302+
"hu-HU": spaceThousCommaDec,
303+
"da-DK": dotThousCommaDec,
304+
"nb-NO": spaceThousCommaDec
305+
};
306+
307+
var currencyFormatMap = {
308+
en: "pre",
309+
it: "post",
310+
fr: "post",
311+
de: "post",
312+
nl: "prespace",
313+
"de-DE": "post",
314+
"de-AT": "prespace",
315+
"de-CH": "prespace",
316+
"de-LI": "post",
317+
"de-BE": "post",
318+
ro: "post",
319+
"ro-RO": "post",
320+
hu: "post",
321+
"hu-HU": "post",
322+
"da-DK": "post",
323+
"nb-NO": "post"
324+
};
325+
326+
var currencySymbols = {
327+
"afn": "؋",
328+
"ars": "$",
329+
"awg": "ƒ",
330+
"aud": "$",
331+
"azn": "₼",
332+
"bsd": "$",
333+
"bbd": "$",
334+
"byr": "p.",
335+
"bzd": "BZ$",
336+
"bmd": "$",
337+
"bob": "Bs.",
338+
"bam": "KM",
339+
"bwp": "P",
340+
"bgn": "лв",
341+
"brl": "R$",
342+
"bnd": "$",
343+
"khr": "៛",
344+
"cad": "$",
345+
"kyd": "$",
346+
"clp": "$",
347+
"cny": "¥",
348+
"cop": "$",
349+
"crc": "₡",
350+
"hrk": "kn",
351+
"cup": "₱",
352+
"czk": "Kč",
353+
"dkk": "kr",
354+
"dop": "RD$",
355+
"xcd": "$",
356+
"egp": "£",
357+
"svc": "$",
358+
"eek": "kr",
359+
"eur": "€",
360+
"fkp": "£",
361+
"fjd": "$",
362+
"ghc": "¢",
363+
"gip": "£",
364+
"gtq": "Q",
365+
"ggp": "£",
366+
"gyd": "$",
367+
"hnl": "L",
368+
"hkd": "$",
369+
"huf": "Ft",
370+
"isk": "kr",
371+
"inr": "₹",
372+
"idr": "Rp",
373+
"irr": "﷼",
374+
"imp": "£",
375+
"ils": "₪",
376+
"jmd": "J$",
377+
"jpy": "¥",
378+
"jep": "£",
379+
"kes": "KSh",
380+
"kzt": "лв",
381+
"kpw": "₩",
382+
"krw": "₩",
383+
"kgs": "лв",
384+
"lak": "₭",
385+
"lvl": "Ls",
386+
"lbp": "£",
387+
"lrd": "$",
388+
"ltl": "Lt",
389+
"mkd": "ден",
390+
"myr": "RM",
391+
"mur": "₨",
392+
"mxn": "$",
393+
"mnt": "₮",
394+
"mzn": "MT",
395+
"nad": "$",
396+
"npr": "₨",
397+
"ang": "ƒ",
398+
"nzd": "$",
399+
"nio": "C$",
400+
"ngn": "₦",
401+
"nok": "kr",
402+
"omr": "﷼",
403+
"pkr": "₨",
404+
"pab": "B/.",
405+
"pyg": "Gs",
406+
"pen": "S/.",
407+
"php": "₱",
408+
"pln": "zł",
409+
"qar": "﷼",
410+
"ron": "lei",
411+
"rub": "₽",
412+
"shp": "£",
413+
"sar": "﷼",
414+
"rsd": "Дин.",
415+
"scr": "₨",
416+
"sgd": "$",
417+
"sbd": "$",
418+
"sos": "S",
419+
"zar": "R",
420+
"lkr": "₨",
421+
"sek": "kr",
422+
"chf": "CHF",
423+
"srd": "$",
424+
"syp": "£",
425+
"tzs": "TSh",
426+
"twd": "NT$",
427+
"thb": "฿",
428+
"ttd": "TT$",
429+
"try": "",
430+
"trl": "₤",
431+
"tvd": "$",
432+
"ugx": "USh",
433+
"uah": "₴",
434+
"gbp": "£",
435+
"usd": "$",
436+
"uyu": "$U",
437+
"uzs": "лв",
438+
"vef": "Bs",
439+
"vnd": "₫",
440+
"yer": "﷼",
441+
"zwd": "Z$"
442+
};
443+
444+
var currencyFormats = {
445+
pre: "[[code]][[num]]",
446+
post: "[[num]] [[code]]",
447+
prespace: "[[code]] [[num]]"
448+
};
449+
450+
Number.prototype.toLocaleString = function(locale, options) {
451+
if (locale && locale.length < 2)
452+
throw new RangeError("Invalid language tag: " + locale);
453+
454+
var sNum;
455+
if (options && options.minimumFractionDigits) {
456+
sNum = this.toFixed(options.minimumFractionDigits);
457+
} else {
458+
sNum = this.toString();
459+
}
460+
461+
sNum = mapMatch(transformForLocale, locale)(sNum, options);
462+
463+
if(options && options.currency && options.style === "currency") {
464+
var format = currencyFormats[mapMatch(currencyFormatMap, locale)];
465+
if(options.currencyDisplay === "code") {
466+
sNum = renderFormat(format, {
467+
num: sNum,
468+
code: options.currency.toUpperCase()
469+
});
470+
} else {
471+
sNum = renderFormat(format, {
472+
num: sNum,
473+
code: currencySymbols[options.currency.toLowerCase()]
474+
});
475+
}
476+
}
477+
478+
return sNum;
479+
};
480+
}
481+
482+
}());
483+
203484
function formatCurrency(amount,minfrac,maxfrac) {
204-
if (minfrac === undefined) minfrac = 2;
205-
if (maxfrac === undefined) maxfrac = 2;
485+
minfrac = minfrac || 2;
486+
maxfrac = maxfrac || 2;
206487

207488
return (1 * amount).toLocaleString(globals.language.replace('_','-'), {
208489
style: 'currency',
@@ -213,8 +494,9 @@ function formatCurrency(amount,minfrac,maxfrac) {
213494
}
214495

215496
function formatNumber(amount,minfrac,maxfrac) {
216-
if (minfrac === undefined) minfrac = 0;
217-
if (maxfrac === undefined) maxfrac = 3;
497+
minfrac = minfrac || 0;
498+
maxfrac = maxfrac || 3;
499+
218500
return (1 * amount).toLocaleString(globals.language.replace('_','-'), {
219501
minimumFractionDigits: minfrac,
220502
maximumFractionDigits: maxfrac
@@ -1219,7 +1501,7 @@ function add_switch() {
12191501
form.find('input[name="switch_[nr]_dimmer_on_duration"]').val(),
12201502
form.find('input[name="switch_[nr]_dimmer_on_percentage"]').val(),
12211503
form.find('input[name="switch_[nr]_dimmer_off_duration"]').val(),
1222-
form.find('input[name="switch_[nr]_dimmer_off_percentage"]').val(),
1504+
form.find('input[name="switch_[nr]_dimmer_off_percentage"]').val()
12231505
);
12241506

12251507
$('.new-switch-form').modal('hide');

0 commit comments

Comments
 (0)