GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( d22a2c...1c88be )
by Jesus
11s
created

tinymce/js/audiomodule.js   B

Complexity

Total Complexity 39
Complexity/F 1.86

Size

Lines of Code 238
Function Count 21

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 39
dl 0
loc 238
rs 8.2857
c 4
b 1
f 0
cc 0
nc 6
mnd 5
bc 32
fnc 21
bpm 1.5238
cpm 1.8571
noi 25

2 Functions

Rating   Name   Duplication   Size   Complexity  
A M.tinymce_recordrtc.capture_audio 0 21 1
A M.tinymce_recordrtc.stop_recording_audio 0 57 1
1
// TinyMCE recordrtc library functions.
2
// @package    tinymce_recordrtc.
3
// @author     Jesus Federico  (jesus [at] blindsidenetworks [dt] com).
4
// @copyright  2016 to present, Blindside Networks Inc.
5
// @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
6
7
/** global: M */
8
/** global: bowser */
9
/** global: recordrtc */
10
/** global: Blob */
11
/** global: player */
12
/** global: startStopBtn */
13
/** global: uploadBtn */
14
/** global: countdownSeconds */
15
/** global: countdownTicker */
16
/** global: recType */
17
/** global: mediaRecorder */
18
/** global: chunks */
19
/** global: blobSize */
20
/** global: maxUploadSize */
21
22
/**
23
 * This function is initialized from PHP
24
 *
25
 * @param {Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
26
 *            Y YUI instance
27
 */
28
M.tinymce_recordrtc.view_init = function() {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
29
    // Assignment of global variables.
30
    alertWarning = Y.one('div#alert-warning');
0 ignored issues
show
Bug introduced by
The variable Y seems to be never declared. If this is a global, consider adding a /** global: Y */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable alertWarning seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.alertWarning.
Loading history...
31
    alertDanger = Y.one('div#alert-danger');
0 ignored issues
show
Bug introduced by
The variable alertDanger seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.alertDanger.
Loading history...
32
    player = Y.one('audio#player');
33
    playerDOM = document.querySelector('audio#player');
0 ignored issues
show
Bug introduced by
The variable playerDOM seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.playerDOM.
Loading history...
34
    startStopBtn = Y.one('button#start-stop');
35
    uploadBtn = Y.one('button#upload');
36
    recType = 'audio';
37
    // Extract the numbers from the string, and convert to bytes.
38
    maxUploadSize = parseInt(recordrtc.maxfilesize.match(/\d+/)[0], 10) * Math.pow(1024, 2);
39
40
    // Show alert and redirect user if connection is not secure.
41
    M.tinymce_recordrtc.check_secure();
42
    // Show alert if using non-ideal browser.
43
    M.tinymce_recordrtc.check_browser();
44
45
    // Run when user clicks on "record" button.
46
    startStopBtn.on('click', function() {
47
        startStopBtn.set('disabled', true);
48
49
        // If button is displaying "Start Recording" or "Record Again".
50
        if ((startStopBtn.get('textContent') === M.util.get_string('startrecording', 'tinymce_recordrtc')) ||
51
            (startStopBtn.get('textContent') === M.util.get_string('recordagain', 'tinymce_recordrtc')) ||
52
            (startStopBtn.get('textContent') === M.util.get_string('recordingfailed', 'tinymce_recordrtc'))) {
53
            // Make sure the audio player and upload button are not shown.
54
            player.ancestor().ancestor().addClass('hide');
55
            uploadBtn.ancestor().ancestor().addClass('hide');
56
57
            // Change look of recording button.
58
            if (!recordrtc.oldermoodle) {
59
                startStopBtn.replaceClass('btn-outline-danger', 'btn-danger');
60
            }
61
62
            // Empty the array containing the previously recorded chunks.
63
            chunks = [];
64
            blobSize = 0;
65
66
            // Initialize common configurations.
67
            var commonConfig = {
68
                // When the stream is captured from the microphone/webcam.
69
                onMediaCaptured: function(stream) {
70
                    // Make audio stream available at a higher level by making it a property of startStopBtn.
71
                    startStopBtn.stream = stream;
72
73
                    M.tinymce_recordrtc.start_recording(recType, startStopBtn.stream);
74
                },
75
76
                // Revert button to "Record Again" when recording is stopped.
77
                onMediaStopped: function(btnLabel) {
78
                    startStopBtn.set('textContent', btnLabel);
79
                },
80
81
                // Handle recording errors.
82
                onMediaCapturingFailed: function(error) {
83
                    var btnLabel = null;
84
85
                    // Handle getUserMedia-thrown errors.
86
                    switch (error.name) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
87
                        case 'AbortError':
88
                            Y.use('moodle-core-notification-alert', function() {
0 ignored issues
show
Bug introduced by
The variable Y seems to be never declared. If this is a global, consider adding a /** global: Y */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
89
                                new M.core.alert({
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new M.core.alert({Identi...(tinymce_recordrtc)))}) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
90
                                    title: M.util.get_string('gumabort_title', 'tinymce_recordrtc'),
91
                                    message: M.util.get_string('gumabort', 'tinymce_recordrtc')
92
                                });
93
                            });
94
95
                            btnLabel = M.util.get_string('recordingfailed', 'tinymce_recordrtc');
96
                            break;
97
                        case 'NotAllowedError':
98
                            Y.use('moodle-core-notification-alert', function() {
0 ignored issues
show
Bug introduced by
The variable Y seems to be never declared. If this is a global, consider adding a /** global: Y */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
99
                                new M.core.alert({
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new M.core.alert({Identi...(tinymce_recordrtc)))}) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
100
                                    title: M.util.get_string('gumnotallowed_title', 'tinymce_recordrtc'),
101
                                    message: M.util.get_string('gumnotallowed', 'tinymce_recordrtc')
102
                                });
103
                            });
104
105
                            btnLabel = M.util.get_string('recordingfailed', 'tinymce_recordrtc');
106
                            break;
107
                        case 'NotFoundError':
108
                            Y.use('moodle-core-notification-alert', function() {
0 ignored issues
show
Bug introduced by
The variable Y seems to be never declared. If this is a global, consider adding a /** global: Y */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
109
                                new M.core.alert({
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new M.core.alert({Identi...(tinymce_recordrtc)))}) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
110
                                    title: M.util.get_string('gumnotfound_title', 'tinymce_recordrtc'),
111
                                    message: M.util.get_string('gumnotfound', 'tinymce_recordrtc')
112
                                });
113
                            });
114
115
                            btnLabel = M.util.get_string('recordingfailed', 'tinymce_recordrtc');
116
                            break;
117
                        case 'NotReadableError':
118
                            Y.use('moodle-core-notification-alert', function() {
0 ignored issues
show
Bug introduced by
The variable Y seems to be never declared. If this is a global, consider adding a /** global: Y */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
119
                                new M.core.alert({
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new M.core.alert({Identi...(tinymce_recordrtc)))}) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
120
                                    title: M.util.get_string('gumnotreadable_title', 'tinymce_recordrtc'),
121
                                    message: M.util.get_string('gumnotreadable', 'tinymce_recordrtc')
122
                                });
123
                            });
124
125
                            btnLabel = M.util.get_string('recordingfailed', 'tinymce_recordrtc');
126
                            break;
127
                        case 'OverConstrainedError':
128
                            Y.use('moodle-core-notification-alert', function() {
0 ignored issues
show
Bug introduced by
The variable Y seems to be never declared. If this is a global, consider adding a /** global: Y */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
129
                                new M.core.alert({
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new M.core.alert({Identi...(tinymce_recordrtc)))}) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
130
                                    title: M.util.get_string('gumoverconstrained_title', 'tinymce_recordrtc'),
131
                                    message: M.util.get_string('gumoverconstrained', 'tinymce_recordrtc')
132
                                });
133
                            });
134
135
                            btnLabel = M.util.get_string('recordingfailed', 'tinymce_recordrtc');
136
                            break;
137
                        case 'SecurityError':
138
                            Y.use('moodle-core-notification-alert', function() {
0 ignored issues
show
Bug introduced by
The variable Y seems to be never declared. If this is a global, consider adding a /** global: Y */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
139
                                new M.core.alert({
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new M.core.alert({Identi...(tinymce_recordrtc)))}) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
140
                                    title: M.util.get_string('gumsecurity_title', 'tinymce_recordrtc'),
141
                                    message: M.util.get_string('gumsecurity', 'tinymce_recordrtc')
142
                                });
143
                            });
144
145
                            tinyMCEPopup.close();
0 ignored issues
show
Bug introduced by
The variable tinyMCEPopup seems to be never declared. If this is a global, consider adding a /** global: tinyMCEPopup */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
146
                            break;
147
                        case 'TypeError':
148
                            Y.use('moodle-core-notification-alert', function() {
0 ignored issues
show
Bug introduced by
The variable Y seems to be never declared. If this is a global, consider adding a /** global: Y */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
149
                                new M.core.alert({
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new M.core.alert({Identi...(tinymce_recordrtc)))}) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
150
                                    title: M.util.get_string('gumtype_title', 'tinymce_recordrtc'),
151
                                    message: M.util.get_string('gumtype', 'tinymce_recordrtc')
152
                                });
153
                            });
154
155
                            btnLabel = M.util.get_string('recordingfailed', 'tinymce_recordrtc');
156
                    }
157
158
                    // Proceed to treat as a stopped recording.
159
                    commonConfig.onMediaStopped(btnLabel);
160
                }
161
            };
162
163
            // Capture audio stream from microphone.
164
            M.tinymce_recordrtc.capture_audio(commonConfig);
165
        } else { // If button is displaying "Stop Recording".
166
            // First of all clears the countdownTicker.
167
            clearInterval(countdownTicker);
168
169
            // Disable "Record Again" button for 1s to allow background processing (closing streams).
170
            setTimeout(function() {
171
                startStopBtn.set('disabled', false);
172
            }, 1000);
173
174
            // Stop recording.
175
            M.tinymce_recordrtc.stop_recording_audio(startStopBtn.stream);
176
177
            // Change button to offer to record again.
178
            startStopBtn.set('textContent', M.util.get_string('recordagain', 'tinymce_recordrtc'));
179
            if (!recordrtc.oldermoodle) {
180
                startStopBtn.replaceClass('btn-danger', 'btn-outline-danger');
181
            }
182
        }
183
    });
184
};
185
186
// Setup to get audio stream from microphone.
187
M.tinymce_recordrtc.capture_audio = function(config) {
188
    M.tinymce_recordrtc.capture_user_media(
189
        // Media constraints.
190
        {
191
            audio: true
192
        },
193
194
        // Success callback.
195
        function(audioStream) {
196
            // Set audio player source to microphone stream.
197
            playerDOM.srcObject = audioStream;
0 ignored issues
show
Bug introduced by
The variable playerDOM seems to be never declared. If this is a global, consider adding a /** global: playerDOM */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
198
199
            config.onMediaCaptured(audioStream);
200
        },
201
202
        // Error callback.
203
        function(error) {
204
            config.onMediaCapturingFailed(error);
205
        }
206
    );
207
};
208
209
M.tinymce_recordrtc.stop_recording_audio = function(stream) {
210
    // Stop recording microphone stream.
211
    mediaRecorder.stop();
212
213
    // Stop each individual MediaTrack.
214
    stream.getTracks().forEach(function(track) {
215
        track.stop();
216
    });
217
218
    // Set source of audio player.
219
    var blob = new Blob(chunks, {type: mediaRecorder.mimeType});
220
    player.set('src', URL.createObjectURL(blob));
0 ignored issues
show
Bug introduced by
The variable URL seems to be never declared. If this is a global, consider adding a /** global: URL */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
221
222
    // Show audio player with controls enabled, and unmute.
223
    player.set('muted', false);
224
    player.set('controls', true);
225
    player.ancestor().ancestor().removeClass('hide');
226
227
    // Show upload button.
228
    uploadBtn.ancestor().ancestor().removeClass('hide');
229
    uploadBtn.set('textContent', M.util.get_string('attachrecording', 'tinymce_recordrtc'));
230
    uploadBtn.set('disabled', false);
231
232
    // Handle when upload button is clicked.
233
    uploadBtn.on('click', function() {
234
        // Trigger error if no recording has been made.
235
        if (!player.get('src') || chunks === []) {
236
            Y.use('moodle-core-notification-alert', function() {
0 ignored issues
show
Bug introduced by
The variable Y seems to be never declared. If this is a global, consider adding a /** global: Y */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
237
                new M.core.alert({
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new M.core.alert({Identi...(tinymce_recordrtc)))}) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
238
                    title: M.util.get_string('norecordingfound_title', 'tinymce_recordrtc'),
239
                    message: M.util.get_string('norecordingfound', 'tinymce_recordrtc')
240
                });
241
            });
242
        } else {
243
            uploadBtn.set('disabled', true);
244
245
            // Upload recording to server.
246
            M.tinymce_recordrtc.upload_to_server(recType, function(progress, fileURLOrError) {
247
                if (progress === 'ended') { // Insert annotation in text.
248
                    uploadBtn.set('disabled', false);
249
                    M.tinymce_recordrtc.insert_annotation(recType, fileURLOrError);
250
                } else if (progress === 'upload-failed') { // Show error message in upload button.
251
                    uploadBtn.set('disabled', false);
252
                    uploadBtn.set('textContent', M.util.get_string('uploadfailed', 'tinymce_recordrtc') + ' ' + fileURLOrError);
253
                } else if (progress === 'upload-failed-404') { // 404 error = File too large in Moodle.
254
                    uploadBtn.set('disabled', false);
255
                    uploadBtn.set('textContent', M.util.get_string('uploadfailed404', 'tinymce_recordrtc'));
256
                } else if (progress === 'upload-aborted') {
257
                    uploadBtn.set('disabled', false);
258
                    uploadBtn.set('textContent', M.util.get_string('uploadaborted', 'tinymce_recordrtc') + ' ' + fileURLOrError);
259
                } else {
260
                    uploadBtn.set('textContent', progress);
261
                }
262
            });
263
        }
264
    });
265
};
266