1 // (c) Benoît PIN 2006-2007
11 FormManager = function(form
, responseTextDest
, lazy
) {
12 if (form
.elements
.namedItem("noAjax")) {return;}
15 this.responseTextDest
= responseTextDest
;
17 var thisManager
= this;
18 this.form
.onsubmit = function(evt
) { thisManager
.submit(evt
); };
19 this.form
.onclick = function(evt
) { thisManager
.click(evt
); };
20 this.submitButton
= null;
22 /* raised on form submit */
23 this.onBeforeSubmit
= null;
24 /* raised after xmlhttp response */
25 this.onResponseLoad
= null;
26 /* raised when the responseText is added inside the main element.
27 * (onResponseLoad may have the default value) */
28 this.onAfterPopulate
= null;
29 this.submitButton
= null;
32 this.form
.onclick = function(evt
){
33 thisManager
.replaceElementByField(evt
);
34 thisManager
.click(evt
);
36 if (browser
.isDOM2Event
) {
37 this.form
.onfocus
= this.form
.onclick
;
39 else if (browser
.isIE6up
) {
40 this.form
.onfocusin
= this.form
.onclick
;
42 this.onResponseLoad = function(req
){ thisManager
.restoreField(req
); };
43 this.lazyListeners
= [];
47 FormManager
.prototype.submit = function(evt
) {
49 var thisManager
= this;
51 var bsMessage
; // before submit message
52 if (!this.onBeforeSubmit
) {
53 var onBeforeSubmit
= form
.elements
.namedItem("onBeforeSubmit");
55 if (onBeforeSubmit
.length
) {
56 onBeforeSubmit
= onBeforeSubmit
[0];
58 /*jslint evil: true */
59 this.onBeforeSubmit
= eval(onBeforeSubmit
.value
);
60 bsMessage
= this.onBeforeSubmit(thisManager
, evt
);
64 bsMessage
= this.onBeforeSubmit(thisManager
, evt
);
67 if (bsMessage
=== 'cancelSubmit') {
68 try {disableDefault(evt
);}
73 if (!this.onResponseLoad
) {
74 var onResponseLoad
= form
.elements
.namedItem("onResponseLoad");
76 this.onResponseLoad
= eval(onResponseLoad
.value
);
79 this.onResponseLoad
= this.loadResponse
;
83 var submitButton
= this.submitButton
;
84 var queryInfo
= this.formData2QueryString();
85 var query
= queryInfo
.query
;
86 this.hasFile
= queryInfo
.hasFile
;
89 if (!this.onAfterPopulate
) {
90 var onAfterPopulate
= form
.elements
.namedItem("onAfterPopulate");
91 if (onAfterPopulate
) {
92 this.onAfterPopulate
= onAfterPopulate
.value
;
95 this.onAfterPopulate = function() {};
100 query
+= submitButton
.name
+ '=' + submitButton
.value
+ '&';
103 if (window
.AJAX_CONFIG
&& ((AJAX_CONFIG
& 1) === 1)) {
104 if (form
.method
.toLowerCase() === 'post') {
115 try {disableDefault(evt
);}
119 FormManager
.prototype._post = function(query
) {
120 // send form by XmlHttpRequest
123 var req
= new XMLHttpRequest();
124 var thisManager
= this;
125 req
.onreadystatechange = function() {
126 switch (req
.readyState
) {
132 if (req
.status
=== 200 || req
.status
=== 204) {
133 thisManager
.onResponseLoad(req
);
136 alert('Error: ' + req
.status
);
141 var url
= this.form
.action
;
142 req
.open("POST", url
, true);
143 req
.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
147 FormManager
.prototype._get = function(query
) {
148 // send form by browser location
149 var url
= this.form
.action
;
151 linkHandler
.loadUrl(url
);
155 FormManager
.prototype.click = function(evt
) {
156 var target
= getTargetedObject(evt
);
157 if(target
.type
=== "submit" || target
.type
=== "image") {
158 this.submitButton
= target
;
159 disablePropagation(evt
);
163 FormManager
.prototype.replaceElementByField = function(evt
) {
164 evt
= getEventObject(evt
);
165 var ob
= getTargetedObject(evt
);
166 var eventType
= evt
.type
;
167 if (eventType
=== 'focus' || eventType
=== 'focusin') {
168 if (this.liveFormField
&& ob
.tagName
!== 'INPUT') {
169 this.pendingEvent
= [ob
, 'click'];
173 var fieldName
= ob
.getAttribute('id');
175 this.fieldTagName
= ob
.tagName
;
176 var tabIndex
= ob
.tabIndex
;
178 if (ob
.firstChild
&& ob
.firstChild
.className
=== 'hidden_value') {
179 text
= ob
.firstChild
.innerHTML
;
184 disablePropagation(evt
);
187 switch (ob
.tagName
) {
189 // create input element
190 var inputText
= document
.createElement("input");
191 inputText
.setAttribute("type", "text");
192 text
= text
.replace(/\n/g, ' ');
193 text
= text
.replace(/\s+/g, ' ');
194 text
= text
.replace(/^ /, '');
195 text
= text
.replace(/ $/, '');
196 inputText
.setAttribute("value", text
);
197 var inputWidth
= text
.length
/ 1.9;
198 inputWidth
= (inputWidth
> 5) ? inputWidth
: 5;
199 inputText
.style
.width
= inputWidth
+ 'em';
200 //inputText.setAttribute("size", text.length);
203 parent
= ob
.parentNode
;
204 parent
.replaceChild(inputText
, ob
);
208 inputText
.setAttribute('name', fieldName
);
209 inputText
.tabIndex
= tabIndex
;
210 inputText
.className
= 'live_field';
211 this.liveFormField
= inputText
;
212 this.lazyListeners
.push({'element': inputText
, 'eventName' : 'blur', 'handler': function(){ thisManager
.submit();}});
213 this.lazyListeners
.push({'element': inputText
, 'eventName' : 'keypress', 'handler': function(evt
){ thisManager
._fitField(evt
);}});
214 this._addLazyListeners();
220 var ta
= document
.createElement('textarea');
221 ta
.style
.display
= 'block';
222 ta
.className
= 'live_field';
223 text
= text
.replace(/^\s*/, '');
224 text
= text
.replace(/\s*$/, '');
228 parent
= ob
.parentNode
;
229 parent
.replaceChild(ta
, ob
);
233 ta
.setAttribute('name', fieldName
);
234 ta
.tabIndex
= tabIndex
;
235 this.liveFormField
= ta
;
236 this.lazyListeners
.push({'element': ta
, 'eventName' : 'blur', 'handler': function(){ thisManager
.submit();}});
237 this._addLazyListeners();
243 FormManager
.prototype._addLazyListeners = function() {
245 for (i
=0 ; i
<this.lazyListeners
.length
; i
++) {
246 handlerInfo
= this.lazyListeners
[i
];
247 addListener(handlerInfo
.element
, handlerInfo
.eventName
, handlerInfo
.handler
);
251 FormManager
.prototype._removeLazyListeners = function() {
253 for (i
=0 ; i
<this.lazyListeners
.length
; i
++) {
254 handlerInfo
= this.lazyListeners
[i
];
255 removeListener(handlerInfo
.element
, handlerInfo
.eventName
, handlerInfo
.handler
);
260 FormManager
.prototype.restoreField = function(req
) {
262 var input
= this.liveFormField
;
263 if (req
.status
=== 200) {
264 if (req
.getResponseHeader('Content-Type').indexOf('text/xml') !== -1) {
265 var out
= '..........';
266 if (req
.responseXML
.documentElement
.firstChild
) {
267 out
= req
.responseXML
.documentElement
.firstChild
.nodeValue
;
270 switch (req
.responseXML
.documentElement
.nodeName
) {
271 case 'computedField':
275 this._removeLazyListeners();
277 this.pendingEvent
= null;
279 this._addLazyListeners();
284 text
= req
.responseText
;
291 if (!text
.match(/\w/)) {
295 var field
= document
.createElement(this.fieldTagName
);
296 field
.innerHTML
= text
;
297 field
.setAttribute('id', input
.getAttribute('name'));
298 field
.className
= 'editable';
299 field
.tabIndex
= input
.tabIndex
;
301 var parent
= input
.parentNode
;
302 parent
.replaceChild(field
, input
);
303 this.liveFormField
= null;
305 if (this.pendingEvent
) {
306 raiseMouseEvent(this.pendingEvent
[0], this.pendingEvent
[1]);
312 FormManager
.prototype.formData2QueryString = function() {
313 // http://www.onlamp.com/pub/a/onlamp/2005/05/19/xmlhttprequest.html
314 var form
= this.form
;
315 var strSubmit
= '', formElem
, elements
;
320 elements
= form
.elements
;
324 var formElements
= form
.elements
;
325 for (i
= 0; i
< formElements
.length
; i
++) {
326 formElem
= formElements
[i
];
327 switch (formElem
.type
) {
329 elements
.push(formElem
);
332 if (formElem
=== this.liveFormField
) {
333 elements
.push(formElem
);
339 for (i
= 0; i
< elements
.length
; i
++) {
340 formElem
= elements
[i
];
341 switch (formElem
.type
) {
342 // text, select, hidden, password, textarea elements
348 strSubmit
+= formElem
.name
+ '=' + encodeURIComponent(formElem
.value
) + '&';
352 if (formElem
.checked
) {
353 strSubmit
+= formElem
.name
+ '=' + encodeURIComponent(formElem
.value
) + '&';
356 case 'select-multiple':
357 var options
= formElem
.getElementsByTagName("OPTION"), option
;
359 for (j
= 0 ; j
< options
.length
; j
++) {
361 if (option
.selected
) {
362 strSubmit
+= formElem
.name
+ '=' + encodeURIComponent(option
.value
) + '&';
367 if (formElem
.value
) {
373 return {'query' : strSubmit
, 'hasFile' : hasFile
};
376 FormManager
.prototype.loadResponse = function(req
) {
378 if (req
.getResponseHeader('Content-Type').indexOf('text/xml') !== -1) {
379 switch(req
.responseXML
.documentElement
.nodeName
) {
382 var sb
= this.submitButton
;
384 var h
= document
.createElement('input');
388 this.form
.appendChild(h
);
394 var fragments
= req
.responseXML
.documentElement
.childNodes
;
397 for (i
=0 ; i
<fragments
.length
; i
++) {
398 fragment
= fragments
[i
];
399 if (fragment
.nodeName
=== 'fragment') {
400 dest
= document
.getElementById(fragment
.getAttribute('id'));
401 dest
.innerHTML
= fragment
.firstChild
.nodeValue
;
403 scripts
= dest
.getElementsByTagName('script');
405 for (j
=0 ; j
< scripts
.length
; j
++) {
406 globalScriptRegistry
.loadScript(scripts
[j
]);
412 alert(req
.responseXML
.documentElement
.firstChild
.nodeValue
);
417 this.responseTextDest
.innerHTML
= req
.responseText
;
418 scripts
= this.responseTextDest
.getElementsByTagName('script');
420 for (k
=0 ; k
< scripts
.length
; k
++) {
421 globalScriptRegistry
.loadScript(scripts
[k
]);
425 var onAfterPopulate
= this.onAfterPopulate
;
427 this.scrollToPortalMessage();
430 FormManager
.prototype.scrollToPortalMessage = function() {
431 var psm
= document
.getElementById('DesktopStatusBar');
433 var msgOffset
= psm
.offsetTop
;
434 smoothScroll(window
.scrollY
, msgOffset
);
435 shake(psm
, 10, 1000);
439 FormManager
.prototype._fitField = function(evt
) {
440 var ob
= getTargetedObject(evt
);
441 var inputWidth
= ob
.value
.length
/ 1.9;
442 inputWidth
= (inputWidth
> 5) ? inputWidth
: 5;
443 ob
.style
.width
= inputWidth
+ 'em';
446 function initForms(baseElement
, lazy
) {
448 baseElement
= document
;
450 var dest
= document
.getElementById("mainCell");
451 var forms
= baseElement
.getElementsByTagName("form");
453 for (i
= 0 ; i
< forms
.length
; i
++ ) {
454 f
= new FormManager(forms
[i
], dest
, lazy
);
458 function smoothScroll(from, to
) {
470 var jump = function() {
471 window
.scroll(0, pos
);
472 pos
= pos
+ step
* dir
;
473 if ((dir
=== 1 && pos
>= to
) ||
474 (dir
=== -1 && pos
<= to
)) {
475 window
.clearInterval(intervalId
);
476 window
.scroll(0, to
);
479 intervalId
= window
.setInterval(jump
, 10);
482 /* adapted from http://xahlee.info/js/js_shake_box.html */
483 function shake(e, distance, time) {
485 if (!time) { time = 500; }
486 if (!distance) { distance = 5; }
488 // Save the original style of e, Make e relatively positioned, Note the animation start time, Start the animation
489 var originalStyle = e.style.cssText;
490 e.style.position = "relative";
491 var start = (new Date()).getTime();
493 // This function checks the elapsed time and updates the position of e.
494 // If the animation is complete, it restores e to its original state.
495 // Otherwise, it updates e's position and schedules itself to run again.
497 var now = (new Date()).getTime();
499 var elapsed = now-start;
500 // How long since we started
501 var fraction = elapsed/time;
502 // What fraction of total time?
504 // If the animation is not yet complete
505 // Compute the x position of e as a function of animation
506 // completion fraction. We use a sinusoidal function, and multiply
507 // the completion fraction by 4pi, so that it shakes back and
509 var x = distance * Math.sin(fraction*8*Math.PI);
510 e.style.left = x + "px";
511 // Try to run again in 25ms or at the end of the total time.
512 // We're aiming for a smooth 40 frames/second animation.
513 setTimeout(animate, Math.min(25, time-elapsed));
516 // Otherwise, the animation is complete
517 e.style.cssText = originalStyle; // Restore the original style