· 8 years ago · Feb 20, 2017, 01:26 PM
1const directory = "assets/index/";
2
3$(document).keypress((e) => {
4 if (e.which == 13 && $('.modal-open').length) {
5 if ($('#tab-1').is(':checked')) {
6 checkEmptyLoginInputs($('#emailLogin'), $('#passLogin'));
7 } else if ($('#tab-2').is(':checked')) {
8 checkEmptyLoginInputs($('#emailSignUp'), $('#passSignUp'));
9 } else if ($('#sendNewPass').length) {
10 forgotEmailCheck();
11 }
12 }
13});
14
15$('#attractionModal').on('show.bs.modal', function (event) {
16 const attractionData = event.relatedTarget.dataset;
17 $(this).find('.modal-title').text(attractionData.name);
18 $('#attrDescription').text(attractionData.description);
19 $('#attrCategory').text(attractionData.category);
20 $('#attrCategory').attr('data-content', attractionData.catdescription);
21 $('#price').data('price', attractionData.price).text(parseFloat(attractionData.price).toFixed(2));
22 $('#ticket-counter').text('1');
23 $('.btn-footer-buy').data('id', attractionData.id);
24 const img = $('#attrid');
25 img.attr('src', attractionData.image);
26 $('#imgClone').remove();
27 setTimeout(() => {
28 const imgClone = img.clone()
29 .attr('id', 'imgClone')
30 .offset({
31 top: img.offset().top,
32 left: img.offset().left
33 })
34 .addClass('image-transition hidden')
35 .appendTo($('body'));
36 }, 400)
37});
38
39$('body')
40 .on('click', 'a.login-link', function () {
41 const modalBody = $('#loginBody');
42 const loginText = "Log in";
43 this.text == loginText ? modalBody.load(directory + "login.html") : modalBody.load(directory + "signup.html");
44 })
45
46 .on('click', 'div.box', function () {
47 changeQuantity();
48 checkMaintenance($(this));
49 })
50
51 .on('click', 'span.change-quant', changeQuantity)
52
53 .on('click', 'a.forgot-link', function () {
54 const modalBody = $('#loginBody');
55 const forgotText = "Forgot Password?";
56 this.text == forgotText ? modalBody.load(directory + "login_forgot_password.html") : modalBody.load(directory + "login.html");
57 })
58
59 .on('click', '#sendNewPass', forgotEmailCheck)
60
61 .on('click', '#logInButton', () => checkEmptyLoginInputs($('#emailLogin'), $('#passLogin')))
62
63 .on('click', '#signUpButton', () => checkEmptyLoginInputs($('#emailSignUp'), $('#passSignUp')))
64
65 .on('keydown', '#emailLogin', emailWarningRemove)
66 .on('keydown', '#emailSignUp', emailWarningRemove)
67 .on('keydown', '#passSignUp', emailWarningRemove)
68 .on('click', 'label', emailWarningRemove)
69
70 .on('click', '.btn-footer-buy', function () {
71 if (!($('.avatar-img').length)) {
72 openLoginWindow();
73 } else {
74 const cnt = parseInt($('#ticket-counter').text(), 10);
75 addItemToCart($(this).data('id'), cnt);
76 animatePicture($(this));
77 }
78 const menuSpan = $('.menu-span');
79 const oldCartCount = parseInt(menuSpan.text());
80 const thisOrderCount = parseInt($('#ticket-counter').text());
81 menuSpan.text(oldCartCount + thisOrderCount);
82 });
83
84$(function () {
85 const access_token = localStorage.getItem('access_token');
86 if (access_token) {
87 getAccountInfo();
88 } else {
89 $('#menuPlace').load(directory + "menu_login.html");
90 }
91 if ((document.referrer.includes('localhost') || document.referrer.includes('ticketsale')) && !access_token) {
92 $('#loginBody').load(directory + "login.html");
93 $('#openModal').modal('show');
94 activationMessage('activateid', 'success', 'Email confirmed!');
95 activationMessage('wrongactivationlink', 'danger', 'Wrong activation link!');
96 }
97 attractionsQuery();
98});
99
100function activationMessage(url, className, message) {
101 const timeout = 800;
102 if (document.referrer.includes(url)) {
103 setTimeout(() => {
104 $('#logInButton').after('<p class="text-center text-' + className
105 + ' font-semi-big" style="margin-top: 60px;">' + message + '</p>');
106 $('.hr').remove();
107 $('.foot-lnk').remove();
108 }, timeout);
109 }
110}
111
112function animatePicture(btn) {
113 if ($(window).width() >= 992){
114 $('#attractionModal').modal('hide');
115 const imgClone = $('#imgClone');
116 const avatar = $('.avatar-img');
117 imgClone.removeClass('hidden');
118 imgClone.animate({
119 'top': avatar.offset().top + 15,
120 'left': avatar.offset().left + 15,
121 'width': 100,
122 'height': 100
123 }, 1000)
124 .animate({
125 'width': 0,
126 'height': 0
127 }, function () {
128 $(this).detach();
129 });
130 } else {
131 const btnCopy = $(btn).clone();
132 $('.modal-footer')
133 .html('<p class="text-center">Item added to your cart!</p>');
134 setTimeout(function () {
135 $('#attractionModal').modal('hide');
136 setTimeout(function () {
137 $('.modal-footer').html(btnCopy);
138 }, 200);
139 }, 1000);
140 }
141}
142
143function changeQuantity() {
144 const quantity = $('#ticket-counter');
145 const sumPrice = $('#price');
146 let ticketCounter = parseInt(quantity.text(), 10);
147 const how = ($(this).data('how')) ? $(this).data('how') : '';
148
149 if (how === 'down') {
150 (ticketCounter > 1) ? ticketCounter -= 1 : ticketCounter = 1;
151 } else if (how === 'up') {
152 ticketCounter += 1;
153 } else ticketCounter = 1;
154 quantity.text(ticketCounter);
155 sumPrice.text((ticketCounter * parseFloat(sumPrice.data('price'))).toFixed(2));
156}
157
158function checkEmptyLoginInputs(input, pass) {
159 if (!input.val().isEmptyString()) {
160 if (input.val().isTrueEmail() && !(pass.val().isEmptyString())) {
161 if (pass.attr('id') == 'passSignUp') {
162 checkPasswords(input, pass, $('#confirmPassSignUp'))
163 } else {
164 loginUser(input, pass);
165 }
166 } else {
167 throwPasswordException(input, pass);
168 }
169 } else {
170 input.attr('placeholder', 'Write your E-Mail!');
171 emailWarningRemove();
172 }
173}
174
175function checkMaintenance(box) {
176 const mtnText = box.data('maintenance');
177 const flag = (mtnText == 'no');
178 const buyButton = $('.btn-footer-buy');
179 $('.maintenance-text').toggleClass('no-display', flag)
180 .text('')
181 .append(mtnText);
182 buyButton.prop('disabled', !flag);
183}
184
185function checkPasswords(login, pass1, pass2) {
186 const validCheck = (pass1.val() == pass2.val());
187 const lengthCheck = (pass1.val().length > 5 && pass1.val().length < 21);
188 if (validCheck) {
189 if (lengthCheck) {
190 signUpUser(login, pass1);
191 } else {
192 setPasswordException('Password length: 6-20 symbols');
193 pass1.val('');
194 pass2.val('');
195 }
196 } else {
197 setPasswordException('Incorrect password confirmation');
198 pass1.val('');
199 pass2.val('');
200 }
201}
202
203function emailWarningRemove() {
204 $('.incorrect-email-text').remove();
205}
206
207function forgotEmailCheck() {
208 const input = $('#emailForgot');
209 if (!input.val().isEmptyString()) {
210 if (input.val().isTrueEmail()) {
211 newPasswordQuery(input.val());
212 } else {
213 throwWrongEmailException('Incorrect Email address!');
214 }
215 } else {
216 input.attr('placeholder', 'Write your E-Mail!');
217 }
218}
219
220function loadAttractions(data) {
221 data.forEach((attraction, i) => {
222 $('<img />').attr('src',attraction.image.substr(1)).appendTo('body').hide();
223 let mtnText = 'no';
224 if (attraction.maintenance) {
225 mtnText = 'Attraction is currently on maintenance';
226 if (attraction.maintenance.enddate) {
227 const enddate = attraction.maintenance.enddate;
228 mtnText += (' till ' + enddate);
229 }
230 if (attraction.maintenance.reason) {
231 const reason = attraction.maintenance.reason;
232 mtnText += (', reason: ' + reason);
233 }
234 }
235 const category = (attraction.category) ? attraction.category.name : 'Not indicated';
236 let desc = category;
237 if (attraction.category) {
238 desc = 'Age: ' + attraction.category.minAge + '+, height: ' + attraction.category.minHeight + '+ cm';
239 }
240 $('#main').append('<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 no-padding">'
241 + '<div class="box" id="attr' + i + '" data-toggle="modal" data-target="#attractionModal" '
242 + 'data-name="' + attraction.name + '" data-description="' + attraction.description + '" '
243 + 'data-category="' + category + '" data-catdescription="' + desc + '" data-price="'
244 + attraction.price + '" data-id="' + attraction.id + '" '
245 + 'data-maintenance="' + mtnText + '" data-image="' + attraction.image.substr(1) + '">'
246 + '<div class="box-icon"></div><div class="box-title">' + attraction.name + '</div></div></div>');
247 $('#attr' + i).find('.box-icon').css('background-image', 'url(' + attraction.thumbnail.substr(1) + ')');
248 });
249}
250
251function loginUser(input, pass) {
252 $.post(server + "oauth/token", {
253 client_id: 'web',
254 client_secret: 'ticketsale',
255 grant_type: 'password',
256 username: $(input).val(),
257 password: md5($(pass).val())
258 }, setAccessToken)
259 .error((e) => {
260 console.log(e);
261 if (e.status == 400) {
262 throwPasswordException(input, pass, e);
263 }
264 });
265}
266
267function newPasswordQuery(email) {
268 const mailString='?mail=' + email;
269 $.ajax({
270 url: server + "api/accounts/newpass" + mailString,
271 headers: {
272 'Accept': 'application/json',
273 'Content-Type': 'application/json'
274 },
275 data: {},
276 type: 'GET',
277 success: function(json){
278 console.log('woohoo', json);
279 $('.login-form').html('<p class="check-email-text">Message sent!<br>Check your E-Mail.</p>');
280 },
281 error: (e) => {
282 throwWrongEmailException(e.responseText['error_description']);
283 }
284 });
285}
286
287function openLoginWindow() {
288 $('#attractionModal').modal('hide');
289 $('#loginBody').load(directory + "login.html");
290 $('#openModal').modal('show');
291}
292
293function setPasswordException(exceptionText) {
294 $('.log-in-htm').after('<p class="text-warning incorrect-email-text incorrect-pass-confirm">' + exceptionText + '</p>');
295}
296
297function signUpSuccessMessage() {
298 $('#loginBody').html('<a href="#" class="close-new" data-dismiss="modal" aria-label="Close"><span>×</span></a>'
299 + '<p class="sign-up-success-text text-center">Confirmation message sent!<br>Check your E-Mail.</p>');
300}
301
302function signUpUser(login, pass) {
303 $.postJSON(server + "api/accounts", {
304 mail: $(login).val(),
305 password: md5(pass.val())
306 }, (info) => {
307 signUpSuccessMessage();
308 })
309 .error(function(e) {
310 throwPasswordException(login, pass, e);
311 console.log(e);
312 });
313}
314
315function throwPasswordException(input, pass, e=0) {
316 const errorText = (e) ? (JSON.parse(e.responseText).error_description) : 'Incorrect Email or empty password!';
317 $('.log-in-htm').after('<p class="text-warning incorrect-email-text">' + errorText + '</p>');
318 input.val('');
319 pass.val('');
320 $('#confirmPassSignUp').val('');
321 input.attr('placeholder', '');
322}
323
324function throwWrongEmailException(message = 'Error!') {
325 $('#emailForgot').find('text-warning').remove();
326 $('#forgot-text').after('<p class="text-warning"><br>' + message + '</p>')
327}