2008-01-09 Igor Zelmanovich <igorz@mainsoft.com>
[mono.git] / mcs / class / System.Web / resources / webform.js
index 1bd8bc1f7774377b8b3aaa1a5c5c8231a563ee51..b70701d46b4e9607d3aedb266779190f5f90f9e8 100644 (file)
@@ -4,6 +4,7 @@
  * Authors:
  *   Chris Toshok (toshok@ximian.com)
  *   Lluis Sanchez Gual (lluis@novell.com)
+ *   Igor Zelmanovich (igorz@mainsoft.com)
  *
  * (c) 2005 Novell, Inc. (http://www.novell.com)
  *
  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
+function WebForm_Initialize(webForm) {
 
-function WebForm_AutoFocus (id)
+webForm.__pendingCallbacks = new Array();
+
+webForm.WebForm_AutoFocus = function (id)
 {
-       var x = document.getElementById ? document.getElementById (id) :
-                                         ((document.all) ? document.all [id] : null);
+       var x = this.WebForm_GetElementById (id);
+
+       if (x && (!this.WebForm_CanFocus(x))) {
+               x = this.WebForm_FindFirstFocusableChild(x);
+       }
+       if (x) { x.focus(); }
+}
 
-       if (typeof (x) != 'undefined') {
-               x.focus();
+webForm.WebForm_CanFocus = function (element) {
+       if (!element || !(element.tagName) || element.disabled) {
+               return false;
+       }
+       if (element.type && element.type.toLowerCase() == "hidden") {
+               return false;
        }
+       var tagName = element.tagName.toLowerCase();
+       return (tagName == "input" ||
+                       tagName == "textarea" ||
+                       tagName == "select" ||
+                       tagName == "button" ||
+                       tagName == "a");
 }
 
-function wasControlEnabled (id)
+webForm.WebForm_FindFirstFocusableChild = function (element) {
+       if (!element || !(element.tagName)) {
+               return null;
+       }
+       var tagName = element.tagName.toLowerCase();
+       if (tagName == "undefined") {
+               return null;
+       }
+       var children = element.childNodes;
+       if (children) {
+               for (var i = 0; i < children.length; i++) {
+                       try {
+                               if (this.WebForm_CanFocus(children[i])) {
+                                       return children[i];
+                               }
+                               else {
+                                       var focused = this.WebForm_FindFirstFocusableChild(children[i]);
+                                       if (this.WebForm_CanFocus(focused)) {
+                                               return focused;
+                                       }
+                               }
+                       } catch (e) {
+                       }
+               }
+       }
+       return null;
+}
+
+webForm.WebForm_ReEnableControls = function  ()
 {
-       if (typeof (__enabledControlArray) == 'undefined')
+       if (typeof(this._form.__enabledControlArray) != 'undefined' && this._form.__enabledControlArray != null)
+               __enabledControlArray = this._form.__enabledControlArray;
+       
+       if (typeof(__enabledControlArray) == 'undefined' || __enabledControlArray == null)
                return false;
+       
+       this._form.__disabledControlArray = new Array();
+       for (var i = 0; i < __enabledControlArray.length; i++) {
+               var c = this.WebForm_GetElementById (__enabledControlArray[i]);
+               if ((typeof(c) != "undefined") && (c != null) && (c.disabled == true)) {
+                       c.disabled = false;
+                       this._form.__disabledControlArray[this._form.__disabledControlArray.length] = c;
+               }
+       }
+       setTimeout((this._instanceVariableName ? this._instanceVariableName + "." : "") + "WebForm_ReDisableControls ()", 0);
+       return true;
+}
+
+webForm.WebForm_ReDisableControls = function  ()
+{
+       for (var i = 0; i < this._form.__disabledControlArray.length; i++) {
+               this._form.__disabledControlArray[i].disabled = true;
+       }
+}
+
+webForm.WebForm_DoPostback = function (eventTarget, eventArgument, actionUrl, autoPostBack, validation, trackFocus, clientSubmit, validationGroup)
+{
+       webForm.WebForm_DoPostBackWithOptions({
+               "eventTarget" : eventTarget,
+               "eventArgument" : eventArgument,
+               "validation" : validation,
+               "validationGroup" : validationGroup,
+               "actionUrl" : actionUrl,
+               "trackFocus" : trackFocus,
+               "clientSubmit" : clientSubmit,
+               "autoPostBack" : autoPostBack
+               });
+}
+
+webForm.WebForm_DoPostBackWithOptions = function  (options) {
+       var validationResult = true;
+       if (options.validation && typeof(webForm.Page_ClientValidate) == "function")
+               validationResult =  webForm.Page_ClientValidate(options.validationGroup);
+
+       if (validationResult) {
+               if ((typeof(options.actionUrl) != "undefined") && (options.actionUrl != null) && (options.actionUrl.length > 0))
+                       webForm._form.action = options.actionUrl;
+               if (options.trackFocus) {
+                       var lastFocus = webForm._form.elements["__LASTFOCUS"];
+                       if ((typeof(lastFocus) != "undefined") && (lastFocus != null))
+                               lastFocus.value = options.eventTarget;
+               }
+       }               
+       if (options.clientSubmit)
+               webForm.__doPostBack (options.eventTarget, options.eventArgument);
+}
+
+webForm.WebForm_DoCallback = function (id, arg, callback, ctx, errorCallback, useAsync)
+{
+       var qs = webForm.WebForm_getFormData () + "__CALLBACKTARGET=" + id + "&__CALLBACKARGUMENT=" + encodeURIComponent(arg);
+  
+  if (webForm._form["__EVENTVALIDATION"])
+    qs += "&__EVENTVALIDATION=" + encodeURIComponent(webForm._form["__EVENTVALIDATION"].value);
+  
+       webForm.WebForm_httpPost (webForm._form.serverURL || document.URL, qs, function (httpPost) { webForm.WebForm_ClientCallback (httpPost, ctx, callback, errorCallback); });
+}
+
+webForm.WebForm_ClientCallback = function (httpPost, ctx, callback, errorCallback)
+{
+       var doc = httpPost.responseText;
+       if (doc.charAt(0) == "e") {
+               if ((typeof(errorCallback) != "undefined") && (errorCallback != null))
+                       errorCallback(doc.substring(1), ctx);
+       } else {
+               var separatorIndex = doc.indexOf("|");
+               if (separatorIndex != -1) {
+                       var validationFieldLength = parseInt(doc.substring(0, separatorIndex));
+                       if (!isNaN(validationFieldLength)) {
+                               var validationField = doc.substring(separatorIndex + 1, separatorIndex + validationFieldLength + 1);
+                               if (validationField != "") {
+                                       var validationFieldElement = this._form["__EVENTVALIDATION"];
+                                       if (!validationFieldElement) {
+                                               validationFieldElement = document.createElement("INPUT");
+                                               validationFieldElement.type = "hidden";
+                                               validationFieldElement.name = "__EVENTVALIDATION";
+                                               validationFieldElement.id = validationFieldElement.name;
+                                               this._form.appendChild(validationFieldElement);
+                                       }
+                                       validationFieldElement.value = validationField;
+                               }
+                               if ((typeof(callback) != "undefined") && (callback != null))
+                                       callback (doc.substring(separatorIndex + validationFieldLength + 1), ctx);
+                       }
+               } else {
+                       if ((typeof(callback) != "undefined") && (callback != null))
+                               callback (doc, ctx);
+               }
+       }
+}
+
+webForm.WebForm_getFormData = function ()
+{
+       var qs = "";
+       var len = this._form.elements.length;
+       for (n=0; n<len; n++) {
+               var elem = this._form.elements [n];
+               var tagName = elem.tagName.toLowerCase();
+               if (tagName == "input") {
+                       var type = elem.type;
+                       if ((type == "text" || type == "hidden" || type == "password" ||
+                               ((type == "checkbox" || type == "radio") && elem.checked)) &&
+          (elem.id != "__EVENTVALIDATION")) {
+                               qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
+                       }
+               } else if (tagName == "select") {
+                       var selectCount = elem.options.length;
+                       for (var j = 0; j < selectCount; j++) {
+                               var selectChild = elem.options[j];
+                               if (selectChild.selected == true) {
+                                       qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
+                               }
+                       }
+               }
+               else if (tagName == "textarea") {
+                       qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
+               }
+       }
+       return qs;
+}
 
-       for (var i = 0; i < __enabledControlArray.length; i ++) {
-               if (id == __enabledControlArray[i])
-                       return true;
+webForm.WebForm_httpPost = function (url, data, callback)
+{
+       var httpPost = null;
+       
+       if (typeof XMLHttpRequest != "undefined") {
+               httpPost = new XMLHttpRequest ();
+       } else {
+               if (this.axName != null)
+                       httpPost = new ActiveXObject (this.axName);
+               else {
+                       var clsnames = new Array ("MSXML", "MSXML2", "MSXML3", "Microsoft");
+                       for (n = 0; n < clsnames.length && httpPost == null; n++) {
+                               this.axName = clsnames [n] + ".XMLHTTP";
+                               try {
+                                       httpPost = new ActiveXObject (this.axName);
+                               } catch (e) { this.axName = null; }
+                       }
+                       if (httpPost == null)
+                               throw new Error ("XMLHTTP object could not be created.");
+               }
+       }
+       httpPost.onreadystatechange = function () { if (httpPost.readyState == 4) callback (httpPost); };
+       
+       httpPost.open ("POST", url, true);      // async
+       httpPost.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
+       setTimeout (function () { httpPost.send (data); }, 10);
+}
+
+webForm.WebForm_GetElementById = function (id)
+{
+       return document.getElementById ? document.getElementById (id) :
+              document.all ? document.all [id] :
+                  document [id];
+}
+
+webForm.WebForm_FireDefaultButton = function (event, target)
+{
+       if (event.keyCode != 13) {
+               return true;
+       }
+       if(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea")) {
+               return true;
+       }
+       var defaultButton = this.WebForm_GetElementById(target);
+       if (!defaultButton)
+               return true;
+       
+       if (typeof(defaultButton.click) != "undefined") {
+               defaultButton.click();
+               event.cancelBubble = true;
+               return false;
+       }
+       
+       if (defaultButton.href && defaultButton.href.match(/^javascript:/i)) {
+               var jsCode = defaultButton.href.match(/^javascript:(.*)/i)[1]; 
+               eval(jsCode);
+               event.cancelBubble = true;
+               return false;
        }
+       
+       return true;
+}
 
-       return false;
+webForm.WebForm_SaveScrollPositionSubmit = function ()
+{
+       var pos = this.WebForm_GetElementPosition(this._form);
+       this._form.elements['__SCROLLPOSITIONX'].value = this.WebForm_GetScrollX() - pos.x;
+       this._form.elements['__SCROLLPOSITIONY'].value = this.WebForm_GetScrollY() - pos.y;
+       if ((typeof(this._form.oldSubmit) != "undefined") && (this._form.oldSubmit != null)) {
+               return this._form.oldSubmit();
+       }
+       return true;
 }
 
-function WebForm_ReEnableControls()
+webForm.WebForm_SaveScrollPositionOnSubmit = function ()
 {
-       if (typeof (theForm) == 'undefined')
-               return;
+       var pos = this.WebForm_GetElementPosition(this._form);
+       this._form.elements['__SCROLLPOSITIONX'].value = this.WebForm_GetScrollX() - pos.x;
+       this._form.elements['__SCROLLPOSITIONY'].value = this.WebForm_GetScrollY() - pos.y;
+       if ((typeof(this._form.oldOnSubmit) != "undefined") && (this._form.oldOnSubmit != null)) {
+               return this._form.oldOnSubmit();
+       }
+       return true;
+}
 
-       for (var i = 0; i < theForm.childNodes.length; i ++) {
-               var node = theForm.childNodes[i];
-               if (node.disabled && wasControlEnabled (node.id))
-                       node.disabled = false;
+webForm.WebForm_RestoreScrollPosition = function ()
+{
+       var pos = this.WebForm_GetElementPosition(this._form);
+       var ScrollX = parseInt(this._form.elements['__SCROLLPOSITIONX'].value);
+       var ScrollY = parseInt(this._form.elements['__SCROLLPOSITIONY'].value);
+       ScrollX = (isNaN(ScrollX)) ? pos.x : (ScrollX + pos.x);
+       ScrollY = (isNaN(ScrollY)) ? pos.y : (ScrollY + pos.y);
+       window.scrollTo(ScrollX, ScrollY);
+       if ((typeof(this._form.oldOnLoad) != "undefined") && (this._form.oldOnLoad != null)) {
+               return this._form.oldOnLoad();
        }
+       return true;
+}
+
+webForm.WebForm_GetScrollX = function () {
+    if (window.pageXOffset) {
+        return window.pageXOffset;
+    }
+    else if (document.documentElement && document.documentElement.scrollLeft) {
+        return document.documentElement.scrollLeft;
+    }
+    else if (document.body) {
+        return document.body.scrollLeft;
+    }
+    return 0;
 }
 
-function WebForm_DoPostback (ctrl, par, url, apb, pval, tf, csubm, vg)
+webForm.WebForm_GetScrollY = function () {
+    if (window.pageYOffset) {
+        return window.pageYOffset;
+    }
+    else if (document.documentElement && document.documentElement.scrollTop) {
+        return document.documentElement.scrollTop;
+    }
+    else if (document.body) {
+        return document.body.scrollTop;
+    }
+    return 0;
+}
+
+webForm.WebForm_GetElementPosition = function (element)
 {
-       if (pval && typeof(Page_ClientValidate) == "function" && !Page_ClientValidate(vg))
-               return;
-
-       if (url != null)
-               theForm.action = url;
-               
-       if (csubm)
-               __doPostBack (ctrl, par);
+       var result = new Object();
+       result.x = 0;
+       result.y = 0;
+       result.width = 0;
+       result.height = 0;
+       result.x = element.offsetLeft;
+       result.y = element.offsetTop;
+       var parent = element.offsetParent;
+       while (parent) {
+               result.x += parent.offsetLeft;
+               result.y += parent.offsetTop;
+               parent = parent.offsetParent;
+       }
+       result.width = element.offsetWidth;
+       result.height = element.offsetHeight;
+       return result;
+}
 }