Projet

Général

Profil

0001-js-prevent-autosave-while-user-is-active-58276.patch

Benjamin Dauvergne, 28 octobre 2021 23:46

Télécharger (2,56 ko)

Voir les différences:

Subject: [PATCH 1/4] js: prevent autosave while user is active (#58276)

 wcs/qommon/static/js/qommon.forms.js | 29 ++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)
wcs/qommon/static/js/qommon.forms.js
123 123

  
124 124
$(function() {
125 125
  var autosave_timeout_id = null;
126
  var autosave_is_running = false;
126 127
  if ($('form[data-has-draft]').length == 1) {
127 128
    var last_auto_save = $('form[data-has-draft]').serialize();
128 129
    function autosave() {
130
      autosave_is_running = true;
129 131
      var $form = $('form[data-has-draft]');
130 132
      if ($form.hasClass('disabled-during-submit')) return;
131 133
      var new_auto_save = $form.serialize();
132 134
      if (last_auto_save == new_auto_save) {
133
        autosave_timeout_id = window.setTimeout(autosave, 5000);
135
        install_autosave();
134 136
        return;
135 137
      }
136 138
      $.ajax({
......
143 145
          }
144 146
        },
145 147
        complete: function() {
146
          autosave_timeout_id = window.setTimeout(autosave, 5000);
148
          autosave_is_running = false;
149
          if (autosave_timeout_id !== null) {
150
              install_autosave();
151
          }
147 152
        }
148 153
      });
149 154
    }
150
    autosave_timeout_id = window.setTimeout(autosave, 5000);
155

  
156
    function install_autosave() {
157
       // debounce
158
       window.clearTimeout(autosave_timeout_id);
159
       autosave_timeout_id = window.setTimeout(autosave, 8000);
160
    }
161

  
162
    $(document).on('mouseover scroll keydown', function() {
163
        if (autosave_timeout_id !== null && ! autosave_is_running) {
164
            install_autosave();
165
        }
166
    });
167

  
168
    install_autosave();
169

  
151 170
    $('#tracking-code a').on('click', autosave);
152 171
    $(document).on('wcs:set-last-auto-save', function() {
153 172
      last_auto_save = $('form[data-has-draft]').serialize();
......
360 379
  });
361 380
  $('form').on('submit', function(event) {
362 381
    var $form = $(this);
363
    if (autosave_timeout_id) {
382
    /* prevent more autosave */
383
    if (autosave_timeout_id !== null) {
364 384
      window.clearTimeout(autosave_timeout_id);
385
      autosave_timeout_id = null;
365 386
    }
366 387
    $form.addClass('disabled-during-submit');
367 388
    if ($form.hasClass('download-button-clicked')) {
368
-