]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/widget/EditPane.js
e3262defc901d049bf30cf266bdb65a23b47b395
[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('openils.widget.AutoFieldWidget');
5     dojo.require('fieldmapper.Fieldmapper');
6     dojo.require('dijit.layout.ContentPane');
7     dojo.require('openils.Util');
8     dojo.require('openils.PermaCrud');
9     dojo.require('dijit.form.Button');
10
11     dojo.declare(
12         'openils.widget.EditPane',
13         [dijit.layout.ContentPane, openils.widget.AutoWidget],
14         {
15             mode : 'update',
16             onPostSubmit : null, // apply callback
17             onCancel : null, // cancel callback
18             hideActionButtons : false,
19             fieldDocs : null,
20             existingTable : null,
21             suppressFields : null,
22             requiredFields : null,
23             paneStackCount : 1, // how many fields to add to each row, for compressing display
24
25             constructor : function(args) {
26                 this.fieldList = [];
27                 for(var k in args)
28                     this[k] = args[k];
29             },
30
31             /**
32              * Builds a basic table of key / value pairs.  Keys are IDL display labels.
33              * Values are dijit's, when values set
34              */
35             startup : function() {
36                 this.inherited(arguments);
37                 this.initAutoEnv();
38                 if(this.readOnly)
39                     this.hideSaveButton = true;
40
41                 // grab any field-level docs
42                 /*
43                 var pcrud = new openils.PermaCrud();
44                 this.fieldDocs = pcrud.search('fdoc', {fm_class:this.fmClass});
45                 */
46
47                 var table = this.existingTable;
48                 if(!table) {
49                     var table = this.table = document.createElement('table');
50                     this.domNode.appendChild(table);
51                 }
52                 var tbody = document.createElement('tbody');
53                 table.appendChild(tbody);
54
55                 this.limitPerms = [];
56                 if(this.fmIDL.permacrud && this.fmIDL.permacrud[this.mode])
57                     this.limitPerms = this.fmIDL.permacrud[this.mode].perms;
58
59                 if(!this.overrideWidgets)
60                     this.overrideWidgets = {};
61
62                 if(!this.overrideWidgetClass)
63                     this.overrideWidgetClass = {};
64
65                 if(!this.overrideWidgetArgs)
66                     this.overrideWidgetArgs = {};
67
68                 var idx = 0;
69                 var currentRow;
70                 for(var f in this.sortedFieldList) {
71                     var field = this.sortedFieldList[f];
72                     if(!field || field.virtual || field.nonIdl) continue;
73
74                     if(this.suppressFields && this.suppressFields.indexOf(field.name) > -1)
75                         continue;
76
77                     if(field.name == this.fmIDL.pkey && this.mode == 'create' && this.fmIDL.pkey_sequence)
78                         continue; /* don't show auto-generated fields on create */
79
80                     if((idx++ % this.paneStackCount) == 0 || !currentRow) {
81                         // time to start a new row
82                         currentRow = document.createElement('tr');
83                         tbody.appendChild(currentRow);
84                     }
85
86                     //var docTd = document.createElement('td');
87                     var nameTd = document.createElement('td');
88                     var valTd = document.createElement('td');
89                     var valSpan = document.createElement('span');
90                     valTd.appendChild(valSpan);
91                     dojo.addClass(nameTd, 'openils-widget-editpane-name-cell');
92                     dojo.addClass(valTd, 'openils-widget-editpane-value-cell');
93
94                     /*
95                     if(this.fieldDocs[field]) {
96                         var helpLink = dojo.create('a');
97                         var helpImg = dojo.create('img', {src:'/opac/images/advancedsearch-icon.png'}); // TODO Config
98                         helpLink.appendChild(helpImg);
99                         docTd.appendChild(helpLink);
100                     }
101                     */
102
103                     nameTd.appendChild(document.createTextNode(field.label));
104                     currentRow.setAttribute('fmfield', field.name);
105                     //currentRow.appendChild(docTd);
106                     currentRow.appendChild(nameTd);
107                     currentRow.appendChild(valTd);
108                     //dojo.addClass(docTd, 'oils-fm-edit-pane-help');
109
110                     if(!this.overrideWidgetArgs[field.name])
111                         this.overrideWidgetArgs[field.name] = {};
112
113                     var args = dojo.mixin(
114                         {   // defaults
115                             idlField : field, 
116                             fmObject : this.fmObject,
117                             fmClass : this.fmClass,
118                             parentNode : valSpan,
119                             orgLimitPerms : this.limitPerms,
120                             readOnly : this.readOnly,
121                             widget : this.overrideWidgets[field.name],
122                             widgetClass : this.overrideWidgetClass[field.name],
123                             disableWidgetTest : this.disableWidgetTest
124                         },
125                         this.overrideWidgetArgs[field.name] // per-field overrides
126                     );
127
128                     if (this.overrideWidgets[field.name]) {
129                         if (this.overrideWidgets[field.name].shove) {
130                             args.shove = dojo.mixin(
131                                 {"mode": this.mode},
132                                 this.overrideWidgets[field.name].shove
133                             );
134                         }
135                     }
136
137                     if(args.readOnly) {
138                         dojo.addClass(nameTd, 'openils-widget-editpane-ro-name-cell');
139                         dojo.addClass(valTd, 'openils-widget-editpane-ro-value-cell');
140                     }
141
142                     if(this.requiredFields && this.requiredFields.indexOf(field.name) >= 0) {
143                         if(!args.dijitArgs) args.dijitArgs = {};
144                         args.dijitArgs.required = true;
145                     }
146
147                     var widget = new openils.widget.AutoFieldWidget(args);
148
149                     widget.build();
150                     this.fieldList.push({name:field.name, widget:widget});
151                 }
152                 if(!this.hideActionButtons)
153                     this.buildActionButtons(tbody);
154
155                 openils.Util.addCSSClass(table, 'oils-fm-edit-pane');
156             },
157
158             applySaveOnEnter : function(widget) {
159                 var self = this;
160                 dojo.connect(this, 'onKeyDown',
161                     function(e) {
162                         if(e.keyCode == dojo.keys.ENTER) 
163                             self.performAutoEditAction();
164                     }
165                 );
166             },
167
168             buildActionButtons : function(tbody) {
169                 var row = document.createElement('tr');
170                 var cancelTd = document.createElement('td');
171                 var applyTd = document.createElement('td');
172                 var cancelSpan = document.createElement('span');
173                 var applySpan = document.createElement('span');
174                 row.appendChild(cancelTd);
175                 row.appendChild(applyTd);
176                 cancelTd.appendChild(cancelSpan);
177                 applyTd.appendChild(applySpan);
178                 tbody.appendChild(row);
179
180                 var self = this;
181                 new dijit.form.Button({
182                     label:'Cancel', // XXX
183                     onClick : this.onCancel
184                 }, cancelSpan);
185
186                 if(this.hideSaveButton) return;
187
188                 new dijit.form.Button({
189                     label:'Save',  // XXX
190                     onClick: function() {self.performAutoEditAction();}
191                 }, applySpan);
192             },
193
194             getFields : function() {
195                 return this.fieldList.map(function(a) { return a.name });
196             },
197
198             getFieldValue : function(field) {
199                 for(var i in this.fieldList) {
200                     if(field == this.fieldList[i].name) {
201                         var val = this.fieldList[i].widget.getFormattedValue();
202                         if (val == null && /* XXX stricter check needed? */
203                             this.fieldList[i].widget.isRequired()) {
204                             throw new Error("req");
205                         }
206                         return val;
207                     }
208                 }
209             },
210
211             performAutoEditAction : function() {
212                 var self = this;
213                 self.performEditAction({
214                     oncomplete:function(req, cudResults) {
215                         if(self.onPostSubmit)
216                             self.onPostSubmit(req, cudResults);
217                     }
218                 });
219             },
220
221             performEditAction : function(opts) {
222                 var self = this;
223                 var fields = this.getFields();
224                 if(this.mode == 'create')
225                     this.fmObject = new fieldmapper[this.fmClass]();
226                 try {
227                     for(var idx in fields) {
228                         this.fmObject[fields[idx]](
229                             this.getFieldValue(fields[idx])
230                         );
231                     }
232                 } catch (E) {
233                     if (E.message == "req") /* req'd field set to null. bail. */
234                         return;
235                     else /* something else went wrong? */
236                         throw E;
237                 }
238                 if(this.mode == 'create' && this.fmIDL.pkey_sequence)
239                     this.fmObject[this.fmIDL.pkey](null);
240                 if (typeof(this.onSubmit) == "function") {
241                     this.onSubmit(this.fmObject, opts, self);
242                 } else {
243                     (new openils.PermaCrud())[this.mode](this.fmObject, opts);
244                 }
245             }
246         }
247     );
248 }
249