@osimatic/helpers-js 1.5.2 → 1.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/count_down.js CHANGED
@@ -1,7 +1,12 @@
1
1
  class CountDown {
2
2
 
3
- constructor(div, callbackOnRefreshData) {
4
- // console.log('constructor');
3
+ static init(div, options = {}) {
4
+ const {
5
+ onRefreshData,
6
+ labelNextUpdate = 'Prochaine mise à jour',
7
+ labelDoUpdate = 'Mettre à jour',
8
+ } = options;
9
+
5
10
  if (!div.length) {
6
11
  return;
7
12
  }
@@ -14,43 +19,63 @@ class CountDown {
14
19
  .append('<div class="count_down_link"><a href="#" data-loading-text="<i class=\'fa fa-circle-notch fa-spin\'></i>">'+labelDoUpdate+'</a></div>')
15
20
  ;
16
21
 
17
- this.div = div;
18
- this.callbackOnRefreshData = callbackOnRefreshData;
22
+ let alreadyMakingRequest = false;
23
+ let secondsBefRefresh = 10;
24
+ let refreshIntervalMillis = 60;
25
+ let currentMillis = 0;
26
+ let currentSecond = 0;
27
+
28
+ function refreshData() {
29
+ currentMillis = 0;
30
+
31
+ //Pour ne pas relancer une requête si la précédente n'est pas encore finie
32
+ if (true === alreadyMakingRequest) {
33
+ console.log('Already making request, no new request lauched.');
34
+ return;
35
+ }
19
36
 
20
- this.alreadyMakingRequest = false;
21
- this.secondsBefRefresh = 10;
22
- this.refreshIntervalMillis = 60;
23
- this.currentMillis = 0;
24
- this.currentSecond = 0;
37
+ if (typeof onRefreshData == 'function') {
38
+ alreadyMakingRequest = true;
39
+ div.find('.count_down_link a').attr('disabled', true).button('loading');
40
+
41
+ onRefreshData(
42
+ // completeCallback
43
+ () => {
44
+ alreadyMakingRequest = false;
45
+ div.find('.count_down_link a').attr('disabled', false).button('reset');
46
+ }
47
+ );
48
+ }
49
+ }
25
50
 
26
51
  if (div.find('.count_down_link a').length) {
27
52
  div.find('.count_down_link a').click(() => {
28
- this.refreshData();
53
+ refreshData();
29
54
  return false;
30
55
  });
31
56
  }
32
57
 
33
58
  setInterval(() => {
34
59
  if (!div.find('.count_down_link a').length || !div.find('.count_down_link a').prop('disabled')) {
35
- this.currentMillis += this.refreshIntervalMillis;
60
+ currentMillis += refreshIntervalMillis;
36
61
  }
37
62
  else {
38
- this.currentMillis = 0;
63
+ currentMillis = 0;
39
64
  }
40
65
 
41
- this.currentSecond = parseInt(this.currentMillis / 1000);
66
+ currentSecond = parseInt(currentMillis / 1000);
42
67
 
43
68
  //countDownRefresh();
44
69
  var divCountDownText;
45
70
  var divCountDownCurrentSizePx;
46
71
 
47
- if (this.currentSecond >= this.secondsBefRefresh) {
72
+ if (currentSecond >= secondsBefRefresh) {
48
73
  divCountDownCurrentSizePx = 120;
49
74
  divCountDownText = '0s';
50
75
  }
51
76
  else {
52
- divCountDownCurrentSizePx = Math.round((120/(this.secondsBefRefresh*1000)) * this.currentMillis);
53
- divCountDownText = (this.secondsBefRefresh-this.currentSecond) + 's';
77
+ divCountDownCurrentSizePx = Math.round((120/(secondsBefRefresh*1000)) * currentMillis);
78
+ divCountDownText = (secondsBefRefresh-currentSecond) + 's';
54
79
  }
55
80
 
56
81
  if (div.find('.count_down_current').length) {
@@ -60,42 +85,15 @@ class CountDown {
60
85
  div.find('.count_down_text').html(divCountDownText);
61
86
  }
62
87
 
63
- if (this.currentSecond >= this.secondsBefRefresh) {
64
- this.currentMillis = 0;
88
+ if (currentSecond >= secondsBefRefresh) {
89
+ currentMillis = 0;
65
90
  setTimeout(() => {
66
- this.refreshData();
91
+ refreshData();
67
92
  }, 100);
68
93
  }
69
- }, this.refreshIntervalMillis);
70
-
71
- this.refreshData();
72
- }
94
+ }, refreshIntervalMillis);
73
95
 
74
- setCallbackOnRefreshData(callback) {
75
- this.callbackOnRefreshData = callback;
76
- }
77
-
78
- refreshData() {
79
- this.currentMillis = 0;
80
-
81
- //Pour ne pas relancer une requête si la précédente n'est pas encore finie
82
- if (true === this.alreadyMakingRequest) {
83
- console.log('Already making request, no new request lauched.');
84
- return;
85
- }
86
-
87
- if (typeof this.callbackOnRefreshData == 'function') {
88
- CountDown.alreadyMakingRequest = true;
89
- this.div.find('.count_down_link a').attr('disabled', true).button('loading');
90
-
91
- this.callbackOnRefreshData(
92
- // completeCallback
93
- () => {
94
- this.alreadyMakingRequest = false;
95
- this.div.find('.count_down_link a').attr('disabled', false).button('reset');
96
- }
97
- );
98
- }
96
+ refreshData();
99
97
  }
100
98
 
101
99
  }
package/date_time.js CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  require('./string');
3
2
 
4
3
  class DateTimeFormatter {
@@ -1,121 +1,136 @@
1
+ const { HTTPClient } = require('./http_client');
2
+
1
3
  class DetailsSubArray {
2
4
 
3
- static initDetailsLink(table, callbackOnDetailsActionRequestSuccess, callbackOnDetailsActionRequestError, callbackOnDetailsActionRequestBeforeSend) {
5
+ static initDetailsLink(table, options = {}) {
6
+ const {
7
+ onSuccess,
8
+ onError,
9
+ onBeforeSend,
10
+ labelErrorOccurred = 'Une erreur s\'est produite.',
11
+ showDetailsLabel = 'Afficher les détails',
12
+ hideDetailsLabel = 'Masquer les détails',
13
+ } = options;
14
+
4
15
  function getNbColumns(tr) {
5
- return tr.closest('table').find('thead tr').children().length;
16
+ return tr.closest('table').querySelector('thead tr').children.length;
6
17
  }
7
18
  function displayErrorRow(tr) {
8
- tr.after($('<tr class="text-error"><td colspan="'+getNbColumns(tr)+'">'+(typeof labelErrorOccured != 'undefined' ? labelErrorOccured : 'Une erreur s’est produite.')+'</td></tr>'));
19
+ tr.insertAdjacentHTML('afterend', '<tr class="text-error"><td colspan="'+getNbColumns(tr)+'">'+labelErrorOccurred+'</td></tr>');
9
20
  }
10
21
  function displayDetailsRow(tr, content) {
11
- var trContent = $(''
12
- + '<tr class="participants">'
13
- + '<td colspan="'+getNbColumns(tr)+'">'
14
- + '</td>'
15
- + '</tr>'
16
- );
17
- trContent.find('td').append(content);
18
- tr.after(trContent);
22
+ const td = document.createElement('td');
23
+ td.setAttribute('colspan', getNbColumns(tr));
24
+ if (content instanceof Node) {
25
+ td.appendChild(content);
26
+ } else {
27
+ td.innerHTML = content || '';
28
+ }
29
+ const trContent = document.createElement('tr');
30
+ trContent.className = 'participants';
31
+ trContent.appendChild(td);
32
+ tr.insertAdjacentElement('afterend', trContent);
19
33
  }
20
34
 
21
35
  function hideDetailsRow(link) {
22
- var tr = link.closest('tr').addClass('folded');
23
- if (tr.next().hasClass('participants') ) {
24
- tr.next().remove();
36
+ const tr = link.closest('tr');
37
+ tr.classList.add('folded');
38
+ const next = tr.nextElementSibling;
39
+ if (next && next.classList.contains('participants')) {
40
+ next.remove();
25
41
  }
26
42
  showPlusButton(link);
27
43
  }
28
44
 
29
45
  function showPlusButton(link) {
30
- link.prop('title', showDetailsLabel).attr('disabled', false).html($('<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>'));
46
+ link.title = showDetailsLabel;
47
+ link.disabled = false;
48
+ link.innerHTML = '<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>';
31
49
  }
32
50
  function showMinusButton(link) {
33
- link.prop('title', hideDetailsLabel).attr('disabled', false).html($('<span class="glyphicon glyphicon-minus" aria-hidden="true"></span>'));
51
+ link.title = hideDetailsLabel;
52
+ link.disabled = false;
53
+ link.innerHTML = '<span class="glyphicon glyphicon-minus" aria-hidden="true"></span>';
34
54
  }
35
55
 
36
56
  function displayLoading(link) {
37
- link.attr('disabled', true).html($('<i class="fa fa-circle-notch fa-spin"></i>'));
38
- var tr = link.closest('tr');
39
- tr.after($('<tr class="waiting_icon"><td colspan="'+getNbColumns(tr)+'" class="center"><i class="fa fa-circle-notch fa-spin"></i></td></tr>'));
57
+ link.disabled = true;
58
+ link.innerHTML = '<i class="fa fa-circle-notch fa-spin"></i>';
59
+ const tr = link.closest('tr');
60
+ tr.insertAdjacentHTML('afterend', '<tr class="waiting_icon"><td colspan="'+getNbColumns(tr)+'" class="center"><i class="fa fa-circle-notch fa-spin"></i></td></tr>');
40
61
  }
41
- function hideLoading(link) {
62
+ function hideLoading() {
42
63
  // todo : cacher que le loader du lien au lieu de tous les loaders (cas ou l'user clique sur tous les boutons rapidement)
43
- if ($('tr.waiting_icon').length > 0) {
44
- $(('tr.waiting_icon')).remove();
45
- }
64
+ document.querySelectorAll('tr.waiting_icon').forEach(el => el.remove());
46
65
  }
47
66
 
48
67
  function setHideDetailsLink(link) {
49
68
  showMinusButton(link);
50
- link.click(function() {
51
- $(this).stop();
69
+ link.onclick = function(e) {
70
+ e.preventDefault();
52
71
  hideDetailsRow(link);
53
72
  setDisplayDetailsLink(link);
54
- return false;
55
- });
73
+ };
56
74
  }
57
75
 
58
76
  function setDisplayDetailsLink(link) {
59
- link.click(function() {
60
- $(this).stop().off('click');
77
+ link.onclick = function(e) {
78
+ e.preventDefault();
79
+ link.onclick = null;
61
80
  hideDetailsRow(link);
62
81
  doDetailsActionRequest(link);
63
- return false;
64
- });
82
+ };
65
83
  }
66
84
 
67
85
  function doDetailsActionRequest(link) {
68
86
  displayLoading(link);
69
87
 
70
- if (typeof callbackOnDetailsActionRequestBeforeSend != 'undefined' && callbackOnDetailsActionRequestBeforeSend != null) {
71
- displayDetailsRow(link.closest('tr'), callbackOnDetailsActionRequestBeforeSend(link));
72
- hideLoading(link);
88
+ if (onBeforeSend != null) {
89
+ displayDetailsRow(link.closest('tr'), onBeforeSend(link));
90
+ hideLoading();
73
91
  setHideDetailsLink(link);
74
92
  return;
75
93
  }
76
94
 
77
95
  function onComplete() {
78
- hideLoading(link);
96
+ hideLoading();
79
97
  setHideDetailsLink(link);
80
- //link.attr('disabled', false).button('reset');
81
98
  }
82
99
 
83
- //link.attr('disabled', true).button('loading');
84
- HTTPClient.request('GET', link.data('url_details'), null,
100
+ HTTPClient.request('GET', link.dataset.url_details, null,
85
101
  (jsonObj) => {
86
102
  if (jsonObj == null) {
87
- if (typeof callbackOnDetailsActionRequestError != 'undefined' && callbackOnDetailsActionRequestError != null) {
88
- callbackOnDetailsActionRequestError(link);
103
+ if (onError != null) {
104
+ onError(link);
89
105
  return;
90
106
  }
91
107
  displayErrorRow(link.closest('tr'));
92
108
  return;
93
109
  }
94
110
 
95
- if (typeof callbackOnDetailsActionRequestSuccess != 'undefined' && callbackOnDetailsActionRequestSuccess != null) {
96
- displayDetailsRow(link.closest('tr'), callbackOnDetailsActionRequestSuccess(jsonObj, link));
111
+ if (onSuccess != null) {
112
+ displayDetailsRow(link.closest('tr'), onSuccess(jsonObj, link));
97
113
  }
98
114
 
99
115
  onComplete();
100
116
  },
101
117
  () => {
102
- if (typeof callbackOnDetailsActionRequestError != 'undefined' && callbackOnDetailsActionRequestError != null) {
103
- callbackOnDetailsActionRequestError(link);
118
+ if (onError != null) {
119
+ onError(link);
104
120
  return;
105
121
  }
106
122
 
107
- link.closest('tr').after($('<tr class="error"><td colspan="6" class="center">'+(typeof labelErrorOccured != 'undefined' ? labelErrorOccured : 'Une erreur sest produite.')+'</td></tr>'));
123
+ link.closest('tr').insertAdjacentHTML('afterend', '<tr class="error"><td colspan="6" class="center">'+(labelErrorOccurred ?? 'Une erreur s\'est produite.')+'</td></tr>');
108
124
 
109
125
  onComplete();
110
- //window.location.replace(decodeURIComponent(urlRetour));
111
126
  }
112
127
  );
113
128
  }
114
129
 
115
- table.find('a.details_link').each(function(idx, link) {
116
- $(link).removeClass('hide');
117
- setDisplayDetailsLink($(link));
118
- showPlusButton($(link));
130
+ table.querySelectorAll('a.details_link').forEach((link) => {
131
+ link.classList.remove('hide');
132
+ setDisplayDetailsLink(link);
133
+ showPlusButton(link);
119
134
  });
120
135
  }
121
136
 
package/flash_message.js CHANGED
@@ -14,14 +14,18 @@ class FlashMessage {
14
14
  modal.modal('hide');
15
15
  }
16
16
 
17
- $('div.snackbar').remove();
18
- let snackbar = $('<div class="snackbar '+type+'" '+(null !== domId ? 'id="'+domId+'"' : '')+'></div>');
19
- $('html body').append(snackbar);
20
- snackbar.html(message);
21
- snackbar.addClass('show');
17
+ document.querySelectorAll('div.snackbar').forEach(el => el.remove());
18
+ const snackbar = document.createElement('div');
19
+ snackbar.className = 'snackbar ' + type;
20
+ if (null !== domId) {
21
+ snackbar.id = domId;
22
+ }
23
+ snackbar.innerHTML = message;
24
+ snackbar.classList.add('show');
25
+ document.querySelector('body').appendChild(snackbar);
22
26
 
23
27
  setTimeout(function () {
24
- $('div.snackbar').remove();
28
+ document.querySelectorAll('div.snackbar').forEach(el => el.remove());
25
29
  if (typeof onMessageHidden == 'function') {
26
30
  onMessageHidden();
27
31
  }