Passed
Push — master ( 090fdb...04525a )
by Jacob
01:45
created

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

Complexity

Conditions 5
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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