@osimatic/helpers-js 1.0.102 → 1.0.103

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/duration.js CHANGED
@@ -116,7 +116,7 @@ class Duration {
116
116
  // pas d'arrondissement
117
117
  }
118
118
  else {
119
- var halfRoundPrecision = roundPrecision / 2;
119
+ let halfRoundPrecision = roundPrecision / 2;
120
120
  hoursRounded = hours;
121
121
  secondsRounded = 0;
122
122
  if (roundMode === 'up' || (roundMode === 'close' && minutesRemainingAndSecondsAsCentieme > halfRoundPrecision)) {
@@ -149,14 +149,10 @@ class Duration {
149
149
  return Math.floor(durationInSeconds / 60);
150
150
  }
151
151
 
152
- /*
153
- static getNbMinutesRemaining(durationInSeconds) {
152
+ /*static getNbMinutesRemaining(durationInSeconds) {
154
153
  var remander = ( durationInSeconds % 3600 ) - ( durationInSeconds % 60 );
155
154
  return ( remander / 60 );
156
155
  }
157
- */
158
-
159
- /*
160
156
  static getNbHoursOfDurationInSeconds(durationInSeconds) {
161
157
  // return (this.getNbDaysOfDurationInSeconds(durationInSeconds)*24)+this.getNbHoursRemainingOfDurationInSeconds(durationInSeconds);
162
158
  return (durationInSeconds - (durationInSeconds % 3600)) / 3600;
@@ -164,8 +160,7 @@ class Duration {
164
160
  static getNbMinutesOfDurationInSeconds(durationInSeconds) {
165
161
  // return (this.getNbDaysOfDurationInSeconds(durationInSeconds)*24*60)+(this.getNbHoursRemainingOfDurationInSeconds(durationInSeconds)*60)+this.getNbMinutesRemainingOfDurationInSeconds(durationInSeconds);
166
162
  return (durationInSeconds - (durationInSeconds % 60)) / 60;
167
- }
168
- */
163
+ }*/
169
164
 
170
165
  static getNbHoursRemainingOfDurationInSeconds(durationInSeconds) {
171
166
  return this.getNbHoursOfDurationInSeconds(durationInSeconds % 86400);
package/flash_message.js CHANGED
@@ -2,7 +2,9 @@ class FlashMessage {
2
2
  static displaySuccess(message, reload, modal) {
3
3
  this.display('success', message, reload, modal);
4
4
  }
5
-
5
+ static displayWarning(message, modal) {
6
+ this.display('warning', message, false, modal);
7
+ }
6
8
  static displayError(message, modal) {
7
9
  this.display('danger', message, false, modal);
8
10
  }
@@ -14,7 +16,7 @@ class FlashMessage {
14
16
  if ($('div.snackbar').length !== 0) {
15
17
  $('div.snackbar').remove();
16
18
  }
17
- var snackbar = $('<div class="snackbar '+type+'"></div>');
19
+ let snackbar = $('<div class="snackbar '+type+'"></div>');
18
20
  $('html body').append(snackbar);
19
21
  snackbar.html(message);
20
22
  snackbar.addClass('show');
@@ -27,12 +29,6 @@ class FlashMessage {
27
29
  document.location.reload();
28
30
  }
29
31
  }
30
-
31
- /** @deprecated **/
32
- static displayRequestFailure(status, exception, modal) {
33
- console.log('request failure. Status: ', status, ' Exception: ', exception);
34
- this.display('danger', typeof labelErrorOccured != 'undefined' ? labelErrorOccured : 'Une erreur s’est produite.', false, modal);
35
- }
36
32
  }
37
33
 
38
34
  module.exports = { FlashMessage };
package/http_client.js CHANGED
@@ -1,23 +1,33 @@
1
1
 
2
2
  class HTTPClient {
3
+ // URL appelée pour rafraichir le token. Le token est mis à jour dans JwtSession. En cas de succès, on appelle HTTPClient.onSuccessRefreshTokenCallback et on réexécute toutes les requêtes HTTP en attente de nouveau token. En cas d'échec, on redirige vers HTTPClient.onInvalidRefreshTokenRedirectUrl et/ou on appelle HTTPClient.onInvalidRefreshTokenCallback.
3
4
  static setRefreshTokenUrl(url) {
4
5
  HTTPClient.refreshTokenUrl = url;
5
6
  }
7
+ // Callback appelé pour remplacer l'appel à HTTPClient.refreshTokenUrl (et donc pas de gestion de la session avec JwtSession)
6
8
  static setRefreshTokenCallback(callback) {
7
9
  HTTPClient.refreshTokenCallback = callback;
8
10
  }
9
11
 
12
+ static setOnSuccessRefreshTokenCallback(callback) {
13
+ HTTPClient.onSuccessRefreshTokenCallback = callback;
14
+ }
15
+ static setOnInvalidRefreshTokenCallback(callback) {
16
+ HTTPClient.onInvalidRefreshTokenCallback = callback;
17
+ }
18
+ static setOnInvalidRefreshTokenRedirectUrl(url) {
19
+ HTTPClient.onInvalidRefreshTokenRedirectUrl = url;
20
+ }
21
+
22
+ // URL de la page vers laquelle on redirige si le token est invalide apres destruction de la JwtSession.
10
23
  static setOnInvalidTokenRedirectUrl(url) {
11
24
  HTTPClient.onInvalidTokenRedirectUrl = url;
12
25
  }
26
+ // Callback appelé lorsqu'un token est invalide (destruction de la JwtSession dans tous les cas).
13
27
  static setOnInvalidTokenCallback(callback) {
14
28
  HTTPClient.onInvalidTokenCallback = callback;
15
29
  }
16
30
 
17
- static setOnInvalidRefreshTokenRedirectUrl(url) {
18
- HTTPClient.onInvalidRefreshTokenRedirectUrl = url;
19
- }
20
-
21
31
  static getHeaders(asObject) {
22
32
  HTTPClient.setAuthorizationToken(JwtSession.getToken());
23
33
 
@@ -160,12 +170,7 @@ class HTTPClient {
160
170
  }
161
171
 
162
172
  static onInvalidToken() {
163
- if (typeof HTTPClient.onInvalidTokenCallback == 'function') {
164
- HTTPClient.onInvalidTokenCallback();
165
- return;
166
- }
167
-
168
- JwtSession.logout(HTTPClient.onInvalidTokenRedirectUrl);
173
+ JwtSession.logout(HTTPClient.onInvalidTokenRedirectUrl, HTTPClient.onInvalidTokenCallback);
169
174
  }
170
175
 
171
176
  static async request(method, url, data, successCallback, errorCallback, formErrorCallback) {
@@ -333,12 +338,12 @@ class HTTPClient {
333
338
 
334
339
  HTTPClient.request('POST', HTTPClient.refreshTokenUrl, payload,
335
340
  (data) => {
336
- JwtSession.updateToken(data['token'], data['refresh_token']);
341
+ JwtSession.updateToken(data['token'], data['refresh_token'], HTTPClient.onSuccessRefreshTokenCallback);
337
342
  HTTPClient.setAuthorizationToken(JwtSession.getToken());
338
343
  onRefreshTokenComplete();
339
344
  },
340
345
  () => {
341
- JwtSession.logout(HTTPClient.onInvalidRefreshTokenRedirectUrl);
346
+ JwtSession.expireSession(HTTPClient.onInvalidRefreshTokenRedirectUrl, HTTPClient.onInvalidRefreshTokenCallback);
342
347
  errorCallback();
343
348
  }
344
349
  );
package/jwt.js CHANGED
@@ -32,15 +32,18 @@ class JwtToken {
32
32
  }
33
33
 
34
34
  class JwtSession {
35
- static setOnNewTokenCallback(callback) {
36
- JwtSession.onNewTokenCallback = callback;
37
- }
38
35
  static setOnLoginCallback(callback) {
39
36
  JwtSession.onLoginCallback = callback;
40
37
  }
41
38
  static setOnLogoutCallback(callback) {
42
39
  JwtSession.onLogoutCallback = callback;
43
40
  }
41
+ static setOnNewTokenCallback(callback) {
42
+ JwtSession.onNewTokenCallback = callback;
43
+ }
44
+ static setOnSessionExpireCallback(callback) {
45
+ JwtSession.onSessionExpireCallback = callback;
46
+ }
44
47
 
45
48
  static getToken() {
46
49
  return localStorage.getItem('access_token');
@@ -56,7 +59,7 @@ class JwtSession {
56
59
  localStorage.setItem('refresh_token', token);
57
60
  }
58
61
 
59
- static login(data, redirectUrl) {
62
+ static login(data, redirectUrl, onComplete) {
60
63
  console.log('JwtSession.login()');
61
64
  JwtSession.setToken(data['access_token'] || data['token']);
62
65
  JwtSession.setRefreshToken(data['refresh_token']);
@@ -66,13 +69,16 @@ class JwtSession {
66
69
  if (typeof JwtSession.onLoginCallback == 'function') {
67
70
  JwtSession.onLoginCallback();
68
71
  }
72
+ if (typeof onComplete == 'function') {
73
+ onComplete();
74
+ }
69
75
 
70
76
  if (typeof redirectUrl != 'undefined' && null != redirectUrl) {
71
77
  window.location.href = redirectUrl;
72
78
  }
73
79
  }
74
80
 
75
- static updateToken(accessToken, refreshToken) {
81
+ static updateToken(accessToken, refreshToken, onComplete) {
76
82
  console.log('JwtSession.updateToken()');
77
83
  JwtSession.setToken(accessToken);
78
84
 
@@ -83,9 +89,12 @@ class JwtSession {
83
89
  if (typeof JwtSession.onNewTokenCallback == 'function') {
84
90
  JwtSession.onNewTokenCallback();
85
91
  }
92
+ if (typeof onComplete == 'function') {
93
+ onComplete();
94
+ }
86
95
  }
87
96
 
88
- static logout(redirectUrl) {
97
+ static logout(redirectUrl, onComplete) {
89
98
  console.log('JwtSession.logout()');
90
99
  localStorage.removeItem('access_token');
91
100
  localStorage.removeItem('refresh_token');
@@ -95,6 +104,28 @@ class JwtSession {
95
104
  if (typeof JwtSession.onLogoutCallback == 'function') {
96
105
  JwtSession.onLogoutCallback();
97
106
  }
107
+ if (typeof onComplete == 'function') {
108
+ onComplete();
109
+ }
110
+
111
+ if (typeof redirectUrl != 'undefined' && null != redirectUrl) {
112
+ window.location.href = redirectUrl;
113
+ }
114
+ }
115
+
116
+ static expireSession(redirectUrl, onComplete) {
117
+ console.log('JwtSession.expireSession()');
118
+ localStorage.removeItem('access_token');
119
+ localStorage.removeItem('refresh_token');
120
+
121
+ localStorage.removeItem('real_users');
122
+
123
+ if (typeof JwtSession.onSessionExpireCallback == 'function') {
124
+ JwtSession.onSessionExpireCallback();
125
+ }
126
+ if (typeof onComplete == 'function') {
127
+ onComplete();
128
+ }
98
129
 
99
130
  if (typeof redirectUrl != 'undefined' && null != redirectUrl) {
100
131
  window.location.href = redirectUrl;
@@ -134,7 +165,7 @@ class JwtSession {
134
165
  return realUsers;
135
166
  }
136
167
 
137
- static simulateLogin(loginData, onSuccess) {
168
+ static simulateLogin(loginData, redirectUrl, onComplete) {
138
169
  console.log('JwtSession.simulateLogin');
139
170
 
140
171
  // on sauvegarde les tokens de l'utilisateur réellement connecté
@@ -149,27 +180,37 @@ class JwtSession {
149
180
  JwtSession.setToken(loginData['access_token'] || loginData['token']);
150
181
  JwtSession.setRefreshToken(loginData['refresh_token']);
151
182
 
152
- if (typeof onSuccess == 'function') {
153
- onSuccess();
183
+ if (typeof onComplete == 'function') {
184
+ onComplete();
185
+ }
186
+
187
+ if (typeof redirectUrl != 'undefined' && null != redirectUrl) {
188
+ window.location.href = redirectUrl;
154
189
  }
155
190
  }
156
191
 
157
- static cancelSimulatedLogin(onSuccess) {
192
+ static cancelSimulatedLogin(redirectUrl, onComplete) {
158
193
  console.log('JwtSession.cancelSimulatedLogin');
159
194
 
160
195
  // on récupère les tokens de l'utilisateur réellement connecté
161
196
  let realUsers = JwtSession.getRealLoggedUsers();
162
197
  let loginData = realUsers.pop();
163
198
 
164
- if (typeof loginData != 'undefined' && null != loginData) {
165
- localStorage.setItem('real_users', JSON.stringify(realUsers));
199
+ if (typeof loginData == 'undefined' || null == loginData) {
200
+ return;
201
+ }
202
+
203
+ localStorage.setItem('real_users', JSON.stringify(realUsers));
166
204
 
167
- JwtSession.setToken(loginData['access_token'] || loginData['token']);
168
- JwtSession.setRefreshToken(loginData['refresh_token']);
205
+ JwtSession.setToken(loginData['access_token'] || loginData['token']);
206
+ JwtSession.setRefreshToken(loginData['refresh_token']);
169
207
 
170
- if (typeof onSuccess == 'function') {
171
- onSuccess();
172
- }
208
+ if (typeof onComplete == 'function') {
209
+ onComplete();
210
+ }
211
+
212
+ if (typeof redirectUrl != 'undefined' && null != redirectUrl) {
213
+ window.location.href = redirectUrl;
173
214
  }
174
215
  }
175
216
 
package/number.js CHANGED
@@ -22,7 +22,7 @@ if (!Number.format) {
22
22
  * @param integer x: length of sections
23
23
  */
24
24
  Number.prototype.formatForDisplay = Number.prototype.formatForDisplay || function(n, s, c, x) {
25
- var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')',
25
+ let re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')',
26
26
  num = this.toFixed(Math.max(0, ~~n));
27
27
  return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ','));
28
28
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.0.102",
3
+ "version": "1.0.103",
4
4
  "main": "main.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
package/string.js CHANGED
@@ -4,7 +4,7 @@ String.prototype.copyToClipboard = String.prototype.copyToClipboard || function(
4
4
  window.clipboardData.setData('Text', this);
5
5
  }
6
6
  else if (document.body.createTextRange) {
7
- var textRange = document.body.createTextRange();
7
+ let textRange = document.body.createTextRange();
8
8
  textRange.moveToElementText(this);
9
9
  textRange.execCommand("Copy");
10
10
  }
@@ -18,7 +18,7 @@ String.prototype.copyToClipboard = String.prototype.copyToClipboard || function(
18
18
  alert("Impossible d'accéder au presse-papier.");
19
19
  }
20
20
  // Initialisation du composant fournit par Mozilla.
21
- var gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
21
+ let gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
22
22
  // Copie du texte dans le presse papier.
23
23
  gClipboardHelper.copyString(this);
24
24
  }
@@ -33,14 +33,14 @@ String.prototype.truncateOnWord = String.prototype.truncateOnWord || function(li
33
33
  if (fromLeft) {
34
34
  return this.reverseString(this.truncateOnWord(this.reverseString(), limit));
35
35
  }
36
- var TRIM_CHARS = '\u0009\u000A\u000B\u000C\u000D\u0020\u00A0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u2028\u2029\u3000\uFEFF';
37
- var words = this.split(RegExp('(?=['+TRIM_CHARS+'])'));
38
- var count = 0;
36
+ let TRIM_CHARS = '\u0009\u000A\u000B\u000C\u000D\u0020\u00A0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u2028\u2029\u3000\uFEFF';
37
+ let words = this.split(RegExp('(?=['+TRIM_CHARS+'])'));
38
+ let count = 0;
39
39
 
40
40
  function filter(arr, fn) {
41
- var result = [];
42
- for (var i = 0, len = arr.length; i < len; i++) {
43
- var el = arr[i];
41
+ let result = [];
42
+ for (let i = 0, len = arr.length; i < len; i++) {
43
+ let el = arr[i];
44
44
  if (i in arr && fn(el, i)) {
45
45
  result.push(el);
46
46
  }
@@ -105,7 +105,7 @@ String.prototype.escapeRegExp = String.prototype.escapeRegExp || function() {
105
105
  return this.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
106
106
  };
107
107
  String.prototype.format = String.prototype.format || function() {
108
- var args = arguments;
108
+ let args = arguments;
109
109
  return this.replace(/{(\d+)}/g, (match, number) => (typeof args[number] != 'undefined' ? args[number] : match));
110
110
  };
111
111
 
@@ -136,7 +136,7 @@ String.prototype.isNumeric = String.prototype.isNumeric || function() {
136
136
  // s'utilise : "ma chaine {0} de caracteres"
137
137
  if (!String.format) {
138
138
  String.format = function(format) {
139
- var args = Array.prototype.slice.call(arguments, 1);
139
+ let args = Array.prototype.slice.call(arguments, 1);
140
140
  return format.replace(/{(\d+)}/g, (match, number) => (typeof args[number] != 'undefined' ? args[number] : match));
141
141
  };
142
142
  }