/
index_1.1.js
201 lines (152 loc) · 5.71 KB
/
index_1.1.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
"use strict";
var theDocument = jQuery(document);
theDocument.ajaxSend(function (event, xhr) {
var cartId = getCookie('UltraCartShoppingCartID');
var merchantId = window.merchantId || getCookie('UltraCartMerchantID');
xhr.setRequestHeader("cache-control", "no-cache");
xhr.setRequestHeader("X-UC-Merchant-Id", merchantId);
xhr.setRequestHeader("X-UC-Shopping-Cart-Id", cartId);
});
var templates = {};
function initialize() {
ultracart.myAccount.loggedIn({
success: function (settings) {
if (!settings) {
displayLoginForm();
jQuery('#recent-activity').html('');
} else {
displayWelcome(settings);
loadRecentOrders();
}
}
});
}
function displayLoginForm() {
enableButtons();
jQuery('.welcome-message').html('Welcome to your account management screen. Please login below.');
jQuery('#login-form,#register-form').show();
}
function displayWelcome(settings) {
jQuery('.nav-logout').show();
jQuery('#login-form,#register-form').hide(); // might already be hidden. that's okay.
var template = "Welcome <strong>{{firstName}} {{lastName}}</strong> to your account management screen.";
var welcomeHB = Handlebars.compile(template);
var html = welcomeHB(settings);
jQuery('.welcome-message').html(html);
}
function emailPassword() {
clearAllMessages();
var email = jQuery.trim(jQuery('#email').val());
if (!email) {
showError("Please enter your email to receive your password.");
return;
}
disableButtons();
//noinspection JSUnusedLocalSymbols
ultracart.myAccount.forgotPassword(email, {
success: function (account) {
enableButtons();
showInfo("Your password was emailed to you. Please check your inbox.");
},
failure: function (textStatus, errorThrown) {
showError("We could not email your password to you at this time. Please try again in a few minutes.");
enableButtons();
}
});
}
function disableButtons() {
jQuery('#login-button,#email-password-button,#register-button').attr('disabled', true);
}
function enableButtons() {
jQuery('#login-button,#email-password-button,#register-button').attr('disabled', false);
}
function login() {
clearAllMessages();
var email = jQuery.trim(jQuery('#email').val());
var password = jQuery.trim(jQuery('#password').val());
if (!email || !password) {
showError("Both email and password are required to login.");
return;
}
disableButtons();
//noinspection JSUnusedLocalSymbols
ultracart.myAccount.login(email, password, {
success: function (account) {
enableButtons();
displayWelcome(account);
loadRecentOrders();
},
failure: function (textStatus, errorThrown) {
showError("Login failed. Please check your credentials and try again.");
enableButtons();
}
});
}
function register() {
clearAllMessages();
var email = jQuery.trim(jQuery('#register-email').val());
var password = jQuery.trim(jQuery('#register-password').val());
var passwordAgain = jQuery.trim(jQuery('#register-password-again').val());
var redirectUrl = null;
var redirectUrlEl = jQuery('#redirect-url');
if (redirectUrlEl) {
redirectUrl = redirectUrlEl.val();
} else {
redirectUrl = location.href;
redirectUrl += redirectUrl.indexOf('?') > -1 ? "&validated=true" : "?validated=true";
}
var themeCode = null; // theme code is only useful for MyAccount portals hosted on the UltraCart, which this code base is not intended for.
if (!email || !password || !passwordAgain) {
showError("All fields are required to register.");
return false;
}
if (password !== passwordAgain) {
showError("Passwords do not match. Please re-type your password in both fields.");
return false;
}
disableButtons();
//noinspection JSUnusedLocalSymbols
var settings = {'email': email, 'password': password};
ultracart.myAccount.createAccount(settings, {
success: function (msg) {
showInfo(msg);
enableButtons();
},
failure: function (jqXHR, textStatus) {
var message = jqXHR.getResponseHeader('UC-REST-ERROR');
if (!message) {
message = textStatus;
}
showError("Registration failed with the following message: " + message);
enableButtons();
},
redirectUrl: redirectUrl,
themeCode: themeCode
});
}
function loadRecentOrders() {
//noinspection JSUnusedLocalSymbols
ultracart.myAccount.getOrders({
filter: 'last6months',
success: function (orders, pagination) {
var html = '<h4>Recent Activity</h4>No recent activity.';
if (orders && orders.length) {
var context = {'orders': orders};
html = templates.recentOrders(context);
}
jQuery('#recent-activity').html(html);
}
});
}
jQuery(document).ready(function () {
enablePleaseWaitMessage();
templates.recentOrders = Handlebars.compile(jQuery('#recent-orders-template').html());
initialize();
jQuery('#login-button').bind('click', login);
jQuery('#register-button').bind('click', register);
jQuery('#email-password-button').bind('click', emailPassword);
// if you provide a custom redirect url, you'll probably want to que off something other than this parameter.
if (location.href.indexOf("validated=true") > -1) {
showSuccess("Your email was validated. Please login.");
}
});