webstaff: Provide a fieldmapper-aware cloning method
authorMike Rylander <mrylander@gmail.com>
Fri, 4 Sep 2015 17:56:33 +0000 (13:56 -0400)
committerJason Stephenson <jstephenson@mvlc.org>
Mon, 14 Sep 2015 19:44:19 +0000 (15:44 -0400)
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
Signed-off-by: Jason Stephenson <jstephenson@mvlc.org>
Open-ILS/web/js/ui/default/staff/services/idl.js

index 5934063..681a63a 100644 (file)
@@ -20,6 +20,59 @@ angular.module('egCoreMod')
 
     var service = {};
 
+    // Clones data structures containing fieldmapper objects
+    service.Clone = function(old) {
+        var obj;
+        if (old._isfieldmapper) {
+            obj = new service[old.classname]()
+
+            for( var i in old.a ) {
+                var thing = old.a[i];
+                if(thing === null) continue;
+
+                if( thing._isfieldmapper ) {
+                    obj.a[i] = service.Clone(thing);
+                } else {
+
+                    if(angular.isArray(thing)) {
+                        obj.a[i] = [];
+
+                        for( var j in thing ) {
+
+                            if( thing[j]._isfieldmapper )
+                                obj.a[i][j] = service.Clone(thing[j]);
+                            else
+                                obj.a[i][j] = angular.copy(thing[j]);
+                        }
+                    } else {
+                        obj.a[i] = angular.copy(thing);
+                    }
+                }
+            }
+        } else {
+            if(angular.isArray(old)) {
+                obj = [];
+                for( var j in old ) {
+                    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( old[j]._isfieldmapper )
+                        obj[j] = service.Clone(old[j]);
+                    else
+                        obj[j] = angular.copy(old[j]);
+                }
+            } else {
+                obj = angular.copy(old);
+            }
+        }
+        return obj;
+    };
+
     service.parseIDL = function() {
         //console.debug('egIDL.parseIDL()');