webstaff: IDL Clone
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / services / idl.js
index 1b47ec1..29b7989 100644 (file)
@@ -21,61 +21,31 @@ 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') {
+        if (typeof old == 'undefined' || old === null) {
             return old;
         } else if (old._isfieldmapper) {
             obj = new service[old.classname]()
-
-            for( var i in old.a ) {
-                var thing = old.a[i];
-                if(thing === null) continue;
-
-                if (typeof thing == 'undefined') {
-                    obj.a[i] = thing;
-                } else if (thing._isfieldmapper) {
-                    obj.a[i] = service.Clone(thing);
-                } else {
-
-                    if(angular.isArray(thing)) {
-                        obj.a[i] = [];
-
-                        for( var j in thing ) {
-
-                            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
-                                obj.a[i][j] = angular.copy(thing[j]);
-                        }
-                    }
-                }
-            }
+            if (old.a) obj.a = service.Clone(old.a, depth); // pass same depth because we're still cloning this same object
         } else {
             if(angular.isArray(old)) {
                 obj = [];
-                for( var j in old ) {
-                    if (typeof old[j] == 'undefined')
-                        obj[j] = old[j];
-                    else if( old[j]._isfieldmapper )
-                        obj[j] = service.Clone(old[j]);
-                    else
-                        obj[j] = angular.copy(old[j]);
-                }
             } else if(angular.isObject(old)) {
                 obj = {};
-                for( var j in old ) {
-                    if (typeof old[j] == 'undefined')
-                        obj[j] = old[j];
-                    else if( old[j]._isfieldmapper )
-                        obj[j] = service.Clone(old[j]);
-                    else
-                        obj[j] = angular.copy(old[j]);
-                }
             } else {
-                obj = angular.copy(old);
+                 return angular.copy(old);
+            }
+
+            for( var j in old ) {
+                if (old[j] === null || typeof old[j] == 'undefined') {
+                    obj[j] = old[j];
+                } else if( old[j]._isfieldmapper ) {
+                    if (depth) obj[j] = service.Clone(old[j], depth - 1);
+                } else {
+                    obj[j] = angular.copy(old[j]);
+                }
             }
         }
         return obj;