MicrosoftMvcAjax.debug.js 15 KB

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