31aeea16a8f90e093519faf35027c26c3336cfc2
[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 = WebForm_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) {
143                 if ((typeof(url) != "undefined") && (url != null) && (url.length > 0))
144                         currForm.action = url;
145                 if (tf) {
146                         var lastFocus = currForm.elements["__LASTFOCUS"];
147                         if ((typeof(lastFocus) != "undefined") && (lastFocus != null))
148                                 lastFocus.value = id;
149                 }
150         }               
151         if (csubm)
152                 currForm.__doPostBack (id, par);
153 }
154
155 function WebForm_DoCallback (id, arg, callback, ctx, errorCallback, useAsync, currForm)
156 {
157         currForm = WebForm_GetFormFromCtrl (id, currForm);
158         var qs = WebForm_getFormData (currForm) + "__CALLBACKTARGET=" + id + "&__CALLBACKARGUMENT=" + encodeURIComponent(arg);
159   
160   if (currForm["__EVENTVALIDATION"])
161     qs += "&__EVENTVALIDATION=" + encodeURIComponent(currForm["__EVENTVALIDATION"].value);
162   
163         WebForm_httpPost (currForm.serverURL || document.URL, qs, function (httpPost) { WebForm_ClientCallback (httpPost, ctx, callback, errorCallback, currForm); });
164 }
165
166 function WebForm_ClientCallback (httpPost, ctx, callback, errorCallback, currForm)
167 {
168         var doc = httpPost.responseText;
169         if (doc.charAt(0) == "e") {
170                 if ((typeof(errorCallback) != "undefined") && (errorCallback != null))
171                         errorCallback(doc.substring(1), ctx);
172         } else {
173                 var separatorIndex = doc.indexOf("|");
174                 if (separatorIndex != -1) {
175                         var validationFieldLength = parseInt(doc.substring(0, separatorIndex));
176                         if (!isNaN(validationFieldLength)) {
177                                 var validationField = doc.substring(separatorIndex + 1, separatorIndex + validationFieldLength + 1);
178                                 if (validationField != "") {
179                                         var validationFieldElement = currForm["__EVENTVALIDATION"];
180                                         if (!validationFieldElement) {
181                                                 validationFieldElement = document.createElement("INPUT");
182                                                 validationFieldElement.type = "hidden";
183                                                 validationFieldElement.name = "__EVENTVALIDATION";
184             validationFieldElement.id = validationFieldElement.name;
185                                                 currForm.appendChild(validationFieldElement);
186                                         }
187                                         validationFieldElement.value = validationField;
188                                 }
189                                 if ((typeof(callback) != "undefined") && (callback != null))
190                                         callback (doc.substring(separatorIndex + validationFieldLength + 1), ctx);
191                         }
192                 } else {
193                         if ((typeof(callback) != "undefined") && (callback != null))
194                                 callback (doc, ctx);
195                 }
196         }
197 }
198
199 function WebForm_getFormData (currForm)
200 {
201         var qs = "";
202         var len = currForm.elements.length;
203         for (n=0; n<len; n++) {
204                 var elem = currForm.elements [n];
205                 var tagName = elem.tagName.toLowerCase();
206                 if (tagName == "input") {
207                         var type = elem.type;
208                         if ((type == "text" || type == "hidden" || type == "password" ||
209                                 ((type == "checkbox" || type == "radio") && elem.checked)) &&
210           (elem.id != "__EVENTVALIDATION")) {
211                                 qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
212                         }
213                 } else if (tagName == "select") {
214                         var selectCount = elem.options.length;
215                         for (var j = 0; j < selectCount; j++) {
216                                 var selectChild = elem.options[j];
217                                 if (selectChild.selected == true) {
218                                         qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
219                                 }
220                         }
221                 }
222                 else if (tagName == "textarea") {
223                         qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
224                 }
225         }
226         return qs;
227 }
228
229 var axName = null;
230 function WebForm_httpPost (url, data, callback)
231 {
232         var httpPost = null;
233         
234         if (typeof XMLHttpRequest != "undefined") {
235                 httpPost = new XMLHttpRequest ();
236         } else {
237                 if (axName != null)
238                         httpPost = new ActiveXObject (axName);
239                 else {
240                         var clsnames = new Array ("MSXML", "MSXML2", "MSXML3", "Microsoft");
241                         for (n = 0; n < clsnames.length && httpPost == null; n++) {
242                                 axName = clsnames [n] + ".XMLHTTP";
243                                 try {
244                                         httpPost = new ActiveXObject (axName);
245                                 } catch (e) { axName = null; }
246                         }
247                         if (httpPost == null)
248                                 throw new Error ("XMLHTTP object could not be created.");
249                 }
250         }
251         httpPost.onreadystatechange = function () { if (httpPost.readyState == 4) callback (httpPost); };
252         
253         httpPost.open ("POST", url, true);      // async
254         httpPost.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
255         setTimeout (function () { httpPost.send (data); }, 10);
256 }
257
258 function WebForm_GetFormFromCtrl (id, currForm)
259 {
260         if (currForm)
261                 return currForm;
262
263         // We need to translate the id from ASPX UniqueID to its ClientID.
264         var ctrl = WebForm_GetElementById (id.replace(/\$/g, "_"));
265         while (ctrl != null) {
266                 if (ctrl.isAspForm)
267                         return ctrl;
268                 ctrl = ctrl.parentNode;
269         }
270         return theForm;
271 }
272
273 function WebForm_GetElementById (id)
274 {
275         return document.getElementById ? document.getElementById (id) :
276                document.all ? document.all [id] :
277                    document [id];
278 }
279
280 function WebForm_FireDefaultButton(event, target)
281 {
282         if (event.keyCode != 13) {
283                 return true;
284         }
285         if(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea")) {
286                 return true;
287         }
288         var defaultButton = WebForm_GetElementById(target);
289         if (defaultButton && typeof(defaultButton.click) != "undefined") {
290                 defaultButton.click();
291                 event.cancelBubble = true;
292                 return false;
293         }
294         return true;
295 }
296
297 function WebForm_SaveScrollPositionSubmit()
298 {
299         var pos = WebForm_GetElementPosition(this);
300         this.elements['__SCROLLPOSITIONX'].value = WebForm_GetScrollX() - pos.x;
301         this.elements['__SCROLLPOSITIONY'].value = WebForm_GetScrollY() - pos.y;
302         if ((typeof(this.oldSubmit) != "undefined") && (this.oldSubmit != null)) {
303                 return this.oldSubmit();
304         }
305         return true;
306 }
307
308 function WebForm_SaveScrollPositionOnSubmit()
309 {
310         var pos = WebForm_GetElementPosition(this);
311         this.elements['__SCROLLPOSITIONX'].value = WebForm_GetScrollX() - pos.x;
312         this.elements['__SCROLLPOSITIONY'].value = WebForm_GetScrollY() - pos.y;
313         if ((typeof(this.oldOnSubmit) != "undefined") && (this.oldOnSubmit != null)) {
314                 return this.oldOnSubmit();
315         }
316         return true;
317 }
318
319 function WebForm_RestoreScrollPosition(currForm)
320 {
321         currForm = currForm || theForm;
322         var pos = WebForm_GetElementPosition(currForm);
323         var ScrollX = parseInt(currForm.elements['__SCROLLPOSITIONX'].value);
324         var ScrollY = parseInt(currForm.elements['__SCROLLPOSITIONY'].value);
325         ScrollX = (isNaN(ScrollX)) ? pos.x : (ScrollX + pos.x);
326         ScrollY = (isNaN(ScrollY)) ? pos.y : (ScrollY + pos.y);
327         window.scrollTo(ScrollX, ScrollY);
328         if ((typeof(currForm.oldOnLoad) != "undefined") && (currForm.oldOnLoad != null)) {
329                 return currForm.oldOnLoad();
330         }
331         return true;
332 }
333
334 function WebForm_GetScrollX() {
335     if (window.pageXOffset) {
336         return window.pageXOffset;
337     }
338     else if (document.documentElement && document.documentElement.scrollLeft) {
339         return document.documentElement.scrollLeft;
340     }
341     else if (document.body) {
342         return document.body.scrollLeft;
343     }
344     return 0;
345 }
346
347 function WebForm_GetScrollY() {
348     if (window.pageYOffset) {
349         return window.pageYOffset;
350     }
351     else if (document.documentElement && document.documentElement.scrollTop) {
352         return document.documentElement.scrollTop;
353     }
354     else if (document.body) {
355         return document.body.scrollTop;
356     }
357     return 0;
358 }
359
360 function WebForm_GetElementPosition(element)
361 {
362         var result = new Object();
363         result.x = 0;
364         result.y = 0;
365         result.width = 0;
366         result.height = 0;
367         result.x = element.offsetLeft;
368         result.y = element.offsetTop;
369         var parent = element.offsetParent;
370         while (parent) {
371                 result.x += parent.offsetLeft;
372                 result.y += parent.offsetTop;
373                 parent = parent.offsetParent;
374         }
375         result.width = element.offsetWidth;
376         result.height = element.offsetHeight;
377         return result;
378 }
379
380