]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/dojo/openils/widget/EditPane.js
rearranging some classes around for better re-use. added an initial auto-grid, need...
[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
10     dojo.declare(
11         'openils.widget.EditPane',
12         [dijit.layout.ContentPane, openils.widget.AutoWidget],
13         {
14             mode : 'update',
15             fieldList : [], // holds the field name + associated widget
16             onPostApply : null, // apply callback
17             onCancel : null, // cancel callback
18             hideActionButtons : false,
19
20             constructor : function(args) {
21                 for(var k in args)
22                     this[k] = args[k];
23             },
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.initAutoEnv();
32
33                 var table = document.createElement('table');
34                 var tbody = document.createElement('tbody');
35                 this.domNode.appendChild(table);
36                 table.appendChild(tbody);
37
38                 this.limitPerms = [];
39                 if(this.fmIDL.permacrud && this.fmIDL.permacrud[this.mode])
40                     this.limitPerms = this.fmIDL.permacrud[this.mode].perms;
41
42                 for(var f in this.sortedFieldList) {
43                     var field = this.sortedFieldList[f];
44                     if(!field || field.virtual) continue;
45
46                     var row = document.createElement('tr');
47                     var nameTd = document.createElement('td');
48                     var valTd = document.createElement('td');
49
50                     nameTd.appendChild(document.createTextNode(field.label));
51                     row.appendChild(nameTd);
52                     row.appendChild(valTd);
53                     tbody.appendChild(row);
54
55                     var widget = new openils.widget.AutoFieldWidget({
56                         idlField : field, 
57                         fmObject : this.fmObject,
58                         parentNode : valTd,
59                         orgLimitPerms : this.limitPerms
60                     });
61                     widget.build();
62                     this.fieldList.push({name:field.name, widget:widget});
63                 }
64                 if(!this.hideActionButtons)
65                     this.buildActionButtons(tbody);
66     
67                 openils.Util.addCSSClass(table, 'oils-fm-edit-dialog');
68             },
69
70             buildActionButtons : function(tbody) {
71                 var row = document.createElement('tr');
72                 var cancelTd = document.createElement('td');
73                 var applyTd = document.createElement('td');
74                 row.appendChild(cancelTd);
75                 row.appendChild(applyTd);
76                 tbody.appendChild(row);
77
78                 var self = this;
79                 new dijit.form.Button({
80                     label:'Cancel', // XXX
81                     onClick : this.onCancel
82                 }, cancelTd);
83
84                 new dijit.form.Button({
85                     label:'Save',  // XXX
86                     onClick: function() {
87                         self.performEditAction({
88                             oncomplete:function() {
89                                 if(self.onPostApply)
90                                     self.onPostApply();
91                             }
92                         });
93                     }
94                 }, applyTd);
95             },
96
97             getFields : function() {
98                 return this.fieldList.map(function(a) { return a.name });
99             },
100
101             getFieldValue : function(field) {
102                 for(var i in this.fieldList) {
103                     if(field == this.fieldList[i].name)
104                         return this.fieldList[i].widget.getFormattedValue();
105                 }
106             },
107
108             performEditAction : function(opts) {
109                 var pcrud = new openils.PermaCrud();
110                 var fields = this.getFields();
111                 if(this.mode == 'create')
112                     this.fmObject = new fieldmapper[this.fmClass]();
113                 for(var idx in fields) 
114                     this.fmObject[fields[idx]](this.getFieldValue(fields[idx]));
115                 pcrud[this.mode](this.fmObject, opts);
116             }
117         }
118     );
119 }
120