10
|
1 //!----------------------------------------------------------
|
|
2 //! Copyright (C) Microsoft Corporation. All rights reserved.
|
|
3 //!----------------------------------------------------------
|
|
4 //! MicrosoftMvcAjax.js
|
|
5
|
|
6 Type.registerNamespace('Sys.Mvc');
|
|
7
|
|
8 ////////////////////////////////////////////////////////////////////////////////
|
|
9 // Sys.Mvc.AjaxOptions
|
|
10
|
|
11 Sys.Mvc.$create_AjaxOptions = function Sys_Mvc_AjaxOptions() { return {}; }
|
|
12
|
|
13
|
|
14 ////////////////////////////////////////////////////////////////////////////////
|
|
15 // Sys.Mvc.InsertionMode
|
|
16
|
|
17 Sys.Mvc.InsertionMode = function() {
|
|
18 /// <field name="replace" type="Number" integer="true" static="true">
|
|
19 /// </field>
|
|
20 /// <field name="insertBefore" type="Number" integer="true" static="true">
|
|
21 /// </field>
|
|
22 /// <field name="insertAfter" type="Number" integer="true" static="true">
|
|
23 /// </field>
|
|
24 };
|
|
25 Sys.Mvc.InsertionMode.prototype = {
|
|
26 replace: 0,
|
|
27 insertBefore: 1,
|
|
28 insertAfter: 2
|
|
29 }
|
|
30 Sys.Mvc.InsertionMode.registerEnum('Sys.Mvc.InsertionMode', false);
|
|
31
|
|
32
|
|
33 ////////////////////////////////////////////////////////////////////////////////
|
|
34 // Sys.Mvc.AjaxContext
|
|
35
|
|
36 Sys.Mvc.AjaxContext = function Sys_Mvc_AjaxContext(request, updateTarget, loadingElement, insertionMode) {
|
|
37 /// <param name="request" type="Sys.Net.WebRequest">
|
|
38 /// </param>
|
|
39 /// <param name="updateTarget" type="Object" domElement="true">
|
|
40 /// </param>
|
|
41 /// <param name="loadingElement" type="Object" domElement="true">
|
|
42 /// </param>
|
|
43 /// <param name="insertionMode" type="Sys.Mvc.InsertionMode">
|
|
44 /// </param>
|
|
45 /// <field name="_insertionMode" type="Sys.Mvc.InsertionMode">
|
|
46 /// </field>
|
|
47 /// <field name="_loadingElement" type="Object" domElement="true">
|
|
48 /// </field>
|
|
49 /// <field name="_response" type="Sys.Net.WebRequestExecutor">
|
|
50 /// </field>
|
|
51 /// <field name="_request" type="Sys.Net.WebRequest">
|
|
52 /// </field>
|
|
53 /// <field name="_updateTarget" type="Object" domElement="true">
|
|
54 /// </field>
|
|
55 this._request = request;
|
|
56 this._updateTarget = updateTarget;
|
|
57 this._loadingElement = loadingElement;
|
|
58 this._insertionMode = insertionMode;
|
|
59 }
|
|
60 Sys.Mvc.AjaxContext.prototype = {
|
|
61 _insertionMode: 0,
|
|
62 _loadingElement: null,
|
|
63 _response: null,
|
|
64 _request: null,
|
|
65 _updateTarget: null,
|
|
66
|
|
67 get_data: function Sys_Mvc_AjaxContext$get_data() {
|
|
68 /// <value type="String"></value>
|
|
69 if (this._response) {
|
|
70 return this._response.get_responseData();
|
|
71 }
|
|
72 else {
|
|
73 return null;
|
|
74 }
|
|
75 },
|
|
76
|
|
77 get_insertionMode: function Sys_Mvc_AjaxContext$get_insertionMode() {
|
|
78 /// <value type="Sys.Mvc.InsertionMode"></value>
|
|
79 return this._insertionMode;
|
|
80 },
|
|
81
|
|
82 get_loadingElement: function Sys_Mvc_AjaxContext$get_loadingElement() {
|
|
83 /// <value type="Object" domElement="true"></value>
|
|
84 return this._loadingElement;
|
|
85 },
|
|
86
|
|
87 get_object: function Sys_Mvc_AjaxContext$get_object() {
|
|
88 /// <value type="Object"></value>
|
|
89 var executor = this.get_response();
|
|
90 return (executor) ? executor.get_object() : null;
|
|
91 },
|
|
92
|
|
93 get_response: function Sys_Mvc_AjaxContext$get_response() {
|
|
94 /// <value type="Sys.Net.WebRequestExecutor"></value>
|
|
95 return this._response;
|
|
96 },
|
|
97 set_response: function Sys_Mvc_AjaxContext$set_response(value) {
|
|
98 /// <value type="Sys.Net.WebRequestExecutor"></value>
|
|
99 this._response = value;
|
|
100 return value;
|
|
101 },
|
|
102
|
|
103 get_request: function Sys_Mvc_AjaxContext$get_request() {
|
|
104 /// <value type="Sys.Net.WebRequest"></value>
|
|
105 return this._request;
|
|
106 },
|
|
107
|
|
108 get_updateTarget: function Sys_Mvc_AjaxContext$get_updateTarget() {
|
|
109 /// <value type="Object" domElement="true"></value>
|
|
110 return this._updateTarget;
|
|
111 }
|
|
112 }
|
|
113
|
|
114
|
|
115 ////////////////////////////////////////////////////////////////////////////////
|
|
116 // Sys.Mvc.AsyncHyperlink
|
|
117
|
|
118 Sys.Mvc.AsyncHyperlink = function Sys_Mvc_AsyncHyperlink() {
|
|
119 }
|
|
120 Sys.Mvc.AsyncHyperlink.handleClick = function Sys_Mvc_AsyncHyperlink$handleClick(anchor, evt, ajaxOptions) {
|
|
121 /// <param name="anchor" type="Object" domElement="true">
|
|
122 /// </param>
|
|
123 /// <param name="evt" type="Sys.UI.DomEvent">
|
|
124 /// </param>
|
|
125 /// <param name="ajaxOptions" type="Sys.Mvc.AjaxOptions">
|
|
126 /// </param>
|
|
127 evt.preventDefault();
|
|
128 Sys.Mvc.MvcHelpers._asyncRequest(anchor.href, 'post', '', anchor, ajaxOptions);
|
|
129 }
|
|
130
|
|
131
|
|
132 ////////////////////////////////////////////////////////////////////////////////
|
|
133 // Sys.Mvc.MvcHelpers
|
|
134
|
|
135 Sys.Mvc.MvcHelpers = function Sys_Mvc_MvcHelpers() {
|
|
136 }
|
|
137 Sys.Mvc.MvcHelpers._serializeSubmitButton = function Sys_Mvc_MvcHelpers$_serializeSubmitButton(element, offsetX, offsetY) {
|
|
138 /// <param name="element" type="Object" domElement="true">
|
|
139 /// </param>
|
|
140 /// <param name="offsetX" type="Number" integer="true">
|
|
141 /// </param>
|
|
142 /// <param name="offsetY" type="Number" integer="true">
|
|
143 /// </param>
|
|
144 /// <returns type="String"></returns>
|
|
145 if (element.disabled) {
|
|
146 return null;
|
|
147 }
|
|
148 var name = element.name;
|
|
149 if (name) {
|
|
150 var tagName = element.tagName.toUpperCase();
|
|
151 var encodedName = encodeURIComponent(name);
|
|
152 var inputElement = element;
|
|
153 if (tagName === 'INPUT') {
|
|
154 var type = inputElement.type;
|
|
155 if (type === 'submit') {
|
|
156 return encodedName + '=' + encodeURIComponent(inputElement.value);
|
|
157 }
|
|
158 else if (type === 'image') {
|
|
159 return encodedName + '.x=' + offsetX + '&' + encodedName + '.y=' + offsetY;
|
|
160 }
|
|
161 }
|
|
162 else if ((tagName === 'BUTTON') && (name.length) && (inputElement.type === 'submit')) {
|
|
163 return encodedName + '=' + encodeURIComponent(inputElement.value);
|
|
164 }
|
|
165 }
|
|
166 return null;
|
|
167 }
|
|
168 Sys.Mvc.MvcHelpers._serializeForm = function Sys_Mvc_MvcHelpers$_serializeForm(form) {
|
|
169 /// <param name="form" type="Object" domElement="true">
|
|
170 /// </param>
|
|
171 /// <returns type="String"></returns>
|
|
172 var formElements = form.elements;
|
|
173 var formBody = new Sys.StringBuilder();
|
|
174 var count = formElements.length;
|
|
175 for (var i = 0; i < count; i++) {
|
|
176 var element = formElements[i];
|
|
177 var name = element.name;
|
|
178 if (!name || !name.length) {
|
|
179 continue;
|
|
180 }
|
|
181 var tagName = element.tagName.toUpperCase();
|
|
182 if (tagName === 'INPUT') {
|
|
183 var inputElement = element;
|
|
184 var type = inputElement.type;
|
|
185 if ((type === 'text') || (type === 'password') || (type === 'hidden') || (((type === 'checkbox') || (type === 'radio')) && element.checked)) {
|
|
186 formBody.append(encodeURIComponent(name));
|
|
187 formBody.append('=');
|
|
188 formBody.append(encodeURIComponent(inputElement.value));
|
|
189 formBody.append('&');
|
|
190 }
|
|
191 }
|
|
192 else if (tagName === 'SELECT') {
|
|
193 var selectElement = element;
|
|
194 var optionCount = selectElement.options.length;
|
|
195 for (var j = 0; j < optionCount; j++) {
|
|
196 var optionElement = selectElement.options[j];
|
|
197 if (optionElement.selected) {
|
|
198 formBody.append(encodeURIComponent(name));
|
|
199 formBody.append('=');
|
|
200 formBody.append(encodeURIComponent(optionElement.value));
|
|
201 formBody.append('&');
|
|
202 }
|
|
203 }
|
|
204 }
|
|
205 else if (tagName === 'TEXTAREA') {
|
|
206 formBody.append(encodeURIComponent(name));
|
|
207 formBody.append('=');
|
|
208 formBody.append(encodeURIComponent((element.value)));
|
|
209 formBody.append('&');
|
|
210 }
|
|
211 }
|
|
212 var additionalInput = form._additionalInput;
|
|
213 if (additionalInput) {
|
|
214 formBody.append(additionalInput);
|
|
215 formBody.append('&');
|
|
216 }
|
|
217 return formBody.toString();
|
|
218 }
|
|
219 Sys.Mvc.MvcHelpers._asyncRequest = function Sys_Mvc_MvcHelpers$_asyncRequest(url, verb, body, triggerElement, ajaxOptions) {
|
|
220 /// <param name="url" type="String">
|
|
221 /// </param>
|
|
222 /// <param name="verb" type="String">
|
|
223 /// </param>
|
|
224 /// <param name="body" type="String">
|
|
225 /// </param>
|
|
226 /// <param name="triggerElement" type="Object" domElement="true">
|
|
227 /// </param>
|
|
228 /// <param name="ajaxOptions" type="Sys.Mvc.AjaxOptions">
|
|
229 /// </param>
|
|
230 if (ajaxOptions.confirm) {
|
|
231 if (!confirm(ajaxOptions.confirm)) {
|
|
232 return;
|
|
233 }
|
|
234 }
|
|
235 if (ajaxOptions.url) {
|
|
236 url = ajaxOptions.url;
|
|
237 }
|
|
238 if (ajaxOptions.httpMethod) {
|
|
239 verb = ajaxOptions.httpMethod;
|
|
240 }
|
|
241 if (body.length > 0 && !body.endsWith('&')) {
|
|
242 body += '&';
|
|
243 }
|
|
244 body += 'X-Requested-With=XMLHttpRequest';
|
|
245 var upperCaseVerb = verb.toUpperCase();
|
|
246 var isGetOrPost = (upperCaseVerb === 'GET' || upperCaseVerb === 'POST');
|
|
247 if (!isGetOrPost) {
|
|
248 body += '&';
|
|
249 body += 'X-HTTP-Method-Override=' + upperCaseVerb;
|
|
250 }
|
|
251 var requestBody = '';
|
|
252 if (upperCaseVerb === 'GET' || upperCaseVerb === 'DELETE') {
|
|
253 if (url.indexOf('?') > -1) {
|
|
254 if (!url.endsWith('&')) {
|
|
255 url += '&';
|
|
256 }
|
|
257 url += body;
|
|
258 }
|
|
259 else {
|
|
260 url += '?';
|
|
261 url += body;
|
|
262 }
|
|
263 }
|
|
264 else {
|
|
265 requestBody = body;
|
|
266 }
|
|
267 var request = new Sys.Net.WebRequest();
|
|
268 request.set_url(url);
|
|
269 if (isGetOrPost) {
|
|
270 request.set_httpVerb(verb);
|
|
271 }
|
|
272 else {
|
|
273 request.set_httpVerb('POST');
|
|
274 request.get_headers()['X-HTTP-Method-Override'] = upperCaseVerb;
|
|
275 }
|
|
276 request.set_body(requestBody);
|
|
277 if (verb.toUpperCase() === 'PUT') {
|
|
278 request.get_headers()['Content-Type'] = 'application/x-www-form-urlencoded;';
|
|
279 }
|
|
280 request.get_headers()['X-Requested-With'] = 'XMLHttpRequest';
|
|
281 var updateElement = null;
|
|
282 if (ajaxOptions.updateTargetId) {
|
|
283 updateElement = $get(ajaxOptions.updateTargetId);
|
|
284 }
|
|
285 var loadingElement = null;
|
|
286 if (ajaxOptions.loadingElementId) {
|
|
287 loadingElement = $get(ajaxOptions.loadingElementId);
|
|
288 }
|
|
289 var ajaxContext = new Sys.Mvc.AjaxContext(request, updateElement, loadingElement, ajaxOptions.insertionMode);
|
|
290 var continueRequest = true;
|
|
291 if (ajaxOptions.onBegin) {
|
|
292 continueRequest = ajaxOptions.onBegin(ajaxContext) !== false;
|
|
293 }
|
|
294 if (loadingElement) {
|
|
295 Sys.UI.DomElement.setVisible(ajaxContext.get_loadingElement(), true);
|
|
296 }
|
|
297 if (continueRequest) {
|
|
298 request.add_completed(Function.createDelegate(null, function(executor) {
|
|
299 Sys.Mvc.MvcHelpers._onComplete(request, ajaxOptions, ajaxContext);
|
|
300 }));
|
|
301 request.invoke();
|
|
302 }
|
|
303 }
|
|
304 Sys.Mvc.MvcHelpers._onComplete = function Sys_Mvc_MvcHelpers$_onComplete(request, ajaxOptions, ajaxContext) {
|
|
305 /// <param name="request" type="Sys.Net.WebRequest">
|
|
306 /// </param>
|
|
307 /// <param name="ajaxOptions" type="Sys.Mvc.AjaxOptions">
|
|
308 /// </param>
|
|
309 /// <param name="ajaxContext" type="Sys.Mvc.AjaxContext">
|
|
310 /// </param>
|
|
311 ajaxContext.set_response(request.get_executor());
|
|
312 if (ajaxOptions.onComplete && ajaxOptions.onComplete(ajaxContext) === false) {
|
|
313 return;
|
|
314 }
|
|
315 var statusCode = ajaxContext.get_response().get_statusCode();
|
|
316 if ((statusCode >= 200 && statusCode < 300) || statusCode === 304 || statusCode === 1223) {
|
|
317 if (statusCode !== 204 && statusCode !== 304 && statusCode !== 1223) {
|
|
318 var contentType = ajaxContext.get_response().getResponseHeader('Content-Type');
|
|
319 if ((contentType) && (contentType.indexOf('application/x-javascript') !== -1)) {
|
|
320 eval(ajaxContext.get_data());
|
|
321 }
|
|
322 else {
|
|
323 Sys.Mvc.MvcHelpers.updateDomElement(ajaxContext.get_updateTarget(), ajaxContext.get_insertionMode(), ajaxContext.get_data());
|
|
324 }
|
|
325 }
|
|
326 if (ajaxOptions.onSuccess) {
|
|
327 ajaxOptions.onSuccess(ajaxContext);
|
|
328 }
|
|
329 }
|
|
330 else {
|
|
331 if (ajaxOptions.onFailure) {
|
|
332 ajaxOptions.onFailure(ajaxContext);
|
|
333 }
|
|
334 }
|
|
335 if (ajaxContext.get_loadingElement()) {
|
|
336 Sys.UI.DomElement.setVisible(ajaxContext.get_loadingElement(), false);
|
|
337 }
|
|
338 }
|
|
339 Sys.Mvc.MvcHelpers.updateDomElement = function Sys_Mvc_MvcHelpers$updateDomElement(target, insertionMode, content) {
|
|
340 /// <param name="target" type="Object" domElement="true">
|
|
341 /// </param>
|
|
342 /// <param name="insertionMode" type="Sys.Mvc.InsertionMode">
|
|
343 /// </param>
|
|
344 /// <param name="content" type="String">
|
|
345 /// </param>
|
|
346 if (target) {
|
|
347 switch (insertionMode) {
|
|
348 case Sys.Mvc.InsertionMode.replace:
|
|
349 target.innerHTML = content;
|
|
350 break;
|
|
351 case Sys.Mvc.InsertionMode.insertBefore:
|
|
352 if (content && content.length > 0) {
|
|
353 target.innerHTML = content + target.innerHTML.trimStart();
|
|
354 }
|
|
355 break;
|
|
356 case Sys.Mvc.InsertionMode.insertAfter:
|
|
357 if (content && content.length > 0) {
|
|
358 target.innerHTML = target.innerHTML.trimEnd() + content;
|
|
359 }
|
|
360 break;
|
|
361 }
|
|
362 }
|
|
363 }
|
|
364
|
|
365
|
|
366 ////////////////////////////////////////////////////////////////////////////////
|
|
367 // Sys.Mvc.AsyncForm
|
|
368
|
|
369 Sys.Mvc.AsyncForm = function Sys_Mvc_AsyncForm() {
|
|
370 }
|
|
371 Sys.Mvc.AsyncForm.handleClick = function Sys_Mvc_AsyncForm$handleClick(form, evt) {
|
|
372 /// <param name="form" type="Object" domElement="true">
|
|
373 /// </param>
|
|
374 /// <param name="evt" type="Sys.UI.DomEvent">
|
|
375 /// </param>
|
|
376 var additionalInput = Sys.Mvc.MvcHelpers._serializeSubmitButton(evt.target, evt.offsetX, evt.offsetY);
|
|
377 form._additionalInput = additionalInput;
|
|
378 }
|
|
379 Sys.Mvc.AsyncForm.handleSubmit = function Sys_Mvc_AsyncForm$handleSubmit(form, evt, ajaxOptions) {
|
|
380 /// <param name="form" type="Object" domElement="true">
|
|
381 /// </param>
|
|
382 /// <param name="evt" type="Sys.UI.DomEvent">
|
|
383 /// </param>
|
|
384 /// <param name="ajaxOptions" type="Sys.Mvc.AjaxOptions">
|
|
385 /// </param>
|
|
386 evt.preventDefault();
|
|
387 var validationCallbacks = form.validationCallbacks;
|
|
388 if (validationCallbacks) {
|
|
389 for (var i = 0; i < validationCallbacks.length; i++) {
|
|
390 var callback = validationCallbacks[i];
|
|
391 if (!callback()) {
|
|
392 return;
|
|
393 }
|
|
394 }
|
|
395 }
|
|
396 var body = Sys.Mvc.MvcHelpers._serializeForm(form);
|
|
397 Sys.Mvc.MvcHelpers._asyncRequest(form.action, form.method || 'post', body, form, ajaxOptions);
|
|
398 }
|
|
399
|
|
400
|
|
401 Sys.Mvc.AjaxContext.registerClass('Sys.Mvc.AjaxContext');
|
|
402 Sys.Mvc.AsyncHyperlink.registerClass('Sys.Mvc.AsyncHyperlink');
|
|
403 Sys.Mvc.MvcHelpers.registerClass('Sys.Mvc.MvcHelpers');
|
|
404 Sys.Mvc.AsyncForm.registerClass('Sys.Mvc.AsyncForm');
|
|
405
|
|
406 // ---- Do not remove this footer ----
|
|
407 // Generated using Script# v0.5.0.0 (http://projects.nikhilk.net)
|
|
408 // -----------------------------------
|