Passed
Push — master ( 8be289...5c19e6 )
by Jacob
01:49
created

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

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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