浏览代码

HUE-5547 [core] Introduce Knockout maxLength extender for a field

Enrico Berti 9 年之前
父节点
当前提交
740a6d1
共有 1 个文件被更改,包括 54 次插入27 次删除
  1. 54 27
      desktop/core/src/desktop/static/desktop/js/ko.hue-bindings.js

+ 54 - 27
desktop/core/src/desktop/static/desktop/js/ko.hue-bindings.js

@@ -15,6 +15,60 @@
 // limitations under the License.
 
 (function () {
+  ko.extenders.numeric = function (target, config) {
+    var precision = typeof config.precision === 'undefined' ? config : config.precision;
+    var roundingMultiplier = Math.pow(10, precision);
+
+    var result = ko.computed({
+      read: target,
+      write: function (newValue) {
+        var current = target(),
+            newValueAsNum = isNaN(newValue) ? 0 : parseFloat(+newValue),
+            valueToWrite = Math.round(newValueAsNum * roundingMultiplier) / roundingMultiplier;
+
+        if (newValue === '' && config.allowEmpty) {
+          valueToWrite = newValue;
+        }
+        if (valueToWrite !== current) {
+          target(valueToWrite);
+        } else {
+          if (newValue !== current) {
+            target.notifySubscribers(valueToWrite);
+          }
+        }
+      }
+    }).extend({ notify: 'always' });
+    result(target());
+    return result;
+  };
+
+  ko.extenders.maxLength = function (target, maxLength) {
+    var result = ko.computed({
+      read: target,
+      write: function (val) {
+        if (maxLength > 0) {
+          if (val.length > maxLength) {
+            var limitedVal = val.substring(0, maxLength);
+            if (target() === limitedVal) {
+              target.notifySubscribers();
+            }
+            else {
+              target(limitedVal);
+            }
+          }
+          else {
+            target(val);
+          }
+        }
+        else {
+          target(val);
+        }
+      }
+    }).extend({notify: 'always'});
+    result(target());
+    return result;
+  };
+
   ko.observableDefault = function () {
     var prop = arguments[0], defvalue = arguments[1] || null;
     return ko.observable(typeof prop != "undefined" && prop != null ? prop : defvalue);
@@ -842,33 +896,6 @@
     update: function() {}
   };
 
-  ko.extenders.numeric = function (target, config) {
-    var precision = typeof config.precision === 'undefined' ? config : config.precision;
-    var roundingMultiplier = Math.pow(10, precision);
-
-    var result = ko.computed({
-      read: target,
-      write: function (newValue) {
-        var current = target(),
-            newValueAsNum = isNaN(newValue) ? 0 : parseFloat(+newValue),
-            valueToWrite = Math.round(newValueAsNum * roundingMultiplier) / roundingMultiplier;
-
-        if (newValue === '' && config.allowEmpty) {
-          valueToWrite = newValue;
-        }
-        if (valueToWrite !== current) {
-          target(valueToWrite);
-        } else {
-          if (newValue !== current) {
-            target.notifySubscribers(valueToWrite);
-          }
-        }
-      }
-    }).extend({ notify: 'always' });
-    result(target());
-    return result;
-  };
-
   ko.bindingHandlers.numericTextInput = {
     init: function (element, valueAccessor, allBindings) {
       var bindingOptions = ko.unwrap(valueAccessor());