added option to launch creation dialog from auto grid. passing final pcrud response...
[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
10     dojo.declare(
11         'openils.widget.EditPane',
12         [dijit.layout.ContentPane, openils.widget.AutoWidget],
13         {
14             mode : 'update',
15             onPostSubmit : null, // apply callback
16             onCancel : null, // cancel callback
17             hideActionButtons : false,
18
19             constructor : function(args) {
20                 this.fieldList = [];
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
62                     widget.build();
63                     this.fieldList.push({name:field.name, widget:widget});
64                     this.applySaveOnEnter(widget);
65                 }
66                 if(!this.hideActionButtons)
67                     this.buildActionButtons(tbody);
68     
69                 openils.Util.addCSSClass(table, 'oils-fm-edit-dialog');
70             },
71
72             applySaveOnEnter : function(widget) {
73                 var self = this;
74                 dojo.connect(this, 'onKeyDown',
75                     function(e) {
76                         if(e.keyCode == dojo.keys.ENTER) 
77                             self.performAutoEditAction();
78                     }
79                 );
80             },
81
82             buildActionButtons : function(tbody) {
83                 var row = document.createElement('tr');
84                 var cancelTd = document.createElement('td');
85                 var applyTd = document.createElement('td');
86                 row.appendChild(cancelTd);
87                 row.appendChild(applyTd);
88                 tbody.appendChild(row);
89
90                 var self = this;
91                 new dijit.form.Button({
92                     label:'Cancel', // XXX
93                     onClick : this.onCancel
94                 }, cancelTd);
95
96                 new dijit.form.Button({
97                     label:'Save',  // XXX
98                     onClick: function() {self.performAutoEditAction();}
99                 }, applyTd);
100             },
101
102             getFields : function() {
103                 return this.fieldList.map(function(a) { return a.name });
104             },
105
106             getFieldValue : function(field) {
107                 for(var i in this.fieldList) {
108                     if(field == this.fieldList[i].name)
109                         return this.fieldList[i].widget.getFormattedValue();
110                 }
111             },
112
113             performAutoEditAction : function() {
114                 var self = this;
115                 self.performEditAction({
116                     oncomplete:function(r) {
117                         if(self.onPostSubmit)
118                             self.onPostSubmit(r);
119                     }
120                 });
121             },
122
123             performEditAction : function(opts) {
124                 var pcrud = new openils.PermaCrud();
125                 var fields = this.getFields();
126                 if(this.mode == 'create')
127                     this.fmObject = new fieldmapper[this.fmClass]();
128                 for(var idx in fields)  
129                     this.fmObject[fields[idx]](this.getFieldValue(fields[idx]));
130                 if(this.mode == 'create')
131                     this.fmObject[fieldmapper[this.fmClass].Identifier](null);
132                 pcrud[this.mode](this.fmObject, opts);
133             }
134         }
135     );
136 }
137