PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/themes/default-bootstrap/js/modules/blockwishlist/js/ajax-wishlist.js

https://gitlab.com/ivandevp/petshop
JavaScript | 480 lines | 386 code | 29 blank | 65 comment | 39 complexity | 3ba239a524031a3a11079d15cf7fb211 MD5 | raw file
  1. /*
  2. * 2007-2016 PrestaShop
  3. *
  4. * NOTICE OF LICENSE
  5. *
  6. * This source file is subject to the Academic Free License (AFL 3.0)
  7. * that is bundled with this package in the file LICENSE.txt.
  8. * It is also available through the world-wide-web at this URL:
  9. * http://opensource.org/licenses/afl-3.0.php
  10. * If you did not receive a copy of the license and are unable to
  11. * obtain it through the world-wide-web, please send an email
  12. * to license@prestashop.com so we can send you a copy immediately.
  13. *
  14. * DISCLAIMER
  15. *
  16. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  17. * versions in the future. If you wish to customize PrestaShop for your
  18. * needs please refer to http://www.prestashop.com for more information.
  19. *
  20. * @author PrestaShop SA <contact@prestashop.com>
  21. * @copyright 2007-2016 PrestaShop SA
  22. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  23. * International Registered Trademark & Property of PrestaShop SA
  24. */
  25. /**
  26. * Update WishList Cart by adding, deleting, updating objects
  27. *
  28. * @return void
  29. */
  30. //global variables
  31. var wishlistProductsIds = [];
  32. $(document).ready(function(){
  33. wishlistRefreshStatus();
  34. $(document).on('change', 'select[name=wishlists]', function(){
  35. WishlistChangeDefault('wishlist_block_list', $(this).val());
  36. });
  37. $("#wishlist_button").popover({
  38. html: true,
  39. content: function () {
  40. return $("#popover-content").html();
  41. }
  42. });
  43. $('.wishlist').each(function() {
  44. current = $(this);
  45. $(this).children('.wishlist_button_list').popover({
  46. html: true,
  47. content: function () {
  48. return current.children('.popover-content').html();
  49. }
  50. });
  51. });
  52. });
  53. function WishlistCart(id, action, id_product, id_product_attribute, quantity, id_wishlist)
  54. {
  55. $.ajax({
  56. type: 'GET',
  57. url: baseDir + 'modules/blockwishlist/cart.php?rand=' + new Date().getTime(),
  58. headers: { "cache-control": "no-cache" },
  59. async: true,
  60. cache: false,
  61. data: 'action=' + action + '&id_product=' + id_product + '&quantity=' + quantity + '&token=' + static_token + '&id_product_attribute=' + id_product_attribute + '&id_wishlist=' + id_wishlist,
  62. success: function(data)
  63. {
  64. if (action == 'add')
  65. {
  66. if (isLogged == true) {
  67. wishlistProductsIdsAdd(id_product);
  68. wishlistRefreshStatus();
  69. if (!!$.prototype.fancybox)
  70. $.fancybox.open([
  71. {
  72. type: 'inline',
  73. autoScale: true,
  74. minHeight: 30,
  75. content: '<p class="fancybox-error">' + added_to_wishlist + '</p>'
  76. }
  77. ], {
  78. padding: 0
  79. });
  80. else
  81. alert(added_to_wishlist);
  82. }
  83. else
  84. {
  85. if (!!$.prototype.fancybox)
  86. $.fancybox.open([
  87. {
  88. type: 'inline',
  89. autoScale: true,
  90. minHeight: 30,
  91. content: '<p class="fancybox-error">' + loggin_required + '</p>'
  92. }
  93. ], {
  94. padding: 0
  95. });
  96. else
  97. alert(loggin_required);
  98. }
  99. }
  100. if (action == 'delete') {
  101. wishlistProductsIdsRemove(id_product);
  102. wishlistRefreshStatus();
  103. }
  104. if($('#' + id).length != 0)
  105. {
  106. $('#' + id).slideUp('normal');
  107. document.getElementById(id).innerHTML = data;
  108. $('#' + id).slideDown('normal');
  109. }
  110. }
  111. });
  112. }
  113. /**
  114. * Change customer default wishlist
  115. *
  116. * @return void
  117. */
  118. function WishlistChangeDefault(id, id_wishlist)
  119. {
  120. $.ajax({
  121. type: 'GET',
  122. url: baseDir + 'modules/blockwishlist/cart.php?rand=' + new Date().getTime(),
  123. headers: { "cache-control": "no-cache" },
  124. async: true,
  125. data: 'id_wishlist=' + id_wishlist + '&token=' + static_token,
  126. cache: false,
  127. success: function(data)
  128. {
  129. $('#' + id).slideUp('normal');
  130. document.getElementById(id).innerHTML = data;
  131. $('#' + id).slideDown('normal');
  132. }
  133. });
  134. }
  135. /**
  136. * Buy Product
  137. *
  138. * @return void
  139. */
  140. function WishlistBuyProduct(token, id_product, id_product_attribute, id_quantity, button, ajax)
  141. {
  142. if(ajax)
  143. ajaxCart.add(id_product, id_product_attribute, false, button, 1, [token, id_quantity]);
  144. else
  145. {
  146. $('#' + id_quantity).val(0);
  147. WishlistAddProductCart(token, id_product, id_product_attribute, id_quantity)
  148. document.forms['addtocart' + '_' + id_product + '_' + id_product_attribute].method='POST';
  149. document.forms['addtocart' + '_' + id_product + '_' + id_product_attribute].action=baseUri + '?controller=cart';
  150. document.forms['addtocart' + '_' + id_product + '_' + id_product_attribute].elements['token'].value = static_token;
  151. document.forms['addtocart' + '_' + id_product + '_' + id_product_attribute].submit();
  152. }
  153. return (true);
  154. }
  155. function WishlistAddProductCart(token, id_product, id_product_attribute, id_quantity)
  156. {
  157. if ($('#' + id_quantity).val() <= 0)
  158. return (false);
  159. $.ajax({
  160. type: 'GET',
  161. url: baseDir + 'modules/blockwishlist/buywishlistproduct.php?rand=' + new Date().getTime(),
  162. headers: { "cache-control": "no-cache" },
  163. data: 'token=' + token + '&static_token=' + static_token + '&id_product=' + id_product + '&id_product_attribute=' + id_product_attribute,
  164. async: true,
  165. cache: false,
  166. success: function(data)
  167. {
  168. if (data)
  169. {
  170. if (!!$.prototype.fancybox)
  171. $.fancybox.open([
  172. {
  173. type: 'inline',
  174. autoScale: true,
  175. minHeight: 30,
  176. content: '<p class="fancybox-error">' + data + '</p>'
  177. }
  178. ], {
  179. padding: 0
  180. });
  181. else
  182. alert(data);
  183. }
  184. else
  185. $('#' + id_quantity).val($('#' + id_quantity).val() - 1);
  186. }
  187. });
  188. return (true);
  189. }
  190. /**
  191. * Show wishlist managment page
  192. *
  193. * @return void
  194. */
  195. function WishlistManage(id, id_wishlist)
  196. {
  197. $.ajax({
  198. type: 'GET',
  199. async: true,
  200. url: baseDir + 'modules/blockwishlist/managewishlist.php?rand=' + new Date().getTime(),
  201. headers: { "cache-control": "no-cache" },
  202. data: 'id_wishlist=' + id_wishlist + '&refresh=' + false,
  203. cache: false,
  204. success: function(data)
  205. {
  206. $('#' + id).hide();
  207. document.getElementById(id).innerHTML = data;
  208. $('#' + id).fadeIn('slow');
  209. $('.wishlist_change_button').each(function(index) {
  210. $(this).popover({
  211. html: true,
  212. content: function () {
  213. return $(this).next('.popover-content').html();
  214. }
  215. });
  216. });
  217. }
  218. });
  219. }
  220. /**
  221. * Show wishlist product managment page
  222. *
  223. * @return void
  224. */
  225. function WishlistProductManage(id, action, id_wishlist, id_product, id_product_attribute, quantity, priority)
  226. {
  227. $.ajax({
  228. type: 'GET',
  229. async: true,
  230. url: baseDir + 'modules/blockwishlist/managewishlist.php?rand=' + new Date().getTime(),
  231. headers: { "cache-control": "no-cache" },
  232. data: 'action=' + action + '&id_wishlist=' + id_wishlist + '&id_product=' + id_product + '&id_product_attribute=' + id_product_attribute + '&quantity=' + quantity + '&priority=' + priority + '&refresh=' + true,
  233. cache: false,
  234. success: function(data)
  235. {
  236. if (action == 'delete')
  237. $('#wlp_' + id_product + '_' + id_product_attribute).fadeOut('fast');
  238. else if (action == 'update')
  239. {
  240. $('#wlp_' + id_product + '_' + id_product_attribute).fadeOut('fast');
  241. $('#wlp_' + id_product + '_' + id_product_attribute).fadeIn('fast');
  242. }
  243. nb_products = 0;
  244. $("[id^='quantity']").each(function(index, element){
  245. nb_products += parseInt(element.value);
  246. });
  247. $("#wishlist_"+id_wishlist).children('td').eq(1).html(nb_products);
  248. }
  249. });
  250. }
  251. /**
  252. * Delete wishlist
  253. *
  254. * @return boolean succeed
  255. */
  256. function WishlistDelete(id, id_wishlist, msg)
  257. {
  258. var res = confirm(msg);
  259. if (res == false)
  260. return (false);
  261. if (typeof mywishlist_url == 'undefined')
  262. return (false);
  263. $.ajax({
  264. type: 'GET',
  265. async: true,
  266. dataType: "json",
  267. url: mywishlist_url,
  268. headers: { "cache-control": "no-cache" },
  269. cache: false,
  270. data: {
  271. rand: new Date().getTime(),
  272. deleted: 1,
  273. myajax: 1,
  274. id_wishlist: id_wishlist,
  275. action: 'deletelist'
  276. },
  277. success: function(data)
  278. {
  279. var mywishlist_siblings_count = $('#' + id).siblings().length;
  280. $('#' + id).fadeOut('slow').remove();
  281. $("#block-order-detail").html('');
  282. if (mywishlist_siblings_count == 0)
  283. $("#block-history").remove();
  284. if (data.id_default)
  285. {
  286. var td_default = $("#wishlist_"+data.id_default+" > .wishlist_default");
  287. $("#wishlist_"+data.id_default+" > .wishlist_default > a").remove();
  288. td_default.append('<p class="is_wish_list_default"><i class="icon icon-check-square"></i></p>');
  289. }
  290. }
  291. });
  292. }
  293. function WishlistDefault(id, id_wishlist)
  294. {
  295. if (typeof mywishlist_url == 'undefined')
  296. return (false);
  297. $.ajax({
  298. type: 'GET',
  299. async: true,
  300. url: mywishlist_url,
  301. headers: { "cache-control": "no-cache" },
  302. cache: false,
  303. data: {
  304. rand:new Date().getTime(),
  305. 'default': 1,
  306. id_wishlist:id_wishlist,
  307. myajax: 1,
  308. action: 'setdefault'
  309. },
  310. success: function (data)
  311. {
  312. var old_default_id = $(".is_wish_list_default").parents("tr").attr("id");
  313. var td_check = $(".is_wish_list_default").parent();
  314. $(".is_wish_list_default").remove();
  315. td_check.append('<a href="#" onclick="javascript:event.preventDefault();(WishlistDefault(\''+old_default_id+'\', \''+old_default_id.replace("wishlist_", "")+'\'));"><i class="icon icon-square"></i></a>');
  316. var td_default = $("#"+id+" > .wishlist_default");
  317. $("#"+id+" > .wishlist_default > a").remove();
  318. td_default.append('<p class="is_wish_list_default"><i class="icon icon-check-square"></i></p>');
  319. }
  320. });
  321. }
  322. /**
  323. * Hide/Show bought product
  324. *
  325. * @return void
  326. */
  327. function WishlistVisibility(bought_class, id_button)
  328. {
  329. if ($('#hide' + id_button).is(':hidden'))
  330. {
  331. $('.' + bought_class).slideDown('fast');
  332. $('#show' + id_button).hide();
  333. $('#hide' + id_button).css('display', 'block');
  334. }
  335. else
  336. {
  337. $('.' + bought_class).slideUp('fast');
  338. $('#hide' + id_button).hide();
  339. $('#show' + id_button).css('display', 'block');
  340. }
  341. }
  342. /**
  343. * Send wishlist by email
  344. *
  345. * @return void
  346. */
  347. function WishlistSend(id, id_wishlist, id_email)
  348. {
  349. $.post(
  350. baseDir + 'modules/blockwishlist/sendwishlist.php',
  351. {
  352. token: static_token,
  353. id_wishlist: id_wishlist,
  354. email1: $('#' + id_email + '1').val(),
  355. email2: $('#' + id_email + '2').val(),
  356. email3: $('#' + id_email + '3').val(),
  357. email4: $('#' + id_email + '4').val(),
  358. email5: $('#' + id_email + '5').val(),
  359. email6: $('#' + id_email + '6').val(),
  360. email7: $('#' + id_email + '7').val(),
  361. email8: $('#' + id_email + '8').val(),
  362. email9: $('#' + id_email + '9').val(),
  363. email10: $('#' + id_email + '10').val()
  364. },
  365. function(data)
  366. {
  367. if (data)
  368. {
  369. if (!!$.prototype.fancybox)
  370. $.fancybox.open([
  371. {
  372. type: 'inline',
  373. autoScale: true,
  374. minHeight: 30,
  375. content: '<p class="fancybox-error">' + data + '</p>'
  376. }
  377. ], {
  378. padding: 0
  379. });
  380. else
  381. alert(data);
  382. }
  383. else
  384. WishlistVisibility(id, 'hideSendWishlist');
  385. }
  386. );
  387. }
  388. function wishlistProductsIdsAdd(id)
  389. {
  390. if ($.inArray(parseInt(id),wishlistProductsIds) == -1)
  391. wishlistProductsIds.push(parseInt(id))
  392. }
  393. function wishlistProductsIdsRemove(id)
  394. {
  395. wishlistProductsIds.splice($.inArray(parseInt(id),wishlistProductsIds), 1)
  396. }
  397. function wishlistRefreshStatus()
  398. {
  399. $('.addToWishlist').each(function(){
  400. if ($.inArray(parseInt($(this).prop('rel')),wishlistProductsIds)!= -1)
  401. $(this).addClass('checked');
  402. else
  403. $(this).removeClass('checked');
  404. });
  405. }
  406. function wishlistProductChange(id_product, id_product_attribute, id_old_wishlist, id_new_wishlist)
  407. {
  408. if (typeof mywishlist_url == 'undefined')
  409. return (false);
  410. var quantity = $('#quantity_' + id_product + '_' + id_product_attribute).val();
  411. $.ajax({
  412. type: 'GET',
  413. url: mywishlist_url,
  414. headers: { "cache-control": "no-cache" },
  415. async: true,
  416. cache: false,
  417. dataType: "json",
  418. data: {
  419. id_product:id_product,
  420. id_product_attribute:id_product_attribute,
  421. quantity: quantity,
  422. priority: $('#priority_' + id_product + '_' + id_product_attribute).val(),
  423. id_old_wishlist:id_old_wishlist,
  424. id_new_wishlist:id_new_wishlist,
  425. myajax: 1,
  426. action: 'productchangewishlist'
  427. },
  428. success: function (data)
  429. {
  430. if (data.success == true) {
  431. $('#wlp_' + id_product + '_' + id_product_attribute).fadeOut('slow');
  432. $('#wishlist_' + id_old_wishlist + ' td:nth-child(2)').text($('#wishlist_' + id_old_wishlist + ' td:nth-child(2)').text() - quantity);
  433. $('#wishlist_' + id_new_wishlist + ' td:nth-child(2)').text(+$('#wishlist_' + id_new_wishlist + ' td:nth-child(2)').text() + +quantity);
  434. }
  435. else
  436. {
  437. if (!!$.prototype.fancybox)
  438. $.fancybox.open([
  439. {
  440. type: 'inline',
  441. autoScale: true,
  442. minHeight: 30,
  443. content: '<p class="fancybox-error">' + data.error + '</p>'
  444. }
  445. ], {
  446. padding: 0
  447. });
  448. }
  449. }
  450. });
  451. }