3c4e712e35b9ead308571f4a2143161140283b9b
[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             onPostApply : 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                     widget.build();
62                     this.fieldList.push({name:field.name, widget:widget});
63                     this.applySaveOnEnter(widget);
64                 }
65                 if(!this.hideActionButtons)
66                     this.buildActionButtons(tbody);
67     
68                 openils.Util.addCSSClass(table, 'oils-fm-edit-dialog');
69             },
70
71             applySaveOnEnter : function(widget) {
72                 var self = this;
73                 dojo.connect(this, 'onKeyDown',
74                     function(e) {
75                         if(e.keyCode == dojo.keys.ENTER) 
76                             self.performAutoEditAction();
77                     }
78                 );
79             },
80
81             buildActionButtons : function(tbody) {
82                 var row = document.createElement('tr');
83                 var cancelTd = document.createElement('td');
84                 var applyTd = document.createElement('td');
85                 row.appendChild(cancelTd);
86                 row.appendChild(applyTd);
87                 tbody.appendChild(row);
88
89                 var self = this;
90                 new dijit.form.Button({
91                     label:'Cancel', // XXX
92                     onClick : this.onCancel
93                 }, cancelTd);
94
95                 new dijit.form.Button({
96                     label:'Save',  // XXX
97                     onClick: function() {self.performAutoEditAction();}
98                 }, applyTd);
99             },
100
101             getFields : function() {
102                 return this.fieldList.map(function(a) { return a.name });
103             },
104
105             getFieldValue : function(field) {
106                 for(var i in this.fieldList) {
107                     if(field == this.fieldList[i].name)
108                         return this.fieldList[i].widget.getFormattedValue();
109                 }
110             },
111
112             performAutoEditAction : function() {
113                 var self = this;
114                 self.performEditAction({
115                     oncomplete:function() {
116                         if(self.onPostApply)
117                             self.onPostApply();
118                     }
119                 });
120             },
121
122             performEditAction : function(opts) {
123                 var pcrud = new openils.PermaCrud();
124                 var fields = this.getFields();
125                 if(this.mode == 'create')
126                     this.fmObject = new fieldmapper[this.fmClass]();
127                 for(var idx in fields)  
128                     this.fmObject[fields[idx]](this.getFieldValue(fields[idx]));
129                 pcrud[this.mode](this.fmObject, opts);
130             }
131         }
132     );
133 }
134