1 // (c) Benoît PIN 2006-2007
11 FormManager = function(form
, responseTextDest
, lazy
, noHistory
) {
12 if (form
.elements
.namedItem("noAjax")) {return;}
15 this.responseTextDest
= responseTextDest
;
17 this.noHistory
= noHistory
;
18 var thisManager
= this;
19 this.form
.onsubmit = function(evt
) { thisManager
.submit(evt
); };
20 this.form
.onclick = function(evt
) { thisManager
.click(evt
); };
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
;
395 var element
, dest
, i
, j
;
396 for (i
=0 ; i
< fragments
.length
; i
++) {
397 element
= fragments
[i
];
398 switch (element
.nodeName
) {
400 dest
= document
.getElementById(element
.getAttribute('id'));
402 dest
.innerHTML
= element
.firstChild
.nodeValue
;
403 scripts
= dest
.getElementsByTagName('script');
404 for (j
=0 ; j
< scripts
.length
; j
++) {
405 globalScriptRegistry
.loadScript(scripts
[j
]); }
409 var headBase
= document
.getElementsByTagName('base');
410 if (headBase
.length
> 0) {
411 headBase
[0].setAttribute('href', element
.getAttribute('href'));
414 headBase
= document
.createElement('base');
415 headBase
.setAttribute('href', element
.getAttribute('href'));
416 document
.head
.appendChild(headBase
);
423 alert(req
.responseXML
.documentElement
.firstChild
.nodeValue
);
428 this.responseTextDest
.innerHTML
= req
.responseText
;
429 scripts
= this.responseTextDest
.getElementsByTagName('script');
431 for (k
=0 ; k
< scripts
.length
; k
++) {
432 globalScriptRegistry
.loadScript(scripts
[k
]);
436 var onAfterPopulate
= this.onAfterPopulate
;
438 this.scrollToPortalMessage();
439 var url
= this.form
.action
;
440 if (!this.noHistory
){ history
.pushState(url
, document
.title
, url
); }
443 FormManager
.prototype.scrollToPortalMessage = function() {
444 var psm
= document
.getElementById('DesktopStatusBar');
446 var msgOffset
= psm
.offsetTop
;
447 smoothScroll(window
.scrollY
, msgOffset
);
448 shake(psm
, 10, 1000);
452 FormManager
.prototype._fitField = function(evt
) {
453 var ob
= getTargetedObject(evt
);
454 var inputWidth
= ob
.value
.length
/ 1.9;
455 inputWidth
= (inputWidth
> 5) ? inputWidth
: 5;
456 ob
.style
.width
= inputWidth
+ 'em';
459 function initForms(baseElement
, lazy
) {
461 baseElement
= document
;
463 var dest
= document
.getElementById("mainCell");
464 var forms
= baseElement
.getElementsByTagName("form");
466 for (i
= 0 ; i
< forms
.length
; i
++ ) {
467 f
= new FormManager(forms
[i
], dest
, lazy
);
471 function smoothScroll(from, to
) {
483 var jump = function() {
484 window
.scroll(0, pos
);
485 pos
= pos
+ step
* dir
;
486 if ((dir
=== 1 && pos
>= to
) ||
487 (dir
=== -1 && pos
<= to
)) {
488 window
.clearInterval(intervalId
);
489 window
.scroll(0, to
);
492 intervalId
= window
.setInterval(jump
, 10);
495 /* adapted from http://xahlee.info/js/js_shake_box.html */
496 function shake(e, distance, time) {
498 if (!time) { time = 500; }
499 if (!distance) { distance = 5; }
501 // Save the original style of e, Make e relatively positioned, Note the animation start time, Start the animation
502 var originalStyle = e.style.cssText;
503 e.style.position = "relative";
504 var start = (new Date()).getTime();
506 // This function checks the elapsed time and updates the position of e.
507 // If the animation is complete, it restores e to its original state.
508 // Otherwise, it updates e's position and schedules itself to run again.
510 var now = (new Date()).getTime();
512 var elapsed = now-start;
513 // How long since we started
514 var fraction = elapsed/time;
515 // What fraction of total time?
517 // If the animation is not yet complete
518 // Compute the x position of e as a function of animation
519 // completion fraction. We use a sinusoidal function, and multiply
520 // the completion fraction by 4pi, so that it shakes back and
522 var x = distance * Math.sin(fraction*8*Math.PI);
523 e.style.left = x + "px";
524 // Try to run again in 25ms or at the end of the total time.
525 // We're aiming for a smooth 40 frames/second animation.
526 setTimeout(animate, Math.min(25, time-elapsed));
529 // Otherwise, the animation is complete
530 e.style.cssText = originalStyle; // Restore the original style