X-Git-Url: https://scm.cri.minesparis.psl.eu/git/Plinn.git/blobdiff_plain/c0074b891b1f9f79f152b1c2f50431d77ea77192..959d888c17d1403d2eeecc19bc4b5e2c8d1debf6:/skins/ajax_scripts/ajax_form_manager.js?ds=inline diff --git a/skins/ajax_scripts/ajax_form_manager.js b/skins/ajax_scripts/ajax_form_manager.js deleted file mode 100644 index 508a4f9..0000000 --- a/skins/ajax_scripts/ajax_form_manager.js +++ /dev/null @@ -1,523 +0,0 @@ -// (c) Benoît PIN 2006-2007 -// http://plinn.org -// Licence GPL -// -// - -var FormManager; - -(function(){ - -FormManager = function(form, responseTextDest, lazy) { - if (form.elements.namedItem("noAjax")) {return;} - - this.form = form; - this.responseTextDest = responseTextDest; - this.lazy = lazy; - var thisManager = this; - this.form.onsubmit = function(evt) { thisManager.submit(evt); }; - this.form.onclick = function(evt) { thisManager.click(evt); }; - - /* raised on form submit */ - this.onBeforeSubmit = null; - /* raised after xmlhttp response */ - this.onResponseLoad = null; - /* raised when the responseText is added inside the main element. - * (onResponseLoad may have the default value) */ - this.onAfterPopulate = null; - this.submitButton = null; - - if (this.lazy) { - this.form.onclick = function(evt){ - thisManager.replaceElementByField(evt); - thisManager.click(evt); - }; - if (browser.isDOM2Event) { - this.form.onfocus = this.form.onclick; - } - else if (browser.isIE6up) { - this.form.onfocusin = this.form.onclick; - } - this.onResponseLoad = function(req){ thisManager.restoreField(req); }; - this.lazyListeners = []; - } -}; - -FormManager.prototype.submit = function(evt) { - var form = this.form; - var thisManager = this; - - var bsMessage; // before submit message - if (!this.onBeforeSubmit) { - var onBeforeSubmit = form.elements.namedItem("onBeforeSubmit"); - if (onBeforeSubmit) { - if (onBeforeSubmit.length) { - onBeforeSubmit = onBeforeSubmit[0]; - } - /*jslint evil: true */ - this.onBeforeSubmit = eval(onBeforeSubmit.value); - bsMessage = this.onBeforeSubmit(thisManager, evt); - } - } - else { - bsMessage = this.onBeforeSubmit(thisManager, evt); - } - - if (bsMessage === 'cancelSubmit') { - try {disableDefault(evt);} - catch (e){} - return; - } - - if (!this.onResponseLoad) { - var onResponseLoad = form.elements.namedItem("onResponseLoad"); - if (onResponseLoad) { - this.onResponseLoad = eval(onResponseLoad.value); - } - else { - this.onResponseLoad = this.loadResponse; - } - } - - var submitButton = this.submitButton; - var queryInfo = this.formData2QueryString(); - var query = queryInfo.query; - this.hasFile = queryInfo.hasFile; - - - if (!this.onAfterPopulate) { - var onAfterPopulate = form.elements.namedItem("onAfterPopulate"); - if (onAfterPopulate) { - this.onAfterPopulate = onAfterPopulate.value; - } - else { - this.onAfterPopulate = function() {}; - } - } - - if (submitButton) { - query += submitButton.name + '=' + submitButton.value + '&'; - } - - if (window.AJAX_CONFIG && ((AJAX_CONFIG & 1) === 1)) { - if (form.method.toLowerCase() === 'post') { - this._post(query); - } - else { - this._get(query); - } - } - else { - this._post(query); - } - - try {disableDefault(evt);} - catch (e2){} -}; - -FormManager.prototype._post = function(query) { - // send form by XmlHttpRequest - query += "ajax=1"; - - var req = new XMLHttpRequest(); - var thisManager = this; - req.onreadystatechange = function() { - switch (req.readyState) { - case 1 : - showProgressImage(); - break; - case 4 : - hideProgressImage(); - if (req.status === 200 || req.status === 204) { - thisManager.onResponseLoad(req); - } - else { - alert('Error: ' + req.status); - } - break; - } - }; - var url = this.form.action; - req.open("POST", url, true); - req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); - req.send(query); -}; - -FormManager.prototype._get = function(query) { - // send form by browser location - var url = this.form.action; - url += '?' + query; - linkHandler.loadUrl(url); -}; - - -FormManager.prototype.click = function(evt) { - var target = getTargetedObject(evt); - if(target.type === "submit" || target.type === "image") { - this.submitButton = target; - disablePropagation(evt); - } -}; - -FormManager.prototype.replaceElementByField = function(evt) { - evt = getEventObject(evt); - var ob = getTargetedObject(evt); - var eventType = evt.type; - if (eventType === 'focus' || eventType === 'focusin') { - if (this.liveFormField && ob.tagName !== 'INPUT') { - this.pendingEvent = [ob, 'click']; - } - return; - } - var fieldName = ob.getAttribute('id'); - if (fieldName) { - this.fieldTagName = ob.tagName; - var tabIndex = ob.tabIndex; - var text; - if (ob.firstChild && ob.firstChild.className === 'hidden_value') { - text = ob.firstChild.innerHTML; - } - else { - text = ob.innerHTML; - } - disablePropagation(evt); - var parent; - thisManager = this; - switch (ob.tagName) { - case 'SPAN' : - // create input element - var inputText = document.createElement("input"); - inputText.setAttribute("type", "text"); - text = text.replace(/\n/g, ' '); - text = text.replace(/\s+/g, ' '); - text = text.replace(/^ /, ''); - text = text.replace(/ $/, ''); - inputText.setAttribute("value", text); - var inputWidth = text.length / 1.9; - inputWidth = (inputWidth > 5) ? inputWidth : 5; - inputText.style.width = inputWidth + 'em'; - //inputText.setAttribute("size", text.length); - - // replacement - parent = ob.parentNode; - parent.replaceChild(inputText, ob); - - inputText.focus(); - inputText.select(); - inputText.setAttribute('name', fieldName); - inputText.tabIndex = tabIndex; - inputText.className = 'live_field'; - this.liveFormField = inputText; - this.lazyListeners.push({'element': inputText, 'eventName' : 'blur', 'handler': function(){ thisManager.submit();}}); - this.lazyListeners.push({'element': inputText, 'eventName' : 'keypress', 'handler': function(evt){ thisManager._fitField(evt);}}); - this._addLazyListeners(); - break; - - case 'DIV' : - case 'P' : - // create textarea - var ta = document.createElement('textarea'); - ta.style.display = 'block'; - ta.className = 'live_field'; - text = text.replace(/^\s*/, ''); - text = text.replace(/\s*$/, ''); - ta.value = text; - - // replacement - parent = ob.parentNode; - parent.replaceChild(ta, ob); - - ta.focus(); - ta.select(); - ta.setAttribute('name', fieldName); - ta.tabIndex = tabIndex; - this.liveFormField = ta; - this.lazyListeners.push({'element': ta, 'eventName' : 'blur', 'handler': function(){ thisManager.submit();}}); - this._addLazyListeners(); - break; - } - } -}; - -FormManager.prototype._addLazyListeners = function() { - var i, handlerInfo; - for (i=0 ; i 5) ? inputWidth : 5; - ob.style.width = inputWidth + 'em'; -}; - -function initForms(baseElement, lazy) { - if (!baseElement) { - baseElement = document; - } - var dest = document.getElementById("mainCell"); - var forms = baseElement.getElementsByTagName("form"); - var f, i; - for (i = 0 ; i < forms.length ; i++ ) { - f = new FormManager(forms[i], dest, lazy); - } -} - -function smoothScroll(from, to) { - var intervalId; - var step = 25; - var pos = from; - var dir; - if (to > from) { - dir = 1; - } - else { - dir = -1; - } - - var jump = function() { - window.scroll(0, pos); - pos = pos + step * dir; - if ((dir === 1 && pos >= to) || - (dir === -1 && pos <= to)) { - window.clearInterval(intervalId); - window.scroll(0, to); - } - }; - intervalId = window.setInterval(jump, 10); -} - -/* adapted from http://xahlee.info/js/js_shake_box.html */ -function shake(e, distance, time) { - // Handle arguments - if (!time) { time = 500; } - if (!distance) { distance = 5; } - - // Save the original style of e, Make e relatively positioned, Note the animation start time, Start the animation - var originalStyle = e.style.cssText; - e.style.position = "relative"; - var start = (new Date()).getTime(); - - // This function checks the elapsed time and updates the position of e. - // If the animation is complete, it restores e to its original state. - // Otherwise, it updates e's position and schedules itself to run again. - function animate() { - var now = (new Date()).getTime(); - // Get current time - var elapsed = now-start; - // How long since we started - var fraction = elapsed/time; - // What fraction of total time? - if (fraction < 1) { - // If the animation is not yet complete - // Compute the x position of e as a function of animation - // completion fraction. We use a sinusoidal function, and multiply - // the completion fraction by 4pi, so that it shakes back and - // forth twice. - var x = distance * Math.sin(fraction*8*Math.PI); - e.style.left = x + "px"; - // Try to run again in 25ms or at the end of the total time. - // We're aiming for a smooth 40 frames/second animation. - setTimeout(animate, Math.min(25, time-elapsed)); - } - else { - // Otherwise, the animation is complete - e.style.cssText = originalStyle; // Restore the original style - } - } - animate(); -} - -}());