aboutsummaryrefslogtreecommitdiffhomepage
path: root/app/Resources/static/themes/_global/js/tools.js
blob: 6386688ba58a32942270cffacb579de363e37cc9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import $ from 'jquery';
import './shortcuts/main';
import './shortcuts/entry';

/* Allows inline call qr-code call */
import jrQrcode from 'jr-qrcode'; // eslint-disable-line

function savePercent(id, percent) {
  fetch(`/progress/${id}/${percent * 100}`, { credentials: 'include' });
}

function retrievePercent() {
  const percent = $('#article').attr('data-progress');
  console.log(percent);
  const scroll = $(document).height() * percent;

  $('html,body').animate({ scrollTop: scroll }, 'fast');
}

function initFilters() {
  // no display if filters not available
  if ($('div').is('#filters')) {
    $('#button_filters').show();
    $('.js-filters-action').sideNav({ edge: 'right' });
    $('#clear_form_filters').on('click', () => {
      $('#filters input').val('');
      $('#filters :checked').removeAttr('checked');
      return false;
    });
  }
}

function initExport() {
  // no display if export not available
  if ($('div').is('#export')) {
    $('#button_export').show();
    $('.js-export-action').sideNav({ edge: 'right' });
  }
}

function throttle(callback, delay) {
  let last;
  let timer;
  return function () {
    const context = this;
    const now = new Date();
    const args = arguments;
    if (last && now < last + delay) {
      // le délai n'est pas écoulé on reset le timer
      clearTimeout(timer);
      timer = setTimeout(function () {
        last = now;
        callback.apply(context, args);
      }, delay);
    } else {
      last = now;
      callback.apply(context, args);
    }
  };
}

export { savePercent, retrievePercent, initFilters, initExport, throttle };