]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/fieldmapper/Fieldmapper.js
use dojo.query instead of getElementsByTagName ... IE--
[working/Evergreen.git] / Open-ILS / web / js / dojo / fieldmapper / Fieldmapper.js
1 /*
2 # ---------------------------------------------------------------------------
3 # Copyright (C) 2008  Georgia Public Library Service / Equinox Software, Inc
4 # Mike Rylander <miker@esilibrary.com>
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 # ---------------------------------------------------------------------------
16 */
17
18 if(!dojo._hasResource["fieldmapper.Fieldmapper"]){
19
20 /* generate fieldmapper javascript classes.  This expects a global variable
21         called 'fmclasses' to be fleshed with the classes we need to build */
22
23         function FMEX(message) { this.message = message; }
24         FMEX.toString = function() { return "FieldmapperException: " + this.message + "\n"; }
25
26
27         dojo._hasResource["fieldmapper.Fieldmapper"] = true;
28         dojo.provide("fieldmapper.Fieldmapper");
29         dojo.require("DojoSRF");
30
31         dojo.declare( "fieldmapper.Fieldmapper", null, {
32
33                 constructor : function (initArray) {
34                         if (initArray) {
35                                 if (dojo.isArray(initArray)) {
36                                         this.a = initArray;
37                                 } else {
38                                         this.a = [];
39                                 }
40                         }
41                 },
42
43                 _isfieldmapper : true,
44
45                 clone : function() {
46                         var obj = new this.constructor();
47
48                         for( var i in this.a ) {
49                                 var thing = this.a[i];
50                                 if(thing == null) continue;
51
52                                 if( thing._isfieldmapper ) {
53                                         obj.a[i] = thing.clone();
54                                 } else {
55
56                                         if(instanceOf(thing, Array)) {
57                                                 obj.a[i] = new Array();
58
59                                                 for( var j in thing ) {
60
61                                                         if( thing[j]._isfieldmapper )
62                                                                 obj.a[i][j] = thing[j].clone();
63                                                         else
64                                                                 obj.a[i][j] = thing[j];
65                                                 }
66                                         } else {
67                                                 obj.a[i] = thing;
68                                         }
69                                 }
70                         }
71                         return obj;
72                 },
73
74         RequiredField : function (f) {
75             if (!f) return;
76             if (fieldmapper.IDL && fieldmapper.IDL.loaded)
77                 return this.Structure.fields[f].required;
78             return;
79         },
80
81         ValidateField : function (f) {
82             if (!f) return;
83             if (fieldmapper.IDL && fieldmapper.IDL.loaded) {
84                 if (this.Structure.fields[f] && this.Structure.fields[f].validate) {
85                     return this.Structure.fields[f].validate.test(this[f]());
86                 }
87                 return true;
88             }
89             return;
90         }
91             
92
93  
94 /*
95                 isnew : function(n) { if(arguments.length == 1) this.a[0] =n; return this.a[0]; },
96                 ischanged : function(n) { if(arguments.length == 1) this.a[1] =n; return this.a[1]; },
97                 isdeleted : function(n) { if(arguments.length == 1) this.a[2] =n; return this.a[2]; }
98 */
99
100         });
101
102     fieldmapper.vivicateClass = function (cl) {
103                 dojo.provide( cl );
104                 dojo.declare( cl , fieldmapper.Fieldmapper, {
105                         constructor : function () {
106                                 if (!this.a) this.a = [];
107                                 this.classname = this.declaredClass;
108                 this._fields = [];
109
110                 if (fieldmapper.IDL && fieldmapper.IDL.loaded) {
111                     this.Structure = fieldmapper.IDL.fmclasses[this.classname]
112
113                     for (var f in fieldmapper.IDL.fmclasses[this.classname].fields) {
114                         var field = fieldmapper.IDL.fmclasses[this.classname].fields[f];
115                         var p = field.array_position;
116                                 this._fields.push( field.name );
117                                         this[field.name]=new Function('n', 'if(arguments.length==1)this.a['+p+']=n;return this.a['+p+'];');
118                     }
119                 } else {
120                                     this._fields = fmclasses[this.classname];
121
122                                 for( var pos = 0; pos <  this._fields.length; pos++ ) {
123                                         var p = parseInt(pos);
124                                         var f = this._fields[pos];
125                                         this[f]=new Function('n', 'if(arguments.length==1)this.a['+p+']=n;return this.a['+p+'];');
126                                 }
127                 }
128
129                         }
130                 });
131                 fieldmapper[cl] = window[cl]; // alias into place
132         if (fieldmapper.IDL && fieldmapper.IDL.loaded) fieldmapper[cl].Identifier = fieldmapper.IDL.fmclasses[cl].pkey;
133     };
134
135     if (!window.fmclasses) dojo.require("fieldmapper.fmall", true);
136     for( var cl in fmclasses ) {
137         fieldmapper.vivicateClass(cl);
138     }
139
140     // if we were NOT called by the IDL loader ...
141     // XXX This is now deprecated in preference to fieldmapper.AutoIDL
142     if ( !(fieldmapper.IDL && fieldmapper.IDL.loaded) ) {
143
144         fieldmapper.i18n_l.Identifier = 'code';
145         fieldmapper.ccpbt.Identifier = 'code';
146         fieldmapper.ccnbt.Identifier = 'code';
147         fieldmapper.cbrebt.Identifier = 'code';
148         fieldmapper.cubt.Identifier = 'code';
149         fieldmapper.ccm.Identifier = 'code';
150         fieldmapper.cvrfm.Identifier = 'code';
151         fieldmapper.clm.Identifier = 'code';
152         fieldmapper.cam.Identifier = 'code';
153         fieldmapper.cifm.Identifier = 'code';
154         fieldmapper.citm.Identifier = 'code';
155         fieldmapper.cblvl.Identifier = 'code';
156         fieldmapper.clfm.Identifier = 'code';
157         fieldmapper.mous.Identifier = 'usr';
158         fieldmapper.moucs.Identifier = 'usr';
159         fieldmapper.mucs.Identifier = 'usr';
160         fieldmapper.mus.Identifier = 'usr';
161         fieldmapper.rxbt.Identifier = 'xact';
162         fieldmapper.rxpt.Identifier = 'xact';
163         fieldmapper.cxt.Identifier = 'name';
164         fieldmapper.amtr.Identifier = 'matchpoint';
165         fieldmapper.coust.Identifier = 'name';
166
167     }
168
169         fieldmapper._request = function ( meth, staff, params ) {
170                 var ses = OpenSRF.CachedClientSession( meth[0] );
171                 if (!ses) return null;
172
173                 var result = null;
174                 var args = {};
175
176                 if (dojo.isArray(params)) {
177                         args.params = params;
178                 } else {
179
180                         if (dojo.isObject(params)) {
181                                 args = params;
182                         } else {
183                 args.params = [].splice.call(arguments, 1, arguments.length - 1);
184                         }
185
186                 }
187
188         if (!args.async && !args.timeout) args.timeout = 10;
189
190         if(!args.onmethoderror) {
191             args.onmethoderror = function(r, stat, stat_text) {
192                 throw new Error('Method error: ' + r.stat + ' : ' + stat_text);
193             }
194         }
195
196         if(!args.ontransporterror) {
197             args.ontransporterror = function(xreq) {
198                 throw new Error('Transport error method='+args.method+', status=' + xreq.status);
199             }
200         }
201
202                 if (!args.onerror) {
203                         args.onerror = function (r) {
204                                 throw new Error('Request error encountered! ' + r);
205                         }
206                 }
207
208                 if (!args.oncomplete) {
209                         args.oncomplete = function (r) {
210                                 var x = r.recv();
211                                 if (x) result = x.content();
212                         }
213                 }
214
215                 args.method = meth[1];
216                 if (staff && meth[2]) args.method += '.staff';
217
218                 ses.request(args).send();
219
220                 return result;
221         };
222
223         fieldmapper.standardRequest = function (meth, params) { return fieldmapper._request(meth, false, params) };
224         fieldmapper.Fieldmapper.prototype.standardRequest = fieldmapper.standardRequest;
225
226         fieldmapper.staffRequest = function (meth, params) { return fieldmapper._request(meth, true, params) };
227         fieldmapper.Fieldmapper.prototype.staffRequest = fieldmapper.staffRequest;
228
229         fieldmapper.OpenSRF = {};
230
231         /*      Methods are defined as [ service, method, have_staff ]
232                 An optional 3rd component is when a method is followed by true, such methods
233                 have a staff counterpart and should have ".staff" appended to the method 
234                 before the method is called when in XUL mode */
235         fieldmapper.OpenSRF.methods = {
236                 SEARCH_MRS : ['open-ils.search','open-ils.search.metabib.multiclass',true],
237                 SEARCH_RS : ['open-ils.search','open-ils.search.biblio.multiclass',true],
238                 SEARCH_MRS_QUERY : ['open-ils.search','open-ils.search.metabib.multiclass.query',true],
239                 SEARCH_RS_QUERY : ['open-ils.search','open-ils.search.biblio.multiclass.query',true],
240                 FETCH_SEARCH_RIDS : ['open-ils.search','open-ils.search.biblio.record.class.search',true],
241                 FETCH_MRMODS : ['open-ils.search','open-ils.search.biblio.metarecord.mods_slim.retrieve'],
242                 FETCH_MODS_FROM_COPY : ['open-ils.search','open-ils.search.biblio.mods_from_copy'],
243                 FETCH_MR_COPY_COUNTS : ['open-ils.search','open-ils.search.biblio.metarecord.copy_count',true],
244                 FETCH_RIDS : ['open-ils.search','open-ils.search.biblio.metarecord_to_records',true],
245                 FETCH_RMODS : ['open-ils.search','open-ils.search.biblio.record.mods_slim.retrieve'],
246                 FETCH_R_COPY_COUNTS : ['open-ils.search','open-ils.search.biblio.record.copy_count',true],
247                 FETCH_FLESHED_USER : ['open-ils.actor','open-ils.actor.user.fleshed.retrieve'],
248                 FETCH_SESSION : ['open-ils.auth','open-ils.auth.session.retrieve'],
249                 LOGIN_INIT : ['open-ils.auth','open-ils.auth.authenticate.init'],
250                 LOGIN_COMPLETE : ['open-ils.auth','open-ils.auth.authenticate.complete'],
251                 LOGIN_DELETE : ['open-ils.auth','open-ils.auth.session.delete'],
252                 FETCH_USER_PREFS : ['open-ils.actor','open-ils.actor.patron.settings.retrieve'], 
253                 UPDATE_USER_PREFS : ['open-ils.actor','open-ils.actor.patron.settings.update'], 
254                 FETCH_COPY_STATUSES : ['open-ils.search','open-ils.search.config.copy_status.retrieve.all'],
255                 FETCH_COPY_COUNTS_SUMMARY : ['open-ils.search','open-ils.search.biblio.copy_counts.summary.retrieve'],
256                 FETCH_MARC_HTML : ['open-ils.search','open-ils.search.biblio.record.html'],
257                 FETCH_CHECKED_OUT_SUM : ['open-ils.actor','open-ils.actor.user.checked_out'],
258                 FETCH_HOLDS : ['open-ils.circ','open-ils.circ.holds.retrieve'],
259                 FETCH_FINES_SUMMARY : ['open-ils.actor','open-ils.actor.user.fines.summary'],
260                 FETCH_TRANSACTIONS : ['open-ils.actor','open-ils.actor.user.transactions.have_charge.fleshed'],
261                 FETCH_MONEY_BILLING : ['open-ils.circ','open-ils.circ.money.billing.retrieve.all'],
262                 FETCH_CROSSREF : ['open-ils.search','open-ils.search.authority.crossref'],
263                 FETCH_CROSSREF_BATCH : ['open-ils.search','open-ils.search.authority.crossref.batch'],
264                 CREATE_HOLD : ['open-ils.circ','open-ils.circ.holds.create'],
265                 CREATE_HOLD_OVERRIDE : ['open-ils.circ','open-ils.circ.holds.create.override'],
266                 CANCEL_HOLD : ['open-ils.circ','open-ils.circ.hold.cancel'],
267                 UPDATE_USERNAME : ['open-ils.actor','open-ils.actor.user.username.update'],
268                 UPDATE_PASSWORD : ['open-ils.actor','open-ils.actor.user.password.update'],
269                 UPDATE_EMAIL : ['open-ils.actor','open-ils.actor.user.email.update'],
270                 RENEW_CIRC : ['open-ils.circ','open-ils.circ.renew'],
271                 CHECK_SPELL : ['open-ils.search','open-ils.search.spellcheck'],
272                 FETCH_REVIEWS : ['open-ils.search','open-ils.search.added_content.review.retrieve.all'],
273                 FETCH_TOC : ['open-ils.search','open-ils.search.added_content.toc.retrieve'],
274                 FETCH_ACONT_SUMMARY : ['open-ils.search','open-ils.search.added_content.summary.retrieve'],
275                 FETCH_USER_BYBARCODE : ['open-ils.actor','open-ils.actor.user.fleshed.retrieve_by_barcode'],
276                 FETCH_ADV_MARC_MRIDS : ['open-ils.search','open-ils.search.biblio.marc',true],
277                 FETCH_ADV_ISBN_RIDS : ['open-ils.search','open-ils.search.biblio.isbn'],
278                 FETCH_ADV_ISSN_RIDS : ['open-ils.search','open-ils.search.biblio.issn'],
279                 FETCH_ADV_TCN_RIDS : ['open-ils.search','open-ils.search.biblio.tcn'],
280                 FETCH_CNBROWSE : ['open-ils.search','open-ils.search.callnumber.browse'],
281                 FETCH_CONTAINERS : ['open-ils.actor','open-ils.actor.container.retrieve_by_class'],
282                 FETCH_CONTAINERS : ['open-ils.actor','open-ils.actor.container.retrieve_by_class'],
283                 CREATE_CONTAINER : ['open-ils.actor','open-ils.actor.container.create'],
284                 DELETE_CONTAINER : ['open-ils.actor','open-ils.actor.container.full_delete'],
285                 CREATE_CONTAINER_ITEM : ['open-ils.actor','open-ils.actor.container.item.create'],
286                 DELETE_CONTAINER_ITEM : ['open-ils.actor','open-ils.actor.container.item.delete'],
287                 FLESH_CONTAINER : ['open-ils.actor','open-ils.actor.container.flesh'],
288                 FLESH_PUBLIC_CONTAINER : ['open-ils.actor','open-ils.actor.container.public.flesh'],
289                 UPDATE_CONTAINER : ['open-ils.actor','open-ils.actor.container.update'],
290                 FETCH_COPY : ['open-ils.search','open-ils.search.asset.copy.retrieve'],
291                 FETCH_FLESHED_COPY : ['open-ils.search','open-ils.search.asset.copy.fleshed2.retrieve'],
292                 CHECK_HOLD_POSSIBLE : ['open-ils.circ','open-ils.circ.title_hold.is_possible'],
293                 UPDATE_HOLD : ['open-ils.circ','open-ils.circ.hold.update'],
294                 FETCH_COPIES_FROM_VOLUME : ['open-ils.search','open-ils.search.asset.copy.retrieve_by_cn_label',true],
295                 FETCH_VOLUME_BY_INFO : ['open-ils.search','open-ils.search.call_number.retrieve_by_info'], /* XXX staff method? */
296                 FETCH_VOLUME : ['open-ils.search','open-ils.search.asset.call_number.retrieve'],
297                 FETCH_COPY_LOCATIONS : ['open-ils.circ','open-ils.circ.copy_location.retrieve.all'],
298                 FETCH_COPY_NOTES : ['open-ils.circ','open-ils.circ.copy_note.retrieve.all'],
299                 FETCH_COPY_STAT_CATS : ['open-ils.circ','open-ils.circ.asset.stat_cat_entries.fleshed.retrieve_by_copy'],
300                 FETCH_LIT_FORMS : ['open-ils.search','open-ils.search.biblio.lit_form_map.retrieve.all'],
301                 FETCH_ITEM_FORMS : ['open-ils.search','open-ils.search.biblio.item_form_map.retrieve.all'],
302                 FETCH_ITEM_TYPES : ['open-ils.search','open-ils.search.biblio.item_type_map.retrieve.all'],
303                 FETCH_AUDIENCES : ['open-ils.search','open-ils.search.biblio.audience_map.retrieve.all'],
304                 FETCH_HOLD_STATUS : ['open-ils.circ','open-ils.circ.hold.status.retrieve'],
305                 FETCH_NON_CAT_CIRCS : ['open-ils.circ','open-ils.circ.open_non_cataloged_circulation.user'],
306                 FETCH_NON_CAT_CIRC : ['open-ils.circ','open-ils.circ.non_cataloged_circulation.retrieve'],
307                 FETCH_NON_CAT_TYPES : ['open-ils.circ','open-ils.circ.non_cat_types.retrieve.all'],
308                 FETCH_BRE : ['open-ils.search','open-ils.search.biblio.record_entry.slim.retrieve'],
309                 CHECK_USERNAME : ['open-ils.actor','open-ils.actor.username.exists'],
310                 FETCH_CIRC_BY_ID : ['open-ils.circ','open-ils.circ.retrieve'],
311                 FETCH_MR_DESCRIPTORS : ['open-ils.search','open-ils.search.metabib.record_to_descriptors'],
312                 FETCH_HIGHEST_PERM_ORG : ['open-ils.actor','open-ils.actor.user.perm.highest_org.batch'],
313                 FETCH_USER_NOTES : ['open-ils.actor','open-ils.actor.note.retrieve.all'],
314                 FETCH_ORG_BY_SHORTNAME : ['open-ils.actor','open-ils.actor.org_unit.retrieve_by_shortname'],
315                 FETCH_BIB_ID_BY_BARCODE : ['open-ils.search','open-ils.search.bib_id.by_barcode'],
316                 FETCH_ORG_SETTING : ['open-ils.actor','open-ils.actor.ou_setting.ancestor_default'],
317                 FETCH_ORG_SETTING_BATCH : ['open-ils.actor','open-ils.actor.ou_setting.ancestor_default.batch']
318         };
319
320 }
321
322
323