Passed
Push — master ( 82426c...2baee7 )
by Jacob
01:53
created

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

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
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
                m = premium ? M.atto_recordrtc.premiumcommonmodule : M.atto_recordrtc.commonmodule;
122
123
            if (closed) {
124
                if (premium) {
125
                    // Disconnect the socket.
126
                    m.socket.disconnect(true);
127
                } else {
128
                    // Clear the countdown timer.
129
                    window.clearInterval(m.countdownTicker);
130
                }
131
132
                if (m.mediaRecorder && m.mediaRecorder.state !== 'inactive') {
133
                    m.mediaRecorder.stop();
134
                }
135
136
                if (m.stream) {
137
                    m.stream.getTracks().forEach(function(track) {
138
                        if (track.readyState !== 'ended') {
139
                            track.stop();
140
                        }
141
                    });
142
                }
143
            }
144
        });
145
146
        // Require Bowser, adapter.js and Socket.io libraries.
147
        require(['atto_recordrtc/bowser'], function(bowser) {
148
            window.bowser = bowser;
149
        });
150
        require(['atto_recordrtc/adapter'], function(adapter) {
151
            window.adapter = adapter;
152
        });
153
        require(['atto_recordrtc/socket.io'], function(io) {
154
            window.io = io;
155
        });
156
    },
157
158
    /**
159
     * Add the buttons to the Atto toolbar.
160
     *
161
     * @method _addButton
162
     * @private
163
     */
164
    _addButton: function(type, callback) {
165
        this.addButton({
166
            buttonName: type,
167
            icon: this.get(type + 'rtcicon'),
168
            iconComponent: PLUGINNAME,
169
            callback: callback,
170
            title: type + 'rtc',
171
            tags: type + 'rtc',
172
            tagMatchRequiresAll: false
173
        });
174
    },
175
176
    /**
177
     * Toggle audiortc and normal display mode
178
     *
179
     * @method _audio
180
     * @private
181
     */
182
    _audio: function() {
183
        var dialogue = this.getDialogue();
184
185
        dialogue.set('height', 260);
186
        dialogue.set('headerContent', M.util.get_string('audiortc', 'atto_recordrtc'));
187
        dialogue.set('bodyContent', this._createContent('audio'));
188
189
        dialogue.show();
190
191
        if (this.get('premiumservice') === '1') {
192
            M.atto_recordrtc.premiumaudiomodule.init(this);
193
        } else {
194
            M.atto_recordrtc.audiomodule.init(this);
195
        }
196
    },
197
198
    /**
199
     * Toggle videortc and normal display mode
200
     *
201
     * @method _video
202
     * @private
203
     */
204
    _video: function() {
205
        var dialogue = this.getDialogue();
206
207
        dialogue.set('height', 700);
208
        dialogue.set('headerContent', M.util.get_string('videortc', 'atto_recordrtc'));
209
        dialogue.set('bodyContent', this._createContent('video'));
210
211
        dialogue.show();
212
213
        if (this.get('premiumservice') === '1') {
214
            M.atto_recordrtc.premiumvideomodule.init(this);
215
        } else {
216
            M.atto_recordrtc.videomodule.init(this);
217
        }
218
    },
219
220
    /**
221
     * Create the HTML to be displayed in the dialogue box
222
     *
223
     * @method _createContent
224
     * @private
225
     */
226
    _createContent: function(type) {
227
        var audio = (type === 'audio'),
228
            bsRow = this.get('oldermoodle') ? 'row-fluid' : 'row',
229
            bsCol = this.get('oldermoodle') ? 'span' : 'col-xs-',
230
            bsAlWarn = this.get('oldermoodle') ? '' : 'alert-warning',
231
            bsAlDang = this.get('oldermoodle') ? 'alert-error' : 'alert-danger',
232
            bsSsBtn = this.get('oldermoodle') ? 'btn btn-large btn-danger btn-block'
233
                                              : 'btn btn-lg btn-outline-danger btn-block';
234
235
        var bodyContent = Y.Handlebars.compile(TEMPLATE)({
236
            PLUGINNAME: PLUGINNAME,
237
            audio: audio,
238
            bs_row: bsRow,
239
            bs_col: bsCol,
240
            bs_al_warn: bsAlWarn,
241
            bs_al_dang: bsAlDang,
242
            bs_ss_btn: bsSsBtn,
243
            bs_ul_btn: 'btn btn-primary btn-block',
244
            browseralert_title: M.util.get_string('browseralert_title', 'atto_recordrtc'),
245
            browseralert: M.util.get_string('browseralert', 'atto_recordrtc'),
246
            insecurealert_title: M.util.get_string('insecurealert_title', 'atto_recordrtc'),
247
            insecurealert: M.util.get_string('insecurealert', 'atto_recordrtc'),
248
            startrecording: M.util.get_string('startrecording', 'atto_recordrtc'),
249
            attachrecording: M.util.get_string('attachrecording', 'atto_recordrtc')
250
        });
251
252
        return bodyContent;
253
    },
254
255
    /**
256
     * Close the dialogue without further action.
257
     *
258
     * @method closeDialogue
259
     * @param {Object} scope The "this" context of the editor.
260
     */
261
    closeDialogue: function(scope) {
262
        scope.getDialogue().hide();
263
264
        scope.editor.focus();
265
    },
266
267
    /**
268
     * Insert the annotation link in the editor.
269
     *
270
     * @method setLink
271
     * @param {Object} scope The "this" context of the editor.
272
     * @param {String} annotation The HTML link to the recording.
273
     */
274
    setLink: function(scope, annotation) {
275
        scope.getDialogue().hide();
276
277
        scope.editor.focus();
278
        scope.get('host').insertContentAtFocusPoint(annotation);
279
        scope.markUpdated();
280
    }
281
}, {
282
    ATTRS: {
283
        /**
284
         * The contextid to use when generating this recordrtc.
285
         *
286
         * @attribute contextid
287
         * @type String
288
         */
289
        contextid: {
290
            value: null
291
        },
292
293
        /**
294
         * The sesskey to use when generating this recordrtc.
295
         *
296
         * @attribute sesskey
297
         * @type String
298
         */
299
        sesskey: {
300
            value: null
301
        },
302
303
        /**
304
         * The root to use when loading the recordrtc.
305
         *
306
         * @attribute recordrtcroot
307
         * @type String
308
         */
309
        recordrtcroot: {
310
            value: null
311
        },
312
313
        /**
314
         * The allowedtypes to use when generating this recordrtc.
315
         *
316
         * @attribute allowedtypes
317
         * @type String
318
         */
319
        allowedtypes: {
320
            value: null
321
        },
322
323
        /**
324
         * The audiobitrate to use when generating this recordrtc.
325
         *
326
         * @attribute audiobitrate
327
         * @type String
328
         */
329
        audiobitrate: {
330
            value: null
331
        },
332
333
        /**
334
         * The videobitrate to use when generating this recordrtc.
335
         *
336
         * @attribute videobitrate
337
         * @type String
338
         */
339
        videobitrate: {
340
            value: null
341
        },
342
343
        /**
344
         * The timelimit to use when generating this recordrtc.
345
         *
346
         * @attribute timelimit
347
         * @type String
348
         */
349
        timelimit: {
350
            value: null
351
        },
352
353
        /**
354
         * Whether or not to use premium recording service.
355
         *
356
         * @attribute premiumservice
357
         * @type Boolean
358
         */
359
        premiumservice: {
360
            value: null
361
        },
362
363
        /**
364
         * The URL of the premium recording server.
365
         *
366
         * @attribute serverurl
367
         * @type String
368
         */
369
        serverurl: {
370
            value: null
371
        },
372
373
        /**
374
         * The API key for the premium recording service.
375
         *
376
         * @attribute apikey
377
         * @type String
378
         */
379
        apikey: {
380
            value: null
381
        },
382
383
        /**
384
         * The API shared secret for the premium recording service.
385
         *
386
         * @attribute apisecret
387
         * @type String
388
         */
389
        apisecret: {
390
            value: null
391
        },
392
393
        /**
394
         * The audiortcicon to use when generating this recordrtc.
395
         *
396
         * @attribute audiortcicon
397
         * @type String
398
         */
399
        audiortcicon: {
400
            value: null
401
        },
402
403
        /**
404
         * The videortcicon to use when generating this recordrtc.
405
         *
406
         * @attribute videortcicon
407
         * @type String
408
         */
409
        videortcicon: {
410
            value: null
411
        },
412
413
        /**
414
         * True if Moodle is version < 3.2.
415
         *
416
         * @attribute oldermoodle
417
         * @type Boolean
418
         */
419
        oldermoodle: {
420
            value: null
421
        },
422
423
        /**
424
         * Maximum upload size set on server, in MB.
425
         *
426
         * @attribute maxrecsize
427
         * @type String
428
         */
429
        maxrecsize: {
430
            value: null
431
        }
432
    }
433
});
434