-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrafity.forms.js
executable file
·100 lines (82 loc) · 2.94 KB
/
crafity.forms.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
/* globals window */
(function (crafity, $, window) {
"use strict";
crafity.ready(function () {
return;
// subscribe on the submit event of all forms in this DOM
$(window.document.body).delegate("form", "submit", function () {
var form$ = $(this)
, formData = form$.serialize()
, bookmark = form$.hasClass("bookmark")
, submitButton$ = $("input[type=submit]", form$)
, target, target$
, href = form$.attr("action");
$("input, textarea", form$).attr("disabled", "disabled");
submitButton$.val(submitButton$.attr("data-busy-value") || submitButton$.val()).addClass("busy");
if (form$.attr("data-async") === "content") {
target = form$.attr("data-async-target");
target$ = form$.nearest(target);
}
if (!target$ || !target$.length) {
throw new Error("Unable to find target element for form result");
}
$("html").addClass("loading");
$.ajax({
type: 'POST',
url: href + (href.indexOf("?") > -1 ? "&" : "?") + ("layout=false"),
data: formData,
success: function (data, textStatus, xhr) {
if (form$.hasClass("async")) {
try {
var url = xhr.getResponseHeader("x-crafity-location").split("?")[0]
, data$ = $("<div/>").append(data)
, showContent;
console.log("data$", data$);
console.log("SUCCESS arguments", arguments);
console.log("--> url", url);
target$.attr("data-href", url);
if (bookmark) {
crafity.navigation.hashInfo.update("");
crafity.navigation.hashInfo.change({ href: url });
}
target$.removeClass('loading').addClass('loaded');
target$.find(".columns").addClass("open");
data$.find(".columns").addClass("open");
showContent = function showContent(data$, url) {
data$ = target$.empty().append(data$.children()[0]);
target$.attr("data-href", url);
$("html").removeClass("not-ready").addClass("ready");
setTimeout(function () {
data$.find(".columns").removeClass("open");
}, 1);
};
if ($("html").hasClass("ready")) {
setTimeout(function () {
showContent(data$, url);
}, 500);
} else {
showContent(data$, url);
}
} catch (err) {
console.log("ERROR arguments", err);
crafity.flash.show({ message: err.message, type: "error" });
}
return;
}
form$
.closest(".body")
.empty()
.append(data);
form$.closest("section").removeClass("visible").addClass("visible");
$("html").removeClass("loading");
console.log("postResult", data);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log("jqXHR, textStatus, errorThrown", jqXHR, jqXHR.error(), textStatus, errorThrown);
crafity.flash.show({ message: jqXHR.responseText, type: jqXHR.status !== 400 ? "error" : "warning" });
}
});
return false;
});
});
}(window.crafity = window.crafity || {}, window.jQuery, window));