-
Notifications
You must be signed in to change notification settings - Fork 7
/
ajaxsubmit.js
152 lines (150 loc) · 4.9 KB
/
ajaxsubmit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Ajax Submit v0.1.4
* http://github.com/bogdan/ajaxsubmit
*
* Copyright 2011-2012, Bogdan Gusiev
* Released under the MIT License
*/
// Generated by CoffeeScript 1.6.3
(function() {
(function($) {
var applyValidation, applyValidationMessage;
$.errors = {
attribute: "validate",
activationClass: "validation-active",
format: "<div class='validation-block'><div class='validation-message'></div></div>",
messageClass: "validation-message"
};
applyValidationMessage = function(div, message) {
var message_div;
if (!div.hasClass($.errors.activationClass)) {
div.addClass($.errors.activationClass);
message_div = div.find("." + $.errors.messageClass);
if (message_div.size() === 0) {
div.append($.errors.format);
}
message_div = div.find("." + $.errors.messageClass);
if (message_div.size() > 0) {
return message_div.html(message);
} else {
throw new Error("configuration error: $.errors.format must have elment with class " + $.errors.messageClass);
}
}
};
applyValidation = function(form, field, message) {
var div;
div = form.find("[" + $.errors.attribute + "~='" + field + "']");
if (div.size() === 0) {
div = $(("<div " + $.errors.attribute + "='" + field + "'>") + ("Unassigned error: Add validate=\"" + field + "\" attribute somewhere in a form.</div>"));
form.prepend(div);
}
return applyValidationMessage(div, message);
};
$.fn.applyErrors = function(errors) {
$(this).clearErrors();
return $(this).addErrors(errors);
};
$.fn.addErrors = function(errors) {
var form, old_errors;
form = $(this);
if ($.type(errors) === "object") {
old_errors = errors;
errors = [];
$.each(old_errors, function(key, value) {
if (value && !($.isArray(value) && value.length === 0)) {
return errors.push([key, value]);
}
});
}
return $(errors).each(function(key, error) {
var field, message;
field = error[0];
message = error[1];
if ($.isArray(message)) {
message = message[0];
}
return applyValidation(form, field, message);
});
};
return $.fn.clearErrors = function() {
var validators;
validators = $(this).find("[" + $.errors.attribute + "]");
validators.find("." + $.errors.messageClass).html("");
return validators.removeClass($.errors.activationClass);
};
})(jQuery);
}).call(this);
// Generated by CoffeeScript 1.6.3
(function() {
(function($) {
var ajaxFormErrorHandler, ajaxFormSuccessHandler;
$.fn.ajaxSubmit = function(options) {
var $form, callback, data, error_callback, method, url;
if (options == null) {
options = {};
}
$form = $(this);
$form.clearErrors();
if (typeof options === "function") {
options.success = options;
}
if (options.redirect && !options.success) {
options.success = function() {
return window.location = options.redirect;
};
}
callback = options.success;
error_callback = options.error;
http_error_callback = options.http_error;
method = $form.attr("method") || "get";
url = $form.attr("action");
data = $form.serialize();
if (!jQuery.isEmptyObject(options.data)) {
data = data + "&" + $.param(options.data);
}
return $.ajax({
type: options.type || method,
url: options.url || url,
data: data,
dataType: "json",
success: function(data) {
return ajaxFormSuccessHandler($form, data, callback, error_callback);
},
error: function(xhr, status, str) {
return ajaxFormErrorHandler($form, xhr, http_error_callback);
}
});
};
ajaxFormSuccessHandler = function($form, data, callback, error_callback) {
if ($.isEmptyObject(data && data.errors)) {
if (typeof callback === "function") {
return callback.call($form[0], data);
} else if (data.redirect) {
return window.location = data.redirect;
}
} else {
if (typeof error_callback === "function") {
error_callback.call($form, data);
}
return $form.applyErrors(data.errors);
}
};
ajaxFormErrorHandler = function($form, xhr, http_error_callback) {
if (xhr.responseJSON.errors) {
return $form.applyErrors(xhr.responseJSON.errors);
}
if (typeof http_error_callback == "function"){
http_error_callback.call($form[0], xhr)
}
}
return $.fn.ajaxForm = function(options) {
if (options == null) {
options = {};
}
return $(this).bind("submit", function(event) {
event.preventDefault();
return $(this).ajaxSubmit(options);
});
};
})(jQuery);
}).call(this);