LP1713064: String / Float Value ACP Editor Fix
authorJason Boyer <jboyer@library.in.gov>
Fri, 25 Aug 2017 15:13:22 +0000 (11:13 -0400)
committerGalen Charlton <gmc@equinoxinitiative.org>
Fri, 25 Aug 2017 20:06:04 +0000 (16:06 -0400)
Add an Angular directive to the vol/copy editor
to convert and format numbers stored as strings
in acp objects.

Signed-off-by: Jason Boyer <jboyer@library.in.gov>
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Open-ILS/src/templates/staff/cat/volcopy/t_attr_edit.tt2
Open-ILS/web/js/ui/default/staff/cat/volcopy/app.js

index 70fac46..6e40e32 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" ng-model="working.price" type="number"/>
+                    <input class="form-control" ng-disabled="!defaults.attributes.price" string-to-number precision="2" ng-model="working.price" type="number" step="0.01"/>
                 </div>
             </div>
 
                     </select>
                 </div>
                 <div class="col-md-6" ng-class="{'bg-success': working.cost !== undefined}">
-                    <input class="form-control" ng-disabled="!defaults.attributes.cost" ng-model="working.cost" type="number"/>
+                    <input class="form-control" ng-disabled="!defaults.attributes.cost" string-to-number precision="2" ng-model="working.cost" type="number" step="0.01"/>
                 </div>
             </div>
 
                     </div>
                 </div>
                 <div class="col-md-6" ng-class="{'bg-success': working.deposit_amount !== undefined}">
-                    <input class="form-control" ng-disabled="!defaults.attributes.deposit_amount" ng-model="working.deposit_amount" type="number"/>
+                    <input class="form-control" ng-disabled="!defaults.attributes.deposit_amount" string-to-number precision="2" ng-model="working.deposit_amount" type="number" step="0.01"/>
                 </div>
             </div>
 
index 19596b9..285f5e8 100644 (file)
@@ -328,6 +328,34 @@ function(egCore , $q) {
     return service;
 }])
 
+.directive('stringToNumber', function() {
+    return {
+        require: 'ngModel',
+        link: function(scope, element, attrs, ngModel) {
+            var precision = attrs.precision || 0;
+            ngModel.$parsers.push(function(value) {
+                return value;
+            });
+
+            function updateDisplay() {
+                var value = parseFloat(ngModel.$viewValue);
+                if ( isNaN(value) ) { return; }
+                    element.val(value.toFixed(precision));
+            }
+
+            ngModel.$formatters.push(function(value) {
+                return parseFloat(value);
+            });
+
+            ngModel.$viewChangeListeners.push(updateDisplay);
+
+            ngModel.$render = function() {
+                updateDisplay();
+            }
+        }
+    };
+})
+
 .directive("egVolCopyEdit", function () {
     return {
         restrict: 'E',