Projet

Général

Profil

0001-forms-use-ajax-to-add-a-block-row-51369.patch

Frédéric Péters, 23 février 2021 22:49

Télécharger (3,69 ko)

Voir les différences:

Subject: [PATCH] forms: use ajax to add a block row (#51369)

 wcs/qommon/static/js/qommon.forms.js | 61 ++++++++++++++++++++--------
 1 file changed, 45 insertions(+), 16 deletions(-)
wcs/qommon/static/js/qommon.forms.js
366 366
      }
367 367
    });
368 368
  });
369
  $('form div[data-live-source] input, form div[data-live-source] select, form div[data-live-source] textarea').on('change input paste wcs:change', function(ev) {
370
      var modified_field = $(this).parents('[data-field-id]').data('field-id');
371
      $(this).parents('form').trigger('wcs:change', {modified_field: modified_field});
372
  });
369
  if ($('div[data-live-source]').length) {
370
    $('form').on('change input paste wcs:change',
371
        'div[data-live-source] input, div[data-live-source] select, div[data-live-source] textarea',
372
        function(ev) {
373
          var modified_field = $(this).parents('[data-field-id]').data('field-id');
374
         $(this).parents('form').trigger('wcs:change', {modified_field: modified_field});
375
    });
376
  }
373 377
  $('form div[data-live-source]').parents('form').trigger('wcs:change', {modified_field: 'init'});
374 378

  
375 379
  /* searchable select */
......
479 483
    });
480 484
  }
481 485

  
482
  disable_single_block_remove_button();
483
  $('.BlockSubWidget button.remove-button').on('click', function() {
484
    if ($(this).parents('.BlockWidget').find('.BlockSubWidget').length > 1) {
485
      $(this).parents('.BlockWidget').find('.list-add').show();
486
      $(this).parents('.BlockSubWidget').remove();
487
      disable_single_block_remove_button();
488
    }
489
    return false;
490
  });
486
  if ($('.BlockWidget').length) {
487
    disable_single_block_remove_button();
488
    $('form').on('click', '.BlockSubWidget button.remove-button', function() {
489
      if ($(this).parents('.BlockWidget').find('.BlockSubWidget').length > 1) {
490
        $(this).parents('.BlockWidget').find('.list-add').show();
491
        $(this).parents('.BlockSubWidget').remove();
492
        disable_single_block_remove_button();
493
      }
494
      return false;
495
    });
491 496

  
492
  const $added_sub_widget = $('.wcs-block-add-clicked').find('.BlockSubWidget').last();
493
  if ($added_sub_widget.length) {
494
    $added_sub_widget[0].scrollIntoView({behavior: "instant", block: "center", inline: "nearest"});
497
    $('form').on('click keyup', 'div.BlockWidget .list-add button', function(ev) {
498
      if (event.type == 'keyup' && event.keyCode !== 13) {
499
        return;
500
      }
501
      ev.preventDefault();
502
      const $block = $(this).parents('.BlockWidget');
503
      const block_id = $block.data('field-id');
504
      const $button = $(this);
505
      const $form = $(this).parents('form');
506
      var form_data = $form.serialize();
507
      form_data += '&' + $button.attr('name') + '=' + $button.val();
508
      $.ajax({
509
        type: 'POST',
510
        url: $form.attr('action') || window.location.pathname,
511
        data: form_data,
512
        headers: {'x-wcs-ajax-action': 'block-add-row'},
513
        success: function(result, text_status, jqXHR) {
514
          result = $(result);
515
          $block.replaceWith(result.find('[data-field-id="' + block_id + '"]'));
516
        }
517
      });
518
    });
519

  
520
    const $added_sub_widget = $('.wcs-block-add-clicked').find('.BlockSubWidget').last();
521
    if ($added_sub_widget.length) {
522
      $added_sub_widget[0].scrollIntoView({behavior: "instant", block: "center", inline: "nearest"});
523
    }
495 524
  }
496 525
});
497
-