Projet

Général

Profil

0001-do-not-refresh-table-when-tab-is-invisible-or-user-i.patch

Benjamin Dauvergne, 04 mars 2019 09:40

Télécharger (2,96 ko)

Voir les différences:

Subject: [PATCH] do not refresh table when tab is invisible or user idle
 (#26804)

- refresh timer is cleared when tab becomes invisible (clear idle_id)
- refresh timer is cleared after 5 minutes of inactivity (clear idle_id,
  clear longidle_id)
- table is refreshed immediately when tab becomes visible and user has been inactive
  more than 5 minutes (! longidle_id)
 wcs/qommon/static/js/wcs.listing.js | 45 +++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
wcs/qommon/static/js/wcs.listing.js
1
/**
2
 * on_tab_visibility_change() -> bool : is tab visible ?
3
 * on_tab_visibility_change(aFunction) -> call aFunction on visibility change.
4
 */
5
var on_tab_visibility_change = (function(){
6
    var stateKey, eventKey, keys = {
7
        hidden: "visibilitychange",
8
        webkitHidden: "webkitvisibilitychange",
9
        mozHidden: "mozvisibilitychange",
10
        msHidden: "msvisibilitychange"
11
    };
12
    for (stateKey in keys) {
13
        if (stateKey in document) {
14
            eventKey = keys[stateKey];
15
            break;
16
        }
17
    }
18
    return function(c) {
19
        if (c) document.addEventListener(eventKey, c);
20
        return !document[stateKey];
21
    }
22
})();
23

  
1 24
function prepare_page_links() {
2 25
  $('#page-links a').click(function() {
3 26
    $('form#listing-settings input[name="offset"]').val($(this).data('offset'));
......
188 211
    return false;
189 212
  });
190 213

  
214
  /* refresh every 30 seconds (idle_id) after any user activity
215
   * on inactivity for more than 5 minutes (longidle_id), stop refreshing (clear idle_id)
216
   */
191 217
  if ($('#statistics').length == 0) {
192 218
    var idle_id = null;
219
    var longidle_id = null;
220
    on_tab_visibility_change(function () {
221
      if (! on_tab_visibility_change()) {
222
        /* stop refreshing in invisible tabs */
223
        if (idle_id) idle_id = window.clearInterval(idle_id);
224
      } else {
225
        /* if tab invisible for more than 5 minutes, refresh now */
226
        if (! longidle_id) autorefresh_table();
227
      }
228
    }
193 229
    $(window).on('mousemove keydown mousedown touchstart', function() {
230
      /* if refresh timer exists, clear it */
194 231
      if (idle_id) window.clearInterval(idle_id);
232
      /* if stop refreshing timer exists, clear it */
233
      if (longidle_id) window.clearTimeout(longidle_id);
234
      /* launch timer to refresh every 30 seconds */
195 235
      idle_id = setInterval(autorefresh_table, 30000);
236
      /* launch timer to stop refreshing after 5 minutes idle */
237
      longidle_id = setTimeout(function () {
238
          if (idle_id) idle_id = window.clearInterval(idle_id);
239
          longidle_id = undefined;
240
      }, 300 * 1000);
196 241
    });
197 242
  }
198 243

  
199
-