From 52560d9e1cd36973a0c0569890a9a3f0f07da928 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 26 Jul 2011 16:06:43 +0200 Subject: [PATCH] Made several improvements to WCF.js addThousandsSeparator now also works with floats Added formatNumeric for localizing Added WCF.Number for number-related functions Added #-modifier to templates Added Markus as author, with his consent, he created a large part of WCF.js --- .../install/files/acp/templates/header.tpl | 1 + wcfsetup/install/files/js/WCF.js | 112 ++++++++++++------ 2 files changed, 80 insertions(+), 33 deletions(-) diff --git a/wcfsetup/install/files/acp/templates/header.tpl b/wcfsetup/install/files/acp/templates/header.tpl index 58e817665a..39d24d99ce 100644 --- a/wcfsetup/install/files/acp/templates/header.tpl +++ b/wcfsetup/install/files/acp/templates/header.tpl @@ -57,6 +57,7 @@ 'wcf.global.date.dateTimeFormat': '{lang}wcf.global.date.dateTimeFormat{/lang}', '__days': [ '{lang}wcf.global.date.day.sunday{/lang}', '{lang}wcf.global.date.day.monday{/lang}', '{lang}wcf.global.date.day.tuesday{/lang}', '{lang}wcf.global.date.day.wednesday{/lang}', '{lang}wcf.global.date.day.thursday{/lang}', '{lang}wcf.global.date.day.friday{/lang}', '{lang}wcf.global.date.day.saturday{/lang}' ], 'wcf.global.thousandsSeparator': '{capture assign=thousandsSeparator}{lang}wcf.global.thousandsSeparator{/lang}{/capture}{@$thousandsSeparator|encodeJS}', + 'wcf.global.decimalPoint': '{capture assign=decimalPoint}{lang}wcf.global.decimalPoint{/lang}{/capture}{$decimalPoint|encodeJS}', 'wcf.global.page.next': '{capture assign=pageNext}{lang}wcf.global.page.next{/lang}{/capture}{@$pageNext|encodeJS}', 'wcf.global.page.previous': '{capture assign=pagePrevious}{lang}wcf.global.page.previous{/lang}{/capture}{@$pagePrevious|encodeJS}' }); diff --git a/wcfsetup/install/files/js/WCF.js b/wcfsetup/install/files/js/WCF.js index d226cca5cc..e4e4a12ddb 100644 --- a/wcfsetup/install/files/js/WCF.js +++ b/wcfsetup/install/files/js/WCF.js @@ -1,7 +1,7 @@ /** * Class and function collection for WCF * - * @author Tim Düsterhus, Alexander Ebert + * @author Markus Bartz, Tim Düsterhus, Alexander Ebert * @copyright 2001-2011 WoltLab GmbH * @license GNU Lesser General Public License */ @@ -1072,10 +1072,70 @@ WCF.Language = { } }; +/** + * Number utilities. + */ +WCF.Number = { + /** + * Rounds a number to a given number of floating points digits. Defaults to 0. + * + * @param number number + * @param floatingPoint number of digits + * @return number + */ + round: function (number, floatingPoint) { + floatingPoint = Math.pow(10, (floatingPoint || 0)); + + return Math.round(number * floatingPoint) / floatingPoint; + } +} + /** * String utilities. */ WCF.String = { + /** + * Adds thousands separators to a given number. + * + * @param mixed number + * @return string + */ + addThousandsSeparator: function(number) { + var $numberString = String(number); + var parts = $numberString.split(/[^0-9]/); + + var $decimalPoint = $numberString.match(/[^0-9]/g); + + $numberString = parts[0]; + delete parts[0]; + var $decimalPart = $decimalPoint.join('')+parts.join(''); + + if (parseInt(number) >= 1000 || parseInt(number) <= -1000) { + var $negative = false; + if (parseInt(number) <= -1000) { + $negative = true; + $numberString = $numberString.substring(1); + } + var $separator = WCF.Language.get('wcf.global.thousandsSeparator'); + + if ($separator != null && $separator != '') { + var $numElements = new Array(); + var $firstPart = $numberString.length % 3 + if ($firstPart == 0) $firstPart = 3; + for (var $i = 0; $i < Math.ceil($numberString.length / 3); $i++) { + if ($i == 0) $numElements.push($numberString.substring(0, $firstPart)); + else { + var $start = (($i - 1) * 3) + $firstPart + $numElements.push($numberString.substring($start, $start + 3)); + } + } + $numberString = (($negative) ? ('-') : ('')) + $numElements.join($separator); + } + } + + return $numberString + $decimalPart; + }, + /** * Escapes special HTML-characters within a string * @@ -1098,47 +1158,26 @@ WCF.String = { }, /** - * Makes a string's first character uppercase + * Rounds number to given count of floating point digits, localizes decimal-point and inserts thousands-separators * - * @param string string + * @param mixed number * @return string */ - ucfirst: function(string) { - return string.substring(0, 1).toUpperCase() + string.substring(1); + formatNumeric: function(number, floatingPoint) { + number = String(WCF.Number.round(number, floatingPoint || 2)); + number = number.replace('.', WCF.Language.get('wcf.global.decimalPoint')); + + return this.addThousandsSeparator(number); }, /** - * Adds thousands separators to a given number. + * Makes a string's first character uppercase * - * @param mixed number + * @param string string * @return string */ - addThousandsSeparator: function(number) { - var $numberString = String(number); - if (number >= 1000 || number <= -1000) { - var $negative = false; - if (number <= -1000) { - $negative = true; - $numberString = $numberString.substring(1); - } - var $separator = WCF.Language.get('wcf.global.thousandsSeparator'); - - if ($separator != null && $separator != '') { - var $numElements = new Array(); - var $firstPart = $numberString.length % 3 - if ($firstPart == 0) $firstPart = 3; - for (var $i = 0; $i < Math.ceil($numberString.length / 3); $i++) { - if ($i == 0) $numElements.push($numberString.substring(0, $firstPart)); - else { - var $start = (($i - 1) * 3) + $firstPart - $numElements.push($numberString.substring($start, $start + 3)); - } - } - $numberString = (($negative) ? ('-') : ('')) + $numElements.join($separator); - } - } - - return $numberString; + ucfirst: function(string) { + return string.substring(0, 1).toUpperCase() + string.substring(1); } }; @@ -1249,6 +1288,7 @@ WCF.Template.prototype = { // insert them :) for (var $key in $variables) { $result = $result.replace(new RegExp(WCF.String.escapeRegExp('{$'+$key+'}'), 'g'), WCF.String.escapeHTML($variables[$key])); + $result = $result.replace(new RegExp(WCF.String.escapeRegExp('{#$'+$key+'}'), 'g'), WCF.String.formatNumeric($variables[$key])); $result = $result.replace(new RegExp(WCF.String.escapeRegExp('{@$'+$key+'}'), 'g'), $variables[$key]); } @@ -1291,6 +1331,12 @@ WCF.Template.prototype = { // ($name) ? $name : '$match' // -> $v.muh ? $v.muh : '{$muh}' return "' + WCF.String.escapeHTML("+ $name + " ? " + $name + " : '" + $match + "') + '"; + }).replace(/\{#\$(.*?)\}/g, function ($match) { + var $name = '$v.' + $match.substring(3, $match.length - 1); + // trinary operator to maintain compatibility with uncompiled template + // ($name) ? $name : '$match' + // -> $v.muh ? $v.muh : '{$muh}' + return "' + WCF.String.formatNumeric("+ $name + " ? " + $name + " : '" + $match + "') + '"; }).replace(/\{@\$(.*?)\}/g, function ($match) { var $name = '$v.' + $match.substring(3, $match.length - 1); // trinary operator to maintain compatibility with uncompiled template -- 2.20.1