]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/fieldmapper/IDL.js
adding back-support for fmclasses
[working/Evergreen.git] / Open-ILS / web / js / dojo / fieldmapper / IDL.js
1 if(!dojo._hasResource["fieldmapper.IDL"]) {
2     dojo.provide("fieldmapper.IDL");
3     dojo.declare('fieldmapper.IDL', null, {
4     
5         _URL_PATH : '/reports/fm_IDL.xml', // XXX locale?
6         // -- just need to set up xmlent and use '/reports/'+dojo.locale+'/fm_IDL.xml'
7         NS_REPORTS : 'http://open-ils.org/spec/opensrf/IDL/reporter/v1',
8         NS_PERSIST : 'http://open-ils.org/spec/opensrf/IDL/persistence/v1',
9         NS_OBJ : 'http://open-ils.org/spec/opensrf/IDL/objects/v1',
10
11         constructor : function(callback, force) {
12             if(!fieldmapper.IDL.fmclasses || force) {
13                 var self = this;
14                 dojo.xhrGet({
15                     url : this._URL_PATH,
16                     handleAs : 'xml',
17                     sync : true,
18                     timeout : 10000,
19                     load : function (response) {
20                         self._parse(response, callback);
21                         fieldmapper.IDL.loaded = true;
22                     },
23                     error : function (response) {
24                         fieldmapper.IDL.loaded = false;
25                         dojo.require('fieldmapper.fmall', true);
26                         if(callback)
27                             callback();
28                     }
29                 });
30             }
31
32             return dojo.require('fieldmapper.Fieldmapper');
33         },
34
35         _parse : function(xmlNode, callback) {
36             var classes = xmlNode.getElementsByTagName('class');
37             var idl = fieldmapper.IDL.fmclasses = {};
38     
39             for(var i = 0; i < classes.length; i++) {
40                 var node = classes[i];
41                 var id = node.getAttribute('id');
42                 var fields = node.getElementsByTagName('fields')[0];
43                 window.fmclasses[id] = [];
44     
45                 var obj = { 
46                     fields  : this._parseFields(node, id),
47                     name    : node.getAttribute('id'),
48                     //table   : node.getAttributeNS(this.NS_PERSIST, 'tablename'),
49                     //core    : node.getAttributeNS(this.NS_REPORTS, 'core'),
50                     label   : node.getAttributeNS(this.NS_REPORTS, 'label'),
51                     virtual : (node.getAttributeNS(this.NS_PERSIST, 'virtual') == 'true'),
52                     pkey    : fields.getAttributeNS(this.NS_PERSIST, 'primary')
53                 };
54
55                 var permacrud = node.getElementsByTagName('permacrud')[0];
56                 if(permacrud) {
57                     var actions = ['create', 'retrieve', 'update', 'delete'];
58                     obj.permacrud = {};
59                     for(var idx in actions) {
60                         var action = actions[idx];
61                         var pnode = permacrud.getElementsByTagName(action)[0];
62                         if(pnode) {
63                             var permString = pnode.getAttribute('permission');
64                             var permList = null;
65                             if(permString)
66                                 permList = (permString.match(/ /)) ? permString.split(' ') : [permString];
67  
68                             var contextString = pnode.getAttribute('context_field');
69                             var contextList = null;
70                             if(contextString)
71                                 contextList = (contextString.match(/ /)) ? contextString.split(' ') : [contextString];
72     
73                             obj.permacrud[action] = { 
74                                 perms : permList,
75                                 localContextFields : contextList // need to add foreign context fields
76                             }; // add more details as necessary
77                         }
78                     }
79                 }
80     
81                 obj.core = (obj.core == 'true');
82                 obj.label = (obj.label) ? obj.label : obj.name;
83                 idl[id] = obj;
84             }
85     
86             if(callback)
87                 callback();
88         },
89     
90         /* parses the links and fields portion of the IDL */
91         _parseFields : function(node, classname) {
92             var data = [];
93     
94             var fields = node.getElementsByTagName('fields')[0];
95             fields = fields.getElementsByTagName('field');
96     
97             var links = node.getElementsByTagName('links')[0];
98             if( links ) links = links.getElementsByTagName('link');
99             else links = [];
100     
101     
102             for(var i = 0; i < fields.length; i++) {
103                 var field = fields[i];
104                 var name = field.getAttribute('name');
105
106                 var obj = {
107                     field : field,
108                     name        : name,
109                     label : field.getAttributeNS(this.NS_REPORTS,'label'),
110                     datatype : field.getAttributeNS(this.NS_REPORTS,'datatype'),
111                     primitive : field.getAttributeNS(this.NS_PERSIST,'primitive'),
112                     selector : field.getAttributeNS(this.NS_REPORTS,'selector'),
113                     array_position : parseInt(field.getAttributeNS(this.NS_OBJ,'array_position')),
114                     type        : 'field',
115                     virtual : (fields[i].getAttributeNS(this.NS_PERSIST, 'virtual') == 'true') 
116                 };
117
118                 obj.label = obj.label || obj.name;
119                 obj.datatype = obj.datatype || 'text';
120
121                 if (obj.array_position > 2)
122                     window.fmclasses[classname].push(obj.name);
123     
124                 var link = null;
125                 for(var l = 0; l < links.length; l++) {
126                     if(links[l].getAttribute('field') == name) {
127                         link = links[l];
128                         break;
129                     }
130                 }
131     
132                 if(link) {
133                     obj.type = 'link';
134                     obj.key = link.getAttribute('key');
135                     obj['class'] = link.getAttribute('class');
136                     obj.reltype = link.getAttribute('reltype');
137                 } 
138     
139                 data.push(obj);
140             }
141     
142             /*
143             data = data.sort(
144                 function(a,b) {
145                     if( a.label > b.label ) return 1;
146                     if( a.label < b.name ) return -1;
147                     return 0;
148                 }
149             );
150             */
151     
152             return data;
153         }
154
155     });
156
157     window.fmclasses = {};
158     fieldmapper.IDL.load = function (callback, force) { return new fieldmapper.IDL(callback, force); };
159     fieldmapper.IDL.loaded = false;
160
161 }
162