LP1741072: Global String<->Num Directives for ngModel
authorJason Boyer <jboyer@library.in.gov>
Thu, 4 Jan 2018 17:55:16 +0000 (12:55 -0500)
committerBill Erickson <berickxx@gmail.com>
Fri, 5 Jan 2018 15:43:03 +0000 (10:43 -0500)
To increase flexibility the stringToNumber directive
has been separated into int and float versions and
aditional directives are added to convert in the other
direction. The existing use of stringToNumber in the
volume/copy editor are also converted to strToFloat.

Signed-off-by: Jason Boyer <jboyer@library.in.gov>
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/templates/staff/cat/volcopy/t_attr_edit.tt2
Open-ILS/web/js/ui/default/staff/cat/volcopy/app.js
Open-ILS/web/js/ui/default/staff/services/ui.js

index 775c77b..8b61610 100644 (file)
                     </select>
                 </div>
                 <div class="col-md-6" ng-class="{'bg-success': working.price !== undefined}">
-                    <input class="form-control" ng-disabled="!defaults.attributes.price" string-to-number ng-model="working.price" type="number" step="0.01"/>
+                    <input class="form-control" ng-disabled="!defaults.attributes.price" str-to-float ng-model="working.price" type="number" step="0.01"/>
                 </div>
             </div>
 
 
             <div class="row">
                 <div class="col-md-6" ng-class="{'bg-success': working.loan_duration !== undefined}">
-                    <select class="form-control" ng-disabled="!defaults.attributes.loan_duration" ng-model="working.loan_duration">
+                    <select class="form-control" int-to-str ng-disabled="!defaults.attributes.loan_duration" ng-model="working.loan_duration">
                         <option value="1">[% l('Short') %]</option>
                         <option value="2" selected>[% l('Normal') %]</option>
                         <option value="3">[% l('Extended') %]</option>
                     </select>
                 </div>
                 <div class="col-md-6" ng-class="{'bg-success': working.cost !== undefined}">
-                    <input class="form-control" ng-disabled="!defaults.attributes.cost" string-to-number ng-model="working.cost" type="number" step="0.01"/>
+                    <input class="form-control" ng-disabled="!defaults.attributes.cost" str-to-float ng-model="working.cost" type="number" step="0.01"/>
                 </div>
             </div>
 
 
             <div class="row">
                 <div class="col-md-6" ng-class="{'bg-success': working.fine_level !== undefined}">
-                    <select class="form-control" ng-disabled="!defaults.attributes.fine_level" ng-model="working.fine_level">
+                    <select class="form-control" int-to-str ng-disabled="!defaults.attributes.fine_level" ng-model="working.fine_level">
                         <option value="1">[% l('Low') %]</option>
                         <option value="2" selected>[% l('Normal') %]</option>
                         <option value="3">[% l('High') %]</option>
index a9218da..dbafc5a 100644 (file)
@@ -333,11 +333,6 @@ function(egCore , $q) {
                         if ( curr_field["type"] === "stat_cat" ) {
                             stat_cats[field_name] = parseInt(curr_field["value"]);
                         } else {
-                            tmp_val = curr_field['value']; // so... some of the number fields are actually strings. Groovy.
-                            if ( tmp_val.toString().match(/^[-0-9.]+$/) && !(field_name.match(/(?:loan_duration|fine_level)/))) {
-                                tmp_val = parseFloat(tmp_val);
-                            }
-
                             if (field_name.match(/^batch_.*_menulist$/)) {
                                 // special handling for volume fields
                                 if (!("callnumber" in curr_templ)) curr_templ["callnumber"] = {};
@@ -446,21 +441,6 @@ function(egCore , $q) {
     return service;
 }])
 
-.directive('stringToNumber', function() {
-    return {
-        require: 'ngModel',
-        link: function(scope, element, attrs, ngModel) {
-            ngModel.$parsers.push(function(value) {
-                return value;
-            });
-
-            ngModel.$formatters.push(function(value) {
-                return parseFloat(value);
-            });
-        }
-    };
-})
-
 .directive("egVolCopyEdit", function () {
     return {
         restrict: 'E',
index 38f775d..24c6d50 100644 (file)
@@ -73,6 +73,69 @@ function($timeout , $parse) {
     };
 }])
 
+// <select int-to-str ><option value="1">Value</option></select>
+// use integer models for string values
+.directive('intToStr', function() {
+    return {
+        restrict: 'A',
+        require: 'ngModel',
+        link: function(scope, element, attrs, ngModel) {
+            ngModel.$parsers.push(function(value) {
+                return parseInt(value);
+            });
+            ngModel.$formatters.push(function(value) {
+                return '' + value;
+            });
+        }
+    };
+})
+
+// <input str-to-int value="10"/>
+.directive('strToInt', function() {
+    return {
+        restrict: 'A',
+        require: 'ngModel',
+        link: function(scope, element, attrs, ngModel) {
+            ngModel.$parsers.push(function(value) {
+                return '' + value;
+            });
+            ngModel.$formatters.push(function(value) {
+                return parseInt(value);
+            });
+        }
+    };
+})
+
+// <input float-to-str
+.directive('floatToStr', function() {
+    return {
+        restrict: 'A',
+        require: 'ngModel',
+        link: function(scope, element, attrs, ngModel) {
+            ngModel.$parsers.push(function(value) {
+                return parseFloat(value);
+            });
+            ngModel.$formatters.push(function(value) {
+                return '' + value;
+            });
+        }
+    };
+})
+
+.directive('strToFloat', function() {
+    return {
+        restrict: 'A',
+        require: 'ngModel',
+        link: function(scope, element, attrs, ngModel) {
+            ngModel.$parsers.push(function(value) {
+                return '' + value;
+            });
+            ngModel.$formatters.push(function(value) {
+                return parseFloat(value);
+            });
+        }
+    };
+})
 
 // 'reverse' filter 
 // <div ng-repeat="item in items | reverse">{{item.name}}</div>