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 service.parseIDL = function() {
24 //console.debug('egIDL.parseIDL()');
26 // retain a copy of the full IDL within the service
27 service.classes = $window._preload_fieldmapper_IDL;
29 // keep this reference around (note: not a clone, just a ref)
30 // so that unit tests, which repeatedly instantiate the
32 //$window._preload_fieldmapper_IDL = null;
35 * Creates the class constructor and getter/setter
36 * methods for each IDL class.
38 function mkclass(cls, fields) {
40 service[cls] = function(seed) {
43 this._isfieldmapper = true;
46 /** creates the getter/setter methods for each field */
47 angular.forEach(fields, function(field, idx) {
48 service[cls].prototype[fields[idx].name] = function(n) {
49 if (arguments.length==1) this.a[idx] = n;
54 // global class constructors required for JSON_v1.js
55 $window[cls] = service[cls];
58 for (var cls in service.classes)
59 mkclass(cls, service.classes[cls].fields);
63 * Generate a hash version of an IDL object.
65 * Flatten determines if nested objects should be squashed into
68 * If 'flatten' is false, e.g.:
70 * {"call_number" : {"label" : "foo"}}
72 * If 'flatten' is true, e.g.:
74 * e.g. {"call_number.label" : "foo"}
76 service.toHash = function(obj, flatten) {
77 if (!angular.isObject(obj)) return obj; // arrays are objects
79 if (angular.isArray(obj)) { // NOTE: flatten arrays not supported
80 return obj.map(function(item) {return service.toHash(item)});
83 var field_names = obj.classname ?
84 Object.keys(service.classes[obj.classname].field_map) :
92 var val = service.toHash(
93 angular.isFunction(obj[field]) ?
94 obj[field]() : obj[field],
98 if (flatten && angular.isObject(val)) {
99 angular.forEach(val, function(sub_val, key) {
100 var fname = field + '.' + key;
101 hash[fname] = sub_val;
104 } else if (val !== undefined) {
113 // Transforms a flattened hash (see toHash() or egGridFlatDataProvider)
116 // e.g. {"call_number.label" : "foo"} => {"call_number":{"label":"foo"}}
117 service.flatToNestedHash = function(obj) {
119 angular.forEach(obj, function(val, key) {
120 var parts = key.split('.');
123 for (var i = 0; i < parts.length; i++) {
125 if (i == parts.length - 1) {
126 sub_hash[part] = val;
131 sub_hash = sub_hash[part];