Made several improvements to WCF.js
authorTim Düsterhus <timwolla@arcor.de>
Tue, 26 Jul 2011 14:06:43 +0000 (16:06 +0200)
committerTim Düsterhus <timwolla@arcor.de>
Tue, 26 Jul 2011 14:12:00 +0000 (16:12 +0200)
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

wcfsetup/install/files/acp/templates/header.tpl
wcfsetup/install/files/js/WCF.js

index 58e817665ab8a219d39c40bc6a96c2543baed62d..39d24d99ced1f7f62a9b2659d588c54bcc12aba8 100644 (file)
@@ -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}'
                        });
index d226cca5cc8dd33ecef10f6b937ecc0bc50c6fb0..e4e4a12ddb4b38c175309e06141fe55b28135a65 100644 (file)
@@ -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 <http://opensource.org/licenses/lgpl-license.php>
  */
@@ -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