C# 3.0 updates
[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 function WebForm_Initialize(webForm) {
32
33 webForm.__pendingCallbacks = new Array();
34
35 webForm.WebForm_AutoFocus = function (id)
36 {
37         var x = this.WebForm_GetElementById (id);
38
39         if (x && (!this.WebForm_CanFocus(x))) {
40                 x = this.WebForm_FindFirstFocusableChild(x);
41         }
42         if (x) { x.focus(); }
43 }
44
45 webForm.WebForm_CanFocus = function (element) {
46         if (!element || !(element.tagName) || element.disabled) {
47                 return false;
48         }
49         if (element.type && element.type.toLowerCase() == "hidden") {
50                 return false;
51         }
52         var tagName = element.tagName.toLowerCase();
53         return (tagName == "input" ||
54                         tagName == "textarea" ||
55                         tagName == "select" ||
56                         tagName == "button" ||
57                         tagName == "a");
58 }
59
60 webForm.WebForm_FindFirstFocusableChild = function (element) {
61         if (!element || !(element.tagName)) {
62                 return null;
63         }
64         var tagName = element.tagName.toLowerCase();
65         if (tagName == "undefined") {
66                 return null;
67         }
68         var children = element.childNodes;
69         if (children) {
70                 for (var i = 0; i < children.length; i++) {
71                         try {
72                                 if (this.WebForm_CanFocus(children[i])) {
73                                         return children[i];
74                                 }
75                                 else {
76                                         var focused = this.WebForm_FindFirstFocusableChild(children[i]);
77                                         if (this.WebForm_CanFocus(focused)) {
78                                                 return focused;
79                                         }
80                                 }
81                         } catch (e) {
82                         }
83                 }
84         }
85         return null;
86 }
87
88 webForm.WebForm_ReEnableControls = function  ()
89 {
90         if (typeof(this._form.__enabledControlArray) != 'undefined' && this._form.__enabledControlArray != null)
91                 __enabledControlArray = this._form.__enabledControlArray;
92         
93         if (typeof(__enabledControlArray) == 'undefined' || __enabledControlArray == null)
94                 return false;
95         
96         this._form.__disabledControlArray = new Array();
97         for (var i = 0; i < __enabledControlArray.length; i++) {
98                 var c = this.WebForm_GetElementById (__enabledControlArray[i]);
99                 if ((typeof(c) != "undefined") && (c != null) && (c.disabled == true)) {
100                         c.disabled = false;
101                         this._form.__disabledControlArray[this._form.__disabledControlArray.length] = c;
102                 }
103         }
104         setTimeout((this._instanceVariableName ? this._instanceVariableName + "." : "") + "WebForm_ReDisableControls ()", 0);
105         return true;
106 }
107
108 webForm.WebForm_ReDisableControls = function  ()
109 {
110         for (var i = 0; i < this._form.__disabledControlArray.length; i++) {
111                 this._form.__disabledControlArray[i].disabled = true;
112         }
113 }
114
115 webForm.WebForm_DoPostback = function  (id, par, url, apb, pval, tf, csubm, vg)
116 {
117         var validationResult = true;
118         if (pval && typeof(this.Page_ClientValidate) == "function")
119                 validationResult =  this.Page_ClientValidate(vg);
120
121         if (validationResult) {
122                 if ((typeof(url) != "undefined") && (url != null) && (url.length > 0))
123                         this._form.action = url;
124                 if (tf) {
125                         var lastFocus = this._form.elements["__LASTFOCUS"];
126                         if ((typeof(lastFocus) != "undefined") && (lastFocus != null))
127                                 lastFocus.value = id;
128                 }
129         }               
130         if (csubm)
131                 this.__doPostBack (id, par);
132 }
133
134 webForm.WebForm_DoCallback = function (id, arg, callback, ctx, errorCallback, useAsync)
135 {
136         var qs = this.WebForm_getFormData () + "__CALLBACKTARGET=" + id + "&__CALLBACKARGUMENT=" + encodeURIComponent(arg);
137   
138   if (this._form["__EVENTVALIDATION"])
139     qs += "&__EVENTVALIDATION=" + encodeURIComponent(this._form["__EVENTVALIDATION"].value);
140   
141         var This = this;
142         this.WebForm_httpPost (this._form.serverURL || document.URL, qs, function (httpPost) { This.WebForm_ClientCallback (httpPost, ctx, callback, errorCallback); });
143 }
144
145 webForm.WebForm_ClientCallback = function (httpPost, ctx, callback, errorCallback)
146 {
147         var doc = httpPost.responseText;
148         if (doc.charAt(0) == "e") {
149                 if ((typeof(errorCallback) != "undefined") && (errorCallback != null))
150                         errorCallback(doc.substring(1), ctx);
151         } else {
152                 var separatorIndex = doc.indexOf("|");
153                 if (separatorIndex != -1) {
154                         var validationFieldLength = parseInt(doc.substring(0, separatorIndex));
155                         if (!isNaN(validationFieldLength)) {
156                                 var validationField = doc.substring(separatorIndex + 1, separatorIndex + validationFieldLength + 1);
157                                 if (validationField != "") {
158                                         var validationFieldElement = this._form["__EVENTVALIDATION"];
159                                         if (!validationFieldElement) {
160                                                 validationFieldElement = document.createElement("INPUT");
161                                                 validationFieldElement.type = "hidden";
162                                                 validationFieldElement.name = "__EVENTVALIDATION";
163                                                 validationFieldElement.id = validationFieldElement.name;
164                                                 this._form.appendChild(validationFieldElement);
165                                         }
166                                         validationFieldElement.value = validationField;
167                                 }
168                                 if ((typeof(callback) != "undefined") && (callback != null))
169                                         callback (doc.substring(separatorIndex + validationFieldLength + 1), ctx);
170                         }
171                 } else {
172                         if ((typeof(callback) != "undefined") && (callback != null))
173                                 callback (doc, ctx);
174                 }
175         }
176 }
177
178 webForm.WebForm_getFormData = function ()
179 {
180         var qs = "";
181         var len = this._form.elements.length;
182         for (n=0; n<len; n++) {
183                 var elem = this._form.elements [n];
184                 var tagName = elem.tagName.toLowerCase();
185                 if (tagName == "input") {
186                         var type = elem.type;
187                         if ((type == "text" || type == "hidden" || type == "password" ||
188                                 ((type == "checkbox" || type == "radio") && elem.checked)) &&
189           (elem.id != "__EVENTVALIDATION")) {
190                                 qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
191                         }
192                 } else if (tagName == "select") {
193                         var selectCount = elem.options.length;
194                         for (var j = 0; j < selectCount; j++) {
195                                 var selectChild = elem.options[j];
196                                 if (selectChild.selected == true) {
197                                         qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
198                                 }
199                         }
200                 }
201                 else if (tagName == "textarea") {
202                         qs += elem.name + "=" + encodeURIComponent (elem.value) + "&";
203                 }
204         }
205         return qs;
206 }
207
208 webForm.WebForm_httpPost = function (url, data, callback)
209 {
210         var httpPost = null;
211         
212         if (typeof XMLHttpRequest != "undefined") {
213                 httpPost = new XMLHttpRequest ();
214         } else {
215                 if (this.axName != null)
216                         httpPost = new ActiveXObject (this.axName);
217                 else {
218                         var clsnames = new Array ("MSXML", "MSXML2", "MSXML3", "Microsoft");
219                         for (n = 0; n < clsnames.length && httpPost == null; n++) {
220                                 this.axName = clsnames [n] + ".XMLHTTP";
221                                 try {
222                                         httpPost = new ActiveXObject (this.axName);
223                                 } catch (e) { this.axName = null; }
224                         }
225                         if (httpPost == null)
226                                 throw new Error ("XMLHTTP object could not be created.");
227                 }
228         }
229         httpPost.onreadystatechange = function () { if (httpPost.readyState == 4) callback (httpPost); };
230         
231         httpPost.open ("POST", url, true);      // async
232         httpPost.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
233         setTimeout (function () { httpPost.send (data); }, 10);
234 }
235
236 webForm.WebForm_GetElementById = function (id)
237 {
238         return document.getElementById ? document.getElementById (id) :
239                document.all ? document.all [id] :
240                    document [id];
241 }
242
243 webForm.WebForm_FireDefaultButton = function (event, target)
244 {
245         if (event.keyCode != 13) {
246                 return true;
247         }
248         if(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea")) {
249                 return true;
250         }
251         var defaultButton = this.WebForm_GetElementById(target);
252         if (!defaultButton)
253                 return true;
254         
255         if (typeof(defaultButton.click) != "undefined") {
256                 defaultButton.click();
257                 event.cancelBubble = true;
258                 return false;
259         }
260         
261         if (defaultButton.href && defaultButton.href.match(/^javascript:/i)) {
262                 var jsCode = defaultButton.href.match(/^javascript:(.*)/i)[1]; 
263                 eval(jsCode);
264                 event.cancelBubble = true;
265                 return false;
266         }
267         
268         return true;
269 }
270
271 webForm.WebForm_SaveScrollPositionSubmit = function ()
272 {
273         var pos = this.WebForm_GetElementPosition(this._form);
274         this._form.elements['__SCROLLPOSITIONX'].value = this.WebForm_GetScrollX() - pos.x;
275         this._form.elements['__SCROLLPOSITIONY'].value = this.WebForm_GetScrollY() - pos.y;
276         if ((typeof(this._form.oldSubmit) != "undefined") && (this._form.oldSubmit != null)) {
277                 return this._form.oldSubmit();
278         }
279         return true;
280 }
281
282 webForm.WebForm_SaveScrollPositionOnSubmit = function ()
283 {
284         var pos = this.WebForm_GetElementPosition(this._form);
285         this._form.elements['__SCROLLPOSITIONX'].value = this.WebForm_GetScrollX() - pos.x;
286         this._form.elements['__SCROLLPOSITIONY'].value = this.WebForm_GetScrollY() - pos.y;
287         if ((typeof(this._form.oldOnSubmit) != "undefined") && (this._form.oldOnSubmit != null)) {
288                 return this._form.oldOnSubmit();
289         }
290         return true;
291 }
292
293 webForm.WebForm_RestoreScrollPosition = function ()
294 {
295         var pos = this.WebForm_GetElementPosition(this._form);
296         var ScrollX = parseInt(this._form.elements['__SCROLLPOSITIONX'].value);
297         var ScrollY = parseInt(this._form.elements['__SCROLLPOSITIONY'].value);
298         ScrollX = (isNaN(ScrollX)) ? pos.x : (ScrollX + pos.x);
299         ScrollY = (isNaN(ScrollY)) ? pos.y : (ScrollY + pos.y);
300         window.scrollTo(ScrollX, ScrollY);
301         if ((typeof(this._form.oldOnLoad) != "undefined") && (this._form.oldOnLoad != null)) {
302                 return this._form.oldOnLoad();
303         }
304         return true;
305 }
306
307 webForm.WebForm_GetScrollX = function () {
308     if (window.pageXOffset) {
309         return window.pageXOffset;
310     }
311     else if (document.documentElement && document.documentElement.scrollLeft) {
312         return document.documentElement.scrollLeft;
313     }
314     else if (document.body) {
315         return document.body.scrollLeft;
316     }
317     return 0;
318 }
319
320 webForm.WebForm_GetScrollY = function () {
321     if (window.pageYOffset) {
322         return window.pageYOffset;
323     }
324     else if (document.documentElement && document.documentElement.scrollTop) {
325         return document.documentElement.scrollTop;
326     }
327     else if (document.body) {
328         return document.body.scrollTop;
329     }
330     return 0;
331 }
332
333 webForm.WebForm_GetElementPosition = function (element)
334 {
335         var result = new Object();
336         result.x = 0;
337         result.y = 0;
338         result.width = 0;
339         result.height = 0;
340         result.x = element.offsetLeft;
341         result.y = element.offsetTop;
342         var parent = element.offsetParent;
343         while (parent) {
344                 result.x += parent.offsetLeft;
345                 result.y += parent.offsetTop;
346                 parent = parent.offsetParent;
347         }
348         result.width = element.offsetWidth;
349         result.height = element.offsetHeight;
350         return result;
351 }
352 }
353