-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
206 lines (166 loc) · 5.49 KB
/
main.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
var self;
var form;
var config;
var Notification = require("./notification");
module.exports = function init (conf) {
self = this;
config = conf;
// Load the right content for hash
if (window.location.hash) {
showPageFromHash();
}
// hashchange handler
$(window).on('hashchange', function () {
showPageFromHash();
});
// click on the checkbox
$('.showAlt', self.dom).on('change', function() {
var show = $(this).prop('checked');
if (show) {
$('.alt', self.dom).show();
} else {
$('.alt', self.dom).hide();
}
});
};
function showPageFromHash () {
$(".notification").remove();
var hash = window.location.hash.substring(1);
// the cart is outsourced to the bind-cart module
if (hash === 'cart') {
return;
}
self.link('getPageData', { data: { page: hash } }, function (err, data) {
if (err) {
alert(err);
return;
}
if (data.redirect) {
window.location = data.redirect;
return;
}
if (hash !== data.page) {
window.location.hash = data.page;
return;
}
self.emit(data.page);
// show current page
var page = $('.page.' + data.page, self.dom);
$('.page', self.dom).hide();
page.show();
// fill in the form data
for (var i in data.data) {
var elem = $('[data-name="' + data.data[i].name + '"]', page);
switch (elem.prop('tagName')) {
case 'INPUT':
if (elem.attr('type') === 'checkbox') {
elem.prop('checked', !!data.data[i].value);
}
case 'SELECT':
case 'TEXTAREA':
elem.val(data.data[i].value);
break;
default:
elem.text(data.data[i].value);
break;
}
}
// find form in the page
var formInPage = page.find('form');
formInPage.off('submit');
// handler for submit data
formInPage.on('submit', function (e) {
var data = {
page: hash,
form: formInPage.serializeArray()
};
// save and validate form data
self.link("savePageData", { data: data }, function (err, data) {
if (err) {
alert(err);
return;
}
if (hash !== data.page) {
window.location.hash = data.page;
return;
}
if (data.page === "payment") {
formInPage.off("submit");
formInPage.submit();
return;
}
// show the form errors if any
if (data.errors && data.errors.length) {
// show current page
var page = $('.page.' + data.page, self.dom);
$('.page', self.dom).hide();
page.show();
// fill in the form data
for (var i in data.data) {
var elem = $('[data-name="' + data.data[i].name + '"]', page);
elem.val(data.data[i].value);
}
showErrors(data.errors);
} else {
var nextHash = {
address: 'review',
review: 'payment',
payment: 'confirmation'
};
// data was validated and no errors found
window.location.hash = nextHash[data.page];
}
});
return false;
});
// load cart review table in its container
if (data.page === 'review') {
for (var container in config.modules) {
M("#" + container, config.modules[container]);
}
page.find('[name="accept"]').prop('checked', false);
} else if (data.page === 'payment') {
var form = page.find('form');
for (var i in data.data) {
var input = $('<input>')
.attr('type', 'hidden')
.attr('name', i)
.val(data.data[i]);
input.appendTo(form);
}
form.submit();
} else if (data.page === 'confirmation') {
self.link('placeOrder', function (err, data) {
console.log(err, data);
});
}
// show the form errors if any
if (data.errors && data.errors.length) {
showErrors(data.errors);
}
});
}
function showErrors (errors) {
// remove all notifications
$(".notification").remove();
for (var i in errors) {
if (config.i18n) {
self.emit("message", errors[i], function (err, message) {
if (err) { return console.log(err); }
showError(message);
});
} else {
showError(errors[i]);
}
}
$(".notification").fadeIn();
}
function showError(err) {
var notification = Notification.new(err.message, "error", ["notification"]);
var input;
input = $("[name='" + err.name + "']", self.dom);
if (input.attr("type") === "checkbox") {
input = input.parent();
}
input.after(notification);
}