r21252: Add operation works now, still to do mod and del ops
[Samba/ekacnet.git] / webapps / swat / source / class / swat / module / ldbbrowse / LdbModify.js
blobaf6dd12dc328a9601209d29995a73213e4af874a
1 /*                                                                                                                    
2  * Copyright::                                                                                                         
3  *   (C) 2006 by Simo Sorce
4  * 
5  * License: 
6  *   GPL v2 or later
7  */
9 /**
10  * Ldb Modifier Class
11  */
13 qx.OO.defineClass("swat.module.ldbbrowse.LdbModify", qx.ui.layout.VerticalBoxLayout,
14 function(fsm)
16   qx.ui.layout.VerticalBoxLayout.call(this);
18   this._mainArea = new qx.ui.layout.VerticalBoxLayout();
19   this._mainArea.set({
20                      overflow: "auto",
21                      height: "1*",
22                      spacing: 5
23                     });
25   // Add an horizonatl layout for the "New" and "Modify" buttons
26   // We need a vertical box layout for the tree and the buttons
27   this._hlayout = new qx.ui.layout.HorizontalBoxLayout();
28   this._hlayout.set({
29                height: "auto",
30                spacing: 10
31            });
33   // add a spacer to align buttons to the right
34   this._leftSpacer = new qx.ui.basic.HorizontalSpacer();
36   // Add the "Cancel" button
37   this._cancelbtn = new qx.ui.form.Button("Cancel");
38   this._cancelbtn.addEventListener("execute", this._cancelOp, this);
40   // Add the "OK" button
41   this._okbtn = new qx.ui.form.Button("OK");
42   this._okbtn.addEventListener("execute", fsm.eventListener, fsm);
44   // We'll be receiving events on the object, so save its friendly name
45   fsm.addObject("commit", this._okbtn, "swat.main.fsmUtils.disable_during_rpc");
47   // Add the buttons to the hlayout
48   this._hlayout.add(this._leftSpacer, this._cancelbtn, this._okbtn);
49   
50   // Add the hlayout to the vlayout.
51   this.add(this._mainArea, this._hlayout);
53   // By default this is a new record creator
54   this._type = "add";
56   // By default this is inactive
57   this._active = false;
59   this.basedn = "";
61   this._amw = null;
62 });
64 qx.OO.addProperty({ name : "basedn", type : "string" });
66 /**
67  * Set the type of operation
68  * 
69  * @param type {String}
70  *   A string containing "new" or "modify"
71  *
72  * @param data {Object}
73  *   An LDB object with the current object parameters
74  *   Used only if type = "modify"
75  *
76  */
78 qx.Proto.isActive = function() {
79   if (this._active == true) {
80     return true;
81   }
84 /** 
85  * Set the base of the object to add
86  *
87  * @param type {String}
88  *   A string containing the base DN
89  */
91 qx.Proto.setBase = function(base) {
93   this.basedn = base;
95   if (this._active) {
96     if (this._type == "add") {
98       this._basedn.setValue(this.basedn);
99       this._basedn.setWidth(8 * this.basedn.length);
100     }
101   }
104 qx.Proto.initNew = function(callback, obj) {
106   this._setExitCallback(callback, obj);
108   this._active = true;
109   this._type = "add";
111   var hlayout = new qx.ui.layout.HorizontalBoxLayout();
112   hlayout.set({ height: "auto", spacing: 10 });
114   var dnlabel = new qx.ui.basic.Label("DN: ");
116   // The name of the new/modified object
117   // TODO: add validator
118   this._rdn = new qx.ui.form.TextField(""); 
119   this._rdn.setWidth(128);
121   var dnsep = new qx.ui.basic.Label(",");
123   // The basedn of the object
124   // TODO: add validator
125   this._basedn = new qx.ui.form.TextField(this.basedn);
126   this._basedn.setWidth(8 * this.basedn.length);
128   hlayout.add(dnlabel, this._rdn, dnsep, this._basedn);
130   this._mainArea.add(hlayout);
132   this._createAttributesArea();
134   return;
137 qx.Proto.initMod = function(tablemodel, callback, obj) {
139   this._setExitCallback(callback, obj);
141   if (this.basedn == "") {
142     this._callExitCallback();
143     return;
144   }
146   this._active = true;
147   this._type = "modify";
149   this._dn = new qx.ui.basic.Label("DN: " + this.basedn);
151   this._mainArea.add(this._dn);
153   this._createAttributesArea();
155   // for each entry in the table, add new entries in the object
156   var count = tablemodel.getRowCount();
157   for (var i = 0; i < count; i++) {
158     var row = tablemodel.getRowData(i);
159     this._addNewAttribute(row[0], row[1]);
160   }
162   this._modBaseTableModel = tablemodel;
165 qx.Proto._setExitCallback = function(vFunction, vObject) {
167   if(typeof vFunction !== "function") {
168     throw new Error("swat.module.ldbbrowse.LdbModify: setExitCallback(" + vFunction + "' is not a function!");
169   }
171   this._exitCallback = {
172       handler : vFunction,
173       object : vObject
174     }
177 qx.Proto._callExitCallback = function() {
179   // Shortcuts for handler and object
180   vFunction = this._exitCallback.handler;
181   vObject = this._exitCallback.object;
183   // Call object function
184   try
185   {
186     if(typeof vFunction === "function") {
187       vFunction.call(qx.util.Validation.isValid(vObject) ? vObject : this);
188     }
189   }
190   catch(ex)
191   {
192     this.error("swat.module.ldbbrowse.LdbModify: Could not call exit callback: ", ex);
193   }
196 qx.Proto._reset = function() {
198   // Remove existing attributes
199   this._mainArea.removeAll();
200   this._active = false;
201   this._type = "null";
202   return;
205 qx.Proto._cancelOp = function() {
207   this._reset();
208   this._callExitCallback();
211 qx.Proto._okOp = function() {
213   //TODO: disable ok/cancel buttons and call fsm instead
214   this._reset();
215   this._callExitCallback();
218 qx.Proto._addNewAttribute = function(name, value, before) {
220   var hlayout = new qx.ui.layout.HorizontalBoxLayout();
221   hlayout.set({ width: "auto", height: "auto", spacing: 10 });
223   var aButton = new qx.ui.form.Button("+");
224   aButton.set({ width: 15, height: 15});
225   aButton.addEventListener("execute", function() {
226     this._addNewAttribute(name, null, hlayout);
227   }, this);
229   var aNameTextField = new qx.ui.form.TextField(name);
230   aNameTextField.setWidth(150);
232   var aValTextField = new qx.ui.form.TextField(value);
233   aValTextField.setWidth(250);
235   var rButton = new qx.ui.form.Button("-");
236   rButton.set({ left: 5, width: 15, height: 15});
237   rButton.addEventListener("execute", function() {
238     hlayout.setParent(null);
239   }, this);
241   hlayout.add(aButton, aNameTextField, aValTextField, rButton);
242   hlayout.setUserData("attrName", aNameTextField);
243   hlayout.setUserData("attrVal", aValTextField);
245   if (before) {
246     this._attrArea.addAfter(hlayout, before);
247   } else {
248     //TODO: check the same attribute is not already present, if so just add a new value instead
249     this._attrArea.addBefore(hlayout, this._attrAddButton);
250   }
253 qx.Proto._createAttributesArea = function() {
255   this._attrArea = new qx.ui.layout.VerticalBoxLayout();
257   this._attrAddButton = new qx.ui.form.Button("+");
258   this._attrAddButton.set({ width: 15, height: 15});
259   this._attrAddButton.addEventListener("execute", this._addNewAttribute, this);
261   this._attrArea.add(this._attrAddButton);
263   this._mainArea.add(this._attrArea);
266 qx.Proto.getOpType = function() {
267   return this._type;
270 qx.Proto.getLdif = function() {
271   //TODO: modify
272   if (this._type != "add") {
273     return null;
274   }
276   var ldif = "# Add operation\n";
277   ldif = ldif + "dn: " + this._rdn.getValue() + "," + this._basedn.getValue() + "\n";
279   c = this._attrArea.getChildren();
281   for (var i = 0; i < c.length; i++) {
282     if (c[i] instanceof qx.ui.layout.HorizontalBoxLayout) {
283       ldif = ldif + c[i].getUserData("attrName").getComputedValue() + ": " + c[i].getUserData("attrVal").getComputedValue() + "\n";
284     }
285   }
286   // terminate ldif record
287   ldif = ldif + "\n";
289   return ldif;