edit_package.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. App.Actions.PACKAGE.enable_unlimited = function (elm, source_elm) {
  2. $(elm).data('checked', true);
  3. $(elm).data('prev_value', $(elm).val()); // save prev value in order to restore if needed
  4. $(elm).val(Alpine.store('globals').UNLIM_TRANSLATED_VALUE);
  5. $(elm).attr('disabled', true);
  6. $(source_elm).css('opacity', '1');
  7. };
  8. App.Actions.PACKAGE.disable_unlimited = function (elm, source_elm) {
  9. $(elm).data('checked', false);
  10. if ($(elm).data('prev_value') && $(elm).data('prev_value').trim() != '') {
  11. var prev_value = $(elm).data('prev_value').trim();
  12. $(elm).val(prev_value);
  13. if (Alpine.store('globals').isUnlimitedValue(prev_value)) {
  14. $(elm).val('0');
  15. }
  16. } else {
  17. if (Alpine.store('globals').isUnlimitedValue($(elm).val())) {
  18. $(elm).val('0');
  19. }
  20. }
  21. $(elm).attr('disabled', false);
  22. $(source_elm).css('opacity', '0.5');
  23. };
  24. //
  25. App.Actions.PACKAGE.toggle_unlimited_feature = function (evt) {
  26. var elm = $(evt.target);
  27. var ref = elm.prev('.form-control');
  28. if (!$(ref).data('checked')) {
  29. App.Actions.PACKAGE.enable_unlimited(ref, elm);
  30. } else {
  31. App.Actions.PACKAGE.disable_unlimited(ref, elm);
  32. }
  33. };
  34. App.Listeners.PACKAGE.checkbox_unlimited_feature = function () {
  35. $('.unlim-trigger').on('click', App.Actions.PACKAGE.toggle_unlimited_feature);
  36. };
  37. App.Listeners.PACKAGE.init = function () {
  38. $('.unlim-trigger').each(function (i, elm) {
  39. var ref = $(elm).prev('.form-control');
  40. if (Alpine.store('globals').isUnlimitedValue($(ref).val())) {
  41. App.Actions.PACKAGE.enable_unlimited(ref, elm);
  42. } else {
  43. $(ref).data('prev_value', $(ref).val());
  44. App.Actions.PACKAGE.disable_unlimited(ref, elm);
  45. }
  46. });
  47. };
  48. //
  49. // Page entry point
  50. // Trigger listeners
  51. App.Listeners.PACKAGE.init();
  52. App.Listeners.PACKAGE.checkbox_unlimited_feature();
  53. App.Listeners.PACKAGE.submit = function () {
  54. $('input:disabled').each(function (i, elm) {
  55. $(elm).attr('disabled', false);
  56. if (Alpine.store('globals').isUnlimitedValue($(elm).val())) {
  57. $(elm).val(Alpine.store('globals').UNLIM_VALUE);
  58. }
  59. });
  60. };
  61. $(document).ready(function () {
  62. $('.js-add-ns').click(function () {
  63. var n = $('input[name^=v_ns]').length;
  64. if (n < 8) {
  65. var t = $($('input[name=v_ns1]').parents('div')[0]).clone(true, true);
  66. t.find('input').attr({ value: '', name: 'v_ns' + (n + 1) });
  67. t.find('span').show();
  68. $('.js-add-ns').before(t);
  69. }
  70. if (n == 7) {
  71. $('.js-add-ns').addClass('u-hidden');
  72. }
  73. });
  74. $('.js-remove-ns').click(function () {
  75. $(this).parents('div')[0].remove();
  76. $('input[name^=v_ns]').each(function (i, ns) {
  77. $(ns).attr({ name: 'v_ns' + (i + 1) });
  78. i < 2 ? $(ns).parent().find('span').hide() : $(ns).parent().find('span').show();
  79. });
  80. $('.js-add-ns').removeClass('u-hidden');
  81. });
  82. $('input[name^=v_ns]').each(function (i, ns) {
  83. i < 2 ? $(ns).parent().find('span').hide() : $(ns).parent().find('span').show();
  84. });
  85. });