scripts.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. (function(window, $) {
  2. var frontend = function(window, $) {
  3. var ShopProductsLightGalleryLastImage = '';
  4. // Private
  5. function ShopProductsInitLightGallery() {
  6. $('#product_thumbnails').lightGallery({
  7. pager: false,
  8. showThumbByDefault: false,
  9. toogleThumb: false,
  10. controls: false,
  11. download: false
  12. });
  13. $('#product_image img').css('cursor', 'pointer').click(function() {
  14. $($('#product_thumbnails a.thumbnail').get($(this).data('index'))).click();
  15. });
  16. $('#product_thumbnails a.thumbnail').each(function() {
  17. $(this).mouseover(function() {
  18. if($(this).data('hover') != ShopProductsLightGalleryLastImage) {
  19. ShopProductsLightGalleryLastImage = $(this).data('hover');
  20. $('#product_image img').attr('src', $(this).data('hover'));
  21. $('#product_image img').data('index', $(this).data('index'));
  22. }
  23. });
  24. });
  25. };
  26. function ShopBasketBlockObject(object) {
  27. if(object && !$(object).hasClass('click-blocked')) {
  28. $(object).addClass('click-blocked');
  29. }
  30. };
  31. function ShopBasketUnBlockObject(object) {
  32. if(object && $(object).hasClass('click-blocked')) {
  33. $(object).removeClass('click-blocked');
  34. }
  35. };
  36. function ShopBasketObjectIsNotBlocked(object) {
  37. if(!object) {
  38. return true;
  39. }
  40. return !$(object).hasClass('click-blocked');
  41. };
  42. function ShopBasketSetNavBtnProductsCount(value) {
  43. $('#basket-nav-btn .badge').html(value);
  44. $('#basket-mobile-btn').html(value);
  45. };
  46. function ShopBasketAjaxCommand(cmd, product_id, success, fail, always) {
  47. $.ajax({
  48. type: "GET",
  49. dataType: 'json',
  50. url: '/shop/basket/' + cmd + '/' + product_id + '/'
  51. }).done(function(data) {
  52. if(success) { success(data); }
  53. }).fail(function(xhr, status, error) {
  54. if(fail) { fail(xhr, status, error); }
  55. }).always(function() {
  56. if(always) { always(); }
  57. });
  58. };
  59. function ShopBasketAjaxGetInfo(success, fail, always) {
  60. $.ajax({
  61. type: "GET",
  62. dataType: 'json',
  63. url: '/shop/basket/info/'
  64. }).done(function(data) {
  65. if(success && data) { success(data); }
  66. }).fail(function(xhr, status, error) {
  67. if(fail) { fail(xhr, status, error); }
  68. }).always(function() {
  69. if(always) { always(); }
  70. });
  71. };
  72. function ShopBasketAjaxGetCount(success, fail, always) {
  73. ShopBasketAjaxGetInfo(function(data) {
  74. if(success && data && data.total_count != undefined) {
  75. success(data.total_count);
  76. }
  77. }, function(xhr, status, error) {
  78. if(fail) { fail(xhr, status, error); }
  79. }, function() {
  80. if(always) { always(); }
  81. });
  82. };
  83. function ShopBasketAjaxUpdateCount() {
  84. ShopBasketAjaxGetCount(function(count) {
  85. ShopBasketSetNavBtnProductsCount(count);
  86. });
  87. };
  88. function ShopBasketAjaxProductsHtml(success, fail, always) {
  89. ShopBasketAjaxGetInfo(function(data) {
  90. if(data) {
  91. if(data.total_count != undefined && data.total_count > 0) {
  92. var table = '';
  93. table += '<table class="table data-table table-striped table-bordered">';
  94. table += '<thead><tr><th class="thc-1">&nbsp;</th><th class="thc-2">' + ShopBasketTableProduct + '</th><th class="thc-3">' + ShopBasketTablePrice + '</th><th class="thc-4">' + ShopBasketTableQuantity + '</th><th class="thc-5">' + ShopBasketTableSum + '</th><th class="thc-6">&nbsp;</th></tr></thead>';
  95. table += '<tbody>';
  96. for(var i in data.products) {
  97. table += '<tr>';
  98. table += '<td class="thc-1 d-none d-md-table-cell"><img src="' + data.products[i].image + '" width="50" height="50" /></td>';
  99. table += '<td class="thc-2"><a href="' + data.products[i].link + '">' + data.products[i].name + '</a></td>';
  100. table += '<td class="thc-3">' + data.products[i].price + ' ' + data.currency.code + '</td>';
  101. table += '<td class="thc-4"><button type="button" class="btn btn-minus" onclick="frontend.ShopBasketProductMinus(this,' + data.products[i].id + ');"><span>-</span></button><input class="form-control" type="text" value="' + data.products[i].quantity + '" readonly><button type="button" class="btn btn-plus" onclick="frontend.ShopBasketProductPlus(this,' + data.products[i].id + ');"><span>+</span></button></td>';
  102. table += '<td class="thc-5">' + data.products[i].sum + ' ' + data.currency.code + '</td>';
  103. table += '<td class="thc-6"><a href="" onclick="frontend.ShopBasketProductRemove(this,' + data.products[i].id + ');return false;">&times;</a></td>';
  104. table += '</tr>';
  105. }
  106. table += '</tbody>';
  107. table += '</table>';
  108. table += '<div class="total"><span class="caption">' + ShopBasketTotal + '</span><span class="value">' + data.total_sum + ' ' + data.currency.code + '</span></div>';
  109. if(success) { success(table, data.total_count); }
  110. } else {
  111. if(success) { success(ShopBasketEmpty, 0); }
  112. }
  113. } else {
  114. window.location.reload(true);
  115. }
  116. }, function(xhr, status, error) {
  117. if(fail) { fail(xhr, status, error); }
  118. }, function() {
  119. if(always) { always(); }
  120. });
  121. };
  122. function ShopBasketEnableDisableOrderBtn(total) {
  123. $('#sys-modal-shop-basket button.btn-order').prop('disabled', total <= 0);
  124. };
  125. function Initialize() {
  126. // Check if jQuery was loaded
  127. if(typeof $ == 'function') {
  128. ShopProductsInitLightGallery();
  129. } else {
  130. console.log('Error: jQuery is not loaded!');
  131. }
  132. };
  133. // Initialize
  134. if(window.addEventListener) {
  135. // W3C standard
  136. window.addEventListener('load', Initialize, false);
  137. } else if(window.attachEvent) {
  138. // Microsoft
  139. window.attachEvent('onload', Initialize);
  140. };
  141. // Public
  142. return {
  143. ShopBasketBtnCollapse: function() {
  144. if(!$('.navbar-toggler').hasClass('collapsed')) {
  145. $('.navbar-toggler').click();
  146. }
  147. return true;
  148. },
  149. ShopBasketOpen: function(object) {
  150. if(ShopBasketObjectIsNotBlocked(object)) {
  151. ShopBasketBlockObject(object);
  152. var html = '<div class="modal fade" id="sys-modal-shop-basket" tabindex="-1" role="dialog" aria-labelledby="sysModalShopBasketLabel" aria-hidden="true"> \
  153. <div class="modal-dialog modal-dialog-centered" role="document"> \
  154. <div class="modal-content"> \
  155. <input type="hidden" name="action" value="index-user-update-profile"> \
  156. <div class="modal-header"> \
  157. <h5 class="modal-title" id="sysModalShopBasketLabel">' + ShopBasketName + '</h5> \
  158. <button type="button" class="close" data-dismiss="modal" aria-label="Close"> \
  159. <span aria-hidden="true">&times;</span> \
  160. </button> \
  161. </div> \
  162. <div class="modal-body text-left" style="position:relative;"> \
  163. <div class="blocker" style="position:absolute;left:0px;top:0px;width:100%;height:100%;background:#fff;opacity:0.5;display:none;"></div> \
  164. <div class="data"></div> \
  165. <div class="order-form mt-3" style="display:none;"> \
  166. <hr class="mb-4"> \
  167. <form class="data-form" action="/" method="post" autocomplete="off"> \
  168. <div class="hidden"><input type="hidden" name="action" value="shop-order"></div> \
  169. <div class="form-group"> \
  170. <div class="row"> \
  171. <div class="col-md-3"> \
  172. <label for="lbl_client_last_name">' + ShopOrderLabelLastName + '</label> \
  173. </div> \
  174. <div class="col-md-9"> \
  175. <input class="form-control" type="text" id="lbl_client_last_name" name="client_last_name" value="" minlength="1" maxlength="64" autocomplete="off" required> \
  176. </div> \
  177. </div> \
  178. </div> \
  179. <div class="form-group"> \
  180. <div class="row"> \
  181. <div class="col-md-3"> \
  182. <label for="lbl_client_first_name">' + ShopOrderLabelFirstName + '</label> \
  183. </div> \
  184. <div class="col-md-9"> \
  185. <input class="form-control" type="text" id="lbl_client_first_name" name="client_first_name" value="" minlength="1" maxlength="64" autocomplete="off" required> \
  186. </div> \
  187. </div> \
  188. </div> \
  189. <div class="form-group"> \
  190. <div class="row"> \
  191. <div class="col-md-3"> \
  192. <label for="lbl_client_second_name">' + ShopOrderLabelSecondName + '</label> \
  193. </div> \
  194. <div class="col-md-9"> \
  195. <input class="form-control" type="text" id="lbl_client_second_name" name="client_second_name" value="" minlength="1" maxlength="64" autocomplete="off" required> \
  196. </div> \
  197. </div> \
  198. </div> \
  199. <div class="form-group"> \
  200. <div class="row"> \
  201. <div class="col-md-3"> \
  202. <label for="lbl_client_phone">' + ShopOrderLabelMobilePhone + '</label> \
  203. </div> \
  204. <div class="col-md-9"> \
  205. <input class="form-control" type="text" id="lbl_client_phone" name="client_phone" value="" minlength="1" maxlength="20" autocomplete="off" required> \
  206. </div> \
  207. </div> \
  208. </div> \
  209. <div class="form-group"> \
  210. <div class="row"> \
  211. <div class="col-md-3"> \
  212. <label for="lbl_client_email">' + ShopOrderLabelEmailAddress + '</label> \
  213. </div> \
  214. <div class="col-md-9"> \
  215. <input class="form-control" type="text" id="lbl_client_email" name="client_email" value="" minlength="1" maxlength="64" autocomplete="off" required> \
  216. </div> \
  217. </div> \
  218. </div> \
  219. <div class="form-group"> \
  220. <div class="row"> \
  221. <div class="col-md-3"> \
  222. <label for="lbl_client_delivery_comment">' + ShopOrderLabelDelivery + '</label> \
  223. </div> \
  224. <div class="col-md-9"> \
  225. <input class="form-control" type="text" id="lbl_client_delivery_comment" name="client_delivery_comment" value="" minlength="1" maxlength="255" autocomplete="off"> \
  226. </div> \
  227. </div> \
  228. </div> \
  229. <div class="form-group"> \
  230. <div class="row"> \
  231. <div class="col-md-3"> \
  232. <label for="lbl_client_order_comment">' + ShopOrderLabelComment + '</label> \
  233. </div> \
  234. <div class="col-md-9"> \
  235. <textarea class="form-control" id="lbl_client_order_comment" name="client_order_comment" autocomplete="off"></textarea> \
  236. </div> \
  237. </div> \
  238. </div> \
  239. </form> \
  240. </div> \
  241. </div> \
  242. <div class="modal-footer"> \
  243. <button type="button" class="btn btn-close btn-secondary" data-dismiss="modal">' + ShopBasketBtnContinue + '</button> \
  244. <button type="button" class="btn btn-order btn-success" onclick="frontend.ShopBasketMakeOrder(this);return false;" disabled>' + ShopBasketBtnOrder + '</button> \
  245. </div> \
  246. </div> \
  247. </div> \
  248. </div>';
  249. $('#sys-modal-shop-basket-placeholder').html(html);
  250. // ---
  251. // var OrderForm = $('#sys-modal-shop-basket .modal-body .order-form form');
  252. // OrderForm.submit(function(e) {
  253. // if(OrderForm.hasClass('loading')) {
  254. // e.preventDefault();
  255. // return;
  256. // }
  257. // // Block send button
  258. // OrderForm.addClass('loading').addClass('alert-here');
  259. // var button = OrderForm.find('button[type=submit]');
  260. // button.addClass('progress-bar-striped')
  261. // .addClass('progress-bar-animated');
  262. // // Another button
  263. // if(button.attr('data-target') != '') {
  264. // $('#' + button.attr('data-target')).addClass('progress-bar-striped')
  265. // .addClass('progress-bar-animated');
  266. // }
  267. // // Clear form messages
  268. // form.find('.sys-messages').html('');
  269. // $.ajax({
  270. // type: "POST",
  271. // url: form.attr('action'),
  272. // data: form.serialize()
  273. // }).done(function(data) {
  274. // FormDataWasChanged = false;
  275. // if(IsDebugMode()) console.log('done', data);
  276. // AjaxDone(data)
  277. // }).fail(function(xhr, status, error) {
  278. // if(IsDebugMode()) console.log('fail', xhr, status, error);
  279. // AjaxFail(xhr.responseText, status, error);
  280. // }).always(function() {
  281. // // Add delay for one second
  282. // setTimeout(function() {
  283. // form.removeClass('loading').removeClass('alert-here');
  284. // button.removeClass('progress-bar-striped')
  285. // .removeClass('progress-bar-animated');
  286. // // Another button
  287. // if(button.attr('data-target') != '') {
  288. // $('#' + button.attr('data-target'))
  289. // .removeClass('progress-bar-striped')
  290. // .removeClass('progress-bar-animated');
  291. // }
  292. // }, 100);
  293. // });
  294. // // Prevent submit action
  295. // e.preventDefault();
  296. // });
  297. // ---
  298. $("#sys-modal-shop-basket").modal({
  299. backdrop: 'static',
  300. keyboard: true,
  301. show: false,
  302. });
  303. $('#sys-modal-shop-basket').on('hidden.bs.modal', function(e) {
  304. $('#sys-modal-shop-basket-placeholder').html('');
  305. $('#navbar-top').css('margin-right', $('#body').css('padding-right'));
  306. });
  307. ShopBasketAjaxProductsHtml(function(html, total) {
  308. $('#sys-modal-shop-basket .modal-body .data').html(html);
  309. ShopBasketEnableDisableOrderBtn(total);
  310. $("#sys-modal-shop-basket").modal('show');
  311. $('#navbar-top').css('margin-right', $('#body').css('padding-right'));
  312. }, function(xhr, status, error) {
  313. window.location.reload(true);
  314. });
  315. ShopBasketUnBlockObject(object);
  316. }
  317. return false;
  318. },
  319. ShopBasketProductAdd: function(object, product_id) {
  320. if(ShopBasketObjectIsNotBlocked(object)) {
  321. ShopBasketBlockObject(object);
  322. ShopBasketAjaxCommand('plus', product_id, function(data) {
  323. frontend.ShopBasketOpen();
  324. }, function(xhr, status, error) {
  325. window.location.reload(true);
  326. }, function() {
  327. ShopBasketAjaxUpdateCount();
  328. ShopBasketUnBlockObject(object);
  329. });
  330. }
  331. return false;
  332. },
  333. ShopBasketProductPlus: function(object, product_id) {
  334. if(ShopBasketObjectIsNotBlocked(object)) {
  335. ShopBasketBlockObject(object);
  336. $('#sys-modal-shop-basket .modal-body .blocker').css('display', 'block');
  337. ShopBasketAjaxCommand('plus', product_id, function(data) {
  338. ShopBasketAjaxProductsHtml(function(html, total) {
  339. $('#sys-modal-shop-basket .modal-body .data').html(html);
  340. ShopBasketEnableDisableOrderBtn(total);
  341. }, function(xhr, status, error) {
  342. window.location.reload(true);
  343. });
  344. }, function(xhr, status, error) {
  345. window.location.reload(true);
  346. }, function() {
  347. ShopBasketAjaxUpdateCount();
  348. ShopBasketUnBlockObject(object);
  349. $('#sys-modal-shop-basket .modal-body .blocker').css('display', 'none');
  350. });
  351. }
  352. return false;
  353. },
  354. ShopBasketProductMinus: function(object, product_id) {
  355. if(ShopBasketObjectIsNotBlocked(object)) {
  356. ShopBasketBlockObject(object);
  357. $('#sys-modal-shop-basket .modal-body .blocker').css('display', 'block');
  358. ShopBasketAjaxCommand('minus', product_id, function(data) {
  359. ShopBasketAjaxProductsHtml(function(html, total) {
  360. $('#sys-modal-shop-basket .modal-body .data').html(html);
  361. ShopBasketEnableDisableOrderBtn(total);
  362. }, function(xhr, status, error) {
  363. window.location.reload(true);
  364. });
  365. }, function(xhr, status, error) {
  366. window.location.reload(true);
  367. }, function() {
  368. ShopBasketAjaxUpdateCount();
  369. ShopBasketUnBlockObject(object);
  370. $('#sys-modal-shop-basket .modal-body .blocker').css('display', 'none');
  371. });
  372. }
  373. return false;
  374. },
  375. ShopBasketProductRemove: function(object, product_id) {
  376. if(ShopBasketObjectIsNotBlocked(object)) {
  377. ShopBasketBlockObject(object);
  378. $('#sys-modal-shop-basket .modal-body .blocker').css('display', 'block');
  379. ShopBasketAjaxCommand('remove', product_id, function(data) {
  380. ShopBasketAjaxProductsHtml(function(html, total) {
  381. $('#sys-modal-shop-basket .modal-body .data').html(html);
  382. ShopBasketEnableDisableOrderBtn(total);
  383. }, function(xhr, status, error) {
  384. window.location.reload(true);
  385. });
  386. }, function(xhr, status, error) {
  387. window.location.reload(true);
  388. }, function() {
  389. ShopBasketAjaxUpdateCount();
  390. ShopBasketUnBlockObject(object);
  391. $('#sys-modal-shop-basket .modal-body .blocker').css('display', 'none');
  392. });
  393. }
  394. return false;
  395. },
  396. ShopBasketMakeOrder: function(object) {
  397. if(ShopBasketObjectIsNotBlocked(object)) {
  398. var OrderFormBlock = $('#sys-modal-shop-basket .modal-body .order-form');
  399. if(OrderFormBlock.css('display') == 'none') {
  400. OrderFormBlock.css('display', 'block');
  401. setTimeout(function() { OrderFormBlock.find('input.form-control').first().focus(); }, 500);
  402. return;
  403. }
  404. // Validate
  405. // Send form
  406. ShopBasketBlockObject(object);
  407. var OrderForm = $('#sys-modal-shop-basket .modal-body .order-form form');
  408. $.ajax({
  409. type: "POST",
  410. url: OrderForm.attr('action'),
  411. data: OrderForm.serialize()
  412. }).done(function(data) {
  413. // FormDataWasChanged = false;
  414. // if(IsDebugMode()) console.log('done', data);
  415. // AjaxDone(data);
  416. // ---
  417. // ShopOrderSuccess
  418. // ShopOrderErrorMobilePhone
  419. // ShopOrderErrorEmailAddress
  420. console.log('Order action done', data);
  421. }).fail(function(xhr, status, error) {
  422. // if(IsDebugMode()) console.log('fail', xhr, status, error);
  423. // AjaxFail(xhr.responseText, status, error);
  424. console.log('Order action fail', xhr, status, error);
  425. }).always(function() {
  426. ShopBasketUnBlockObject(object);
  427. });
  428. }
  429. },
  430. };
  431. }(window, $);
  432. window.frontend = frontend;
  433. }(window, jQuery));