You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
6.3 KiB

  1. /* NUGET: BEGIN LICENSE TEXT
  2. *
  3. * Microsoft grants you the right to use these script files for the sole
  4. * purpose of either: (i) interacting through your browser with the Microsoft
  5. * website or online service, subject to the applicable licensing or use
  6. * terms; or (ii) using the files as included with a Microsoft product subject
  7. * to that product's license terms. Microsoft reserves all other rights to the
  8. * files not expressly granted by Microsoft, whether by implication, estoppel
  9. * or otherwise. Insofar as a script file is dual licensed under GPL,
  10. * Microsoft neither took the code under GPL nor distributes it thereunder but
  11. * under the terms set out in this paragraph. All notices and licenses
  12. * below are for informational purposes only.
  13. *
  14. * NUGET: END LICENSE TEXT */
  15. /*!
  16. ** Unobtrusive Ajax support library for jQuery
  17. ** Copyright (C) Microsoft Corporation. All rights reserved.
  18. */
  19. /*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true, strict: false */
  20. /*global window: false, jQuery: false */
  21. (function ($) {
  22. var data_click = "unobtrusiveAjaxClick",
  23. data_validation = "unobtrusiveValidation";
  24. function getFunction(code, argNames) {
  25. var fn = window, parts = (code || "").split(".");
  26. while (fn && parts.length) {
  27. fn = fn[parts.shift()];
  28. }
  29. if (typeof (fn) === "function") {
  30. return fn;
  31. }
  32. argNames.push(code);
  33. return Function.constructor.apply(null, argNames);
  34. }
  35. function isMethodProxySafe(method) {
  36. return method === "GET" || method === "POST";
  37. }
  38. function asyncOnBeforeSend(xhr, method) {
  39. if (!isMethodProxySafe(method)) {
  40. xhr.setRequestHeader("X-HTTP-Method-Override", method);
  41. }
  42. }
  43. function asyncOnSuccess(element, data, contentType) {
  44. var mode;
  45. if (contentType.indexOf("application/x-javascript") !== -1) { // jQuery already executes JavaScript for us
  46. return;
  47. }
  48. mode = (element.getAttribute("data-ajax-mode") || "").toUpperCase();
  49. $(element.getAttribute("data-ajax-update")).each(function (i, update) {
  50. var top;
  51. switch (mode) {
  52. case "BEFORE":
  53. top = update.firstChild;
  54. $("<div />").html(data).contents().each(function () {
  55. update.insertBefore(this, top);
  56. });
  57. break;
  58. case "AFTER":
  59. $("<div />").html(data).contents().each(function () {
  60. update.appendChild(this);
  61. });
  62. break;
  63. default:
  64. $(update).html(data);
  65. break;
  66. }
  67. });
  68. }
  69. function asyncRequest(element, options) {
  70. var confirm, loading, method, duration;
  71. confirm = element.getAttribute("data-ajax-confirm");
  72. if (confirm && !window.confirm(confirm)) {
  73. return;
  74. }
  75. loading = $(element.getAttribute("data-ajax-loading"));
  76. duration = element.getAttribute("data-ajax-loading-duration") || 0;
  77. $.extend(options, {
  78. type: element.getAttribute("data-ajax-method") || undefined,
  79. url: element.getAttribute("data-ajax-url") || undefined,
  80. beforeSend: function (xhr) {
  81. var result;
  82. asyncOnBeforeSend(xhr, method);
  83. result = getFunction(element.getAttribute("data-ajax-begin"), ["xhr"]).apply(this, arguments);
  84. if (result !== false) {
  85. loading.show(duration);
  86. }
  87. return result;
  88. },
  89. complete: function () {
  90. loading.hide(duration);
  91. getFunction(element.getAttribute("data-ajax-complete"), ["xhr", "status"]).apply(this, arguments);
  92. },
  93. success: function (data, status, xhr) {
  94. asyncOnSuccess(element, data, xhr.getResponseHeader("Content-Type") || "text/html");
  95. getFunction(element.getAttribute("data-ajax-success"), ["data", "status", "xhr"]).apply(this, arguments);
  96. },
  97. error: getFunction(element.getAttribute("data-ajax-failure"), ["xhr", "status", "error"])
  98. });
  99. options.data.push({ name: "X-Requested-With", value: "XMLHttpRequest" });
  100. method = options.type.toUpperCase();
  101. if (!isMethodProxySafe(method)) {
  102. options.type = "POST";
  103. options.data.push({ name: "X-HTTP-Method-Override", value: method });
  104. }
  105. $.ajax(options);
  106. }
  107. function validate(form) {
  108. var validationInfo = $(form).data(data_validation);
  109. return !validationInfo || !validationInfo.validate || validationInfo.validate();
  110. }
  111. $(document).on("click", "a[data-ajax=true]", function (evt) {
  112. evt.preventDefault();
  113. asyncRequest(this, {
  114. url: this.href,
  115. type: "GET",
  116. data: []
  117. });
  118. });
  119. $(document).on("click", "form[data-ajax=true] input[type=image]", function (evt) {
  120. var name = evt.target.name,
  121. $target = $(evt.target),
  122. form = $target.parents("form")[0],
  123. offset = $target.offset();
  124. $(form).data(data_click, [
  125. { name: name + ".x", value: Math.round(evt.pageX - offset.left) },
  126. { name: name + ".y", value: Math.round(evt.pageY - offset.top) }
  127. ]);
  128. setTimeout(function () {
  129. $(form).removeData(data_click);
  130. }, 0);
  131. });
  132. $(document).on("click", "form[data-ajax=true] :submit", function (evt) {
  133. var name = evt.target.name,
  134. form = $(evt.target).parents("form")[0];
  135. $(form).data(data_click, name ? [{ name: name, value: evt.target.value }] : []);
  136. setTimeout(function () {
  137. $(form).removeData(data_click);
  138. }, 0);
  139. });
  140. $(document).on("submit", "form[data-ajax=true]", function (evt) {
  141. var clickInfo = $(this).data(data_click) || [];
  142. evt.preventDefault();
  143. if (!validate(this)) {
  144. return;
  145. }
  146. asyncRequest(this, {
  147. url: this.action,
  148. type: this.method || "GET",
  149. data: clickInfo.concat($(this).serializeArray())
  150. });
  151. });
  152. }(jQuery));