Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.Statistics/3.3/Scripts/jquery.unobtrusive-ajax.js @ 11552

Last change on this file since 11552 was 11222, checked in by mroscoe, 10 years ago
File size: 7.0 KB
Line 
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
20/*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 */
21/*global window: false, jQuery: false */
22
23(function ($) {
24    var data_click = "unobtrusiveAjaxClick",
25        data_target = "unobtrusiveAjaxClickTarget",
26        data_validation = "unobtrusiveValidation";
27
28    function getFunction(code, argNames) {
29        var fn = window, parts = (code || "").split(".");
30        while (fn && parts.length) {
31            fn = fn[parts.shift()];
32        }
33        if (typeof (fn) === "function") {
34            return fn;
35        }
36        argNames.push(code);
37        return Function.constructor.apply(null, argNames);
38    }
39
40    function isMethodProxySafe(method) {
41        return method === "GET" || method === "POST";
42    }
43
44    function asyncOnBeforeSend(xhr, method) {
45        if (!isMethodProxySafe(method)) {
46            xhr.setRequestHeader("X-HTTP-Method-Override", method);
47        }
48    }
49
50    function asyncOnSuccess(element, data, contentType) {
51        var mode;
52
53        if (contentType.indexOf("application/x-javascript") !== -1) {  // jQuery already executes JavaScript for us
54            return;
55        }
56
57        mode = (element.getAttribute("data-ajax-mode") || "").toUpperCase();
58        $(element.getAttribute("data-ajax-update")).each(function (i, update) {
59            var top;
60
61            switch (mode) {
62            case "BEFORE":
63                top = update.firstChild;
64                $("<div />").html(data).contents().each(function () {
65                    update.insertBefore(this, top);
66                });
67                break;
68            case "AFTER":
69                $("<div />").html(data).contents().each(function () {
70                    update.appendChild(this);
71                });
72                break;
73            case "REPLACE-WITH":
74                $(update).replaceWith(data);
75                break;
76            default:
77                $(update).html(data);
78                break;
79            }
80        });
81    }
82
83    function asyncRequest(element, options) {
84        var confirm, loading, method, duration;
85
86        confirm = element.getAttribute("data-ajax-confirm");
87        if (confirm && !window.confirm(confirm)) {
88            return;
89        }
90
91        loading = $(element.getAttribute("data-ajax-loading"));
92        duration = parseInt(element.getAttribute("data-ajax-loading-duration"), 10) || 0;
93
94        $.extend(options, {
95            type: element.getAttribute("data-ajax-method") || undefined,
96            url: element.getAttribute("data-ajax-url") || undefined,
97            cache: !!element.getAttribute("data-ajax-cache"),
98            beforeSend: function (xhr) {
99                var result;
100                asyncOnBeforeSend(xhr, method);
101                result = getFunction(element.getAttribute("data-ajax-begin"), ["xhr"]).apply(element, arguments);
102                if (result !== false) {
103                    loading.show(duration);
104                }
105                return result;
106            },
107            complete: function () {
108                loading.hide(duration);
109                getFunction(element.getAttribute("data-ajax-complete"), ["xhr", "status"]).apply(element, arguments);
110            },
111            success: function (data, status, xhr) {
112                asyncOnSuccess(element, data, xhr.getResponseHeader("Content-Type") || "text/html");
113                getFunction(element.getAttribute("data-ajax-success"), ["data", "status", "xhr"]).apply(element, arguments);
114            },
115            error: function () {
116                getFunction(element.getAttribute("data-ajax-failure"), ["xhr", "status", "error"]).apply(element, arguments);
117            }
118        });
119
120        options.data.push({ name: "X-Requested-With", value: "XMLHttpRequest" });
121
122        method = options.type.toUpperCase();
123        if (!isMethodProxySafe(method)) {
124            options.type = "POST";
125            options.data.push({ name: "X-HTTP-Method-Override", value: method });
126        }
127
128        $.ajax(options);
129    }
130
131    function validate(form) {
132        var validationInfo = $(form).data(data_validation);
133        return !validationInfo || !validationInfo.validate || validationInfo.validate();
134    }
135
136    $(document).on("click", "a[data-ajax=true]", function (evt) {
137        evt.preventDefault();
138        asyncRequest(this, {
139            url: this.href,
140            type: "GET",
141            data: []
142        });
143    });
144
145    $(document).on("click", "form[data-ajax=true] input[type=image]", function (evt) {
146        var name = evt.target.name,
147            target = $(evt.target),
148            form = $(target.parents("form")[0]),
149            offset = target.offset();
150
151        form.data(data_click, [
152            { name: name + ".x", value: Math.round(evt.pageX - offset.left) },
153            { name: name + ".y", value: Math.round(evt.pageY - offset.top) }
154        ]);
155
156        setTimeout(function () {
157            form.removeData(data_click);
158        }, 0);
159    });
160
161    $(document).on("click", "form[data-ajax=true] :submit", function (evt) {
162        var name = evt.currentTarget.name,
163            target = $(evt.target),
164            form = $(target.parents("form")[0]);
165
166        form.data(data_click, name ? [{ name: name, value: evt.currentTarget.value }] : []);
167        form.data(data_target, target);
168
169        setTimeout(function () {
170            form.removeData(data_click);
171            form.removeData(data_target);
172        }, 0);
173    });
174
175    $(document).on("submit", "form[data-ajax=true]", function (evt) {
176        var clickInfo = $(this).data(data_click) || [],
177            clickTarget = $(this).data(data_target),
178            isCancel = clickTarget && clickTarget.hasClass("cancel");
179        evt.preventDefault();
180        if (!isCancel && !validate(this)) {
181            return;
182        }
183        asyncRequest(this, {
184            url: this.action,
185            type: this.method || "GET",
186            data: clickInfo.concat($(this).serializeArray())
187        });
188    });
189}(jQuery));
Note: See TracBrowser for help on using the repository browser.