7 var WidgetBasedFormManager
;
10 WidgetBasedFormManager = function(widgets
, editingArea
, dataArea
, dataAreaSpecs
, afterShow
) {
11 /* widgets : {'add':element, 'edit':element} element targets nodes to clone.
12 * editingArea : surrounding element where form is
13 * dataArea : element where data are
14 * dataAreaSpecs : by default, param used to indicate the total number of columns
15 * afterShow : function called after a widget insertion
17 var thisWgtManager
= this;
18 this.widgets
= widgets
;
19 this.openedWidget
= null;
20 this.dataArea
= dataArea
;
21 this.dataAreaSpecs
= dataAreaSpecs
;
22 this.afterShow
= afterShow
;
24 var form
= editingArea
.getElementsByTagName('form')[0];
26 var fm
= new FormManager(form
);
27 fm
.onBeforeSubmit = function(fm
, evt
){return thisWgtManager
.onBeforeSubmit(fm
, evt
);};
28 fm
.onResponseLoad = function(req
){thisWgtManager
.loadResponse(req
);};
30 addListener(this.form
, 'click', function(evt
){thisWgtManager
.clickHandler(evt
);});
34 WidgetBasedFormManager
.prototype.showAddWidget = function(dest
) {
35 if (this.openedWidget
)
37 var wdgtCopy
= this.widgets
['add'].cloneNode(true);
38 wdgtCopy
.removeAttribute('id');
39 this.openedWidget
= wdgtCopy
;
40 dest
.appendChild(wdgtCopy
);
42 this.addButton
.style
.visibility
= 'hidden';
47 WidgetBasedFormManager
.prototype.showPopulatedWidget = function(dest
, url
) {
48 var req
= new XMLHttpRequest();
49 req
.open("GET", url
, false);
54 if (req
.status
!= 200){
59 var wdgtCopy
= this.widgets
['edit'].cloneNode(true);
60 wdgtCopy
.removeAttribute('id');
61 var tmpForm
= document
.createElement('form');
62 tmpForm
.appendChild(wdgtCopy
);
64 var fields
= req
.responseXML
.documentElement
.childNodes
;
65 var input
, field
, value
;
66 for (var i
= 0 ; i
<fields
.length
; i
++) {
68 if (!field
.firstChild
)
71 value
= field
.firstChild
.nodeValue
;
72 input
= tmpForm
.elements
.namedItem(field
.nodeName
);
74 switch (input
.tagName
) {
79 input
.appendChild(document
.createTextNode(value
));
82 dest
.appendChild(wdgtCopy
);
87 WidgetBasedFormManager
.prototype.cancelWidget = function() {
88 var parent
= this.openedWidget
.parentNode
;
89 parent
.removeChild(this.openedWidget
);
90 this.openedWidget
= null;
92 this.addButton
.style
.visibility
= 'visible';
93 if (this.previousRecState
) {
94 this.previousRecState
.style
.display
= '';
95 this.previousRecState
= null;
99 WidgetBasedFormManager
.prototype.updateEditedRecord = function(node
) {
100 var parent
= this.openedWidget
.parentNode
;
101 parent
.removeChild(this.openedWidget
);
102 this.openedWidget
= null;
104 this.addButton
.style
.visibility
= 'visible';
106 parent
= this.previousRecState
.parentNode
;
107 parent
.replaceChild(node
, this.previousRecState
);
110 WidgetBasedFormManager
.prototype.clickHandler = function(evt
) {
111 var target
= getTargetedObject(evt
);
112 if (target
.tagName
== 'IMG') {
113 var parent
= target
.parentNode
;
114 if (parent
.tagName
== 'A') {
115 switch (parent
.name
) {
118 disablePropagation(evt
);
120 this.addButton
= parent
;
121 this.showAddWidget(this.form
);
125 disablePropagation(evt
);
126 this.deleteRecord(parent
);
130 disablePropagation(evt
);
131 if (!this.openedWidget
) {
132 var rec
= this._storePreviousState(parent
);
133 var container
= this._prepareEditingContainer(rec
);
134 this.showPopulatedWidget(container
, parent
.href
);
142 WidgetBasedFormManager
.prototype._storePreviousState = function(recChild
) {
144 while(!(this.isRootRecordElement(rec
)))
145 rec
= rec
.parentNode
;
146 rec
.style
.display
= 'none';
147 this.previousRecState
= rec
;
151 WidgetBasedFormManager
.prototype._prepareEditingContainer = function(rec
) {
152 /* default behaviour. May be redefined by an instance */
153 var tbody
= document
.createElement('tbody');
154 var tr
= document
.createElement('tr');
155 var td
= document
.createElement('td')
156 td
.colSpan
= this.dataAreaSpecs
;
158 tbody
.appendChild(tr
);
162 rec
.parentNode
.insertBefore(tbody
, rec
.nextSibling
);
164 rec
.parentNode
.appendChild(tbody
);
166 this.openedWidget
= tbody
;
172 WidgetBasedFormManager
.prototype.deleteRecord = function(link
) {
173 var req
= new XMLHttpRequest();
175 var thisManager
= this;
176 req
.onreadystatechange = function() {
177 switch (req
.readyState
) {
183 if (req
.status
== 200)
184 thisManager
._deleteHtmlRecord(req
, link
);
186 alert('Error: ' + req
.status
);
190 req
.open("POST", url
, true);
195 WidgetBasedFormManager
.prototype._deleteHtmlRecord = function(req
, link
) {
196 var doc
= req
.responseXML
.documentElement
;
197 switch (doc
.nodeName
) {
199 var rec
= link
.parentNode
;
200 while(!(this.isRootRecordElement(rec
)))
201 rec
= rec
.parentNode
;
202 this.dataArea
.removeChild(rec
);
205 alert(doc
.firstChild
.nodeValue
);
210 WidgetBasedFormManager
.prototype.isRootRecordElement = function(e
) {
211 /* default behaviour. May be redefined by an instance */
212 return (e
.tagName
== 'TBODY')
215 WidgetBasedFormManager
.prototype.onBeforeSubmit = function(fm
, evt
) {
216 if (fm
.submitButton
.name
== 'cancel'){
218 return 'cancelSubmit';
223 WidgetBasedFormManager
.prototype.loadResponse = function(req
) {
224 if (req
.status
== 200) {
225 var doc
= req
.responseXML
.documentElement
;
226 switch (doc
.nodeName
) {
227 case 'computedField':
228 switch(doc
.getAttribute('type')) {
231 this.dataArea
.appendChild(getCopyOfNode(doc
.firstChild
));
235 this.updateEditedRecord(getCopyOfNode(doc
.firstChild
));
240 alert(doc
.firstChild
.nodeValue
);
244 var fragments
= req
.responseXML
.documentElement
.childNodes
;
245 var fragment
, dest
, scripts
;
246 for (var i
=0 ; i
<fragments
.length
; i
++) {
247 fragment
= fragments
[i
];
248 if (fragment
.nodeName
== 'fragment') {
249 dest
= document
.getElementById(fragment
.getAttribute('id'));
250 dest
.innerHTML
= fragment
.firstChild
.nodeValue
;
252 scripts
= dest
.getElementsByTagName('script');
253 for (var j
=0 ; j
< scripts
.length
; j
++)
254 globalScriptRegistry
.loadScript(scripts
[j
]);