treat non-idl fields as virtual fields
[working/Evergreen.git] / Open-ILS / web / js / dojo / openils / widget / AutoWidget.js
1 if(!dojo._hasResource['openils.widget.AutoWidget']) {
2     dojo.provide('openils.widget.AutoWidget');
3     dojo.require('dojo.data.ItemFileWriteStore');
4     dojo.require('fieldmapper.dojoData');
5     dojo.require('fieldmapper.IDL');
6
7     // common superclass to auto-generated UIs
8     dojo.declare('openils.widget.AutoWidget', null, {
9
10         fieldOrder : null, // ordered list of field names, optional.
11         sortedFieldList : [], // holds the sorted IDL defs for our fields
12         fmObject : null, // single fielmapper object
13         fmObjectList : null, // list of fieldmapper objects
14         fmClass : '', // our fieldmapper class
15
16         // locates the relevent IDL info
17         initAutoEnv : function() {
18             if(this.fmObjectList && this.fmObjectList.length)
19                 this.fmClass = this.fmObjectList[0].classname;
20             if(this.fmObject)
21                 this.fmClass = this.fmObject.classname;
22             this.fmIDL = fieldmapper.IDL.fmclasses[this.fmClass];
23             this.buildSortedFieldList();
24         },
25
26         buildAutoStore : function() {
27             var list = [];
28             if(this.fmObjectList) {
29                 list = this.fmObjectList;
30             } else {
31                 if(this.fmObject)
32                     list = [this.fmObject];
33             }
34             return new dojo.data.ItemFileWriteStore(
35                 {data:fieldmapper[this.fmClass].toStoreData(list)});
36         },
37
38         buildSortedFieldList : function() {
39             this.sortedFieldList = [];
40
41             if(this.fieldOrder) {
42
43                 var self = this;
44                 dojo.forEach(this.fieldOrder,
45                     function(name) {
46                         var field = self.fmIDL.fields.filter(function(item) { return (item.name == name) } )[0];
47                         if(field) {
48                             self.sortedFieldList.push(field)
49                         } else {
50                             // non-IDL field
51                             self.sortedFieldList.push({name : name, virtual:true});
52                         }
53                     }
54                 );
55
56                 // if the user-defined order does not list all fields, 
57                 // shove the extras on the end.
58                 var anonFields = [];
59                 for(var idx in this.fmIDL.fields)  {
60                     var name = this.fmIDL.fields[idx].name;
61                     if(this.fieldOrder.indexOf(name) < 0) {
62                         anonFields.push(this.fmIDL.fields[idx]);
63                     }
64                 }
65
66                 anonFields = anonFields.sort(
67                     function(a, b) {
68                         if(a.label > b.label) return 1;
69                         if(a.label < b.label) return -1;
70                         return 0;
71                     }
72                 );
73
74                 this.sortedFieldList = this.sortedFieldList.concat(anonFields);
75
76             } else {
77                 // no sort order defined, sort all fields on display label
78
79                 for(var f in this.fmIDL.fields) 
80                     this.sortedFieldList.push(this.fmIDL.fields[f]);
81                 this.sortedFieldList = this.sortedFieldList.sort(
82                     // by default, sort on label
83                     function(a, b) {
84                         if(a.label > b.label) return 1;
85                         if(a.label < b.label) return -1;
86                         return 0;
87                     }
88                 );
89             } 
90         },
91     });
92 }
93