Passed
Branch feature-recording-server (fd2a3c)
by Jacob
01:53
created

Y.Base.create(ꞌbuttonꞌ).initializer   D

Complexity

Conditions 9
Paths 17

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
c 1
b 0
f 0
nc 17
nop 0
dl 0
loc 30
rs 4.909
1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
//
16
17
/**
18
 * Atto recordrtc library functions
19
 *
20
 * @package    atto_recordrtc
21
 * @author     Jesus Federico (jesus [at] blindsidenetworks [dt] com)
22
 * @author     Jacob Prud'homme (jacob [dt] prudhomme [at] blindsidenetworks [dt] com)
23
 * @copyright  2017 Blindside Networks Inc.
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
27
/**
28
 * @module moodle-atto_recordrtc-button
29
 */
30
31
/**
32
 * Atto text editor recordrtc plugin.
33
 *
34
 * @namespace M.atto_recordrtc
35
 * @class button
36
 * @extends M.editor_atto.EditorPlugin
37
 */
38
39
// ESLint directives.
40
/* eslint-disable camelcase, spaced-comment */
41
42
// JSHint directives.
43
/*global M */
44
/*jshint onevar: false */
45
46
// Scrutinizer CI directives.
47
/** global: Y */
48
/** global: M */
49
50
var PLUGINNAME = 'atto_recordrtc',
51
    TEMPLATE = '' +
52
    '<div class="{{PLUGINNAME}} container-fluid">' +
53
      '<div class="{{bs_row}} hide">' +
54
        '<div class="{{bs_col}}12">' +
55
          '<div id="alert-warning" class="alert {{bs_al_warn}}">' +
56
            '<strong>{{browseralert_title}}</strong> {{browseralert}}' +
57
          '</div>' +
58
        '</div>' +
59
      '</div>' +
60
      '<div class="{{bs_row}} hide">' +
61
        '<div class="{{bs_col}}12">' +
62
          '<div id="alert-danger" class="alert {{bs_al_dang}}">' +
63
            '<strong>{{insecurealert_title}}</strong> {{insecurealert}}' +
64
          '</div>' +
65
        '</div>' +
66
      '</div>' +
67
      '<div class="{{bs_row}} hide">' +
68
        '{{#if audio}}' +
69
          '<div class="{{bs_col}}1"></div>' +
70
          '<div class="{{bs_col}}10">' +
71
            '<audio id="player"></audio>' +
72
          '</div>' +
73
          '<div class="{{bs_col}}1"></div>' +
74
        '{{else}}' +
75
          '<div class="{{bs_col}}12">' +
76
            '<video id="player"></video>' +
77
          '</div>' +
78
        '{{/if}}' +
79
      '</div>' +
80
      '<div class="{{bs_row}}">' +
81
        '<div class="{{bs_col}}1"></div>' +
82
        '<div class="{{bs_col}}10">' +
83
          '<button id="start-stop" class="{{bs_ss_btn}}">{{startrecording}}</button>' +
84
        '</div>' +
85
        '<div class="{{bs_col}}1"></div>' +
86
      '</div>' +
87
      '<div class="{{bs_row}} hide">' +
88
        '<div class="{{bs_col}}3"></div>' +
89
        '<div class="{{bs_col}}6">' +
90
          '<button id="upload" class="btn btn-primary btn-block">{{attachrecording}}</button>' +
91
        '</div>' +
92
        '<div class="{{bs_col}}3"></div>' +
93
      '</div>' +
94
    '</div>';
95
96
Y.namespace('M.atto_recordrtc').Button = Y.Base.create('button', Y.M.editor_atto.EditorPlugin, [], {
97
    /**
98
     * The current language by default.
99
     */
100
    _lang: 'en',
101
102
    initializer: function() {
103
        if (this.get('host').canShowFilepicker('media')) {
104
            // Add audio and/or video buttons depending on the settings.
105
            var allowedtypes = this.get('allowedtypes');
106
            if (allowedtypes === 'both' || allowedtypes === 'audio') {
107
                this._addButton('audio', this._audio);
108
            }
109
            if (allowedtypes === 'both' || allowedtypes === 'video') {
110
                this._addButton('video', this._video);
111
            }
112
113
            // Initialize the dialogue box.
114
            var dialogue = this.getDialogue({
115
                width: 1000,
116
                focusAfterHide: null
117
            });
118
119
            // If dialogue is closed during recording, do the following.
120
            var editor = this;
121
            dialogue.after('visibleChange', function() {
122
                var closed = !dialogue.get('visible'),
123
                    premium = editor.get('premiumservice') === '1',
124
                    cm = M.atto_recordrtc.commonmodule,
125
                    hm = premium ? M.atto_recordrtc.premiumhelpermodule
126
                                 : M.atto_recordrtc.helpermodule;
127
128
                if (closed) {
129
                    if (premium) {
130
                        // Disconnect the socket.
131
                        hm.socket.disconnect(true);
132
                    } else {
133
                        // Clear the countdown timer.
134
                        window.clearInterval(hm.countdownTicker);
135
                    }
136
137
                    if (cm.mediaRecorder && cm.mediaRecorder.state !== 'inactive') {
138
                        cm.mediaRecorder.stop();
139
                    }
140
141
                    if (cm.stream) {
142
                        var tracks = cm.stream.getTracks();
143
                        for (var i = 0; i < tracks.length; i++) {
144
                            if (tracks[i].readyState !== 'ended') {
145
                                tracks[i].stop();
146
                            }
147
                        }
148
                    }
149
                }
150
            });
151
152
            // Require Bowser, adapter.js and Socket.io libraries.
153
            require(['atto_recordrtc/bowser'], function(bowser) {
154
                window.bowser = bowser;
155
            });
156
            require(['atto_recordrtc/adapter'], function(adapter) {
157
                window.adapter = adapter;
158
            });
159
            require(['atto_recordrtc/socket.io'], function(io) {
160
                window.io = io;
161
            });
162
        }
163
    },
164
165
    /**
166
     * Add the buttons to the Atto toolbar.
167
     *
168
     * @method _addButton
169
     * @param {string} type Type of button to add.
170
     * @param {callback} callback Function to be launched on button click.
171
     * @private
172
     */
173
    _addButton: function(type, callback) {
174
        this.addButton({
175
            buttonName: type,
176
            icon: this.get(type + 'rtcicon'),
177
            iconComponent: PLUGINNAME,
178
            callback: callback,
179
            title: type + 'rtc',
180
            tags: type + 'rtc',
181
            tagMatchRequiresAll: false
182
        });
183
    },
184
185
    /**
186
     * Toggle audiortc and normal display mode
187
     *
188
     * @method _audio
189
     * @private
190
     */
191
    _audio: function() {
192
        var dialogue = this.getDialogue();
193
194
        dialogue.set('height', 400);
195
        dialogue.set('headerContent', M.util.get_string('audiortc', 'atto_recordrtc'));
196
        dialogue.set('bodyContent', this._createContent('audio'));
197
198
        dialogue.show();
199
200
        if (this.get('premiumservice') === '1') {
201
            M.atto_recordrtc.premiumaudiomodule.init(this);
202
        } else {
203
            M.atto_recordrtc.audiomodule.init(this);
204
        }
205
    },
206
207
    /**
208
     * Toggle videortc and normal display mode
209
     *
210
     * @method _video
211
     * @private
212
     */
213
    _video: function() {
214
        var dialogue = this.getDialogue();
215
216
        dialogue.set('height', 850);
217
        dialogue.set('headerContent', M.util.get_string('videortc', 'atto_recordrtc'));
218
        dialogue.set('bodyContent', this._createContent('video'));
219
220
        dialogue.show();
221
222
        if (this.get('premiumservice') === '1') {
223
            M.atto_recordrtc.premiumvideomodule.init(this);
224
        } else {
225
            M.atto_recordrtc.videomodule.init(this);
226
        }
227
    },
228
229
    /**
230
     * Create the HTML to be displayed in the dialogue box
231
     *
232
     * @method _createContent
233
     * @param {string} type Type of recording layout to generate.
234
     * @returns {string} Compiled Handlebars template.
235
     * @private
236
     */
237
    _createContent: function(type) {
238
        var audio = (type === 'audio'),
239
            bsRow = this.get('oldermoodle') ? 'row-fluid' : 'row',
240
            bsCol = this.get('oldermoodle') ? 'span' : 'col-xs-',
241
            bsAlWarn = this.get('oldermoodle') ? '' : 'alert-warning',
242
            bsAlDang = this.get('oldermoodle') ? 'alert-error' : 'alert-danger',
243
            bsSsBtn = this.get('oldermoodle') ? 'btn btn-large btn-danger btn-block'
244
                                              : 'btn btn-lg btn-outline-danger btn-block';
245
246
        var bodyContent = Y.Handlebars.compile(TEMPLATE)({
247
            PLUGINNAME: PLUGINNAME,
248
            audio: audio,
249
            bs_row: bsRow,
250
            bs_col: bsCol,
251
            bs_al_warn: bsAlWarn,
252
            bs_al_dang: bsAlDang,
253
            bs_ss_btn: bsSsBtn,
254
            bs_ul_btn: 'btn btn-primary btn-block',
255
            browseralert_title: M.util.get_string('browseralert_title', 'atto_recordrtc'),
256
            browseralert: M.util.get_string('browseralert', 'atto_recordrtc'),
257
            insecurealert_title: M.util.get_string('insecurealert_title', 'atto_recordrtc'),
258
            insecurealert: M.util.get_string('insecurealert', 'atto_recordrtc'),
259
            startrecording: M.util.get_string('startrecording', 'atto_recordrtc'),
260
            attachrecording: M.util.get_string('attachrecording', 'atto_recordrtc')
261
        });
262
263
        return bodyContent;
264
    },
265
266
    /**
267
     * Close the dialogue without further action.
268
     *
269
     * @method closeDialogue
270
     * @param {object} scope The "this" context of the editor.
271
     */
272
    closeDialogue: function(scope) {
273
        scope.getDialogue().hide();
274
275
        scope.editor.focus();
276
    },
277
278
    /**
279
     * Insert the annotation link in the editor.
280
     *
281
     * @method setLink
282
     * @param {Object} scope The "this" context of the editor.
283
     * @param {String} annotation The HTML link to the recording.
284
     */
285
    setLink: function(scope, annotation) {
286
        scope.getDialogue().hide();
287
288
        scope.editor.focus();
289
        scope.get('host').insertContentAtFocusPoint(annotation);
290
        scope.markUpdated();
291
    }
292
}, {
293
    ATTRS: {
294
        /**
295
         * The contextid to use when generating this recordrtc.
296
         *
297
         * @attribute contextid
298
         * @type String
299
         */
300
        contextid: {
301
            value: null
302
        },
303
304
        /**
305
         * The sesskey to use when generating this recordrtc.
306
         *
307
         * @attribute sesskey
308
         * @type String
309
         */
310
        sesskey: {
311
            value: null
312
        },
313
314
        /**
315
         * The root to use when loading the recordrtc.
316
         *
317
         * @attribute recordrtcroot
318
         * @type String
319
         */
320
        recordrtcroot: {
321
            value: null
322
        },
323
324
        /**
325
         * The allowedtypes to use when generating this recordrtc.
326
         *
327
         * @attribute allowedtypes
328
         * @type String
329
         */
330
        allowedtypes: {
331
            value: null
332
        },
333
334
        /**
335
         * The audiobitrate to use when generating this recordrtc.
336
         *
337
         * @attribute audiobitrate
338
         * @type String
339
         */
340
        audiobitrate: {
341
            value: null
342
        },
343
344
        /**
345
         * The videobitrate to use when generating this recordrtc.
346
         *
347
         * @attribute videobitrate
348
         * @type String
349
         */
350
        videobitrate: {
351
            value: null
352
        },
353
354
        /**
355
         * The timelimit to use when generating this recordrtc.
356
         *
357
         * @attribute timelimit
358
         * @type String
359
         */
360
        timelimit: {
361
            value: null
362
        },
363
364
        /**
365
         * Whether or not to use premium recording service.
366
         *
367
         * @attribute premiumservice
368
         * @type Boolean
369
         */
370
        premiumservice: {
371
            value: null
372
        },
373
374
        /**
375
         * The URL of the premium recording server.
376
         *
377
         * @attribute serverurl
378
         * @type String
379
         */
380
        serverurl: {
381
            value: null
382
        },
383
384
        /**
385
         * The API key for the premium recording service.
386
         *
387
         * @attribute apikey
388
         * @type String
389
         */
390
        apikey: {
391
            value: null
392
        },
393
394
        /**
395
         * The API shared secret for the premium recording service.
396
         *
397
         * @attribute apisecret
398
         * @type String
399
         */
400
        apisecret: {
401
            value: null
402
        },
403
404
        /**
405
         * The audiortcicon to use when generating this recordrtc.
406
         *
407
         * @attribute audiortcicon
408
         * @type String
409
         */
410
        audiortcicon: {
411
            value: null
412
        },
413
414
        /**
415
         * The videortcicon to use when generating this recordrtc.
416
         *
417
         * @attribute videortcicon
418
         * @type String
419
         */
420
        videortcicon: {
421
            value: null
422
        },
423
424
        /**
425
         * True if Moodle is version < 3.2.
426
         *
427
         * @attribute oldermoodle
428
         * @type Boolean
429
         */
430
        oldermoodle: {
431
            value: null
432
        },
433
434
        /**
435
         * Maximum upload size set on server, in MB.
436
         *
437
         * @attribute maxrecsize
438
         * @type String
439
         */
440
        maxrecsize: {
441
            value: null
442
        }
443
    }
444
});
445