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/videomodule.js   B

Complexity

Total Complexity 39
Complexity/F 1.86

Size

Lines of Code 245
Function Count 21

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 30
Bugs 4 Features 1
Metric Value
wmc 39
dl 0
loc 245
rs 8.2857
c 30
b 4
f 1
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  
B M.tinymce_recordrtc.capture_audio_video 0 26 1
A M.tinymce_recordrtc.stop_recording_video 0 56 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('video#player');
33
    playerDOM = document.querySelector('video#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 = 'video';
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 upload button is not shown.
54
            uploadBtn.ancestor().ancestor().addClass('hide');
55
56
            // Change look of recording button.
57
            if (!recordrtc.oldermoodle) {
58
                startStopBtn.replaceClass('btn-outline-danger', 'btn-danger');
59
            }
60
61
            // Empty the array containing the previously recorded chunks.
62
            chunks = [];
63
            blobSize = 0;
64
65
            // Initialize common configurations.
66
            var commonConfig = {
67
                // When the stream is captured from the microphone/webcam.
68
                onMediaCaptured: function(stream) {
69
                    // Make video stream available at a higher level by making it a property of startStopBtn.
70
                    startStopBtn.stream = stream;
71
72
                    M.tinymce_recordrtc.start_recording(recType, startStopBtn.stream);
73
                },
74
75
                // Revert button to "Record Again" when recording is stopped.
76
                onMediaStopped: function(btnLabel) {
77
                    startStopBtn.set('textContent', btnLabel);
78
                },
79
80
                // Handle recording errors.
81
                onMediaCapturingFailed: function(error) {
82
                    var btnLabel = null;
83
84
                    // Handle getUserMedia-thrown errors.
85
                    switch (error.name) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
86
                        case 'AbortError':
87
                            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...
88
                                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...
89
                                    title: M.util.get_string('gumabort_title', 'tinymce_recordrtc'),
90
                                    message: M.util.get_string('gumabort', 'tinymce_recordrtc')
91
                                });
92
                            });
93
94
                            btnLabel = M.util.get_string('recordingfailed', 'tinymce_recordrtc');
95
                            break;
96
                        case 'NotAllowedError':
97
                            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...
98
                                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...
99
                                    title: M.util.get_string('gumnotallowed_title', 'tinymce_recordrtc'),
100
                                    message: M.util.get_string('gumnotallowed', 'tinymce_recordrtc')
101
                                });
102
                            });
103
104
                            btnLabel = M.util.get_string('recordingfailed', 'tinymce_recordrtc');
105
                            break;
106
                        case 'NotFoundError':
107
                            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...
108
                                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...
109
                                    title: M.util.get_string('gumnotfound_title', 'tinymce_recordrtc'),
110
                                    message: M.util.get_string('gumnotfound', 'tinymce_recordrtc')
111
                                });
112
                            });
113
114
                            btnLabel = M.util.get_string('recordingfailed', 'tinymce_recordrtc');
115
                            break;
116
                        case 'NotReadableError':
117
                            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...
118
                                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...
119
                                    title: M.util.get_string('gumnotreadable_title', 'tinymce_recordrtc'),
120
                                    message: M.util.get_string('gumnotreadable', 'tinymce_recordrtc')
121
                                });
122
                            });
123
124
                            btnLabel = M.util.get_string('recordingfailed', 'tinymce_recordrtc');
125
                            break;
126
                        case 'OverConstrainedError':
127
                            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...
128
                                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...
129
                                    title: M.util.get_string('gumoverconstrained_title', 'tinymce_recordrtc'),
130
                                    message: M.util.get_string('gumoverconstrained', 'tinymce_recordrtc')
131
                                });
132
                            });
133
134
                            btnLabel = M.util.get_string('recordingfailed', 'tinymce_recordrtc');
135
                            break;
136
                        case 'SecurityError':
137
                            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...
138
                                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...
139
                                    title: M.util.get_string('gumsecurity_title', 'tinymce_recordrtc'),
140
                                    message: M.util.get_string('gumsecurity', 'tinymce_recordrtc')
141
                                });
142
                            });
143
144
                            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...
145
                            break;
146
                        case 'TypeError':
147
                            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...
148
                                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...
149
                                    title: M.util.get_string('gumtype_title', 'tinymce_recordrtc'),
150
                                    message: M.util.get_string('gumtype', 'tinymce_recordrtc')
151
                                });
152
                            });
153
154
                            btnLabel = M.util.get_string('recordingfailed', 'tinymce_recordrtc');
155
                    }
156
157
                    // Proceed to treat as a stopped recording.
158
                    commonConfig.onMediaStopped(btnLabel);
159
                }
160
            };
161
162
            // Show video tag without controls to view webcam stream.
163
            player.ancestor().ancestor().removeClass('hide');
164
            player.set('controls', false);
165
166
            // Capture audio+video stream from webcam/microphone.
167
            M.tinymce_recordrtc.capture_audio_video(commonConfig);
168
        } else { // If button is displaying "Stop Recording".
169
            // First of all clears the countdownTicker.
170
            clearInterval(countdownTicker);
171
172
            // Disable "Record Again" button for 1s to allow background processing (closing streams).
173
            setTimeout(function() {
174
                startStopBtn.set('disabled', false);
175
            }, 1000);
176
177
            // Stop recording.
178
            M.tinymce_recordrtc.stop_recording_video(startStopBtn.stream);
179
180
            // Change button to offer to record again.
181
            startStopBtn.set('textContent', M.util.get_string('recordagain', 'tinymce_recordrtc'));
182
            if (!recordrtc.oldermoodle) {
183
                startStopBtn.replaceClass('btn-danger', 'btn-outline-danger');
184
            }
185
        }
186
    });
187
};
188
189
// Setup to get audio+video stream from microphone/webcam.
190
M.tinymce_recordrtc.capture_audio_video = function(config) {
191
    M.tinymce_recordrtc.capture_user_media(
192
        // Media constraints.
193
        {
194
            audio: true,
195
            video: {
196
                width: {ideal: 640},
197
                height: {ideal: 480}
198
            }
199
        },
200
201
        // Success callback.
202
        function(audioVideoStream) {
203
            // Set video player source to microphone+webcam stream, and play it back as it's recording.
204
            playerDOM.srcObject = audioVideoStream;
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...
205
            playerDOM.play();
206
207
            config.onMediaCaptured(audioVideoStream);
208
        },
209
210
        // Error callback.
211
        function(error) {
212
            config.onMediaCapturingFailed(error);
213
        }
214
    );
215
};
216
217
M.tinymce_recordrtc.stop_recording_video = function(stream) {
218
    // Stop recording microphone stream.
219
    mediaRecorder.stop();
220
221
    // Stop each individual MediaTrack.
222
    stream.getTracks().forEach(function(track) {
223
        track.stop();
224
    });
225
226
    // Set source of video player.
227
    var blob = new Blob(chunks, {type: mediaRecorder.mimeType});
228
    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...
229
230
    // Enable controls for video player, and unmute.
231
    player.set('muted', false);
232
    player.set('controls', true);
233
234
    // Show upload button.
235
    uploadBtn.ancestor().ancestor().removeClass('hide');
236
    uploadBtn.set('textContent', M.util.get_string('attachrecording', 'tinymce_recordrtc'));
237
    uploadBtn.set('disabled', false);
238
239
    // Handle when upload button is clicked.
240
    uploadBtn.on('click', function() {
241
        // Trigger error if no recording has been made.
242
        if (!player.get('src') || chunks === []) {
243
            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...
244
                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...
245
                    title: M.util.get_string('norecordingfound_title', 'tinymce_recordrtc'),
246
                    message: M.util.get_string('norecordingfound', 'tinymce_recordrtc')
247
                });
248
            });
249
        } else {
250
            uploadBtn.set('disabled', true);
251
252
            // Upload recording to server.
253
            M.tinymce_recordrtc.upload_to_server(recType, function(progress, fileURLOrError) {
254
                if (progress === 'ended') { // Insert annotation in text.
255
                    uploadBtn.set('disabled', false);
256
                    M.tinymce_recordrtc.insert_annotation(recType, fileURLOrError);
257
                } else if (progress === 'upload-failed') { // Show error message in upload button.
258
                    uploadBtn.set('disabled', false);
259
                    uploadBtn.set('textContent', M.util.get_string('uploadfailed', 'tinymce_recordrtc') + ' ' + fileURLOrError);
260
                } else if (progress === 'upload-failed-404') { // 404 error = File too large in Moodle.
261
                    uploadBtn.set('disabled', false);
262
                    uploadBtn.set('textContent', M.util.get_string('uploadfailed404', 'tinymce_recordrtc'));
263
                } else if (progress === 'upload-aborted') {
264
                    uploadBtn.set('disabled', false);
265
                    uploadBtn.set('textContent', M.util.get_string('uploadaborted', 'tinymce_recordrtc') + ' ' + fileURLOrError);
266
                } else {
267
                    uploadBtn.set('textContent', progress);
268
                }
269
            });
270
        }
271
    });
272
};
273