LP#1292705 trim unneeded fields from IDL2js
[working/Evergreen.git] / Open-ILS / web / js / dojo / fieldmapper / IDL.js
1 if(!dojo._hasResource["fieldmapper.IDL"]) {
2     dojo._hasResource['fieldmapper.IDL'] = true;
3     dojo.require("DojoSRF");
4     dojo.provide("fieldmapper.IDL");
5     dojo.declare('fieldmapper.IDL', null, {
6     
7         _URL_PATH : '/reports/fm_IDL.xml',
8         NS_REPORTS : 'http://open-ils.org/spec/opensrf/IDL/reporter/v1',
9         NS_PERSIST : 'http://open-ils.org/spec/opensrf/IDL/persistence/v1',
10         NS_OBJ : 'http://open-ils.org/spec/opensrf/IDL/objects/v1',
11
12         constructor : function(classlist) {
13
14             var preload = [];
15             if (window._preload_fieldmapper_IDL) {
16                 if (!fieldmapper.IDL.fmclasses) fieldmapper.IDL.fmclasses = {};
17                 if (!window.fmclasses) window.fmclasses = {};
18
19                 for (var c in window._preload_fieldmapper_IDL) {
20                     preload.push(c);
21                     fieldmapper.IDL.fmclasses[c] = window._preload_fieldmapper_IDL[c];
22
23                     if (!fieldmapper.IDL.fmclasses[c].label) {
24                         fieldmapper.IDL.fmclasses[c].label = 
25                             fieldmapper.IDL.fmclasses[c].name;
26                     }
27
28                     window.fmclasses[c] = [];
29                     dojo.forEach(fieldmapper.IDL.fmclasses[c].fields, function(obj){ 
30                         if (!obj.label) obj.label = obj.name;
31                         window.fmclasses[c].push(obj.name); 
32                     });
33
34                     if (classlist && classlist.length)
35                         classlist = dojo.filter(classlist, function(x){return x != c;});
36                 }
37
38                 fieldmapper.IDL.loaded = true;
39                 window._preload_fieldmapper_IDL = null;
40             }
41
42             if(!fieldmapper.IDL.fmclasses || 
43                     !Object.keys(fieldmapper.IDL.fmclasses).length || 
44                     (classlist && classlist.length)) {
45
46                 var idl_url = this._URL_PATH;
47
48                 if (classlist.length && (classlist.length > 1 || classlist[0] != '*')) {
49                     idl_url += '?';
50
51                     for (var i = 0; i < classlist.length; i++) {
52                         var trim_class = classlist[i];
53                         if (!trim_class) continue;
54                         if (fieldmapper.IDL.fmclasses && fieldmapper.IDL.fmclasses[trim_class]) continue;
55
56                         if (i > 0) idl_url += '&';
57                         idl_url += 'class=' + trim_class;
58                     }
59                 }
60                         
61                 if( !idl_url.match(/\?$/) ) { // make sure we have classes that need loading
62
63                     var self = this;
64                     dojo.xhrGet({
65                         url : idl_url,
66                         handleAs : 'xml',
67                         sync : true,
68                         timeout : 10000,
69                         headers : {"Accept-Language": OpenSRF.locale},
70                         load : function (response) {
71                             self._parse(response);
72                             fieldmapper.IDL.loaded = true;
73                         },
74                         error : function (response) {
75                             fieldmapper.IDL.loaded = false;
76                             dojo.require('fieldmapper.fmall', true);
77                         }
78                     });
79                 }
80             }
81             dojo.require('fieldmapper.Fieldmapper'); 
82
83             if (preload.length)
84                 dojo.forEach( classlist, function (c) { fieldmapper.vivicateClass(c); } );
85
86             if (classlist && classlist.length)
87                 dojo.forEach( classlist, function (c) { fieldmapper.vivicateClass(c); } );
88         },
89
90         _parse : function(xmlNode) {
91             var classes = dojo.query('class',xmlNode);
92             if (!fieldmapper.IDL || !fieldmapper.IDL.fmclasses)
93                 fieldmapper.IDL.fmclasses = {};
94
95             for(var i = 0; i < classes.length; i++) {
96                 var node = classes[i];
97                 var id = node.getAttribute('id');
98                 var fields = dojo.query('fields',node)[0];
99                 window.fmclasses[id] = [];
100                 
101                 var fieldData = this._parseFields(node, id);
102     
103                 var obj = { 
104                     fields  : fieldData.list,
105                     field_map : fieldData.map,
106                     name    : node.getAttribute('id'),
107                     //table   : fieldmapper._getAttributeNS(node,this.NS_PERSIST, 'tablename'),
108                     //core    : fieldmapper._getAttributeNS(node,this.NS_REPORTS, 'core'),
109                     label   : fieldmapper._getAttributeNS(node,this.NS_REPORTS, 'label'),
110                     restrict_primary   : fieldmapper._getAttributeNS(node,this.NS_PERSIST, 'restrict_primary'),
111                     virtual : (fieldmapper._getAttributeNS(node,this.NS_PERSIST, 'virtual') == 'true'),
112                     pkey    : fieldmapper._getAttributeNS(fields,this.NS_PERSIST, 'primary'),
113                     pkey_sequence : fieldmapper._getAttributeNS(fields,this.NS_PERSIST, 'sequence')
114                 };
115
116                 var valid = fieldmapper._getAttributeNS(node,this.NS_OBJ, 'validate');
117                 if (valid) obj.validate = new RegExp( valid.replace(/\\/g, '\\\\') );
118
119                 var permacrud = dojo.query('permacrud',node)[0];
120                 if(permacrud) {
121                     var actions = ['create', 'retrieve', 'update', 'delete'];
122                     obj.permacrud = {};
123                     for(var idx in actions) {
124                         var action = actions[idx];
125                         var pnode = dojo.query(action,permacrud)[0];
126                         if(pnode) {
127                             var permString = pnode.getAttribute('permission');
128                             var permList = null;
129                             if(permString)
130                                 permList = (permString.match(/ /)) ? permString.split(' ') : [permString];
131  
132                             var contextString = pnode.getAttribute('context_field');
133                             var contextList = null;
134                             if(contextString)
135                                 contextList = (contextString.match(/ /)) ? contextString.split(' ') : [contextString];
136     
137                             obj.permacrud[action] = { 
138                                 perms : permList,
139                                 localContextFields : contextList // need to add foreign context fields
140                             }; // add more details as necessary
141                         }
142                     }
143                 }
144     
145                 obj.core = (obj.core == 'true');
146                 obj.label = (obj.label) ? obj.label : obj.name;
147                 fieldmapper.IDL.fmclasses[id] = obj;
148             }
149     
150         },
151     
152         /* parses the links and fields portion of the IDL */
153         _parseFields : function(node, classname) {
154             var data = [];
155             var map = {};
156     
157             var fields = dojo.query('fields',node)[0];
158             fields = dojo.query('field',fields);
159     
160             var links = dojo.query('links',node)[0];
161             if( links ) links = dojo.query('link',links);
162             else links = [];
163     
164     
165             var position = 0;
166             for(var i = 0; i < fields.length; i++) {
167                 var field = fields[i];
168                 var name = field.getAttribute('name');
169
170                 if(name == 'isnew' || name == 'ischanged' || name == 'isdeleted') 
171                     continue;
172
173                 var obj = {
174                     field : field,
175                     name : name,
176                     label : fieldmapper._getAttributeNS(field,this.NS_REPORTS,'label'),
177                     datatype : fieldmapper._getAttributeNS(field,this.NS_REPORTS,'datatype'),
178                     primitive : fieldmapper._getAttributeNS(field,this.NS_PERSIST,'primitive'),
179                     selector : fieldmapper._getAttributeNS(field,this.NS_REPORTS,'selector'),
180                     array_position : position++,
181                     type : 'field',
182                     virtual : (fieldmapper._getAttributeNS(fields[i],this.NS_PERSIST, 'virtual') == 'true'),
183                     required : (fieldmapper._getAttributeNS(fields[i],this.NS_OBJ, 'required') == 'true'),
184                     i18n : (fieldmapper._getAttributeNS(fields[i],this.NS_PERSIST, 'i18n') == 'true')
185                 };
186
187                 obj.label = obj.label || obj.name;
188                 obj.datatype = obj.datatype || 'text';
189
190                 window.fmclasses[classname].push(obj.name);
191     
192                 var link = null;
193                 for(var l = 0; l < links.length; l++) {
194                     if(links[l].getAttribute('field') == name) {
195                         link = links[l];
196                         break;
197                     }
198                 }
199     
200                 if(link) {
201                     obj.type = 'link';
202                     obj.key = link.getAttribute('key');
203                     obj['class'] = link.getAttribute('class');
204                     obj.reltype = link.getAttribute('reltype');
205                 } 
206     
207                 data.push(obj);
208                 map[obj.name] = obj;
209             }
210     
211             dojo.forEach(['isnew', 'ischanged', 'isdeleted'],
212                 function(name) {
213                     var obj = {
214                         name : name,
215                         array_position : position++,
216                         type : 'field',
217                         virtual : true
218                     };
219                     data.push(obj);
220                     map[obj.name] = obj;
221                 }
222             );
223
224             return { list : data, map : map };
225         }
226
227     });
228
229     fieldmapper._getAttributeNS = function (node,ns,attr) {
230         if (node.getAttributeNS) return node.getAttributeNS(ns,attr);
231         return node.getAttribute(attr);
232     };
233
234     window.fmclasses = {};
235     fieldmapper.IDL.load = function (list) {
236         if (!list) list = [];
237         return new fieldmapper.IDL(list);
238     };
239     fieldmapper.IDL.loaded = false;
240
241     JSON2js.fallbackObjectifier = function (arg, key_name, val_name) {
242         console.log("Firing IDL loader for " + arg[key_name]);
243         fieldmapper.IDL.load([arg[key_name]]);
244         return decodeJS(arg);
245     };
246  
247 }
248