annotate Agendas/trunk/src/Agendas.Web/Scripts/jquery.unobtrusive-ajax.js @ 277:7439d7a5f8d0

Warning HTML: Cambiado ALT por TITLE.
author juanjose.montesdeocaarbos
date Tue, 20 Dec 2011 08:28:33 -0300
parents c62b77fc33f4
children
rev   line source
10
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
1 /// <reference path="jquery-1.4.4.js" />
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
2
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
3 /*!
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
4 ** Unobtrusive Ajax support library for jQuery
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
5 ** Copyright (C) Microsoft Corporation. All rights reserved.
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
6 */
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
7
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
8 /*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 */
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
9 /*global window: false, jQuery: false */
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
10
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
11 (function ($) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
12 var data_click = "unobtrusiveAjaxClick",
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
13 data_validation = "unobtrusiveValidation";
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
14
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
15 function getFunction(code, argNames) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
16 var fn = window, parts = (code || "").split(".");
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
17 while (fn && parts.length) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
18 fn = fn[parts.shift()];
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
19 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
20 if (typeof (fn) === "function") {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
21 return fn;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
22 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
23 argNames.push(code);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
24 return Function.constructor.apply(null, argNames);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
25 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
26
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
27 function isMethodProxySafe(method) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
28 return method === "GET" || method === "POST";
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
29 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
30
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
31 function asyncOnBeforeSend(xhr, method) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
32 if (!isMethodProxySafe(method)) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
33 xhr.setRequestHeader("X-HTTP-Method-Override", method);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
34 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
35 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
36
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
37 function asyncOnSuccess(element, data, contentType) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
38 var mode;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
39
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
40 if (contentType.indexOf("application/x-javascript") !== -1) { // jQuery already executes JavaScript for us
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
41 return;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
42 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
43
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
44 mode = (element.getAttribute("data-ajax-mode") || "").toUpperCase();
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
45 $(element.getAttribute("data-ajax-update")).each(function (i, update) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
46 var top;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
47
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
48 switch (mode) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
49 case "BEFORE":
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
50 top = update.firstChild;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
51 $("<div />").html(data).contents().each(function () {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
52 update.insertBefore(this, top);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
53 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
54 break;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
55 case "AFTER":
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
56 $("<div />").html(data).contents().each(function () {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
57 update.appendChild(this);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
58 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
59 break;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
60 default:
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
61 $(update).html(data);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
62 break;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
63 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
64 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
65 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
66
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
67 function asyncRequest(element, options) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
68 var confirm, loading, method, duration;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
69
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
70 confirm = element.getAttribute("data-ajax-confirm");
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
71 if (confirm && !window.confirm(confirm)) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
72 return;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
73 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
74
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
75 loading = $(element.getAttribute("data-ajax-loading"));
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
76 duration = element.getAttribute("data-ajax-loading-duration") || 0;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
77
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
78 $.extend(options, {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
79 type: element.getAttribute("data-ajax-method") || undefined,
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
80 url: element.getAttribute("data-ajax-url") || undefined,
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
81 beforeSend: function (xhr) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
82 var result;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
83 asyncOnBeforeSend(xhr, method);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
84 result = getFunction(element.getAttribute("data-ajax-begin"), ["xhr"]).apply(this, arguments);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
85 if (result !== false) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
86 loading.show(duration);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
87 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
88 return result;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
89 },
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
90 complete: function () {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
91 loading.hide(duration);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
92 getFunction(element.getAttribute("data-ajax-complete"), ["xhr", "status"]).apply(this, arguments);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
93 },
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
94 success: function (data, status, xhr) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
95 asyncOnSuccess(element, data, xhr.getResponseHeader("Content-Type") || "text/html");
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
96 getFunction(element.getAttribute("data-ajax-success"), ["data", "status", "xhr"]).apply(this, arguments);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
97 },
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
98 error: getFunction(element.getAttribute("data-ajax-failure"), ["xhr", "status", "error"])
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
99 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
100
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
101 options.data.push({ name: "X-Requested-With", value: "XMLHttpRequest" });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
102
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
103 method = options.type.toUpperCase();
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
104 if (!isMethodProxySafe(method)) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
105 options.type = "POST";
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
106 options.data.push({ name: "X-HTTP-Method-Override", value: method });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
107 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
108
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
109 $.ajax(options);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
110 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
111
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
112 function validate(form) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
113 var validationInfo = $(form).data(data_validation);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
114 return !validationInfo || !validationInfo.validate || validationInfo.validate();
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
115 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
116
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
117 $("a[data-ajax=true]").live("click", function (evt) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
118 evt.preventDefault();
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
119 asyncRequest(this, {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
120 url: this.href,
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
121 type: "GET",
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
122 data: []
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
123 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
124 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
125
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
126 $("form[data-ajax=true] input[type=image]").live("click", function (evt) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
127 var name = evt.target.name,
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
128 $target = $(evt.target),
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
129 form = $target.parents("form")[0],
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
130 offset = $target.offset();
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
131
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
132 $(form).data(data_click, [
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
133 { name: name + ".x", value: Math.round(evt.pageX - offset.left) },
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
134 { name: name + ".y", value: Math.round(evt.pageY - offset.top) }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
135 ]);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
136
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
137 setTimeout(function () {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
138 $(form).removeData(data_click);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
139 }, 0);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
140 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
141
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
142 $("form[data-ajax=true] :submit").live("click", function (evt) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
143 var name = evt.target.name,
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
144 form = $(evt.target).parents("form")[0];
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
145
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
146 $(form).data(data_click, name ? [{ name: name, value: evt.target.value }] : []);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
147
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
148 setTimeout(function () {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
149 $(form).removeData(data_click);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
150 }, 0);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
151 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
152
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
153 $("form[data-ajax=true]").live("submit", function (evt) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
154 var clickInfo = $(this).data(data_click) || [];
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
155 evt.preventDefault();
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
156 if (!validate(this)) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
157 return;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
158 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
159 asyncRequest(this, {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
160 url: this.action,
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
161 type: this.method || "GET",
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
162 data: clickInfo.concat($(this).serializeArray())
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
163 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
164 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
165 }(jQuery));