v1.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * Determines whether its argument represents a JavaScript number.
  3. * @param {*} obj
  4. * @returns bool
  5. */
  6. function isNumeric(obj)
  7. {
  8. return (!isNaN(obj) && typeof obj === 'number') || $.isNumeric(obj);;
  9. }
  10. /**
  11. * Add new item.
  12. *
  13. * @param obj e
  14. * @access public
  15. * @return void
  16. */
  17. window.addItem = function(e)
  18. {
  19. const obj = e.target
  20. const thisRow = $(obj).closest('.form-row');
  21. const newItem = thisRow.clone();
  22. let index = 0;
  23. newItem.find('.add-btn').on('click', addItem);
  24. newItem.find('.del-btn').on('click', removeItem);
  25. let inputName = newItem.find('input').length > 0 ? newItem.find('input').first().attr('name') : newItem.find('select').first().attr('name');
  26. inputName = inputName.slice(0, inputName.indexOf('['));
  27. $('form').find("[name^='" + inputName + "']").each(function() {
  28. let $name = $(this).attr('name');
  29. let id = parseInt($name.slice($name.indexOf('[')+1, $name.indexOf(']')));
  30. if(isNumeric(id) && id >= index) index = id + 1;
  31. })
  32. /* Fix id and value. */
  33. newItem.addClass('newItem');
  34. newItem.find('.form-label').html('');
  35. newItem.find('input').each(function()
  36. {
  37. let name = $(this).attr('name');
  38. name = name.slice(0, name.indexOf('[')+1) + String(index) + name.slice(name.indexOf(']'));
  39. $(this).attr('name', name);
  40. $(this).attr('id', name);
  41. $(this).val('');
  42. });
  43. newItem.find('select').each(function()
  44. {
  45. let name = $(this).attr('name');
  46. name = name.slice(0, name.indexOf('[')+1) + String(index) + name.slice(name.indexOf(']'));
  47. $(this).attr('name', name);
  48. $(this).attr('id', name);
  49. $(this).val('');
  50. });
  51. newItem.find('.picker-box').each(function()
  52. {
  53. $this = $(this);
  54. name = $this.find('input[name]').attr('name');
  55. $this.removeAttr('data-zui-picker').removeAttr('id').attr('data-name', name).empty();
  56. });
  57. $(obj).closest('.form-row').after(newItem);
  58. newItem.find('.picker-box').each(function()
  59. {
  60. $this = $(this);
  61. name = $this.attr('data-name');
  62. id = name.substr(0, name.indexOf('['));
  63. options = thisRow.find('[name^=' + id + ']').zui('picker').options;
  64. $this.picker({name: name, items: options.items, defaultValue: options.defaultValue, required: options.required});
  65. });
  66. }
  67. /**
  68. * Remove item.
  69. *
  70. * @param obj e
  71. * @access public
  72. * @return void
  73. */
  74. window.removeItem = function(e)
  75. {
  76. const obj = e.target
  77. /* Dsiabled btn can't remove line. */
  78. if($(obj).closest('.btn').hasClass('disabled')) return false;
  79. if($(obj).parents('form').find('.action-group').not('.child-hidden').length == 1) return false;
  80. $(obj).closest('.form-row').remove();
  81. let chosenProducts = 0;
  82. $("select[name^='products']").each(function()
  83. {
  84. if($(this).val() > 0) chosenProducts ++;
  85. });
  86. (chosenProducts.length > 1 && (model == 'waterfall' || model == 'waterfallplus')) ? $('.stageBy').removeClass('hide') : $('.stageBy').addClass('hide');
  87. }