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, depth) {
25 if (depth === undefined) depth = 100;
27 if (typeof old == 'undefined' || old === null) {
29 } else if (old._isfieldmapper) {
30 obj = new service[old.classname]()
31 if (old.a) obj.a = service.Clone(old.a, depth); // pass same depth because we're still cloning this same object
33 if(angular.isArray(old)) {
35 } else if(angular.isObject(old)) {
38 return angular.copy(old);
42 if (old[j] === null || typeof old[j] == 'undefined') {
44 } else if( old[j]._isfieldmapper ) {
45 if (depth) obj[j] = service.Clone(old[j], depth - 1);
47 obj[j] = angular.copy(old[j]);
54 service.parseIDL = function() {
55 //console.debug('egIDL.parseIDL()');
57 // retain a copy of the full IDL within the service
58 service.classes = $window._preload_fieldmapper_IDL;
60 // keep this reference around (note: not a clone, just a ref)
61 // so that unit tests, which repeatedly instantiate the
63 //$window._preload_fieldmapper_IDL = null;
66 * Creates the class constructor and getter/setter
67 * methods for each IDL class.
69 function mkclass(cls, fields) {
71 service.classes[cls].core_label = service.classes[cls].core ? 'Core sources' : 'Non-core sources';
72 service.classes[cls].classname = cls;
74 service[cls] = function(seed) {
77 this._isfieldmapper = true;
80 /** creates the getter/setter methods for each field */
81 angular.forEach(fields, function(field, idx) {
82 service[cls].prototype[fields[idx].name] = function(n) {
83 if (arguments.length==1) this.a[idx] = n;
88 // global class constructors required for JSON_v1.js
89 $window[cls] = service[cls];
92 for (var cls in service.classes)
93 mkclass(cls, service.classes[cls].fields);
97 * Generate a hash version of an IDL object.
99 * Flatten determines if nested objects should be squashed into
100 * the top-level hash.
102 * If 'flatten' is false, e.g.:
104 * {"call_number" : {"label" : "foo"}}
106 * If 'flatten' is true, e.g.:
108 * e.g. {"call_number.label" : "foo"}
110 service.toHash = function(obj, flatten) {
111 if (!angular.isObject(obj)) return obj; // arrays are objects
113 if (angular.isArray(obj)) { // NOTE: flatten arrays not supported
114 return obj.map(function(item) {return service.toHash(item)});
117 var field_names = obj.classname ?
118 Object.keys(service.classes[obj.classname].field_map) :
126 var val = service.toHash(
127 angular.isFunction(obj[field]) ?
128 obj[field]() : obj[field],
132 if (flatten && angular.isObject(val)) {
133 angular.forEach(val, function(sub_val, key) {
134 var fname = field + '.' + key;
135 hash[fname] = sub_val;
138 } else if (val !== undefined) {
147 // returns a simple string key=value string of an IDL object.
148 service.toString = function(obj) {
151 service.classes[obj.classname].fields.sort(
152 function(a,b) {return a.name < b.name ? -1 : 1}),
154 s += field.name + '=' + obj[field.name]() + '\n';
160 // hash-to-IDL object translater. Does not support nested values.
161 service.fromHash = function(cls, hash) {
162 if (!service.classes[cls]) {
163 console.error('No such IDL class ' + cls);
167 var new_obj = new service[cls]();
168 angular.forEach(hash, function(val, key) {
169 if (!angular.isFunction(new_obj[key])) return;
170 new_obj[key](hash[key]);
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];
202 // Using IDL links, allow construction of a tree-ish data structure from
203 // the IDL2js web service output. This structure will be directly usable
204 // by the <treecontrol> directive
205 service.classTree = {
209 function _sort_class_fields (a,b) {
210 var aname = a.label || a.name;
211 var bname = b.label || b.name;
212 return aname > bname ? 1 : -1;
215 service.classTree.buildNode = function (cls, args) {
216 if (!cls) return null;
218 var n = service.classes[cls];
222 args = { label : n.label };
226 args.id = args.from + '.' + args.id;
228 return angular.extend( args, {
235 fields : n.fields.sort( _sort_class_fields ),
237 .filter( function(x) { return x.type == 'link'; } )
238 .sort( _sort_class_fields ),
243 service.classTree.fleshNode = function ( node ) {
244 if (node.children.length > 0)
245 return node; // already done already
248 node.links.sort( _sort_class_fields ),
250 var nlabel = n.label ? n.label : n.name;
252 service.classTree.buildNode(
266 service.classTree.setTop = function (cls) {
267 console.debug('setTop: '+cls);
268 return service.classTree.top = service.classTree.fleshNode(
269 service.classTree.buildNode(cls)
273 service.classTree.getTop = function () {
274 return service.classTree.top;