6 * var aou = new egIDL.aou();
7 * var fullIDL = egIDL.classes;
11 * 1. selector field only appears once per class. We could save
12 * a lot of IDL (network) space storing it only once at the
14 * 2. we don't need to store array_position in /IDL2js since it
15 * can be derived at parse time. Ditto saving space.
17 angular.module('egCoreMod')
19 .factory('egIDL', ['$window', function($window) {
23 // Clones data structures containing fieldmapper objects
24 service.Clone = function(old) {
26 if (typeof old == 'undefined') {
28 } else if (old._isfieldmapper) {
29 obj = new service[old.classname]()
31 for( var i in old.a ) {
33 if(thing === null) continue;
35 if (typeof thing == 'undefined') {
37 } else if (thing._isfieldmapper) {
38 obj.a[i] = service.Clone(thing);
41 if(angular.isArray(thing)) {
44 for( var j in thing ) {
46 if (typeof thing[j] == 'undefined')
47 obj.a[i][j] = thing[j];
48 else if( thing[j]._isfieldmapper )
49 obj.a[i][j] = service.Clone(thing[j]);
51 obj.a[i][j] = angular.copy(thing[j]);
54 obj.a[i] = angular.copy(thing);
59 if(angular.isArray(old)) {
62 if (typeof old[j] == 'undefined')
64 else if( old[j]._isfieldmapper )
65 obj[j] = service.Clone(old[j]);
67 obj[j] = angular.copy(old[j]);
69 } else if(angular.isObject(old)) {
72 if (typeof old[j] == 'undefined')
74 else if( old[j]._isfieldmapper )
75 obj[j] = service.Clone(old[j]);
77 obj[j] = angular.copy(old[j]);
80 obj = angular.copy(old);
86 service.parseIDL = function() {
87 //console.debug('egIDL.parseIDL()');
89 // retain a copy of the full IDL within the service
90 service.classes = $window._preload_fieldmapper_IDL;
92 // keep this reference around (note: not a clone, just a ref)
93 // so that unit tests, which repeatedly instantiate the
95 //$window._preload_fieldmapper_IDL = null;
98 * Creates the class constructor and getter/setter
99 * methods for each IDL class.
101 function mkclass(cls, fields) {
103 service[cls] = function(seed) {
105 this.classname = cls;
106 this._isfieldmapper = true;
109 /** creates the getter/setter methods for each field */
110 angular.forEach(fields, function(field, idx) {
111 service[cls].prototype[fields[idx].name] = function(n) {
112 if (arguments.length==1) this.a[idx] = n;
117 // global class constructors required for JSON_v1.js
118 $window[cls] = service[cls];
121 for (var cls in service.classes)
122 mkclass(cls, service.classes[cls].fields);
126 * Generate a hash version of an IDL object.
128 * Flatten determines if nested objects should be squashed into
129 * the top-level hash.
131 * If 'flatten' is false, e.g.:
133 * {"call_number" : {"label" : "foo"}}
135 * If 'flatten' is true, e.g.:
137 * e.g. {"call_number.label" : "foo"}
139 service.toHash = function(obj, flatten) {
140 if (!angular.isObject(obj)) return obj; // arrays are objects
142 if (angular.isArray(obj)) { // NOTE: flatten arrays not supported
143 return obj.map(function(item) {return service.toHash(item)});
146 var field_names = obj.classname ?
147 Object.keys(service.classes[obj.classname].field_map) :
155 var val = service.toHash(
156 angular.isFunction(obj[field]) ?
157 obj[field]() : obj[field],
161 if (flatten && angular.isObject(val)) {
162 angular.forEach(val, function(sub_val, key) {
163 var fname = field + '.' + key;
164 hash[fname] = sub_val;
167 } else if (val !== undefined) {
176 // Transforms a flattened hash (see toHash() or egGridFlatDataProvider)
179 // e.g. {"call_number.label" : "foo"} => {"call_number":{"label":"foo"}}
180 service.flatToNestedHash = function(obj) {
182 angular.forEach(obj, function(val, key) {
183 var parts = key.split('.');
186 for (var i = 0; i < parts.length; i++) {
188 if (i == parts.length - 1) {
189 sub_hash[part] = val;
194 sub_hash = sub_hash[part];