]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/widget/EditPane.js
teach the IDL about the existence of pkey sequences
[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                     var valSpan = document.createElement('span');
50                     valTd.appendChild(valSpan);
51
52                     nameTd.appendChild(document.createTextNode(field.label));
53                     row.appendChild(nameTd);
54                     row.appendChild(valTd);
55                     tbody.appendChild(row);
56
57                     var widget = new openils.widget.AutoFieldWidget({
58                         idlField : field, 
59                         fmObject : this.fmObject,
60                         fmClass : this.fmClass,
61                         parentNode : valSpan,
62                         orgLimitPerms : this.limitPerms
63                     });
64
65                     widget.build();
66                     this.fieldList.push({name:field.name, widget:widget});
67                     this.applySaveOnEnter(widget);
68                 }
69                 if(!this.hideActionButtons)
70                     this.buildActionButtons(tbody);
71     
72                 openils.Util.addCSSClass(table, 'oils-fm-edit-dialog');
73             },
74
75             applySaveOnEnter : function(widget) {
76                 var self = this;
77                 dojo.connect(this, 'onKeyDown',
78                     function(e) {
79                         if(e.keyCode == dojo.keys.ENTER) 
80                             self.performAutoEditAction();
81                     }
82                 );
83             },
84
85             buildActionButtons : function(tbody) {
86                 var row = document.createElement('tr');
87                 var cancelTd = document.createElement('td');
88                 var applyTd = document.createElement('td');
89                 var cancelSpan = document.createElement('span');
90                 var applySpan = document.createElement('span');
91                 row.appendChild(cancelTd);
92                 row.appendChild(applyTd);
93                 cancelTd.appendChild(cancelSpan);
94                 applyTd.appendChild(applySpan);
95                 tbody.appendChild(row);
96
97                 var self = this;
98                 new dijit.form.Button({
99                     label:'Cancel', // XXX
100                     onClick : this.onCancel
101                 }, cancelSpan);
102
103                 new dijit.form.Button({
104                     label:'Save',  // XXX
105                     onClick: function() {self.performAutoEditAction();}
106                 }, applySpan);
107             },
108
109             getFields : function() {
110                 return this.fieldList.map(function(a) { return a.name });
111             },
112
113             getFieldValue : function(field) {
114                 for(var i in this.fieldList) {
115                     if(field == this.fieldList[i].name)
116                         return this.fieldList[i].widget.getFormattedValue();
117                 }
118             },
119
120             performAutoEditAction : function() {
121                 var self = this;
122                 self.performEditAction({
123                     oncomplete:function(r) {
124                         if(self.onPostSubmit)
125                             self.onPostSubmit(r);
126                     }
127                 });
128             },
129
130             performEditAction : function(opts) {
131                 var pcrud = new openils.PermaCrud();
132                 var fields = this.getFields();
133                 if(this.mode == 'create')
134                     this.fmObject = new fieldmapper[this.fmClass]();
135                 for(var idx in fields)  
136                     this.fmObject[fields[idx]](this.getFieldValue(fields[idx]));
137                 if(this.mode == 'create' && this.fmIDL.pkey_sequence)
138                     this.fmObject[fieldmapper[this.fmClass].Identifier](null);
139                 pcrud[this.mode](this.fmObject, opts);
140             }
141         }
142     );
143 }
144