0010168: Allow to add a prefix or suffix to a number field
authorAlexander Stintzing <a.stintzing@metaways.de>
Wed, 27 Aug 2014 15:16:54 +0000 (17:16 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Fri, 29 Aug 2014 21:22:04 +0000 (23:22 +0200)
For e.g. using a currency symbol in a number field

https://forge.tine20.org/mantisbt/view.php?id=10168

Change-Id: I6068842dabf67a9f62bc17732b30585f88fb9b4c
Reviewed-on: http://gerrit.tine20.com/customers/1051
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tine20/Tinebase/Tinebase.jsb2
tine20/Tinebase/js/ux/form/NumberField.js [new file with mode: 0644]

index 4c7b625..e3cc87d 100644 (file)
@@ -1,7 +1,7 @@
 {
   "projectName": "Tine 2.0 - Tinebase",
   "deployDir": "Tinebase",
-  "licenseText": "Tine 2.0 - Tinebase \nCopyright (c) 2007-2012 Metaways Infosystems GmbH (http://www.metaways.de)\nhttp://www.gnu.org/licenses/agpl.html AGPL Version 3",
+  "licenseText": "Tine 2.0 - Tinebase \nCopyright (c) 2007-2013 Metaways Infosystems GmbH (http://www.metaways.de)\nhttp://www.gnu.org/licenses/agpl.html AGPL Version 3",
   "resources": [],
   "pkgs": [
     {
           "path": "js/ux/form/"
         },
         {
+          "text": "NumberField.js",
+          "path": "js/ux/form/"
+        },
+        {
           "text": "LayerCombo.js",
           "path": "js/ux/form/"
         },
diff --git a/tine20/Tinebase/js/ux/form/NumberField.js b/tine20/Tinebase/js/ux/form/NumberField.js
new file mode 100644 (file)
index 0000000..27da939
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * Tine 2.0
+ * 
+ * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
+ * @author      Alexander Stintzing <a.stintzing@metaways.de>
+ * @copyright   Copyright (c) 2014 Metaways Infosystems GmbH (http://www.metaways.de)
+ *
+ */
+Ext.ns('Ext.ux', 'Ext.ux.form');
+
+/**
+ * A NumberField allows to force decimals and prefix, suffix handling
+ * 
+ * @namespace   Ext.ux.form
+ * @class       Ext.ux.form.NumberField
+ * @extends     Ext.form.NumberField
+ */
+Ext.ux.form.NumberField = Ext.extend(Ext.form.NumberField, {
+    
+    /**
+     * allows a prefix before the value
+     * 
+     * @type {String}
+     */
+    suffix: null,
+    
+    /**
+     * allows a suffix after the value
+     * 
+     * @type {String}
+     */
+    prefix: null,
+    
+    /**
+     * @see Ext.form.NumberField
+     * 
+     * if decimalSeparator is not ".", the decimals won't be shown if 0 in superclass, so fix it here
+     * 
+     * @param {String} v
+     * @return {Ext.form.ComboBox}
+     */
+    setValue: function(v) {
+        var combo = Ext.ux.form.NumberField.superclass.setValue.call(this, v);
+        var sep = this.decimalSeparator;
+        
+        if (combo) {
+            var split = combo.value.split(sep);
+            
+            if (split.length == 1) {
+                this.value = split[0] + this.decimalSeparator + '00';
+            } else if (String(split[1]).length == 1) {
+                this.value = split[0] + this.decimalSeparator + split[1] + '0';
+            }
+            
+            this.setRawValue((this.prefix ? this.prefix : '') + this.value + (this.suffix ? this.suffix : ''));
+        }
+        
+        return combo;
+    },
+    
+    /**
+     * @see Ext.form.NumberField
+     * 
+     * strips the prefix and suffix before superclass is validating
+     * 
+     * @param {String} value
+     * @return {String}
+     */
+    validateValue: function(value) {
+        if (this.prefix) {
+            var regex = new RegExp(this.prefix, 'g');
+            value = value.replace(regex, '');
+        }
+        
+        if (this.suffix) {
+            var regex = new RegExp(this.suffix, 'g');
+            value = value.replace(regex, '');
+        }
+        
+        return Ext.ux.form.NumberField.superclass.validateValue.call(this, value);
+    }
+});
+
+Ext.reg('extuxnumberfield', Ext.ux.form.NumberField);
\ No newline at end of file