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

/admin-dev/themes/default/template/controllers/cart_rules/form.js

https://gitlab.com/jslee1/PrestaShop
JavaScript | 505 lines | 425 code | 53 blank | 27 comment | 50 complexity | d7d5efa3f853411b4002a0954faf1a3e MD5 | raw file
  1. /**
  2. * 2007-2015 PrestaShop
  3. *
  4. * NOTICE OF LICENSE
  5. *
  6. * This source file is subject to the Open Software License (OSL 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/osl-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-2015 PrestaShop SA
  22. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  23. * International Registered Trademark & Property of PrestaShop SA
  24. */
  25. function addProductRuleGroup()
  26. {
  27. $('#product_rule_group_table').show();
  28. product_rule_groups_counter += 1;
  29. product_rule_counters[product_rule_groups_counter] = 0;
  30. $.get(
  31. 'ajax-tab.php',
  32. {controller:'AdminCartRules',token:currentToken,newProductRuleGroup:1,product_rule_group_id:product_rule_groups_counter},
  33. function(content) {
  34. if (content != "")
  35. $('#product_rule_group_table').append(content);
  36. }
  37. );
  38. }
  39. function removeProductRuleGroup(id)
  40. {
  41. $('#product_rule_group_' + id + '_tr').remove();
  42. }
  43. function addProductRule(product_rule_group_id)
  44. {
  45. product_rule_counters[product_rule_group_id] += 1;
  46. if ($('#product_rule_type_' + product_rule_group_id).val() != 0)
  47. $.get(
  48. 'ajax-tab.php',
  49. {controller:'AdminCartRules',token:currentToken,newProductRule:1,product_rule_type:$('#product_rule_type_' + product_rule_group_id).val(),product_rule_group_id:product_rule_group_id,product_rule_id:product_rule_counters[product_rule_group_id]},
  50. function(content) {
  51. if (content != "")
  52. $('#product_rule_table_' + product_rule_group_id).append(content);
  53. }
  54. );
  55. }
  56. function removeProductRule(product_rule_group_id, product_rule_id)
  57. {
  58. $('#product_rule_' + product_rule_group_id + '_' + product_rule_id + '_tr').remove();
  59. }
  60. function toggleCartRuleFilter(id)
  61. {
  62. if ($(id).prop('checked'))
  63. $('#' + $(id).attr('id') + '_div').show(400);
  64. else
  65. $('#' + $(id).attr('id') + '_div').hide(200);
  66. }
  67. function removeCartRuleOption(item)
  68. {
  69. var id = $(item).attr('id').replace('_remove', '');
  70. $('#' + id + '_2 option:selected').remove().appendTo('#' + id + '_1');
  71. }
  72. function addCartRuleOption(item)
  73. {
  74. var id = $(item).attr('id').replace('_add', '');
  75. $('#' + id + '_1 option:selected').remove().appendTo('#' + id + '_2');
  76. }
  77. function updateProductRuleShortDescription(item)
  78. {
  79. /******* For IE: put a product in condition on cart rules *******/
  80. if(typeof String.prototype.trim !== 'function') {
  81. String.prototype.trim = function() {
  82. return this.replace(/^\s+|\s+$/g, '');
  83. }
  84. }
  85. var id1 = $(item).attr('id').replace('_add', '').replace('_remove', '');
  86. var id2 = id1.replace('_select', '');
  87. var length = $('#' + id1 + '_2 option').length;
  88. if (length == 1)
  89. $('#' + id2 + '_match').val($('#' + id1 + '_2 option').first().text().trim());
  90. else
  91. $('#' + id2 + '_match').val(length);
  92. }
  93. var restrictions = new Array('country', 'carrier', 'group', 'cart_rule', 'shop');
  94. for (i in restrictions)
  95. {
  96. toggleCartRuleFilter($('#' + restrictions[i] + '_restriction'));
  97. $('#' + restrictions[i] + '_restriction').click(function() {toggleCartRuleFilter(this);});
  98. $('#' + restrictions[i] + '_select_remove').click(function() {removeCartRuleOption(this);});
  99. $('#' + restrictions[i] + '_select_add').click(function() {addCartRuleOption(this);});
  100. }
  101. toggleCartRuleFilter($('#product_restriction'));
  102. $('#product_restriction').click(function() {
  103. toggleCartRuleFilter(this);
  104. if ($(this).prop('checked'))
  105. {
  106. $('#apply_discount_to_selection').prop('disabled', false);
  107. $('#apply_discount_to_selection_warning').hide();
  108. }
  109. else
  110. {
  111. $('#apply_discount_to_selection').prop('disabled', true);
  112. $('#apply_discount_to_selection_warning').show();
  113. }
  114. });
  115. $('#apply_discount_to_selection_shortcut').click(function(e) {
  116. displayCartRuleTab('conditions');
  117. $('#product_restriction').focus();
  118. e.preventDefault();
  119. });
  120. function toggleApplyDiscount(percent, amount, apply_to)
  121. {
  122. if (percent)
  123. {
  124. $('#apply_discount_percent_div').show(400);
  125. if ($('#apply_discount_to_product').prop('checked'))
  126. toggleApplyDiscountTo();
  127. $('#apply_discount_to_cheapest').show();
  128. $('*[for=apply_discount_to_cheapest]').show();
  129. $('#apply_discount_to_selection').show();
  130. $('*[for=apply_discount_to_selection]').show();
  131. }
  132. else
  133. {
  134. $('#apply_discount_percent_div').hide(200);
  135. $('#reduction_percent').val('0');
  136. }
  137. if (amount)
  138. {
  139. $('#apply_discount_amount_div').show(400);
  140. if ($('#apply_discount_to_product').prop('checked'))
  141. toggleApplyDiscountTo();
  142. $('#apply_discount_to_cheapest').hide();
  143. $('*[for=apply_discount_to_cheapest]').hide();
  144. $('#apply_discount_to_cheapest').prop('checked', false);
  145. $('#apply_discount_to_selection').hide();
  146. $('*[for=apply_discount_to_selection]').hide();
  147. $('#apply_discount_to_selection').prop('checked', false);
  148. }
  149. else
  150. {
  151. $('#apply_discount_amount_div').hide(200);
  152. $('#reduction_amount').val('0');
  153. if ($('#apply_discount_off').prop('checked'))
  154. {
  155. $('#apply_discount_to_product').prop('checked', false)
  156. toggleApplyDiscountTo();
  157. }
  158. }
  159. if (apply_to)
  160. $('#apply_discount_to_div').show(400);
  161. else
  162. {
  163. toggleApplyDiscountTo();
  164. $('#apply_discount_to_div').hide(200);
  165. }
  166. }
  167. function toggleApplyDiscountTo()
  168. {
  169. if ($('#apply_discount_to_product').prop('checked'))
  170. $('#apply_discount_to_product_div').show(400);
  171. else
  172. {
  173. $('#apply_discount_to_product_div').hide(200);
  174. $('#reductionProductFilter').val('');
  175. if ($('#apply_discount_to_order').prop('checked'))
  176. $('#reduction_product').val('0');
  177. if ($('#apply_discount_to_cheapest').prop('checked'))
  178. $('#reduction_product').val('-1');
  179. if ($('#apply_discount_to_selection').prop('checked'))
  180. $('#reduction_product').val('-2');
  181. }
  182. }
  183. function toggleGiftProduct()
  184. {
  185. if ($('#free_gift_on').prop('checked'))
  186. $('#free_gift_div').show(400);
  187. else
  188. {
  189. $('#gift_product').val('0');
  190. $('#giftProductFilter').val('');
  191. $('#free_gift_div').hide(200);
  192. }
  193. }
  194. $('#apply_discount_percent').click(function(){
  195. toggleApplyDiscount(true, false, true);
  196. });
  197. if ($('#apply_discount_percent').prop('checked'))
  198. toggleApplyDiscount(true, false, true);
  199. $('#apply_discount_amount').click(function(){
  200. toggleApplyDiscount(false, true, true);
  201. });
  202. if ($('#apply_discount_amount').prop('checked'))
  203. toggleApplyDiscount(false, true, true);
  204. $('#apply_discount_off').click(function(){
  205. toggleApplyDiscount(false, false, false);
  206. });
  207. if ($('#apply_discount_off').prop('checked'))
  208. toggleApplyDiscount(false, false, false);
  209. $('#apply_discount_to_order').click(function(){
  210. toggleApplyDiscountTo();}
  211. );
  212. if ($('#apply_discount_to_order').prop('checked'))
  213. toggleApplyDiscountTo();
  214. $('#apply_discount_to_product').click(function(){
  215. toggleApplyDiscountTo();}
  216. );
  217. if ($('#apply_discount_to_product').prop('checked'))
  218. toggleApplyDiscountTo();
  219. $('#apply_discount_to_cheapest').click(function(){
  220. toggleApplyDiscountTo();}
  221. );
  222. if ($('#apply_discount_to_cheapest').prop('checked'))
  223. toggleApplyDiscountTo();
  224. $('#apply_discount_to_selection').click(function(){
  225. toggleApplyDiscountTo();}
  226. );
  227. if ($('#apply_discount_to_selection').prop('checked'))
  228. toggleApplyDiscountTo();
  229. $('#free_gift_on').click(function(){
  230. toggleGiftProduct();}
  231. );
  232. $('#free_gift_off').click(function(){
  233. toggleGiftProduct();}
  234. );
  235. toggleGiftProduct();
  236. // Main form submit
  237. $('#cart_rule_form').submit(function() {
  238. if ($('#customerFilter').val() == '')
  239. $('#id_customer').val('0');
  240. for (i in restrictions)
  241. {
  242. if ($('#' + restrictions[i] + '_select_1 option').length == 0)
  243. $('#' + restrictions[i] + '_restriction').prop('checked', false);
  244. else
  245. {
  246. $('#' + restrictions[i] + '_select_2 option').each(function(i) {
  247. $(this).prop('selected', true);
  248. });
  249. }
  250. }
  251. $('.product_rule_toselect option').each(function(i) {
  252. $(this).prop('selected', true);
  253. });
  254. });
  255. $('#reductionProductFilter')
  256. .autocomplete(
  257. 'ajax-tab.php', {
  258. minChars: 2,
  259. max: 50,
  260. width: 500,
  261. selectFirst: false,
  262. scroll: false,
  263. dataType: 'json',
  264. formatItem: function(data, i, max, value, term) {
  265. return value;
  266. },
  267. parse: function(data) {
  268. var mytab = new Array();
  269. for (var i = 0; i < data.length; i++)
  270. mytab[mytab.length] = { data: data[i], value: (data[i].reference + ' ' + data[i].name).trim() };
  271. return mytab;
  272. },
  273. extraParams: {
  274. controller: 'AdminCartRules',
  275. token: currentToken,
  276. reductionProductFilter: 1
  277. }
  278. }
  279. )
  280. .result(function(event, data, formatted) {
  281. $('#reduction_product').val(data.id_product);
  282. $('#reductionProductFilter').val((data.reference + ' ' + data.name).trim());
  283. });
  284. $('#customerFilter')
  285. .autocomplete(
  286. 'ajax-tab.php', {
  287. minChars: 2,
  288. max: 50,
  289. width: 500,
  290. selectFirst: false,
  291. scroll: false,
  292. dataType: 'json',
  293. formatItem: function(data, i, max, value, term) {
  294. return value;
  295. },
  296. parse: function(data) {
  297. var mytab = new Array();
  298. for (var i = 0; i < data.length; i++)
  299. mytab[mytab.length] = { data: data[i], value: data[i].cname + ' (' + data[i].email + ')' };
  300. return mytab;
  301. },
  302. extraParams: {
  303. controller: 'AdminCartRules',
  304. token: currentToken,
  305. customerFilter: 1
  306. }
  307. }
  308. )
  309. .result(function(event, data, formatted) {
  310. $('#id_customer').val(data.id_customer);
  311. $('#customerFilter').val(data.cname + ' (' + data.email + ')');
  312. });
  313. function displayCartRuleTab(tab)
  314. {
  315. $('.cart_rule_tab').hide();
  316. $('.tab-row.active').removeClass('active');
  317. $('#cart_rule_' + tab).show();
  318. $('#cart_rule_link_' + tab).parent().addClass('active');
  319. $('#currentFormTab').val(tab);
  320. }
  321. $('.cart_rule_tab').hide();
  322. $('.tab-row.active').removeClass('active');
  323. $('#cart_rule_' + currentFormTab).show();
  324. $('#cart_rule_link_' + currentFormTab).parent().addClass('active');
  325. var date = new Date();
  326. var hours = date.getHours();
  327. if (hours < 10)
  328. hours = "0" + hours;
  329. var mins = date.getMinutes();
  330. if (mins < 10)
  331. mins = "0" + mins;
  332. var secs = date.getSeconds();
  333. if (secs < 10)
  334. secs = "0" + secs;
  335. $('.datepicker').datetimepicker({
  336. beforeShow: function (input, inst) {
  337. setTimeout(function () {
  338. inst.dpDiv.css({
  339. 'z-index': 1031
  340. });
  341. }, 0);
  342. },
  343. prevText: '',
  344. nextText: '',
  345. dateFormat: 'yy-mm-dd',
  346. // Define a custom regional settings in order to use PrestaShop translation tools
  347. currentText: currentText,
  348. closeText:closeText,
  349. ampm: false,
  350. amNames: ['AM', 'A'],
  351. pmNames: ['PM', 'P'],
  352. timeFormat: 'hh:mm:ss tt',
  353. timeSuffix: '',
  354. timeOnlyTitle: timeOnlyTitle,
  355. timeText: timeText,
  356. hourText: hourText,
  357. minuteText: minuteText,
  358. });
  359. $('#giftProductFilter').typeWatch({
  360. captureLength: 2,
  361. highlight: false,
  362. wait: 100,
  363. callback: function(){ searchProducts(); }
  364. });
  365. var gift_product_search = $('#giftProductFilter').val();
  366. function searchProducts()
  367. {
  368. if ($('#giftProductFilter').val() == gift_product_search)
  369. return;
  370. gift_product_search = $('#giftProductFilter').val();
  371. $.ajax({
  372. type: 'POST',
  373. headers: { "cache-control": "no-cache" },
  374. url: 'ajax-tab.php' + '?rand=' + new Date().getTime(),
  375. async: true,
  376. dataType: 'json',
  377. data: {
  378. controller: 'AdminCartRules',
  379. token: currentToken,
  380. action: 'searchProducts',
  381. product_search: $('#giftProductFilter').val()
  382. },
  383. success : function(res)
  384. {
  385. var products_found = '';
  386. var attributes_html = '';
  387. stock = {};
  388. if (res.found)
  389. {
  390. $('#gift_products_err').hide();
  391. $('#gift_products_found').show();
  392. $.each(res.products, function() {
  393. products_found += '<option value="' + this.id_product + '">' + this.name + (this.combinations.length == 0 ? ' - ' + this.formatted_price : '') + '</option>';
  394. attributes_html += '<select class="id_product_attribute" id="ipa_' + this.id_product + '" name="ipa_' + this.id_product + '" style="display:none">';
  395. $.each(this.combinations, function() {
  396. attributes_html += '<option ' + (this.default_on == 1 ? 'selected="selected"' : '') + ' value="' + this.id_product_attribute + '">' + this.attributes + ' - ' + this.formatted_price + '</option>';
  397. });
  398. attributes_html += '</select>';
  399. });
  400. $('#gift_product_list #gift_product').html(products_found);
  401. $('#gift_attributes_list #gift_attributes_list_select').html(attributes_html);
  402. displayProductAttributes();
  403. }
  404. else
  405. {
  406. $('#products_found').hide();
  407. $('#products_err').html(res.notfound);
  408. $('#products_err').show();
  409. }
  410. }
  411. });
  412. }
  413. function displayProductAttributes()
  414. {
  415. if ($('#ipa_' + $('#gift_product option:selected').val() + ' option').length === 0)
  416. $('#gift_attributes_list').hide();
  417. else
  418. {
  419. $('#gift_attributes_list').show();
  420. $('.id_product_attribute').hide();
  421. $('#ipa_' + $('#gift_product option:selected').val()).show();
  422. }
  423. }
  424. $(document).ready(function() {
  425. $(window).keydown(function(event){
  426. if(event.keyCode == 13) {
  427. event.preventDefault();
  428. return false;
  429. }
  430. });
  431. if ($('#cart_rule_select_1').length > 0 && $('#cart_rule_select_2').length > 0) {
  432. $('#cart_rule_select_1').jscroll().data('jscrollapi').load_scroll(baseHref+'&type=unselected&search=');
  433. $('#cart_rule_select_2').jscroll().data('jscrollapi').load_scroll(baseHref+'&type=selected&search=');
  434. $('.uncombinable_search_filter').typeWatch({
  435. captureLength: -1,
  436. highlight: true,
  437. wait: 500,
  438. callback: function(text) { combinable_filter('#cart_rule_select_1', text, 'unselected'); }
  439. });
  440. $('.combinable_search_filter').typeWatch({
  441. captureLength: -1,
  442. highlight: true,
  443. wait: 500,
  444. callback: function(text) { combinable_filter('#cart_rule_select_2', text, 'selected'); }
  445. });
  446. }
  447. });
  448. function combinable_filter(id_rule, search, type)
  449. {
  450. var href = baseHref+'&type='+encodeURIComponent(type)+'&search='+encodeURIComponent(search);
  451. $(id_rule).jscroll().data('jscrollapi').load_scroll(href);
  452. }