2007-02-19 Igor Zelmanovich <igorz@mainsoft.com>
[mono.git] / mcs / class / System.Web / resources / webform.js
1 /*
2  * WebForm.js
3  *
4  * Authors:
5  *   Chris Toshok (toshok@ximian.com)
6  *   Lluis Sanchez Gual (lluis@novell.com)
7  *   Igor Zelmanovich (igorz@mainsoft.com)
8  *
9  * (c) 2005 Novell, Inc. (http://www.novell.com)
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining
12  * a copy of this software and associated documentation files (the
13  * "Software"), to deal in the Software without restriction, including
14  * without limitation the rights to use, copy, modify, merge, publish,
15  * distribute, sublicense, and/or sell copies of the Software, and to
16  * permit persons to whom the Software is furnished to do so, subject to
17  * the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be
20  * included in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29  */
30
31
32 function WebForm_AutoFocus (id)
33 {
34         var x = WebForm_GetElementById (id);
35
36         if (x && (!WebForm_CanFocus(x))) {
37                 x = WebForm_FindFirstFocusableChild(x);
38         }
39         if (x) { x.focus(); }
40 }
41
42 function WebForm_CanFocus(element) {
43         if (!element || !(element.tagName) || element.disabled) {
44                 return false;
45         }
46         if (element.type && element.type.toLowerCase() == "hidden") {
47                 return false;
48         }
49         var tagName = element.tagName.toLowerCase();
50         return (tagName == "input" ||
51                         tagName == "textarea" ||
52                         tagName == "select" ||
53                         tagName == "button" ||
54                         tagName == "a");
55 }
56
57 function WebForm_FindFirstFocusableChild(element) {
58         if (!element || !(element.tagName)) {
59                 return null;
60         }
61         var tagName = element.tagName.toLowerCase();
62         if (tagName == "undefined") {
63                 return null;
64         }
65         var children = element.childNodes;
66         if (children) {
67                 for (var i = 0; i < children.length; i++) {
68                         try {
69                                 if (WebForm_CanFocus(children[i])) {
70                                         return children[i];
71                                 }
72                                 else {
73                                         var focused = WebForm_FindFirstFocusableChild(children[i]);
74                                         if (WebForm_CanFocus(focused)) {
75                                                 return focused;
76                                         }
77                                 }
78                         } catch (e) {
79                         }
80                 }
81         }
82         return null;
83 }
84
85 function WebForm_ReEnableControls (currForm)
86 {
87         if (typeof(currForm.__enabledControlArray) != 'undefined' && currForm.__enabledControlArray != null)
88                 __enabledControlArray = currForm.__enabledControlArray;
89         
90         if (typeof(__enabledControlArray) == 'undefined' || __enabledControlArray == null)
91                 return false;
92         
93         currForm.__disabledControlArray = new Array();
94         for (var i = 0; i < __enabledControlArray.length; i++) {
95                 var c = document.getElementById(__enabledControlArray[i]);
96                 if ((typeof(c) != "undefined") && (c != null) && (c.disabled == true)) {
97                         c.disabled = false;
98                         currForm.__disabledControlArray[currForm.__disabledControlArray.length] = c;
99                 }
100         }
101         __currForm = currForm
102         setTimeout("WebForm_ReDisableControls (__currForm)", 0);
103         return true;
104 }
105
106 function WebForm_ReDisableControls (currForm)
107 {
108         for (var i = 0; i < currForm.__disabledControlArray.length; i++) {
109                 currForm.__disabledControlArray[i].disabled = true;
110         }
111 }
112
113 // This function is only used in the context of TARGET_J2EE for portlets
114 function PortalWebForm_DoPostback (id, par, url, apb, pval, tf, csubm, vg, currForm)
115 {
116         if (url != null) {
117                 currForm = WebForm_GetFormFromCtrl (id, currForm);
118                 if (url.indexOf ("vmw.action.page=") == 0) {
119                         currForm.__NEXTVMWACTIONPAGE.value = url.substring ("vmw.action.page=".length);
120                         url = currForm.action;
121                 }
122                 else if (url.indexOf ("vmw.render.page=") == 0)
123                 {
124                         currForm.__NEXTVMWRENDERPAGE.value = url.substring ("vmw.render.page=".length);
125                         currForm.submit ();
126                         return;
127                 }
128         }
129         return WebForm_DoPostback (id, par, url, apb, pval, tf, csubm, vg, currForm);
130 }
131 function WebForm_DoPostback (id, par, url, apb, pval, tf, csubm, vg, currForm)
132 {
133         currForm = WebForm_GetFormFromCtrl (id, currForm);
134
135         if (typeof(SetValidatorContext) == "function") 
136                 SetValidatorContext (currForm);
137
138         var validationResult = true;
139         if (pval && typeof(Page_ClientValidate) == "function")
140                 validationResult =  Page_ClientValidate(vg);
141
142         if (validationResult && url != null)
143                 currForm.action = url;
144                 
145         if (csubm)
146                 currForm.__doPostBack (id, par);
147 }
148
149 function WebForm_DoCallback (id, arg, callback, ctx, errorCallback, currForm)
150 {
151         currForm = WebForm_GetFormFromCtrl (id, currForm);
152         var qs = WebForm_getFormData (currForm) + "__CALLBACKTARGET=" + id + "&__CALLBACKARGUMENT=" + encodeURIComponent(arg);
153         if (currForm["__EVENTVALIDATION"]) qs += "&__EVENTVALIDATION=" + encodeURIComponent(currForm["__EVENTVALIDATION"].value);
154         WebForm_httpPost (currForm.serverURL || document.URL, qs, function (httpPost) { WebForm_ClientCallback (httpPost, ctx, callback, errorCallback, currForm); });
155 }
156
157 function WebForm_ClientCallback (httpPost, ctx, callback, errorCallback, currForm)
158 {
159         var doc = httpPost.responseText;
160         if (doc.charAt(0) == "e") {
161                 if ((typeof(errorCallback) != "undefined") && (errorCallback != null))
162                         errorCallback(doc.substring(1), ctx);
163         } else {
164                 var separatorIndex = doc.indexOf("|");
165                 if (separatorIndex != -1) {
166                         var validationFieldLength = parseInt(doc.substring(0, separatorIndex));
167                         if (!isNaN(validationFieldLength)) {
168                                 var validationField = doc.substring(separatorIndex + 1, separatorIndex + validationFieldLength + 1);
169                                 if (validationField != "") {
170                                         var validationFieldElement = currForm["__EVENTVALIDATION"];
171                                         if (!validationFieldElement) {
172                                                 validationFieldElement = document.createElement("INPUT");
173                                                 validationFieldElement.type = "hidden";
174                                                 validationFieldElement.name = "__EVENTVALIDATION";
175                                                 currForm.appendChild(validationFieldElement);
176                                         }
177                                         validationFieldElement.value = validationField;
178                                 }
179                                 if ((typeof(callback) != "undefined") && (callback != null))
180                                         callback (doc.substring(separatorIndex + validationFieldLength + 1), ctx);
181                         }
182                 } else {
183                         if ((typeof(callback) != "undefined") && (callback != null))
184                                 callback (doc, ctx);
185                 }
186         }
187 }
188
189 function WebForm_getFormData (currForm)
190 {
191         var qs = "";
192         var len = currForm.elements.length;
193         for (n=0; n<len; n++) {
194                 var elem = currForm.elements [n];
195                 var tagName = elem.tagName.toLowerCase();
196                 if (tagName == "input") {
197                         var type = elem.type;
198                         if ((type == "text" || type == "hidden" || type == "password" ||
199                                 ((type == "checkbox" || type == "radio") && elem.checked)) &&
200                                 (elem.id != "__EVENTVALIDATION")) {
201                                 qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
202                         }
203                 }
204                 else if (tagName == "select") {
205                         var selectCount = elem.options.length;
206                         for (var j = 0; j < selectCount; j++) {
207                                 var selectChild = elem.options[j];
208                                 if (selectChild.selected == true) {
209                                         qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
210                                 }
211                         }
212                 }
213                 else if (tagName == "textarea") {
214                         qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
215                 }
216         }
217         return qs;
218 }
219
220 var axName = null;
221 function WebForm_httpPost (url, data, callback)
222 {
223         var httpPost = null;
224         
225         if (typeof XMLHttpRequest != "undefined") {
226                 httpPost = new XMLHttpRequest ();
227         } else {
228                 if (axName != null)
229                         httpPost = new ActiveXObject (axName);
230                 else {
231                         var clsnames = new Array ("MSXML", "MSXML2", "MSXML3", "Microsoft");
232                         for (n = 0; n < clsnames.length && httpPost == null; n++) {
233                                 axName = clsnames [n] + ".XMLHTTP";
234                                 try {
235                                         httpPost = new ActiveXObject (axName);
236                                 } catch (e) { axName = null; }
237                         }
238                         if (httpPost == null)
239                                 throw new Error ("XMLHTTP object could not be created.");
240                 }
241         }
242         httpPost.onreadystatechange = function () { if (httpPost.readyState == 4) callback (httpPost); };
243         
244         httpPost.open ("POST", url, true);      // async
245         httpPost.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
246         setTimeout (function () { httpPost.send (data); }, 10);
247 }
248
249 function WebForm_GetFormFromCtrl (id, currForm)
250 {
251         if (currForm)
252                 return currForm;
253
254         // We need to translate the id from ASPX UniqueID to its ClientID.
255         var ctrl = WebForm_GetElementById (id.replace(/\$/g, "_"));
256         while (ctrl != null) {
257                 if (ctrl.isAspForm)
258                         return ctrl;
259                 ctrl = ctrl.parentNode;
260         }
261         return theForm;
262 }
263
264 function WebForm_GetElementById (id)
265 {
266         return document.getElementById ? document.getElementById (id) :
267                document.all ? document.all [id] :
268                    document [id];
269 }
270
271 function WebForm_FireDefaultButton(event, target)
272 {
273         if (event.keyCode != 13) {
274                 return true;
275         }
276         if(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea")) {
277                 return true;
278         }
279         var defaultButton = WebForm_GetElementById(target);
280         if (defaultButton && typeof(defaultButton.click) != "undefined") {
281                 defaultButton.click();
282                 event.cancelBubble = true;
283                 return false;
284         }
285         return true;
286 }
287
288 function WebForm_SaveScrollPositionSubmit() {
289     this.elements['__SCROLLPOSITIONX'].value = WebForm_GetScrollX();
290     this.elements['__SCROLLPOSITIONY'].value = WebForm_GetScrollY();
291     if ((typeof(this.oldSubmit) != "undefined") && (this.oldSubmit != null)) {
292         return this.oldSubmit();
293     }
294     return true;
295 }
296
297 function WebForm_SaveScrollPositionOnSubmit() {
298     this.elements['__SCROLLPOSITIONX'].value = WebForm_GetScrollX();
299     this.elements['__SCROLLPOSITIONY'].value = WebForm_GetScrollY();
300     if ((typeof(this.oldOnSubmit) != "undefined") && (this.oldOnSubmit != null)) {
301         return this.oldOnSubmit();
302     }
303     return true;
304 }
305
306 function WebForm_RestoreScrollPosition(currForm) {
307         currForm = currForm || theForm;
308         var ScrollX = currForm.elements['__SCROLLPOSITIONX'].value;
309         var ScrollY = currForm.elements['__SCROLLPOSITIONY'].value;
310         if (ScrollX != "" || ScrollY != "")
311         window.scrollTo(ScrollX, ScrollY);
312     if ((typeof(this.oldOnLoad) != "undefined") && (this.oldOnLoad != null)) {
313         return this.oldOnLoad();
314     }
315     return true;
316 }
317
318 function WebForm_GetScrollX() {
319     if (window.pageXOffset) {
320         return window.pageXOffset;
321     }
322     else if (document.documentElement && document.documentElement.scrollLeft) {
323         return document.documentElement.scrollLeft;
324     }
325     else if (document.body) {
326         return document.body.scrollLeft;
327     }
328     return 0;
329 }
330
331 function WebForm_GetScrollY() {
332     if (window.pageYOffset) {
333         return window.pageYOffset;
334     }
335     else if (document.documentElement && document.documentElement.scrollTop) {
336         return document.documentElement.scrollTop;
337     }
338     else if (document.body) {
339         return document.body.scrollTop;
340     }
341     return 0;
342 }
343
344