@osimatic/helpers-js 1.1.77 → 1.1.78

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/media.js CHANGED
@@ -1,218 +1,218 @@
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 (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
- //}
25
- return false;
26
- });
27
-
28
- div.find('.modal_play_link').off('click').click(function () {
29
- $('#modal_voice_message_play').on('show.bs.modal', function (event) {
30
- let button = $(event.relatedTarget);
31
- let modal = $(this);
32
-
33
- let player = modal.find('audio');
34
- player.prop('src', button.data('play_url'));
35
- player.play();
36
- });
37
-
38
- $('#modal_voice_message_play').modal('show', $(this));
39
- return false;
40
- });
41
- }
42
-
43
- static playAudioUrl(url, onPlayed) {
44
- try {
45
- let context = new (window.AudioContext || window.webkitAudioContext)();
46
- let request = new XMLHttpRequest();
47
- request.open("GET", url, true);
48
- Object.entries(HTTPClient.getHeaders(true)).forEach(([key, value]) => request.setRequestHeader(key, value));
49
- request.responseType = "arraybuffer";
50
-
51
- request.onload = function () {
52
- context.decodeAudioData(request.response, function (buffer) {
53
- let source = context.createBufferSource();
54
- source.buffer = buffer;
55
- source.connect(context.destination);
56
- // auto play
57
- source.start(0); // start was previously noteOn
58
-
59
- source.onended = function (event) {
60
- if (typeof onPlayed == 'function') {
61
- onPlayed(event);
62
- }
63
- }
64
- });
65
- };
66
- request.send();
67
- }
68
- catch (e) {
69
- console.error('web audio api not supported', e);
70
- }
71
- }
72
-
73
- static initAudioVisualization(canvas, audioStream) {
74
- let canvasCtx = canvas.getContext("2d");
75
- let canvasWidth = canvas.width;
76
- let canvasHeight = canvas.height;
77
- let audioCtx = new (window.AudioContext || window.webkitAudioContext)();
78
- let analyser = audioCtx.createAnalyser();
79
-
80
- let distortion = audioCtx.createWaveShaper();
81
- let source = audioCtx.createMediaStreamSource(audioStream);
82
- source.connect(analyser);
83
- analyser.connect(distortion);
84
- distortion.connect(audioCtx.destination);
85
-
86
- analyser.fftSize = 256;
87
- let bufferLength = analyser.frequencyBinCount;
88
- //console.log(bufferLength);
89
- let dataArray = new Uint8Array(bufferLength);
90
-
91
- canvasCtx.clearRect(0, 0, canvasWidth, canvasHeight);
92
-
93
- function draw() {
94
- let drawVisual = requestAnimationFrame(draw);
95
-
96
- analyser.getByteFrequencyData(dataArray);
97
-
98
- canvasCtx.fillStyle = 'rgb(0, 0, 0)';
99
- canvasCtx.fillRect(0, 0, canvasWidth, canvasHeight);
100
-
101
- let barWidth = (canvasWidth / bufferLength) * 2.5;
102
- let barHeight;
103
- let x = 0;
104
- for (let i = 0; i < bufferLength; i++) {
105
- barHeight = dataArray[i] / 2;
106
-
107
- canvasCtx.fillStyle = 'rgb(' + (barHeight + 100) + ',50,50)';
108
- canvasCtx.fillRect(x, canvasHeight - barHeight / 2, barWidth, barHeight);
109
-
110
- x += barWidth + 1;
111
- }
112
- }
113
-
114
- draw();
115
- }
116
- }
117
-
118
- class VideoMedia {
119
- static initPlayPauseClick(videoElement) {
120
- $(videoElement).click(function(e) {
121
- // handle click if not Firefox (Firefox supports this feature natively)
122
- if (typeof InstallTrigger === 'undefined') {
123
- // get click position
124
- let clickY = (e.pageY - $(this).offset().top);
125
- let height = parseFloat( $(this).height() );
126
-
127
- // avoids interference with controls
128
- if (clickY > 0.82*height) return;
129
-
130
- // toggles play / pause
131
- this.paused ? this.play() : this.pause();
132
- }
133
- });
134
- }
135
- }
136
-
137
- //Source : https://www.npmjs.com/package/mic-check
138
- class UserMedia {
139
- static hasGetUserMedia() {
140
- return !!(navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
141
- }
142
-
143
- /** SystemPermissionDenied => (macOS) browser does not have permission to access cam/mic */
144
- /** UserPermissionDenied => user denied permission for site to access cam/mic */
145
- /** CouldNotStartVideoSource = > (Windows) browser does not have permission to access cam/mic OR camera is in use by another application or browser tab */
146
- /** Generic => all other errors */
147
-
148
- static requestMediaPermissions(constraints) {
149
- /*try {
150
- console.log(require.resolve("bowser"));
151
- } catch(e) {
152
- return;
153
- }*/
154
-
155
- return new Promise((resolve, reject) => {
156
- const bowser = require('bowser');
157
- const browser = bowser.getParser(window.navigator.userAgent);
158
- const browserName = browser.getBrowserName();
159
-
160
- navigator.mediaDevices.getUserMedia(constraints !== 'undefined' ? constraints : { audio: true, video: true })
161
- .then((stream) => resolve(stream))
162
- .catch((error) => {
163
- const errName = error.name;
164
- const errMessage = error.message;
165
- let errorType = "Generic";
166
-
167
- if (browserName === 'Chrome') {
168
- if (errName === 'NotAllowedError') {
169
- if (errMessage === 'Permission denied by system') {
170
- errorType = "SystemPermissionDenied";
171
- } else if (errMessage === 'Permission denied') {
172
- errorType = "UserPermissionDenied";
173
- }
174
- } else if (errName === 'NotReadableError') {
175
- errorType = "CouldNotStartVideoSource";
176
- }
177
- } else if (browserName === 'Safari') {
178
- if (errName === 'NotAllowedError') {
179
- errorType = "UserPermissionDenied";
180
- }
181
- } else if (browserName === 'Microsoft Edge') {
182
- if (errName === 'NotAllowedError') {
183
- errorType = "UserPermissionDenied";
184
- } else if (errName === 'NotReadableError') {
185
- errorType = "CouldNotStartVideoSource";
186
- }
187
- } else if (browserName === 'Firefox') {
188
- // https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia#exceptions
189
- if (errName === 'NotFoundError') {
190
- errorType = "SystemPermissionDenied";
191
- } else if (errName === 'NotReadableError') {
192
- errorType = "SystemPermissionDenied";
193
- } else if (errName === 'NotAllowedError') {
194
- errorType = "UserPermissionDenied";
195
- } else if (errName === 'AbortError') {
196
- errorType = "CouldNotStartVideoSource";
197
- }
198
- }
199
-
200
- reject({ type: errorType, name: error.name, message: error.message });
201
- });
202
- });
203
- }
204
-
205
- static requestAudioPermissions(audioConstraints) {
206
- return this.requestMediaPermissions({ audio: (audioConstraints !== 'undefined' ? audioConstraints : true), video: false });
207
- }
208
-
209
- static requestVideoPermissions(audioConstraints, videoConstraints) {
210
- return this.requestMediaPermissions({ audio: (audioConstraints !== 'undefined' ? audioConstraints : true), video: (videoConstraints !== 'undefined' ? videoConstraints : true )});
211
- }
212
-
213
- static requestMutedVideoPermissions(videoConstraints) {
214
- return this.requestMediaPermissions({ audio: false, video: (videoConstraints !== 'undefined' ? videoConstraints : true )});
215
- }
216
- }
217
-
218
- module.exports = { AudioMedia, VideoMedia, UserMedia };
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 (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
+ //}
25
+ return false;
26
+ });
27
+
28
+ div.find('.modal_play_link').off('click').click(function () {
29
+ $('#modal_voice_message_play').on('show.bs.modal', function (event) {
30
+ let button = $(event.relatedTarget);
31
+ let modal = $(this);
32
+
33
+ let player = modal.find('audio');
34
+ player.prop('src', button.data('play_url'));
35
+ player.play();
36
+ });
37
+
38
+ $('#modal_voice_message_play').modal('show', $(this));
39
+ return false;
40
+ });
41
+ }
42
+
43
+ static playAudioUrl(url, onPlayed) {
44
+ try {
45
+ let context = new (window.AudioContext || window.webkitAudioContext)();
46
+ let request = new XMLHttpRequest();
47
+ request.open("GET", url, true);
48
+ Object.entries(HTTPClient.getHeaders(true)).forEach(([key, value]) => request.setRequestHeader(key, value));
49
+ request.responseType = "arraybuffer";
50
+
51
+ request.onload = function () {
52
+ context.decodeAudioData(request.response, function (buffer) {
53
+ let source = context.createBufferSource();
54
+ source.buffer = buffer;
55
+ source.connect(context.destination);
56
+ // auto play
57
+ source.start(0); // start was previously noteOn
58
+
59
+ source.onended = function (event) {
60
+ if (typeof onPlayed == 'function') {
61
+ onPlayed(event);
62
+ }
63
+ }
64
+ });
65
+ };
66
+ request.send();
67
+ }
68
+ catch (e) {
69
+ console.error('web audio api not supported', e);
70
+ }
71
+ }
72
+
73
+ static initAudioVisualization(canvas, audioStream) {
74
+ let canvasCtx = canvas.getContext("2d");
75
+ let canvasWidth = canvas.width;
76
+ let canvasHeight = canvas.height;
77
+ let audioCtx = new (window.AudioContext || window.webkitAudioContext)();
78
+ let analyser = audioCtx.createAnalyser();
79
+
80
+ let distortion = audioCtx.createWaveShaper();
81
+ let source = audioCtx.createMediaStreamSource(audioStream);
82
+ source.connect(analyser);
83
+ analyser.connect(distortion);
84
+ distortion.connect(audioCtx.destination);
85
+
86
+ analyser.fftSize = 256;
87
+ let bufferLength = analyser.frequencyBinCount;
88
+ //console.log(bufferLength);
89
+ let dataArray = new Uint8Array(bufferLength);
90
+
91
+ canvasCtx.clearRect(0, 0, canvasWidth, canvasHeight);
92
+
93
+ function draw() {
94
+ let drawVisual = requestAnimationFrame(draw);
95
+
96
+ analyser.getByteFrequencyData(dataArray);
97
+
98
+ canvasCtx.fillStyle = 'rgb(0, 0, 0)';
99
+ canvasCtx.fillRect(0, 0, canvasWidth, canvasHeight);
100
+
101
+ let barWidth = (canvasWidth / bufferLength) * 2.5;
102
+ let barHeight;
103
+ let x = 0;
104
+ for (let i = 0; i < bufferLength; i++) {
105
+ barHeight = dataArray[i] / 2;
106
+
107
+ canvasCtx.fillStyle = 'rgb(' + (barHeight + 100) + ',50,50)';
108
+ canvasCtx.fillRect(x, canvasHeight - barHeight / 2, barWidth, barHeight);
109
+
110
+ x += barWidth + 1;
111
+ }
112
+ }
113
+
114
+ draw();
115
+ }
116
+ }
117
+
118
+ class VideoMedia {
119
+ static initPlayPauseClick(videoElement) {
120
+ $(videoElement).click(function(e) {
121
+ // handle click if not Firefox (Firefox supports this feature natively)
122
+ if (typeof InstallTrigger === 'undefined') {
123
+ // get click position
124
+ let clickY = (e.pageY - $(this).offset().top);
125
+ let height = parseFloat( $(this).height() );
126
+
127
+ // avoids interference with controls
128
+ if (clickY > 0.82*height) return;
129
+
130
+ // toggles play / pause
131
+ this.paused ? this.play() : this.pause();
132
+ }
133
+ });
134
+ }
135
+ }
136
+
137
+ //Source : https://www.npmjs.com/package/mic-check
138
+ class UserMedia {
139
+ static hasGetUserMedia() {
140
+ return !!(navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
141
+ }
142
+
143
+ /** SystemPermissionDenied => (macOS) browser does not have permission to access cam/mic */
144
+ /** UserPermissionDenied => user denied permission for site to access cam/mic */
145
+ /** CouldNotStartVideoSource = > (Windows) browser does not have permission to access cam/mic OR camera is in use by another application or browser tab */
146
+ /** Generic => all other errors */
147
+
148
+ static requestMediaPermissions(constraints) {
149
+ /*try {
150
+ console.log(require.resolve("bowser"));
151
+ } catch(e) {
152
+ return;
153
+ }*/
154
+
155
+ return new Promise((resolve, reject) => {
156
+ const bowser = require('bowser');
157
+ const browser = bowser.getParser(window.navigator.userAgent);
158
+ const browserName = browser.getBrowserName();
159
+
160
+ navigator.mediaDevices.getUserMedia(constraints !== 'undefined' ? constraints : { audio: true, video: true })
161
+ .then((stream) => resolve(stream))
162
+ .catch((error) => {
163
+ const errName = error.name;
164
+ const errMessage = error.message;
165
+ let errorType = "Generic";
166
+
167
+ if (browserName === 'Chrome') {
168
+ if (errName === 'NotAllowedError') {
169
+ if (errMessage === 'Permission denied by system') {
170
+ errorType = "SystemPermissionDenied";
171
+ } else if (errMessage === 'Permission denied') {
172
+ errorType = "UserPermissionDenied";
173
+ }
174
+ } else if (errName === 'NotReadableError') {
175
+ errorType = "CouldNotStartVideoSource";
176
+ }
177
+ } else if (browserName === 'Safari') {
178
+ if (errName === 'NotAllowedError') {
179
+ errorType = "UserPermissionDenied";
180
+ }
181
+ } else if (browserName === 'Microsoft Edge') {
182
+ if (errName === 'NotAllowedError') {
183
+ errorType = "UserPermissionDenied";
184
+ } else if (errName === 'NotReadableError') {
185
+ errorType = "CouldNotStartVideoSource";
186
+ }
187
+ } else if (browserName === 'Firefox') {
188
+ // https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia#exceptions
189
+ if (errName === 'NotFoundError') {
190
+ errorType = "SystemPermissionDenied";
191
+ } else if (errName === 'NotReadableError') {
192
+ errorType = "SystemPermissionDenied";
193
+ } else if (errName === 'NotAllowedError') {
194
+ errorType = "UserPermissionDenied";
195
+ } else if (errName === 'AbortError') {
196
+ errorType = "CouldNotStartVideoSource";
197
+ }
198
+ }
199
+
200
+ reject({ type: errorType, name: error.name, message: error.message });
201
+ });
202
+ });
203
+ }
204
+
205
+ static requestAudioPermissions(audioConstraints) {
206
+ return this.requestMediaPermissions({ audio: (audioConstraints !== 'undefined' ? audioConstraints : true), video: false });
207
+ }
208
+
209
+ static requestVideoPermissions(audioConstraints, videoConstraints) {
210
+ return this.requestMediaPermissions({ audio: (audioConstraints !== 'undefined' ? audioConstraints : true), video: (videoConstraints !== 'undefined' ? videoConstraints : true )});
211
+ }
212
+
213
+ static requestMutedVideoPermissions(videoConstraints) {
214
+ return this.requestMediaPermissions({ audio: false, video: (videoConstraints !== 'undefined' ? videoConstraints : true )});
215
+ }
216
+ }
217
+
218
+ module.exports = { AudioMedia, VideoMedia, UserMedia };