PageRenderTime 63ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/admin864m691ka/themes/default/template/controllers/cart_rules/form.js

https://gitlab.com/nghiep5890/prestashop
JavaScript | 506 lines | 426 code | 53 blank | 27 comment | 50 complexity | d9397c5e74bfc3148c57b96628ab7e1f 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. $('#gift_products_found').hide();}
  235. );
  236. toggleGiftProduct();
  237. // Main form submit
  238. $('#cart_rule_form').submit(function() {
  239. if ($('#customerFilter').val() == '')
  240. $('#id_customer').val('0');
  241. for (i in restrictions)
  242. {
  243. if ($('#' + restrictions[i] + '_select_1 option').length == 0)
  244. $('#' + restrictions[i] + '_restriction').prop('checked', false);
  245. else
  246. {
  247. $('#' + restrictions[i] + '_select_2 option').each(function(i) {
  248. $(this).prop('selected', true);
  249. });
  250. }
  251. }
  252. $('.product_rule_toselect option').each(function(i) {
  253. $(this).prop('selected', true);
  254. });
  255. });
  256. $('#reductionProductFilter')
  257. .autocomplete(
  258. 'ajax-tab.php', {
  259. minChars: 2,
  260. max: 50,
  261. width: 500,
  262. selectFirst: false,
  263. scroll: false,
  264. dataType: 'json',
  265. formatItem: function(data, i, max, value, term) {
  266. return value;
  267. },
  268. parse: function(data) {
  269. var mytab = new Array();
  270. for (var i = 0; i < data.length; i++)
  271. mytab[mytab.length] = { data: data[i], value: (data[i].reference + ' ' + data[i].name).trim() };
  272. return mytab;
  273. },
  274. extraParams: {
  275. controller: 'AdminCartRules',
  276. token: currentToken,
  277. reductionProductFilter: 1
  278. }
  279. }
  280. )
  281. .result(function(event, data, formatted) {
  282. $('#reduction_product').val(data.id_product);
  283. $('#reductionProductFilter').val((data.reference + ' ' + data.name).trim());
  284. });
  285. $('#customerFilter')
  286. .autocomplete(
  287. 'ajax-tab.php', {
  288. minChars: 2,
  289. max: 50,
  290. width: 500,
  291. selectFirst: false,
  292. scroll: false,
  293. dataType: 'json',
  294. formatItem: function(data, i, max, value, term) {
  295. return value;
  296. },
  297. parse: function(data) {
  298. var mytab = new Array();
  299. for (var i = 0; i < data.length; i++)
  300. mytab[mytab.length] = { data: data[i], value: data[i].cname + ' (' + data[i].email + ')' };
  301. return mytab;
  302. },
  303. extraParams: {
  304. controller: 'AdminCartRules',
  305. token: currentToken,
  306. customerFilter: 1
  307. }
  308. }
  309. )
  310. .result(function(event, data, formatted) {
  311. $('#id_customer').val(data.id_customer);
  312. $('#customerFilter').val(data.cname + ' (' + data.email + ')');
  313. });
  314. function displayCartRuleTab(tab)
  315. {
  316. $('.cart_rule_tab').hide();
  317. $('.tab-row.active').removeClass('active');
  318. $('#cart_rule_' + tab).show();
  319. $('#cart_rule_link_' + tab).parent().addClass('active');
  320. $('#currentFormTab').val(tab);
  321. }
  322. $('.cart_rule_tab').hide();
  323. $('.tab-row.active').removeClass('active');
  324. $('#cart_rule_' + currentFormTab).show();
  325. $('#cart_rule_link_' + currentFormTab).parent().addClass('active');
  326. var date = new Date();
  327. var hours = date.getHours();
  328. if (hours < 10)
  329. hours = "0" + hours;
  330. var mins = date.getMinutes();
  331. if (mins < 10)
  332. mins = "0" + mins;
  333. var secs = date.getSeconds();
  334. if (secs < 10)
  335. secs = "0" + secs;
  336. $('.datepicker').datetimepicker({
  337. beforeShow: function (input, inst) {
  338. setTimeout(function () {
  339. inst.dpDiv.css({
  340. 'z-index': 1031
  341. });
  342. }, 0);
  343. },
  344. prevText: '',
  345. nextText: '',
  346. dateFormat: 'yy-mm-dd',
  347. // Define a custom regional settings in order to use PrestaShop translation tools
  348. currentText: currentText,
  349. closeText:closeText,
  350. ampm: false,
  351. amNames: ['AM', 'A'],
  352. pmNames: ['PM', 'P'],
  353. timeFormat: 'hh:mm:ss tt',
  354. timeSuffix: '',
  355. timeOnlyTitle: timeOnlyTitle,
  356. timeText: timeText,
  357. hourText: hourText,
  358. minuteText: minuteText,
  359. });
  360. $('#giftProductFilter').typeWatch({
  361. captureLength: 2,
  362. highlight: false,
  363. wait: 100,
  364. callback: function(){ searchProducts(); }
  365. });
  366. var gift_product_search = $('#giftProductFilter').val();
  367. function searchProducts()
  368. {
  369. if ($('#giftProductFilter').val() == gift_product_search)
  370. return;
  371. gift_product_search = $('#giftProductFilter').val();
  372. $.ajax({
  373. type: 'POST',
  374. headers: { "cache-control": "no-cache" },
  375. url: 'ajax-tab.php' + '?rand=' + new Date().getTime(),
  376. async: true,
  377. dataType: 'json',
  378. data: {
  379. controller: 'AdminCartRules',
  380. token: currentToken,
  381. action: 'searchProducts',
  382. product_search: $('#giftProductFilter').val()
  383. },
  384. success : function(res)
  385. {
  386. var products_found = '';
  387. var attributes_html = '';
  388. stock = {};
  389. if (res.found)
  390. {
  391. $('#gift_products_err').hide();
  392. $('#gift_products_found').show();
  393. $.each(res.products, function() {
  394. products_found += '<option value="' + this.id_product + '">' + this.name + (this.combinations.length == 0 ? ' - ' + this.formatted_price : '') + '</option>';
  395. attributes_html += '<select class="id_product_attribute" id="ipa_' + this.id_product + '" name="ipa_' + this.id_product + '" style="display:none">';
  396. $.each(this.combinations, function() {
  397. attributes_html += '<option ' + (this.default_on == 1 ? 'selected="selected"' : '') + ' value="' + this.id_product_attribute + '">' + this.attributes + ' - ' + this.formatted_price + '</option>';
  398. });
  399. attributes_html += '</select>';
  400. });
  401. $('#gift_product_list #gift_product').html(products_found);
  402. $('#gift_attributes_list #gift_attributes_list_select').html(attributes_html);
  403. displayProductAttributes();
  404. }
  405. else
  406. {
  407. $('#products_found').hide();
  408. $('#products_err').html(res.notfound);
  409. $('#products_err').show();
  410. }
  411. }
  412. });
  413. }
  414. function displayProductAttributes()
  415. {
  416. if ($('#ipa_' + $('#gift_product option:selected').val() + ' option').length === 0)
  417. $('#gift_attributes_list').hide();
  418. else
  419. {
  420. $('#gift_attributes_list').show();
  421. $('.id_product_attribute').hide();
  422. $('#ipa_' + $('#gift_product option:selected').val()).show();
  423. }
  424. }
  425. $(document).ready(function() {
  426. $(window).keydown(function(event){
  427. if(event.keyCode == 13) {
  428. event.preventDefault();
  429. return false;
  430. }
  431. });
  432. if ($('#cart_rule_select_1').length > 0 && $('#cart_rule_select_2').length > 0) {
  433. $('#cart_rule_select_1').jscroll().data('jscrollapi').load_scroll(baseHref+'&type=unselected&search=');
  434. $('#cart_rule_select_2').jscroll().data('jscrollapi').load_scroll(baseHref+'&type=selected&search=');
  435. $('.uncombinable_search_filter').typeWatch({
  436. captureLength: -1,
  437. highlight: true,
  438. wait: 500,
  439. callback: function(text) { combinable_filter('#cart_rule_select_1', text, 'unselected'); }
  440. });
  441. $('.combinable_search_filter').typeWatch({
  442. captureLength: -1,
  443. highlight: true,
  444. wait: 500,
  445. callback: function(text) { combinable_filter('#cart_rule_select_2', text, 'selected'); }
  446. });
  447. }
  448. });
  449. function combinable_filter(id_rule, search, type)
  450. {
  451. var href = baseHref+'&type='+encodeURIComponent(type)+'&search='+encodeURIComponent(search);
  452. $(id_rule).jscroll().data('jscrollapi').load_scroll(href);
  453. }