]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/widget/EditPane.js
plugged in option to build cancel and 'apply' buttons. next steps are 1. i18n for...
[working/Evergreen.git] / Open-ILS / web / js / dojo / openils / widget / EditPane.js
1 if(!dojo._hasResource['openils.widget.EditPane']) {
2     dojo.provide('openils.widget.EditPane');
3     dojo.require('openils.widget.AutoWidget');
4     dojo.require('fieldmapper.Fieldmapper');
5     dojo.require('dijit.layout.ContentPane');
6     dojo.require('openils.Util');
7     dojo.require('openils.User');
8     dojo.require('fieldmapper.IDL');
9     dojo.require('openils.PermaCrud');
10
11     dojo.declare(
12         'openils.widget.EditPane',
13         [dijit.layout.ContentPane],
14         {
15             fmClass : '',
16             fmObject : null,
17             mode : 'update',
18             fieldOrder : null, // ordered list of field names, optional.
19             fieldList : [], // holds the field name + associated widget
20             sortedFieldList : [], // holds the sorted IDL defs for our fields
21             onPostApply : null, // apply callback
22             onCancel : null, // cancel callback
23             hideActionButtons : false,
24
25             /**
26              * Builds a basic table of key / value pairs.  Keys are IDL display labels.
27              * Values are dijit's, when values set
28              */
29             startup : function() {
30                 this.inherited(arguments);
31                 this.fmClass = (this.fmObject) ? this.fmObject.classname : this.fmClass;
32                 this.fmIDL = fieldmapper.IDL.fmclasses[this.fmClass];
33
34                 var table = document.createElement('table');
35                 var tbody = document.createElement('tbody');
36                 this.domNode.appendChild(table);
37                 table.appendChild(tbody);
38
39                 this.limitPerms = [];
40                 if(this.fmIDL.permacrud && this.fmIDL.permacrud[this.mode])
41                     this.limitPerms = this.fmIDL.permacrud[this.mode].perms;
42
43                 this._buildSortedFieldList()
44
45                 for(var f in this.sortedFieldList) {
46                     var field = this.sortedFieldList[f];
47                     if(!field || field.virtual) continue;
48
49                     var row = document.createElement('tr');
50                     var nameTd = document.createElement('td');
51                     var valTd = document.createElement('td');
52
53                     nameTd.appendChild(document.createTextNode(field.label));
54                     row.appendChild(nameTd);
55                     row.appendChild(valTd);
56                     tbody.appendChild(row);
57
58                     var widget = new openils.widget.AutoWidget({
59                         idlField : field, 
60                         fmObject : this.fmObject,
61                         parentNode : valTd,
62                         orgLimitPerms : this.limitPerms
63                     });
64                     widget.build();
65                     this.fieldList.push({name:field.name, widget:widget});
66                 }
67                 if(!this.hideActionButtons)
68                     this.buildActionButtons(tbody);
69     
70                 openils.Util.addCSSClass(table, 'oils-fm-edit-dialog');
71             },
72
73             buildActionButtons : function(tbody) {
74                 var row = document.createElement('tr');
75                 var cancelTd = document.createElement('td');
76                 var applyTd = document.createElement('td');
77                 row.appendChild(cancelTd);
78                 row.appendChild(applyTd);
79                 tbody.appendChild(row);
80
81                 var self = this;
82                 new dijit.form.Button({
83                     label:'Cancel', // XXX
84                     onClick : this.onCancel
85                 }, cancelTd);
86
87                 new dijit.form.Button({
88                     label:'Save',  // XXX
89                     onClick: function() {
90                         self.performEditAction({
91                             oncomplete:function() {
92                                 if(self.onPostApply)
93                                     self.onPostApply();
94                             }
95                         });
96                     }
97                 }, applyTd);
98             },
99
100             getFields : function() {
101                 return this.fieldList.map(function(a) { return a.name });
102             },
103
104             getFieldValue : function(field) {
105                 for(var i in this.fieldList) {
106                     if(field == this.fieldList[i].name)
107                         return this.fieldList[i].widget.getFormattedValue();
108                 }
109             },
110
111             _buildSortedFieldList : function() {
112                 this.sortedFieldList = [];
113
114                 if(this.fieldOrder) {
115
116                     for(var idx in this.fieldOrder) {
117                         var name = this.fieldOrder[idx];
118                         for(var idx2 in this.fmIDL.fields) {
119                             if(this.fmIDL.fields[idx2].name == name) {
120                                 this.sortedFieldList.push(this.fmIDL.fields[idx2]);
121                                 break;
122                             }
123                         }
124                     }
125                     
126                     // if the user-defined order does not list all fields, 
127                     // shove the extras on the end.
128                     var anonFields = [];
129                     for(var idx in this.fmIDL.fields)  {
130                         var name = this.fmIDL.fields[idx].name;
131                         if(this.fieldOrder.indexOf(name) < 0) {
132                             anonFields.push(this.fmIDL.fields[idx]);
133                         }
134                     }
135
136                     anonFields = anonFields.sort(
137                         function(a, b) {
138                             if(a.label > b.label) return 1;
139                             if(a.label < b.label) return -1;
140                             return 0;
141                         }
142                     );
143
144                     this.sortedFieldList = this.sortedFieldList.concat(anonFields);
145
146                 } else {
147                     // no sort order defined, sort all fields on display label
148
149                     for(var f in this.fmIDL.fields) 
150                         this.sortedFieldList.push(this.fmIDL.fields[f]);
151                     this.sortedFieldList = this.sortedFieldList.sort(
152                         // by default, sort on label
153                         function(a, b) {
154                             if(a.label > b.label) return 1;
155                             if(a.label < b.label) return -1;
156                             return 0;
157                         }
158                     );
159                 } 
160             },
161
162             performEditAction : function(opts) {
163                 var pcrud = new openils.PermaCrud();
164                 var fields = this.getFields();
165                 if(this.mode == 'create')
166                     this.fmObject = new fieldmapper[this.fmClass]();
167                 for(var idx in fields) 
168                     this.fmObject[fields[idx]](this.getFieldValue(fields[idx]));
169                 pcrud[this.mode](this.fmObject, opts);
170             }
171         }
172     );
173 }
174