PageSourceSearch

https://www.isms.org/Scripts/jquery.unobtrusive-ajax.js

js isms.org collected 2026-09-26 03:26:50 UTC 7,013 bytes, 180 lines download raw bytes

vendor: 7,013 bytes, lines 1-180
1// Unobtrusive Ajax support library for jQuery
2// Copyright (C) Microsoft Corporation. All rights reserved.
3// @version v3.2.5
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                    $(update).prepend(data);
60                    break;
61                case "AFTER":
62                    $(update).append(data);
63                    break;
64                case "REPLACE-WITH":
65                    $(update).replaceWith(data);
66                    break;
67                default:
68                    $(update).html(data);
69                    break;
70            }
71        });
72    }
73
74    function asyncRequest(element, options) {
75        var confirm, loading, method, duration;
76
77        confirm = element.getAttribute("data-ajax-confirm");
78        if (confirm && !window.confirm(confirm)) {
79            return;
80        }
81
82        loading = $(element.getAttribute("data-ajax-loading"));
83        duration = parseInt(element.getAttribute("data-ajax-loading-duration"), 10) || 0;
84
85        $.extend(options, {
86            type: element.getAttribute("data-ajax-method") || undefined,
87            url: element.getAttribute("data-ajax-url") || undefined,
88            cache: (element.getAttribute("data-ajax-cache") || "").toLowerCase() === "true",
89            beforeSend: function (xhr) {
90                var result;
91                asyncOnBeforeSend(xhr, method);
92                result = getFunction(element.getAttribute("data-ajax-begin"), ["xhr"]).apply(element, arguments);
93                if (result !== false) {
94                    loading.show(duration);
95                }
96                return result;
97            },
98            complete: function () {
99                loading.hide(duration);
100                getFunction(element.getAttribute("data-ajax-complete"), ["xhr", "status"]).apply(element, arguments);
101            },
102            success: function (data, status, xhr) {
103                asyncOnSuccess(element, data, xhr.getResponseHeader("Content-Type") || "text/html");
104                getFunction(element.getAttribute("data-ajax-success"), ["data", "status", "xhr"]).apply(element, arguments);
105            },
106            error: function () {
107                getFunction(element.getAttribute("data-ajax-failure"), ["xhr", "status", "error"]).apply(element, arguments);
108            }
109        });
110
111        options.data.push({ name: "X-Requested-With", value: "XMLHttpRequest" });
112
113        method = options.type.toUpperCase();
114        if (!isMethodProxySafe(method)) {
115            options.type = "POST";
116            options.data.push({ name: "X-HTTP-Method-Override", value: method });
117        }
118
119        $.ajax(options);
120    }
121
122    function validate(form) {
123        var validationInfo = $(form).data(data_validation);
124        return !validationInfo || !validationInfo.validate || validationInfo.validate();
125    }
126
127    $(document).on("click", "a[data-ajax=true]", function (evt) {
128        evt.preventDefault();
129        asyncRequest(this, {
130            url: this.href,
131            type: "GET",
132            data: []
133        });
134    });
135
136    $(document).on("click", "form[data-ajax=true] input[type=image]", function (evt) {
137        var name = evt.target.name,
138            target = $(evt.target),
139            form = $(target.parents("form")[0]),
140            offset = target.offset();
141
142        form.data(data_click, [
143            { name: name + ".x", value: Math.round(evt.pageX - offset.left) },
144            { name: name + ".y", value: Math.round(evt.pageY - offset.top) }
145        ]);
146
147        setTimeout(function () {
148            form.removeData(data_click);
149        }, 0);
150    });
151
152    $(document).on("click", "form[data-ajax=true] :submit", function (evt) {
153        var name = evt.currentTarget.name,
154            target = $(evt.target),
155            form = $(target.parents("form")[0]);
156
157        form.data(data_click, name ? [{ name: name, value: evt.currentTarget.value }] : []);
158        form.data(data_target, target);
159
160        setTimeout(function () {
161            form.removeData(data_click);
162            form.removeData(data_target);
163        }, 0);
164    });
165
166    $(document).on("submit", "form[data-ajax=true]", function (evt) {
167        var clickInfo = $(this).data(data_click) || [],
168            clickTarget = $(this).data(data_target),
169            isCancel = clickTarget && (clickTarget.hasClass("cancel") || clickTarget.attr('formnovalidate') !== undefined);
170        evt.preventDefault();
171        if (!isCancel && !validate(this)) {
172            return;
173        }
174        asyncRequest(this, {
175            url: this.action,
176            type: this.method || "GET",
177            data: clickInfo.concat($(this).serializeArray())
178        });
179    });
180}(jQuery));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.