@osimatic/helpers-js 1.0.710 → 1.1.0

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/jwt.js CHANGED
@@ -1,81 +1,139 @@
1
1
  class JwtToken {
2
2
  static parseJwt (token) {
3
- var base64Url = token.split('.')[1];
4
- var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
5
- var jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) {
3
+ let base64Url = token.split('.')[1];
4
+ let base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
5
+ let jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) {
6
6
  return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
7
7
  }).join(''));
8
8
 
9
9
  return JSON.parse(jsonPayload);
10
10
  }
11
11
 
12
+ static getData(token, key) {
13
+ if (token == null) {
14
+ return null;
15
+ }
16
+
17
+ let payload = JwtToken.parseJwt(token);
18
+ if (typeof payload[key] != 'undefined') {
19
+ return payload[key];
20
+ }
21
+ return null;
22
+ }
23
+
12
24
  static hasRole(token, role) {
13
25
  if (token == null) {
14
26
  return false;
15
27
  }
16
28
 
17
29
  let payload = JwtToken.parseJwt(token);
18
- return payload.roles.indexOf(role) !== -1;
30
+ return typeof payload['roles'] != 'undefined' && payload['roles'].indexOf(role) !== -1;
19
31
  }
20
32
  }
21
33
 
22
34
  class JwtSession {
23
- static denyAccessUnlessGranted(roles) {
24
- let hasRole = false;
25
-
26
- roles.forEach(role => {
27
- if (JwtSession.isGranted(role)) {
28
- hasRole = true;
29
- }
30
- });
31
-
32
- return hasRole;
35
+ static setOnLoginCallback(callback) {
36
+ JwtSession.onLoginCallback = callback;
33
37
  }
34
-
35
- static setToken(token) {
36
- localStorage.setItem('access_token', token);
38
+ static setOnLogoutCallback(callback) {
39
+ JwtSession.onLogoutCallback = callback;
40
+ }
41
+ static setOnNewTokenCallback(callback) {
42
+ JwtSession.onNewTokenCallback = callback;
43
+ }
44
+ static setOnSessionExpireCallback(callback) {
45
+ JwtSession.onSessionExpireCallback = callback;
37
46
  }
38
47
 
39
48
  static getToken() {
40
49
  return localStorage.getItem('access_token');
41
50
  }
42
-
43
- static setRefreshToken(token) {
44
- localStorage.setItem('refresh_token', token);
51
+ static setToken(token) {
52
+ localStorage.setItem('access_token', token);
45
53
  }
46
54
 
47
55
  static getRefreshToken() {
48
56
  return localStorage.getItem('refresh_token');
49
57
  }
58
+ static setRefreshToken(token) {
59
+ localStorage.setItem('refresh_token', token);
60
+ }
61
+
62
+ static login(data, redirectUrl, onComplete) {
63
+ console.log('JwtSession.login()');
64
+ JwtSession.setToken(data['access_token'] || data['token']);
65
+ JwtSession.setRefreshToken(data['refresh_token']);
66
+
67
+ localStorage.removeItem('real_users');
68
+
69
+ if (typeof JwtSession.onLoginCallback == 'function') {
70
+ JwtSession.onLoginCallback();
71
+ }
72
+ if (typeof onComplete == 'function') {
73
+ onComplete();
74
+ }
50
75
 
51
- static isSimulationConnexion() {
52
- return localStorage.getItem('admin_refresh_token') != null && localStorage.getItem('admin_access_token') != null;
76
+ if (typeof redirectUrl != 'undefined' && null != redirectUrl) {
77
+ window.location.href = redirectUrl;
78
+ }
53
79
  }
54
80
 
55
- static cancelSimulationConnexion() {
56
- localStorage.setItem('refresh_token', localStorage.getItem('admin_refresh_token'));
57
- localStorage.setItem('access_token', localStorage.getItem('admin_access_token'));
81
+ static updateToken(accessToken, refreshToken, onComplete) {
82
+ console.log('JwtSession.updateToken()');
83
+ JwtSession.setToken(accessToken);
58
84
 
59
- localStorage.removeItem('admin_refresh_token');
60
- localStorage.removeItem('admin_access_token');
85
+ if (typeof refreshToken != 'undefined' && null != refreshToken) {
86
+ JwtSession.setRefreshToken(refreshToken);
87
+ }
88
+
89
+ if (typeof JwtSession.onNewTokenCallback == 'function') {
90
+ JwtSession.onNewTokenCallback();
91
+ }
92
+ if (typeof onComplete == 'function') {
93
+ onComplete();
94
+ }
61
95
  }
62
96
 
63
- static logout() {
97
+ static logout(redirectUrl, onComplete) {
98
+ console.log('JwtSession.logout()');
64
99
  localStorage.removeItem('access_token');
65
100
  localStorage.removeItem('refresh_token');
101
+
102
+ localStorage.removeItem('real_users');
103
+
104
+ if (typeof JwtSession.onLogoutCallback == 'function') {
105
+ JwtSession.onLogoutCallback();
106
+ }
107
+ if (typeof onComplete == 'function') {
108
+ onComplete();
109
+ }
110
+
111
+ if (typeof redirectUrl != 'undefined' && null != redirectUrl) {
112
+ window.location.href = redirectUrl;
113
+ }
66
114
  }
67
115
 
68
- static getData(key) {
69
- let token = JwtSession.getToken();
70
- if (token == null) {
71
- return null;
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();
72
128
  }
73
129
 
74
- let payload = JwtToken.parseJwt(token);
75
- if (typeof payload[key] != 'undefined') {
76
- return payload[key];
130
+ if (typeof redirectUrl != 'undefined' && null != redirectUrl) {
131
+ window.location.href = redirectUrl;
77
132
  }
78
- return null;
133
+ }
134
+
135
+ static getData(key) {
136
+ return JwtToken.getData(JwtSession.getToken(), key);
79
137
  }
80
138
 
81
139
  static isAnonymous() {
@@ -83,22 +141,14 @@ class JwtSession {
83
141
  }
84
142
 
85
143
  static isGranted(role) {
86
- let token = localStorage.getItem('access_token');
87
- if (token == null) {
88
- return false;
89
- }
90
-
91
- let payload = JwtToken.parseJwt(token);
92
- return payload.roles.indexOf(role) !== -1;
144
+ return JwtToken.hasRole(JwtSession.getToken(), role);
93
145
  }
94
- }
95
146
 
96
- class ApiTokenSession {
97
147
  static denyAccessUnlessGranted(roles) {
98
148
  let hasRole = false;
99
149
 
100
150
  roles.forEach(role => {
101
- if (ApiTokenSession.isGranted(role)) {
151
+ if (JwtSession.isGranted(role)) {
102
152
  hasRole = true;
103
153
  }
104
154
  });
@@ -106,6 +156,67 @@ class ApiTokenSession {
106
156
  return hasRole;
107
157
  }
108
158
 
159
+
160
+ static getRealLoggedUsers() {
161
+ let realUsers = [];
162
+ if (localStorage.getItem('real_users') != null) {
163
+ realUsers = JSON.parse(localStorage.getItem('real_users'));
164
+ }
165
+ return realUsers;
166
+ }
167
+
168
+ static simulateLogin(loginData, redirectUrl, onComplete) {
169
+ console.log('JwtSession.simulateLogin');
170
+
171
+ // on sauvegarde les tokens de l'utilisateur réellement connecté
172
+ let realUsers = JwtSession.getRealLoggedUsers();
173
+ realUsers.push({
174
+ access_token: JwtSession.getToken(),
175
+ refresh_token: JwtSession.getRefreshToken(),
176
+ });
177
+ localStorage.setItem('real_users', JSON.stringify(realUsers));
178
+
179
+ // on enregistre la session de l'utilisateur simulé
180
+ JwtSession.setToken(loginData['access_token'] || loginData['token']);
181
+ JwtSession.setRefreshToken(loginData['refresh_token']);
182
+
183
+ if (typeof onComplete == 'function') {
184
+ onComplete();
185
+ }
186
+
187
+ if (typeof redirectUrl != 'undefined' && null != redirectUrl) {
188
+ window.location.href = redirectUrl;
189
+ }
190
+ }
191
+
192
+ static cancelSimulatedLogin(redirectUrl, onComplete) {
193
+ console.log('JwtSession.cancelSimulatedLogin');
194
+
195
+ // on récupère les tokens de l'utilisateur réellement connecté
196
+ let realUsers = JwtSession.getRealLoggedUsers();
197
+ let loginData = realUsers.pop();
198
+
199
+ if (typeof loginData == 'undefined' || null == loginData) {
200
+ return;
201
+ }
202
+
203
+ localStorage.setItem('real_users', JSON.stringify(realUsers));
204
+
205
+ JwtSession.setToken(loginData['access_token'] || loginData['token']);
206
+ JwtSession.setRefreshToken(loginData['refresh_token']);
207
+
208
+ if (typeof onComplete == 'function') {
209
+ onComplete();
210
+ }
211
+
212
+ if (typeof redirectUrl != 'undefined' && null != redirectUrl) {
213
+ window.location.href = redirectUrl;
214
+ }
215
+ }
216
+
217
+ }
218
+
219
+ class ApiTokenSession {
109
220
  static getToken() {
110
221
  return localStorage.getItem('api_token');
111
222
  }
@@ -161,6 +272,18 @@ class ApiTokenSession {
161
272
 
162
273
  return roles.indexOf(role) !== -1;
163
274
  }
275
+
276
+ static denyAccessUnlessGranted(roles) {
277
+ let hasRole = false;
278
+
279
+ roles.forEach(role => {
280
+ if (ApiTokenSession.isGranted(role)) {
281
+ hasRole = true;
282
+ }
283
+ });
284
+
285
+ return hasRole;
286
+ }
164
287
  }
165
288
 
166
289
  module.exports = { JwtToken, JwtSession, ApiTokenSession };
package/location.js CHANGED
@@ -288,11 +288,32 @@ class Country {
288
288
  }
289
289
 
290
290
  class PostalAddress {
291
+ static setAutocomplete(input, onPlaceChanged) {
292
+ let autocomplete = new google.maps.places.Autocomplete(
293
+ input[0],
294
+ {types: ['geocode']}
295
+ );
296
+ //console.log(autocomplete);
297
+
298
+ // When the user selects an address from the dropdown, populate the address fields in the form.
299
+ autocomplete.addListener('place_changed', function() {
300
+ let place = autocomplete.getPlace();
301
+ input.val('');
302
+ if (typeof onPlaceChanged == 'function') {
303
+ onPlaceChanged(place['formatted_address']);
304
+ }
305
+ });
306
+ }
307
+
291
308
  static format(addressData, separator) {
292
309
  if (typeof separator == 'undefined') {
293
310
  separator = '<br/>';
294
311
  }
295
312
 
313
+ function empty(value) {
314
+ return typeof value == 'undefined' || value == null || value === '';
315
+ }
316
+
296
317
  /*
297
318
  var address = new Address({
298
319
  country: "USA",
@@ -307,23 +328,25 @@ class PostalAddress {
307
328
  console.log(formatted);
308
329
  */
309
330
 
310
- var addressDataForPluging = {
311
- streetAddress: addressData.streetAddress+(addressData.additionalAddress!=null&&addressData.additionalAddress!==''?"\n"+addressData.additionalAddress:''),
312
- postalCode: addressData.postalCode,
313
- locality: addressData.locality,
314
- region: addressData.state,
315
- countryCode: addressData.countryCode,
316
- country: Country.getCountryName(addressData.countryCode),
331
+ addressData['countryCode'] = !empty(addressData['countryCode'])?addressData['countryCode']:null;
332
+
333
+ let addressDataForPluging = {
334
+ streetAddress: (!empty(addressData['streetAddress'])?addressData['streetAddress']:'')+(!empty(addressData['additionalAddress'])?"\n"+addressData['additionalAddress']:''),
335
+ postalCode: !empty(addressData['postalCode'])?addressData['postalCode']:null,
336
+ locality: !empty(addressData['locality'])?addressData['locality']:null,
337
+ region: !empty(addressData['state'])?addressData['state']:null,
338
+ countryCode: addressData['countryCode'],
339
+ country: Country.getCountryName(addressData['countryCode']),
317
340
  };
318
- if (addressDataForPluging.locality == null) {
319
- addressDataForPluging.locality = addressData.suburb;
341
+ if (addressDataForPluging['locality'] == null && !empty(addressData['suburb'])) {
342
+ addressDataForPluging['locality'] = addressData['suburb'];
320
343
  }
321
- if (addressDataForPluging.locality == null) {
322
- addressDataForPluging.locality = addressData.stateDistrict;
344
+ if (addressDataForPluging['locality'] == null && !empty(addressData['stateDistrict'])) {
345
+ addressDataForPluging['locality'] = addressData['stateDistrict'];
323
346
  }
324
347
 
325
- var af = new AddressFmt();
326
- var formattedAddress = af.format(new Address(addressDataForPluging));
348
+ let af = new AddressFmt();
349
+ let formattedAddress = af.format(new Address(addressDataForPluging));
327
350
  return formattedAddress.replace(/\n+/g, separator);
328
351
  }
329
352
 
package/main.js ADDED
File without changes
package/media.js CHANGED
@@ -15,14 +15,13 @@ class AudioMedia {
15
15
  });
16
16
 
17
17
  div.find('.play_asynchronously_link').off('click').click(function () {
18
- if ($(this).buttonLoader('loading') != null) {
19
- let button = $(this).buttonLoader('loading');
20
- AudioMedia.playAudioUrl($(this).data('url'), () => button.buttonLoader('reset'));
21
- } else {
22
- let button = $(this).attr('disabled', true).button('loading');
23
- AudioMedia.playAudioUrl($(this).data('url'), () => button.attr('disabled', false).button('reset'));
24
- }
25
-
18
+ //if (FormHelper.buttonLoader($(this), 'loading') != null) {
19
+ let button = FormHelper.buttonLoader($(this), 'loading');
20
+ AudioMedia.playAudioUrl($(this).data('url'), () => FormHelper.buttonLoader(button, 'reset'));
21
+ //} else {
22
+ // let button = $(this).attr('disabled', true).button('loading');
23
+ // AudioMedia.playAudioUrl($(this).data('url'), () => button.attr('disabled', false).button('reset'));
24
+ //}
26
25
  return false;
27
26
  });
28
27
 
@@ -46,7 +45,7 @@ class AudioMedia {
46
45
  let context = new (window.AudioContext || window.webkitAudioContext)();
47
46
  let request = new XMLHttpRequest();
48
47
  request.open("GET", url, true);
49
- Object.entries(HTTPRequest.getHeaders(true)).forEach(([key, value]) => request.setRequestHeader(key, value));
48
+ Object.entries(HTTPClient.getHeaders(true)).forEach(([key, value]) => request.setRequestHeader(key, value));
50
49
  request.responseType = "arraybuffer";
51
50
 
52
51
  request.onload = function () {
@@ -6,7 +6,7 @@ class MultipleActionInTable {
6
6
  return;
7
7
  }
8
8
 
9
- var divBtn = MultipleActionInTable.getDivBtn(table);
9
+ let divBtn = MultipleActionInTable.getDivBtn(table);
10
10
  if (divBtn == null) {
11
11
  return;
12
12
  }
@@ -26,23 +26,19 @@ class MultipleActionInTable {
26
26
  }
27
27
  });
28
28
 
29
- table.find('input.action_multiple_checkbox').each(function(idx, el) {
30
- var th = $(el).closest('table').find('thead tr th').first();
31
- if (th.find('input').length === 0) {
32
- // console.log(th);
33
- th.html('<input type="checkbox" class="action_multiple_check_all" />');
34
- // th.html('Coucou');
35
- }
36
- });
29
+ let firstTh = table.find('thead tr th').first();
30
+ if (firstTh.find('input').length === 0) {
31
+ firstTh.html('<input type="checkbox" class="action_multiple_check_all" />');
32
+ }
37
33
 
38
34
  table.find('input.action_multiple_checkbox').change(function() {
39
35
  MultipleActionInTable.updateCheckbox(table);
40
36
  });
41
37
 
42
38
  table.find('input.action_multiple_check_all').off('click').click(function() {
43
- var table = $(this).closest('table');
44
- var checkbox = table.find('input.action_multiple_checkbox');
45
- var checkboxChecked = table.find('input.action_multiple_checkbox:checked');
39
+ let table = $(this).closest('table');
40
+ let checkbox = table.find('input.action_multiple_checkbox');
41
+ let checkboxChecked = table.find('input.action_multiple_checkbox:checked');
46
42
  if (checkbox.length === checkboxChecked.length) {
47
43
  checkbox.prop('checked', false);
48
44
  }
@@ -58,9 +54,9 @@ class MultipleActionInTable {
58
54
  static updateCheckbox(table) {
59
55
  MultipleActionInTable.showButtonsAction(table);
60
56
 
61
- var allCheckbox = table.find('input.action_multiple_checkbox');
62
- var allCheckboxChecked = table.find('input.action_multiple_checkbox:checked');
63
- var checkboxSelectAll = table.find('thead tr th input.action_multiple_check_all');
57
+ let allCheckbox = table.find('input.action_multiple_checkbox');
58
+ let allCheckboxChecked = table.find('input.action_multiple_checkbox:checked');
59
+ let checkboxSelectAll = table.find('thead tr th input.action_multiple_check_all');
64
60
  if (allCheckbox.length === allCheckboxChecked.length) {
65
61
  checkboxSelectAll.prop('checked', true);
66
62
  }
@@ -70,8 +66,8 @@ class MultipleActionInTable {
70
66
  }
71
67
 
72
68
  static getDivBtn(table) {
73
- var divTableResponsive = table.parent();
74
- var divBtn = divTableResponsive.next();
69
+ let divTableResponsive = table.parent();
70
+ let divBtn = divTableResponsive.next();
75
71
  if (divBtn.hasClass('action_multiple_buttons')) {
76
72
  return divBtn;
77
73
  }
@@ -111,7 +107,109 @@ class MultipleActionInTable {
111
107
 
112
108
  }
113
109
 
114
- module.exports = { MultipleActionInTable };
110
+ class MultipleActionInDivList {
111
+ // init checkbox
112
+ static init(contentDiv) {
113
+ let buttonsDiv = MultipleActionInDivList.getButtonsDiv(contentDiv);
114
+ if (buttonsDiv == null) {
115
+ return;
116
+ }
117
+
118
+ buttonsDiv.addClass('hide');
119
+
120
+ // Si aucune div sélectionnable, on n'applique pas le plugin
121
+ if (!contentDiv.find('.multiple_action').length) {
122
+ return;
123
+ }
124
+
125
+ if (!buttonsDiv.data('action_multiple_buttons_initialized')) {
126
+ buttonsDiv.prepend($('<img src="'+ROOT_PATH+DOSSIER_IMAGES+'arrow_ltr.png" alt="" /> &nbsp;'));
127
+ buttonsDiv.append($('<br/><br/>'));
128
+ buttonsDiv.data('action_multiple_buttons_initialized', 1);
129
+ }
130
+
131
+ // Ajout checkbox pour chaque div sélectionnable
132
+ contentDiv.find('.multiple_action').each(function(idx, div) {
133
+ if ($(div).find('div.multi_select').length === 0) {
134
+ $(div).prepend($('<div class="multi_select float-start me-2"><input type="checkbox" class="action_multiple_checkbox" name="'+$(div).data('action_multiple_input_name')+'" value="'+$(div).data('action_multiple_item_id')+'"></div>'));
135
+ }
136
+ });
137
+
138
+ // Ajout checkbox select all
139
+ if (contentDiv.find('input.action_multiple_check_all').length === 0) {
140
+ contentDiv.prepend('<p class="mb-2"><input type="checkbox" class="action_multiple_check_all" /> Tout sélectionner</p>');
141
+ }
142
+
143
+ contentDiv.find('input.action_multiple_checkbox').change(function() {
144
+ MultipleActionInDivList.updateCheckbox(contentDiv);
145
+ });
146
+
147
+ contentDiv.find('input.action_multiple_check_all').off('click').click(function() {
148
+ let checkbox = contentDiv.find('input.action_multiple_checkbox');
149
+ let checkboxChecked = contentDiv.find('input.action_multiple_checkbox:checked');
150
+ if (checkbox.length === checkboxChecked.length) {
151
+ checkbox.prop('checked', false);
152
+ }
153
+ else {
154
+ checkbox.prop('checked', true);
155
+ }
156
+ MultipleActionInDivList.updateCheckbox(contentDiv);
157
+ });
158
+
159
+ MultipleActionInDivList.updateCheckbox(contentDiv);
160
+ }
161
+
162
+ static updateCheckbox(contentDiv) {
163
+ MultipleActionInDivList.showButtonsAction(contentDiv);
164
+
165
+ let allCheckbox = contentDiv.find('input.action_multiple_checkbox');
166
+ let allCheckboxChecked = contentDiv.find('input.action_multiple_checkbox:checked');
167
+ let checkboxSelectAll = contentDiv.find('input.action_multiple_check_all');
168
+ if (allCheckbox.length === allCheckboxChecked.length) {
169
+ checkboxSelectAll.prop('checked', true);
170
+ }
171
+ else {
172
+ checkboxSelectAll.prop('checked', false);
173
+ }
174
+ }
175
+
176
+ static getButtonsDiv(contentDiv) {
177
+ let buttonsDiv = contentDiv.next();
178
+ if (buttonsDiv.hasClass('action_multiple_buttons')) {
179
+ return buttonsDiv;
180
+ }
181
+ return null;
182
+ }
183
+
184
+ static showButtonsAction(contentDiv) {
185
+ let buttonsDiv = MultipleActionInDivList.getButtonsDiv(contentDiv);
186
+ if (buttonsDiv == null) {
187
+ return;
188
+ }
189
+
190
+ // console.log(divBtn);
191
+ //var nbItems = $('input[name="' + checkbox.attr('name') + '"]:checked').length;
192
+ let nbItems = contentDiv.find('input.action_multiple_checkbox:checked').length;
193
+
194
+ if (nbItems > 0 && buttonsDiv.is(':hidden')) {
195
+ buttonsDiv.removeClass('hide');
196
+ }
197
+ // 13/04/2021 : si le tableau est caché cela veut dire qu'il est en train de s'initialiser (après avoir chargé les données) et donc s'il n'y a pas de ligne sélectionnées, on cache la div buttons
198
+ else if ((nbItems === 0 && buttonsDiv.is(':visible')) || (nbItems === 0 && contentDiv.is(':hidden'))) {
199
+ buttonsDiv.addClass('hide');
200
+ }
201
+
202
+ // affichage aucune action possible si aucun bouton n'est visible
203
+ if (buttonsDiv.is(':visible')) {
204
+ buttonsDiv.find('span.no_button').remove();
205
+ if (buttonsDiv.find('button:visible, a:visible').length === 0) {
206
+ buttonsDiv.find('img').after('<span class="no_button"><em>aucune action possible</em></span>');
207
+ }
208
+ }
209
+ }
210
+ }
211
+
212
+ module.exports = { MultipleActionInTable, MultipleActionInDivList };
115
213
 
116
214
  /*
117
215
  // init checkbox