|
| 1 | +// Unobtrusive Ajax support library for jQuery |
| 2 | +// Copyright (C) Microsoft Corporation. All rights reserved. |
| 3 | +// @version <placeholder> |
| 4 | +// |
| 5 | +// Microsoft grants you the right to use these script files for the sole |
| 6 | +// purpose of either: (i) interacting through your browser with the Microsoft |
| 7 | +// website or online service, subject to the applicable licensing or use |
| 8 | +// terms; or (ii) using the files as included with a Microsoft product subject |
| 9 | +// to that product's license terms. Microsoft reserves all other rights to the |
| 10 | +// files not expressly granted by Microsoft, whether by implication, estoppel |
| 11 | +// or otherwise. Insofar as a script file is dual licensed under GPL, |
| 12 | +// Microsoft neither took the code under GPL nor distributes it thereunder but |
| 13 | +// under the terms set out in this paragraph. All notices and licenses |
| 14 | +// below are for informational purposes only. |
| 15 | + |
| 16 | +/*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 */ |
| 17 | +/*global window: false, jQuery: false */ |
| 18 | + |
| 19 | +(function ($) { |
| 20 | + var data_click = "unobtrusiveAjaxClick", |
| 21 | + data_target = "unobtrusiveAjaxClickTarget", |
| 22 | + data_validation = "unobtrusiveValidation"; |
| 23 | + |
| 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 | + |
| 36 | + function isMethodProxySafe(method) { |
| 37 | + return method === "GET" || method === "POST"; |
| 38 | + } |
| 39 | + |
| 40 | + function asyncOnBeforeSend(xhr, method) { |
| 41 | + if (!isMethodProxySafe(method)) { |
| 42 | + xhr.setRequestHeader("X-HTTP-Method-Override", method); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + function asyncOnSuccess(element, data, contentType) { |
| 47 | + var mode; |
| 48 | + |
| 49 | + if (contentType.indexOf("application/x-javascript") !== -1) { // jQuery already executes JavaScript for us |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + mode = (element.getAttribute("data-ajax-mode") || "").toUpperCase(); |
| 54 | + $(element.getAttribute("data-ajax-update")).each(function (i, update) { |
| 55 | + var top; |
| 56 | + |
| 57 | + switch (mode) { |
| 58 | + case "BEFORE": |
| 59 | + top = update.firstChild; |
| 60 | + $("<div />").html(data).contents().each(function () { |
| 61 | + update.insertBefore(this, top); |
| 62 | + }); |
| 63 | + break; |
| 64 | + case "AFTER": |
| 65 | + $("<div />").html(data).contents().each(function () { |
| 66 | + update.appendChild(this); |
| 67 | + }); |
| 68 | + break; |
| 69 | + case "REPLACE-WITH": |
| 70 | + $(update).replaceWith(data); |
| 71 | + break; |
| 72 | + default: |
| 73 | + $(update).html(data); |
| 74 | + break; |
| 75 | + } |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + function asyncRequest(element, options) { |
| 80 | + var confirm, loading, method, duration; |
| 81 | + |
| 82 | + confirm = element.getAttribute("data-ajax-confirm"); |
| 83 | + if (confirm && !window.confirm(confirm)) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + loading = $(element.getAttribute("data-ajax-loading")); |
| 88 | + duration = parseInt(element.getAttribute("data-ajax-loading-duration"), 10) || 0; |
| 89 | + |
| 90 | + $.extend(options, { |
| 91 | + type: element.getAttribute("data-ajax-method") || undefined, |
| 92 | + url: element.getAttribute("data-ajax-url") || undefined, |
| 93 | + cache: (element.getAttribute("data-ajax-cache") || "").toLowerCase() === "true", |
| 94 | + beforeSend: function (xhr) { |
| 95 | + var result; |
| 96 | + asyncOnBeforeSend(xhr, method); |
| 97 | + result = getFunction(element.getAttribute("data-ajax-begin"), ["xhr"]).apply(element, arguments); |
| 98 | + if (result !== false) { |
| 99 | + loading.show(duration); |
| 100 | + } |
| 101 | + return result; |
| 102 | + }, |
| 103 | + complete: function () { |
| 104 | + loading.hide(duration); |
| 105 | + getFunction(element.getAttribute("data-ajax-complete"), ["xhr", "status"]).apply(element, arguments); |
| 106 | + }, |
| 107 | + success: function (data, status, xhr) { |
| 108 | + asyncOnSuccess(element, data, xhr.getResponseHeader("Content-Type") || "text/html"); |
| 109 | + getFunction(element.getAttribute("data-ajax-success"), ["data", "status", "xhr"]).apply(element, arguments); |
| 110 | + }, |
| 111 | + error: function () { |
| 112 | + getFunction(element.getAttribute("data-ajax-failure"), ["xhr", "status", "error"]).apply(element, arguments); |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + options.data.push({ name: "X-Requested-With", value: "XMLHttpRequest" }); |
| 117 | + |
| 118 | + method = options.type.toUpperCase(); |
| 119 | + if (!isMethodProxySafe(method)) { |
| 120 | + options.type = "POST"; |
| 121 | + options.data.push({ name: "X-HTTP-Method-Override", value: method }); |
| 122 | + } |
| 123 | + |
| 124 | + $.ajax(options); |
| 125 | + } |
| 126 | + |
| 127 | + function validate(form) { |
| 128 | + var validationInfo = $(form).data(data_validation); |
| 129 | + return !validationInfo || !validationInfo.validate || validationInfo.validate(); |
| 130 | + } |
| 131 | + |
| 132 | + $(document).on("click", "a[data-ajax=true]", function (evt) { |
| 133 | + evt.preventDefault(); |
| 134 | + asyncRequest(this, { |
| 135 | + url: this.href, |
| 136 | + type: "GET", |
| 137 | + data: [] |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + $(document).on("click", "form[data-ajax=true] input[type=image]", function (evt) { |
| 142 | + var name = evt.target.name, |
| 143 | + target = $(evt.target), |
| 144 | + form = $(target.parents("form")[0]), |
| 145 | + offset = target.offset(); |
| 146 | + |
| 147 | + form.data(data_click, [ |
| 148 | + { name: name + ".x", value: Math.round(evt.pageX - offset.left) }, |
| 149 | + { name: name + ".y", value: Math.round(evt.pageY - offset.top) } |
| 150 | + ]); |
| 151 | + |
| 152 | + setTimeout(function () { |
| 153 | + form.removeData(data_click); |
| 154 | + }, 0); |
| 155 | + }); |
| 156 | + |
| 157 | + $(document).on("click", "form[data-ajax=true] :submit", function (evt) { |
| 158 | + var name = evt.currentTarget.name, |
| 159 | + target = $(evt.target), |
| 160 | + form = $(target.parents("form")[0]); |
| 161 | + |
| 162 | + form.data(data_click, name ? [{ name: name, value: evt.currentTarget.value }] : []); |
| 163 | + form.data(data_target, target); |
| 164 | + |
| 165 | + setTimeout(function () { |
| 166 | + form.removeData(data_click); |
| 167 | + form.removeData(data_target); |
| 168 | + }, 0); |
| 169 | + }); |
| 170 | + |
| 171 | + $(document).on("submit", "form[data-ajax=true]", function (evt) { |
| 172 | + var clickInfo = $(this).data(data_click) || [], |
| 173 | + clickTarget = $(this).data(data_target), |
| 174 | + isCancel = clickTarget && clickTarget.hasClass("cancel"); |
| 175 | + evt.preventDefault(); |
| 176 | + if (!isCancel && !validate(this)) { |
| 177 | + return; |
| 178 | + } |
| 179 | + asyncRequest(this, { |
| 180 | + url: this.action, |
| 181 | + type: this.method || "GET", |
| 182 | + data: clickInfo.concat($(this).serializeArray()) |
| 183 | + }); |
| 184 | + }); |
| 185 | +}(jQuery)); |
0 commit comments