e22a1802da3ef1c5ca3b820fee6d7ab41fd81850
4 // $Id: widget_form_manager.js 1473 2009-03-06 17:02:21Z pin $
5 // $URL: http://svn.cri.ensmp.fr/svn/Plinn/branches/CMF-2.1/skins/ajax_scripts/widget_form_manager.js $
7 var WidgetBasedFormManager
;
10 WidgetBasedFormManager = function(widgets
, editingArea
, dataArea
, dataAreaSpecs
) {
11 var thisWgtManager
= this;
12 this.widgets
= widgets
;
13 this.openedWidget
= null;
14 this.dataArea
= dataArea
;
15 this.dataAreaSpecs
= dataAreaSpecs
;
17 var form
= editingArea
.getElementsByTagName('form')[0];
19 var fm
= new FormManager(form
);
20 fm
.onBeforeSubmit = function(fm
, evt
){return thisWgtManager
.onBeforeSubmit(fm
, evt
);};
21 fm
.onResponseLoad = function(req
){thisWgtManager
.loadResponse(req
);};
23 addListener(this.form
, 'click', function(evt
){thisWgtManager
.clickHandler(evt
);});
27 WidgetBasedFormManager
.prototype.showAddWidget = function(dest
) {
28 if (this.openedWidget
)
30 var wdgtCopy
= this.widgets
['add'].cloneNode(true);
31 wdgtCopy
.removeAttribute('id');
32 this.openedWidget
= wdgtCopy
;
33 dest
.appendChild(wdgtCopy
);
35 this.addButton
.style
.visibility
= 'hidden';
38 WidgetBasedFormManager
.prototype.showPopulatedWidget = function(dest
, url
) {
39 var req
= new XMLHttpRequest();
40 req
.open("GET", url
, false);
45 if (req
.status
!= 200){
50 var wdgtCopy
= this.widgets
['edit'].cloneNode(true);
51 wdgtCopy
.removeAttribute('id');
52 var tmpForm
= document
.createElement('form');
53 tmpForm
.appendChild(wdgtCopy
);
55 var fields
= req
.responseXML
.documentElement
.childNodes
;
56 var input
, field
, value
;
57 for (var i
= 0 ; i
<fields
.length
; i
++) {
59 if (!field
.firstChild
)
62 value
= field
.firstChild
.nodeValue
;
63 input
= tmpForm
.elements
.namedItem(field
.nodeName
);
65 switch (input
.tagName
) {
70 input
.appendChild(document
.createTextNode(value
));
73 dest
.appendChild(wdgtCopy
);
76 WidgetBasedFormManager
.prototype.cancelWidget = function() {
77 var parent
= this.openedWidget
.parentNode
;
78 parent
.removeChild(this.openedWidget
);
79 this.openedWidget
= null;
81 this.addButton
.style
.visibility
= 'visible';
82 if (this.previousRecState
) {
83 this.previousRecState
.style
.display
= '';
84 this.previousRecState
= null;
88 WidgetBasedFormManager
.prototype.updateEditedRecord = function(node
) {
89 var parent
= this.openedWidget
.parentNode
;
90 parent
.removeChild(this.openedWidget
);
91 this.openedWidget
= null;
93 this.addButton
.style
.visibility
= 'visible';
95 parent
= this.previousRecState
.parentNode
;
96 parent
.replaceChild(node
, this.previousRecState
);
99 WidgetBasedFormManager
.prototype.clickHandler = function(evt
) {
100 var target
= getTargetedObject(evt
);
101 if (target
.tagName
== 'IMG') {
102 var parent
= target
.parentNode
;
103 if (parent
.tagName
== 'A') {
104 switch (parent
.name
) {
107 disablePropagation(evt
);
109 this.addButton
= parent
;
110 this.showAddWidget(this.form
);
114 disablePropagation(evt
);
115 this.deleteRecord(parent
);
119 disablePropagation(evt
);
120 if (!this.openedWidget
) {
121 var rec
= this._storePreviousState(parent
);
122 var container
= this._prepareEditingContainer(rec
);
123 this.showPopulatedWidget(container
, parent
.href
);
131 WidgetBasedFormManager
.prototype._storePreviousState = function(recChild
) {
133 while(!(this.isRootRecordElement(rec
)))
134 rec
= rec
.parentNode
;
135 rec
.style
.display
= 'none';
136 this.previousRecState
= rec
;
140 WidgetBasedFormManager
.prototype._prepareEditingContainer = function(rec
) {
141 /* default behaviour. May be redefined by an instance */
142 var tbody
= document
.createElement('tbody');
143 var tr
= document
.createElement('tr');
144 var td
= document
.createElement('td')
145 td
.colSpan
= this.dataAreaSpecs
;
147 tbody
.appendChild(tr
);
151 rec
.parentNode
.insertBefore(tbody
, rec
.nextSibling
);
153 rec
.parentNode
.appendChild(tbody
);
155 this.openedWidget
= tbody
;
161 WidgetBasedFormManager
.prototype.deleteRecord = function(link
) {
162 var req
= new XMLHttpRequest();
164 var thisManager
= this;
165 req
.onreadystatechange = function() {
166 switch (req
.readyState
) {
172 if (req
.status
== 200)
173 thisManager
._deleteHtmlRecord(req
, link
);
175 alert('Error: ' + req
.status
);
179 req
.open("POST", url
, true);
184 WidgetBasedFormManager
.prototype._deleteHtmlRecord = function(req
, link
) {
185 var doc
= req
.responseXML
.documentElement
;
186 switch (doc
.nodeName
) {
188 var rec
= link
.parentNode
;
189 while(!(this.isRootRecordElement(rec
)))
190 rec
= rec
.parentNode
;
191 this.dataArea
.removeChild(rec
);
194 alert(doc
.firstChild
.nodeValue
);
199 WidgetBasedFormManager
.prototype.isRootRecordElement = function(e
) {
200 /* default behaviour. May be redefined by an instance */
201 return (e
.tagName
== 'TBODY')
204 WidgetBasedFormManager
.prototype.onBeforeSubmit = function(fm
, evt
) {
205 if (fm
.submitButton
.name
== 'cancel'){
207 return 'cancelSubmit';
212 WidgetBasedFormManager
.prototype.loadResponse = function(req
) {
213 if (req
.status
== 200) {
214 var doc
= req
.responseXML
.documentElement
;
215 switch (doc
.nodeName
) {
216 case 'computedField':
217 switch(doc
.getAttribute('type')) {
220 this.dataArea
.appendChild(getCopyOfNode(doc
.firstChild
));
224 this.updateEditedRecord(getCopyOfNode(doc
.firstChild
));
229 alert(doc
.firstChild
.nodeValue
);
233 var fragments
= req
.responseXML
.documentElement
.childNodes
;
234 var fragment
, dest
, scripts
;
235 for (var i
=0 ; i
<fragments
.length
; i
++) {
236 fragment
= fragments
[i
];
237 if (fragment
.nodeName
== 'fragment') {
238 dest
= document
.getElementById(fragment
.getAttribute('id'));
239 dest
.innerHTML
= fragment
.firstChild
.nodeValue
;
241 scripts
= dest
.getElementsByTagName('script');
242 for (var j
=0 ; j
< scripts
.length
; j
++)
243 globalScriptRegistry
.loadScript(scripts
[j
]);