webstaff: Provide a way to get shallow (no nested objects) clones from IDL objects
authorMike Rylander <mrylander@gmail.com>
Thu, 4 May 2017 20:59:07 +0000 (16:59 -0400)
committerGalen Charlton <gmc@equinoxinitiative.org>
Tue, 30 May 2017 16:06:44 +0000 (12:06 -0400)
This will have an impact on the webstaff offline interface, which has a similar
but less advanced implementation of the same.  We want THIS one going forward,
not the one from offline.  HEAD'S UP!

Signed-off-by: Mike Rylander <mrylander@gmail.com>
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Open-ILS/web/js/ui/default/staff/services/idl.js

index 444b820..90a62be 100644 (file)
@@ -21,7 +21,8 @@ angular.module('egCoreMod')
     var service = {};
 
     // Clones data structures containing fieldmapper objects
-    service.Clone = function(old) {
+    service.Clone = function(old, depth) {
+        if (depth === undefined) depth = 100;
         var obj;
         if (typeof old == 'undefined') {
             return old;
@@ -34,23 +35,21 @@ angular.module('egCoreMod')
 
                 if (typeof thing == 'undefined') {
                     obj.a[i] = thing;
-                } else if (thing._isfieldmapper) {
-                    obj.a[i] = service.Clone(thing);
+                } else if (depth && thing._isfieldmapper) {
+                    if (depth) obj.a[i] = service.Clone(thing, depth - 1);
                 } else {
                     if(angular.isArray(thing)) {
                         obj.a[i] = [];
 
-                    if(angular.isArray(thing)) {
-                        obj.a[i] = [];
-
                         for( var j in thing ) {
 
-                            if (typeof thing[j] == 'undefined')
+                            if (typeof thing[j] == 'undefined') {
                                 obj.a[i][j] = thing[j];
-                            else if( thing[j]._isfieldmapper )
-                                obj.a[i][j] = service.Clone(thing[j]);
-                            else
+                            } else if( thing[j]._isfieldmapper ) {
+                                if (depth) obj.a[i][j] = service.Clone(thing[j], depth - 1);
+                            } else {
                                 obj.a[i][j] = angular.copy(thing[j]);
+                            }
                         }
                     } else {
                         obj.a[i] = angular.copy(thing);
@@ -61,22 +60,24 @@ angular.module('egCoreMod')
             if(angular.isArray(old)) {
                 obj = [];
                 for( var j in old ) {
-                    if (typeof old[j] == 'undefined')
+                    if (typeof old[j] == 'undefined') {
                         obj[j] = old[j];
-                    else if( old[j]._isfieldmapper )
-                        obj[j] = service.Clone(old[j], depth - 1);
-                    else
+                    } else if( old[j]._isfieldmapper ) {
+                        if (depth) obj[j] = service.Clone(old[j], depth - 1);
+                    } else {
                         obj[j] = angular.copy(old[j]);
+                    }
                 }
             } else if(angular.isObject(old)) {
                 obj = {};
                 for( var j in old ) {
-                    if (typeof old[j] == 'undefined')
+                    if (typeof old[j] == 'undefined') {
                         obj[j] = old[j];
-                    else if( old[j]._isfieldmapper )
-                        obj[j] = service.Clone(old[j], depth - 1);
-                    else
+                    } else if( old[j]._isfieldmapper ) {
+                        if (depth) obj[j] = service.Clone(old[j], depth - 1);
+                    } else {
                         obj[j] = angular.copy(old[j]);
+                    }
                 }
             } else {
                 obj = angular.copy(old);