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]);
57 if(angular.isArray(old)) {
60 if (typeof old[j] == 'undefined')
62 else if( old[j]._isfieldmapper )
63 obj[j] = service.Clone(old[j]);
65 obj[j] = angular.copy(old[j]);
67 } else if(angular.isObject(old)) {
70 if (typeof old[j] == 'undefined')
72 else if( old[j]._isfieldmapper )
73 obj[j] = service.Clone(old[j]);
75 obj[j] = angular.copy(old[j]);
78 obj = angular.copy(old);
84 service.parseIDL = function() {
85 //console.debug('egIDL.parseIDL()');
87 // retain a copy of the full IDL within the service
88 service.classes = $window._preload_fieldmapper_IDL;
90 // keep this reference around (note: not a clone, just a ref)
91 // so that unit tests, which repeatedly instantiate the
93 //$window._preload_fieldmapper_IDL = null;
96 * Creates the class constructor and getter/setter
97 * methods for each IDL class.
99 function mkclass(cls, fields) {
101 service.classes[cls].core_label = service.classes[cls].core ? 'Core sources' : 'Non-core sources';
102 service.classes[cls].classname = cls;
104 service[cls] = function(seed) {
106 this.classname = cls;
107 this._isfieldmapper = true;
110 /** creates the getter/setter methods for each field */
111 angular.forEach(fields, function(field, idx) {
112 service[cls].prototype[fields[idx].name] = function(n) {
113 if (arguments.length==1) this.a[idx] = n;
118 // global class constructors required for JSON_v1.js
119 $window[cls] = service[cls];
122 for (var cls in service.classes)
123 mkclass(cls, service.classes[cls].fields);
127 * Generate a hash version of an IDL object.
129 * Flatten determines if nested objects should be squashed into
130 * the top-level hash.
132 * If 'flatten' is false, e.g.:
134 * {"call_number" : {"label" : "foo"}}
136 * If 'flatten' is true, e.g.:
138 * e.g. {"call_number.label" : "foo"}
140 service.toHash = function(obj, flatten) {
141 if (!angular.isObject(obj)) return obj; // arrays are objects
143 if (angular.isArray(obj)) { // NOTE: flatten arrays not supported
144 return obj.map(function(item) {return service.toHash(item)});
147 var field_names = obj.classname ?
148 Object.keys(service.classes[obj.classname].field_map) :
156 var val = service.toHash(
157 angular.isFunction(obj[field]) ?
158 obj[field]() : obj[field],
162 if (flatten && angular.isObject(val)) {
163 angular.forEach(val, function(sub_val, key) {
164 var fname = field + '.' + key;
165 hash[fname] = sub_val;
168 } else if (val !== undefined) {
177 // returns a simple string key=value string of an IDL object.
178 service.toString = function(obj) {
181 service.classes[obj.classname].fields.sort(
182 function(a,b) {return a.name < b.name ? -1 : 1}),
184 s += field.name + '=' + obj[field.name]() + '\n';
190 // hash-to-IDL object translater. Does not support nested values.
191 service.fromHash = function(cls, hash) {
192 if (!service.classes[cls]) {
193 console.error('No such IDL class ' + cls);
197 var new_obj = new service[cls]();
198 angular.forEach(hash, function(val, key) {
199 if (!angular.isFunction(new_obj[key])) return;
200 new_obj[key](hash[key]);
206 // Transforms a flattened hash (see toHash() or egGridFlatDataProvider)
209 // e.g. {"call_number.label" : "foo"} => {"call_number":{"label":"foo"}}
210 service.flatToNestedHash = function(obj) {
212 angular.forEach(obj, function(val, key) {
213 var parts = key.split('.');
216 for (var i = 0; i < parts.length; i++) {
218 if (i == parts.length - 1) {
219 sub_hash[part] = val;
224 sub_hash = sub_hash[part];
232 // Using IDL links, allow construction of a tree-ish data structure from
233 // the IDL2js web service output. This structure will be directly usable
234 // by the <treecontrol> directive
235 service.classTree = {
239 function _sort_class_fields (a,b) {
240 var aname = a.label || a.name;
241 var bname = b.label || b.name;
242 return aname > bname ? 1 : -1;
245 service.classTree.buildNode = function (cls, args) {
246 if (!cls) return null;
248 var n = service.classes[cls];
252 args = { label : n.label };
256 args.id = args.from + '.' + args.id;
258 return angular.extend( args, {
265 fields : n.fields.sort( _sort_class_fields ),
267 .filter( function(x) { return x.type == 'link'; } )
268 .sort( _sort_class_fields ),
273 service.classTree.fleshNode = function ( node ) {
274 if (node.children.length > 0)
275 return node; // already done already
278 node.links.sort( _sort_class_fields ),
280 var nlabel = n.label ? n.label : n.name;
282 service.classTree.buildNode(
296 service.classTree.setTop = function (cls) {
297 console.debug('setTop: '+cls);
298 return service.classTree.top = service.classTree.fleshNode(
299 service.classTree.buildNode(cls)
303 service.classTree.getTop = function () {
304 return service.classTree.top;