@osimatic/helpers-js 1.0.2

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 ADDED
@@ -0,0 +1,97 @@
1
+ class JwtToken {
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) {
6
+ return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
7
+ }).join(''));
8
+
9
+ return JSON.parse(jsonPayload);
10
+ }
11
+
12
+ static hasRole(token, role) {
13
+ if (token == null) {
14
+ return false;
15
+ }
16
+
17
+ let payload = JwtToken.parseJwt(token);
18
+ return payload.roles.indexOf(role) !== -1;
19
+ }
20
+ }
21
+
22
+ 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;
33
+ }
34
+
35
+ static setToken(token) {
36
+ localStorage.setItem('access_token', token);
37
+ }
38
+
39
+ static getToken() {
40
+ return localStorage.getItem('access_token');
41
+ }
42
+
43
+ static setRefreshToken(token) {
44
+ localStorage.setItem('refresh_token', token);
45
+ }
46
+
47
+ static getRefreshToken() {
48
+ return localStorage.getItem('refresh_token');
49
+ }
50
+
51
+ static isSimulationConnexion() {
52
+ return localStorage.getItem('admin_refresh_token') != null && localStorage.getItem('admin_access_token') != null;
53
+ }
54
+
55
+ static cancelSimulationConnexion() {
56
+ localStorage.setItem('refresh_token', localStorage.getItem('admin_refresh_token'));
57
+ localStorage.setItem('access_token', localStorage.getItem('admin_access_token'));
58
+
59
+ localStorage.removeItem('admin_refresh_token');
60
+ localStorage.removeItem('admin_access_token');
61
+ }
62
+
63
+ static logout() {
64
+ localStorage.removeItem('access_token');
65
+ localStorage.removeItem('refresh_token');
66
+ }
67
+
68
+ static getData(key) {
69
+ let token = JwtSession.getToken();
70
+ if (token == null) {
71
+ return null;
72
+ }
73
+
74
+ let payload = JwtToken.parseJwt(token);
75
+ if (typeof payload[key] != 'undefined') {
76
+ return payload[key];
77
+ }
78
+ return null;
79
+ }
80
+
81
+ static isAnonymous() {
82
+ return localStorage.getItem('access_token') == null;
83
+ }
84
+
85
+ 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;
93
+ }
94
+ }
95
+
96
+ exports.JwtToken = JwtToken;
97
+ exports.JwtSession = JwtSession;
package/list_box.js ADDED
@@ -0,0 +1,113 @@
1
+ class ListBox {
2
+ /*
3
+ <div class="form-group">
4
+ <div class="listebox_left">
5
+ <span class="titre_listebox">Services interdits :</span><br/>
6
+ <select name="liste_services" id="liste_services" class="liste_box liste_box_grand" size="10" multiple="multiple">
7
+ <?php foreach ($listeServicesClient as $arbo) : ?>
8
+ <option value="<?php echo $arbo->getIdArbo(); ?>"><?php echo $arbo->getLibelleAffiche(); ?></option>
9
+ <?php endforeach ?>
10
+ </select>
11
+ </div>
12
+
13
+ <div class="listebox_cmd">
14
+ <a href="#" id="listebox_lien_right"><img src="<?php echo ROOT_PATH.DOSSIER_IMAGES; ?>icone_listbox_right.png" /></a><br/>
15
+ <a href="#" id="listebox_lien_left"><img src="<?php echo ROOT_PATH.DOSSIER_IMAGES; ?>icone_listbox_left.png" /></a>
16
+ </div>
17
+
18
+ <div class="listebox_right">
19
+ <span class="titre_listebox">Services autorisés :</span><br/>
20
+ <select name="liste_services_stats[]" id="liste_services_stats" class="liste_box liste_box_grand" size="10" multiple="multiple">
21
+ <?php foreach ($listeServicesStatsConsultant as $arbo) : ?>
22
+ <option value="<?php echo $arbo->getIdArbo(); ?>"><?php echo $arbo->getLibelleAffiche(); ?></option>
23
+ <?php endforeach ?>
24
+ </select>
25
+ </div>
26
+ <div class="cl"></div>
27
+ </div>
28
+ */
29
+
30
+ /*
31
+ Listbox Select All / Deselect All JavaScript
32
+ Use :
33
+ listbox_selectall('countryList', true); //select all the options
34
+ listbox_selectall('countryList', false); //deselect all the options
35
+ */
36
+ static selectall(listID, isSelect) {
37
+ var listbox = document.getElementById(listID);
38
+ for (var count=0; count < listbox.options.length; count++) {
39
+ listbox.options[count].selected = isSelect;
40
+ }
41
+ }
42
+
43
+ /*
44
+ Listbox Move up/down options JavaScript
45
+ Use :
46
+ listbox_move('countryList', 'up'); //move up the selected option
47
+ listbox_move('countryList', 'down'); //move down the selected option
48
+ */
49
+ static move(listID, direction) {
50
+ var listbox = document.getElementById(listID);
51
+ var selIndex = listbox.selectedIndex;
52
+
53
+ if (-1 === selIndex) {
54
+ alert("Please select an option to move.");
55
+ return;
56
+ }
57
+
58
+ var increment = -1;
59
+ if (direction == 'up') {
60
+ increment = -1;
61
+ }
62
+ else {
63
+ increment = 1;
64
+ }
65
+
66
+ if ((selIndex + increment) < 0 || (selIndex + increment) > (listbox.options.length-1)) {
67
+ return;
68
+ }
69
+
70
+ var selValue = listbox.options[selIndex].value;
71
+ var selText = listbox.options[selIndex].text;
72
+ listbox.options[selIndex].value = listbox.options[selIndex + increment].value
73
+ listbox.options[selIndex].text = listbox.options[selIndex + increment].text
74
+
75
+ listbox.options[selIndex + increment].value = selValue;
76
+ listbox.options[selIndex + increment].text = selText;
77
+
78
+ listbox.selectedIndex = selIndex + increment;
79
+ }
80
+
81
+ /*
82
+ Listbox swap/move left-right options JavaScript
83
+ Use :
84
+ listbox_moveacross('countryList', 'selectedCountryList');
85
+ */
86
+ static moveacross(sourceID, destID) {
87
+ var src = document.getElementById(sourceID);
88
+ var dest = document.getElementById(destID);
89
+
90
+ for (var count=0; count < src.options.length; count++) {
91
+ if (src.options[count].selected == true) {
92
+ var option = src.options[count];
93
+
94
+ var newOption = document.createElement("option");
95
+ newOption.value = option.value;
96
+ newOption.text = option.text;
97
+ newOption.selected = true;
98
+ try {
99
+ dest.add(newOption, null); //Standard
100
+ src.remove(count, null);
101
+ }
102
+ catch(error) {
103
+ dest.add(newOption); // IE only
104
+ src.remove(count);
105
+ }
106
+ count--;
107
+ }
108
+ }
109
+ }
110
+
111
+ }
112
+
113
+ exports.ListBox = ListBox;
package/location.js ADDED
@@ -0,0 +1,391 @@
1
+
2
+ const COUNTRIES_LIST = {
3
+ AF:'Afghanistan',
4
+ AX:'Åland Islands',
5
+ AL:'Albania',
6
+ DZ:'Algeria',
7
+ AS:'American Samoa',
8
+ AD:'Andorra',
9
+ AO:'Angola',
10
+ AI:'Anguilla',
11
+ AQ:'Antarctica',
12
+ AG:'Antigua and Barbuda',
13
+ AR:'Argentina',
14
+ AM:'Armenia',
15
+ AW:'Aruba',
16
+ AU:'Australia',
17
+ AT:'Austria',
18
+ AZ:'Azerbaijan',
19
+ BS:'Bahamas',
20
+ BH:'Bahrain',
21
+ BD:'Bangladesh',
22
+ BB:'Barbados',
23
+ BY:'Belarus',
24
+ BE:'Belgium',
25
+ BZ:'Belize',
26
+ BJ:'Benin',
27
+ BM:'Bermuda',
28
+ BT:'Bhutan',
29
+ BO:'Bolivia',
30
+ BA:'Bosnia and Herzegovina',
31
+ BW:'Botswana',
32
+ BV:'Bouvet Island',
33
+ BR:'Brazil',
34
+ IO:'British Indian Ocean Territory',
35
+ BN:'Brunei Darussalam',
36
+ BG:'Bulgaria',
37
+ BF:'Burkina Faso',
38
+ BI:'Burundi',
39
+ KH:'Cambodia',
40
+ CM:'Cameroon',
41
+ CA:'Canada',
42
+ CV:'Cape Verde',
43
+ KY:'Cayman Islands',
44
+ CF:'Central African Republic',
45
+ TD:'Chad',
46
+ CL:'Chile',
47
+ CN:'China',
48
+ CX:'Christmas Island',
49
+ CC:'Cocos (Keeling) Islands',
50
+ CO:'Colombia',
51
+ KM:'Comoros',
52
+ CG:'Congo',
53
+ CD:'Congo, The Democratic Republic of The',
54
+ CK:'Cook Islands',
55
+ CR:'Costa Rica',
56
+ CI:'Cote d’Ivoire',
57
+ HR:'Croatia',
58
+ CU:'Cuba',
59
+ CY:'Cyprus',
60
+ CZ:'Czechia',
61
+ DK:'Denmark',
62
+ DJ:'Djibouti',
63
+ DM:'Dominica',
64
+ DO:'Dominican Republic',
65
+ EC:'Ecuador',
66
+ EG:'Egypt',
67
+ SV:'El Salvador',
68
+ GQ:'Equatorial Guinea',
69
+ ER:'Eritrea',
70
+ EE:'Estonia',
71
+ ET:'Ethiopia',
72
+ FK:'Falkland Islands (Malvinas)',
73
+ FO:'Faroe Islands',
74
+ FJ:'Fiji',
75
+ FI:'Finland',
76
+ FR:'France',
77
+ GF:'French Guiana',
78
+ PF:'French Polynesia',
79
+ TF:'French Southern Territories',
80
+ GA:'Gabon',
81
+ GM:'Gambia',
82
+ GE:'Georgia',
83
+ DE:'Germany',
84
+ GH:'Ghana',
85
+ GI:'Gibraltar',
86
+ GR:'Greece',
87
+ GL:'Greenland',
88
+ GD:'Grenada',
89
+ GP:'Guadeloupe',
90
+ GU:'Guam',
91
+ GT:'Guatemala',
92
+ GG:'Guernsey',
93
+ GN:'Guinea',
94
+ GW:'Guinea-bissau',
95
+ GY:'Guyana',
96
+ HT:'Haiti',
97
+ HM:'Heard Island and Mcdonald Islands',
98
+ VA:'Holy See (Vatican City State)',
99
+ HN:'Honduras',
100
+ HK:'Hong Kong',
101
+ HU:'Hungary',
102
+ IS:'Iceland',
103
+ IN:'India',
104
+ ID:'Indonesia',
105
+ IR:'Iran, Islamic Republic of',
106
+ IQ:'Iraq',
107
+ IE:'Ireland',
108
+ IM:'Isle of Man',
109
+ IL:'Israel',
110
+ IT:'Italy',
111
+ JM:'Jamaica',
112
+ JP:'Japan',
113
+ JE:'Jersey',
114
+ JO:'Jordan',
115
+ KZ:'Kazakhstan',
116
+ KE:'Kenya',
117
+ KI:'Kiribati',
118
+ KP:'Korea, Democratic People’s Republic of',
119
+ KR:'Korea, Republic of',
120
+ KW:'Kuwait',
121
+ KG:'Kyrgyzstan',
122
+ LA:'Lao People’s Democratic Republic',
123
+ LV:'Latvia',
124
+ LB:'Lebanon',
125
+ LS:'Lesotho',
126
+ LR:'Liberia',
127
+ LY:'Libyan Arab Jamahiriya',
128
+ LI:'Liechtenstein',
129
+ LT:'Lithuania',
130
+ LU:'Luxembourg',
131
+ MO:'Macao',
132
+ MK:'Macedonia, The Former Yugoslav Republic of',
133
+ MG:'Madagascar',
134
+ MW:'Malawi',
135
+ MY:'Malaysia',
136
+ MV:'Maldives',
137
+ ML:'Mali',
138
+ MT:'Malta',
139
+ MH:'Marshall Islands',
140
+ MQ:'Martinique',
141
+ MR:'Mauritania',
142
+ MU:'Mauritius',
143
+ YT:'Mayotte',
144
+ MX:'Mexico',
145
+ FM:'Micronesia, Federated States of',
146
+ MD:'Moldova, Republic of',
147
+ MC:'Monaco',
148
+ MN:'Mongolia',
149
+ ME:'Montenegro',
150
+ MS:'Montserrat',
151
+ MA:'Morocco',
152
+ MZ:'Mozambique',
153
+ MM:'Myanmar',
154
+ NA:'Namibia',
155
+ NR:'Nauru',
156
+ NP:'Nepal',
157
+ NL:'Netherlands',
158
+ AN:'Netherlands Antilles',
159
+ NC:'New Caledonia',
160
+ NZ:'New Zealand',
161
+ NI:'Nicaragua',
162
+ NE:'Niger',
163
+ NG:'Nigeria',
164
+ NU:'Niue',
165
+ NF:'Norfolk Island',
166
+ MP:'Northern Mariana Islands',
167
+ NO:'Norway',
168
+ OM:'Oman',
169
+ PK:'Pakistan',
170
+ PW:'Palau',
171
+ PS:'Palestinian Territory, Occupied',
172
+ PA:'Panama',
173
+ PG:'Papua New Guinea',
174
+ PY:'Paraguay',
175
+ PE:'Peru',
176
+ PH:'Philippines',
177
+ PN:'Pitcairn',
178
+ PL:'Poland',
179
+ PT:'Portugal',
180
+ PR:'Puerto Rico',
181
+ QA:'Qatar',
182
+ RE:'Reunion',
183
+ RO:'Romania',
184
+ RU:'Russian Federation',
185
+ RW:'Rwanda',
186
+ SH:'Saint Helena',
187
+ KN:'Saint Kitts and Nevis',
188
+ LC:'Saint Lucia',
189
+ PM:'Saint Pierre and Miquelon',
190
+ VC:'Saint Vincent and The Grenadines',
191
+ WS:'Samoa',
192
+ SM:'San Marino',
193
+ ST:'Sao Tome and Principe',
194
+ SA:'Saudi Arabia',
195
+ SN:'Senegal',
196
+ RS:'Serbia',
197
+ SC:'Seychelles',
198
+ SL:'Sierra Leone',
199
+ SG:'Singapore',
200
+ SK:'Slovakia',
201
+ SI:'Slovenia',
202
+ SB:'Solomon Islands',
203
+ SO:'Somalia',
204
+ ZA:'South Africa',
205
+ GS:'South Georgia and The South Sandwich Islands',
206
+ ES:'Spain',
207
+ LK:'Sri Lanka',
208
+ SD:'Sudan',
209
+ SR:'Suriname',
210
+ SJ:'Svalbard and Jan Mayen',
211
+ SZ:'Swaziland',
212
+ SE:'Sweden',
213
+ CH:'Switzerland',
214
+ SY:'Syrian Arab Republic',
215
+ TW:'Taiwan, Province of China',
216
+ TJ:'Tajikistan',
217
+ TZ:'Tanzania, United Republic of',
218
+ TH:'Thailand',
219
+ TL:'Timor-leste',
220
+ TG:'Togo',
221
+ TK:'Tokelau',
222
+ TO:'Tonga',
223
+ TT:'Trinidad and Tobago',
224
+ TN:'Tunisia',
225
+ TR:'Turkey',
226
+ TM:'Turkmenistan',
227
+ TC:'Turks and Caicos Islands',
228
+ TV:'Tuvalu',
229
+ UG:'Uganda',
230
+ UA:'Ukraine',
231
+ AE:'United Arab Emirates',
232
+ GB:'United Kingdom',
233
+ US:'United States',
234
+ UM:'United States Minor Outlying Islands',
235
+ UY:'Uruguay',
236
+ UZ:'Uzbekistan',
237
+ VU:'Vanuatu',
238
+ VE:'Venezuela',
239
+ VN:'Viet Nam',
240
+ VG:'Virgin Islands, British',
241
+ VI:'Virgin Islands, U.S.',
242
+ WF:'Wallis and Futuna',
243
+ EH:'Western Sahara',
244
+ YE:'Yemen',
245
+ ZM:'Zambia',
246
+ ZW:'Zimbabwe',
247
+ };
248
+
249
+ class Country {
250
+ static getFlagPath(countryCode) {
251
+ return flagsPath+countryCode.toLowerCase()+'.png';
252
+ }
253
+ static getFlagImg(countryCode) {
254
+ return '<span><img src="'+Country.getFlagPath(countryCode)+'" alt="" title="'+Country.getCountryName(countryCode)+'" class="flag" /></span>'
255
+ }
256
+
257
+ static fillCountrySelect(select, defaultValue) {
258
+ Object.entries(Country.getCountryList()).forEach(([countryCode, countryName]) => select.append('<option value="'+countryCode+'">'+countryName+'</option>'));
259
+ if (typeof defaultValue != 'undefined') {
260
+ select.val(defaultValue);
261
+ }
262
+ select.selectpicker('refresh');
263
+ }
264
+ static getCountryName(countryCode) {
265
+ if (COUNTRIES_LIST.hasOwnProperty(countryCode)) {
266
+ return COUNTRIES_LIST[countryCode];
267
+ }
268
+ return countryCode;
269
+ }
270
+ static getCountryList() {
271
+ return COUNTRIES_LIST;
272
+ }
273
+ }
274
+
275
+ class PostalAddress {
276
+ static format(addressData, separator) {
277
+ if (typeof separator == 'undefined') {
278
+ separator = '<br/>';
279
+ }
280
+
281
+ /*
282
+ var address = new Address({
283
+ country: "USA",
284
+ countryCode: "US",
285
+ postalCode: "95054",
286
+ region: "CA",
287
+ locality: "Santa Clara",
288
+ streetAddress: "LG Silicon Valley Lab, 5150 Great America Pkwy"
289
+ });
290
+ var af = new AddressFmt();
291
+ var formatted = af.format(address);
292
+ console.log(formatted);
293
+ */
294
+
295
+ var addressDataForPluging = {
296
+ streetAddress: addressData.streetAddress+(addressData.additionalAddress!=null&&addressData.additionalAddress!==''?"\n"+addressData.additionalAddress:''),
297
+ postalCode: addressData.postalCode,
298
+ locality: addressData.locality,
299
+ region: addressData.state,
300
+ countryCode: addressData.countryCode,
301
+ country: Country.getCountryName(addressData.countryCode),
302
+ };
303
+ if (addressDataForPluging.locality == null) {
304
+ addressDataForPluging.locality = addressData.suburb;
305
+ }
306
+ if (addressDataForPluging.locality == null) {
307
+ addressDataForPluging.locality = addressData.stateDistrict;
308
+ }
309
+
310
+ var af = new AddressFmt();
311
+ var formattedAddress = af.format(new Address(addressDataForPluging));
312
+ return formattedAddress.replace(/\n+/g, separator);
313
+ }
314
+
315
+ static getComponentsFromGoogleApi(googleApiResult) {
316
+ /*
317
+ var addressData = {
318
+ streetAddress: address.street,
319
+ additionalAddress: null,
320
+ postalCode: address.zipCode,
321
+ locality: address.city,
322
+ stateDistrict: null,
323
+ state: null,
324
+ countryCode: address.country,
325
+ };
326
+ */
327
+
328
+ var streetNumber = null;
329
+ var route = null;
330
+ var shortRoute = null;
331
+
332
+ var addressData = {};
333
+ googleApiResult.address_components.forEach(function(resultAddressComponent) {
334
+ if (resultAddressComponent.types.indexOf('street_number') !== -1) {
335
+ streetNumber = resultAddressComponent.long_name;
336
+ }
337
+ if (resultAddressComponent.types.indexOf('route') !== -1) {
338
+ route = resultAddressComponent.long_name;
339
+ shortRoute = resultAddressComponent.long_name;
340
+ }
341
+ if (resultAddressComponent.types.indexOf('sublocality_level_1') !== -1) {
342
+ addressData.suburb = resultAddressComponent.long_name;
343
+ }
344
+ if (resultAddressComponent.types.indexOf('locality') !== -1) {
345
+ addressData.locality = resultAddressComponent.long_name;
346
+ }
347
+ if (resultAddressComponent.types.indexOf('postal_town') !== -1) {
348
+ addressData.locality = resultAddressComponent.long_name;
349
+ }
350
+ if (resultAddressComponent.types.indexOf('postal_code') !== -1) {
351
+ addressData.postalCode = resultAddressComponent.long_name;
352
+ }
353
+ if (resultAddressComponent.types.indexOf('administrative_area_level_3') !== -1) {
354
+ addressData.locality = resultAddressComponent.long_name;
355
+ }
356
+ if (resultAddressComponent.types.indexOf('administrative_area_level_2') !== -1) {
357
+ addressData.stateDistrict = resultAddressComponent.long_name;
358
+ }
359
+ if (resultAddressComponent.types.indexOf('administrative_area_level_1') !== -1) {
360
+ addressData.state = resultAddressComponent.long_name;
361
+ }
362
+ if (resultAddressComponent.types.indexOf('country') !== -1) {
363
+ addressData.countryCode = resultAddressComponent.short_name;
364
+ }
365
+ });
366
+ var htmlAddress = $(googleApiResult.adr_address);
367
+ // console.log(googleApiResult.adr_address);
368
+ // console.log(htmlAddress);
369
+ // console.log(htmlAddress.find('span.street-address'));
370
+ //console.log($(htmlAddress[0]).text());
371
+ if (htmlAddress.length && $(htmlAddress[0]).length) {
372
+ addressData.streetAddress = $(htmlAddress[0]).text();
373
+ }
374
+ else {
375
+ addressData.streetAddress = streetNumber+' '+route;
376
+ }
377
+
378
+ return addressData;
379
+ }
380
+ }
381
+
382
+ class Location {
383
+ static checkCoordinates(str) {
384
+ return /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/.test(str);
385
+ }
386
+ }
387
+
388
+ exports.COUNTRIES_LIST = COUNTRIES_LIST;
389
+ exports.Country = Country;
390
+ exports.PostalAddress = PostalAddress;
391
+ exports.Location = Location;
package/media.js ADDED
@@ -0,0 +1,82 @@
1
+ class AudioMedia {
2
+
3
+ static getPlayer(playUrl) {
4
+ return '<audio controls preload="none"><source src="'+playUrl+'" type="audio/x-wav"></audio>';
5
+ }
6
+
7
+ static initPlayLinks(div) {
8
+ // Affiche un lecteur audio
9
+ div.find('.play_link').off('click').click(function() {
10
+ let audio = $(AudioMedia.getPlayer($(this).data('play_url')));
11
+ audio[0].play();
12
+ $(this).after(audio);
13
+ $(this).remove();
14
+ return false;
15
+ });
16
+
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
+
26
+ return false;
27
+ });
28
+
29
+ div.find('.modal_play_link').off('click').click(function() {
30
+ $('#modal_voice_message_play').on('show.bs.modal', function (event) {
31
+ let button = $(event.relatedTarget);
32
+ let modal = $(this);
33
+
34
+ let player = modal.find('audio');
35
+ player.prop('src', button.data('play_url'));
36
+ player.play();
37
+ });
38
+
39
+ $('#modal_voice_message_play').modal('show', $(this));
40
+ return false;
41
+ });
42
+ }
43
+
44
+ static playAudioUrl(url, onPlayed) {
45
+ try {
46
+ let context = new (window.AudioContext || window.webkitAudioContext)();
47
+ let request = new XMLHttpRequest();
48
+ request.open("GET", url, true);
49
+ Object.entries(httpHeaders).forEach(([key, value]) => request.setRequestHeader(key, value));
50
+ request.responseType = "arraybuffer";
51
+
52
+ request.onload = function() {
53
+ context.decodeAudioData(request.response, function(buffer) {
54
+ let source = context.createBufferSource();
55
+ source.buffer = buffer;
56
+ source.connect(context.destination);
57
+ // auto play
58
+ source.start(0); // start was previously noteOn
59
+
60
+ source.onended = function(event) {
61
+ if (typeof onPlayed == 'function') {
62
+ onPlayed(event);
63
+ }
64
+ }
65
+ });
66
+ };
67
+ request.send();
68
+ }
69
+ catch(e) {
70
+ console.log(e);
71
+ console.log('web audio api not supported');
72
+ }
73
+ }
74
+
75
+ }
76
+
77
+ exports.AudioMedia = AudioMedia;
78
+
79
+ //deprecated
80
+ function hasGetUserMedia() {
81
+ return !!(navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
82
+ }