Passed
Push — master ( 98d90b...6d24a2 )
by Jacob
01:27
created

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

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 20
rs 9.2
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
 * @package    atto_recordrtc
19
 * @author     Jesus Federico (jesus [at] blindsidenetworks [dt] com)
20
 * @copyright  2017 Blindside Networks Inc.
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
24
/**
25
 * @module moodle-atto_recordrtc-button
26
 */
27
28
/**
29
 * Atto text editor recordrtc plugin.
30
 *
31
 * @namespace M.atto_recordrtc
32
 * @class button
33
 * @extends M.editor_atto.EditorPlugin
34
 */
35
36
var PLUGINNAME = 'atto_recordrtc',
37
    TEMPLATE = '\
38
    <div class="{{PLUGINNAME}} container-fluid">\
39
      <div class="{{bs_row}} hide">\
40
        <div class="{{bs_col}}12">\
41
          <div id="alert-warning" class="alert {{bs_al_warn}}">\
42
            <strong>{{browseralert_title}}</strong> {{browseralert}}\
43
          </div>\
44
        </div>\
45
      </div>\
46
      <div class="{{bs_row}} hide">\
47
        <div class="{{bs_col}}12">\
48
          <div id="alert-danger" class="alert {{bs_al_dang}}"></div>\
49
        </div>\
50
      </div>\
51
      <div class="{{bs_row}} hide">\
52
        {{#if audio}}\
53
          <div class="{{bs_col}}1"></div>\
54
          <div class="{{bs_col}}10">\
55
            <audio id="player"></audio>\
56
          </div>\
57
          <div class="{{bs_col}}1"></div>\
58
          {{else}}\
59
          <div class="{{bs_col}}12">\
60
            <video id="player"></video>\
61
          </div>\
62
        {{/if}}\
63
      </div>\
64
      <div class="{{bs_row}}">\
65
        <div class="{{bs_col}}1"></div>\
66
        <div class="{{bs_col}}10">\
67
          <button id="start-stop" class="{{bs_ss_btn}}">{{startrecording}}</button>\
68
        </div>\
69
        <div class="{{bs_col}}1"></div>\
70
      </div>\
71
      <div class="{{bs_row}} hide">\
72
        <div class="{{bs_col}}3"></div>\
73
        <div class="{{bs_col}}6">\
74
          <button id="upload" class="btn btn-primary btn-block">{{attachrecording}}</button>\
75
        </div>\
76
        <div class="{{bs_col}}3"></div>\
77
      </div>\
78
    </div>';
79
80
Y.namespace('M.atto_recordrtc').Button = Y.Base.create('button', Y.M.editor_atto.EditorPlugin, [], {
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...
81
    /**
82
     * The current language by default.
83
     */
84
    _lang: 'en',
85
86
    initializer: function() {
87
        // Add audio and/or video buttons depending on the settings.
88
        var allowedtypes = this.get('allowedtypes');
89
        if (allowedtypes === 'both' || allowedtypes === 'audio') {
90
            this._addButton('audio', this._audio);
91
        }
92
        if (allowedtypes === 'both' || allowedtypes === 'video') {
93
            this._addButton('video', this._video);
94
        }
95
96
        // Initialize the dialogue box.
97
        var dialogue = this.getDialogue({
98
            width: 1000,
99
            focusAfterHide: null
100
        });
101
102
        // If dialogue is closed during recording, do the following.
103
        dialogue.on('visibleChange', function() {
104
            // Clear the countdown timer.
105
            clearInterval(M.atto_recordrtc.commonmodule.countdownTicker);
0 ignored issues
show
Bug introduced by
The variable M seems to be never declared. If this is a global, consider adding a /** global: M */ 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...
106
107
            // Stop the media recorder.
108
            if (M.atto_recordrtc.commonmodule.mediaRecorder && M.atto_recordrtc.commonmodule.mediaRecorder.state !== 'inactive') {
109
                console.log('MediaRecorder stopped:', M.atto_recordrtc.commonmodule.mediaRecorder);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
110
                M.atto_recordrtc.commonmodule.mediaRecorder.stop();
111
            }
112
113
            // Stop the getUserMedia audio/video tracks.
114
            if (M.atto_recordrtc.commonmodule.stream) {
115
                M.atto_recordrtc.commonmodule.stream.getTracks().forEach(function(track) {
116
                    if (track.readyState !== 'ended') {
117
                        console.log('MediaTrack stopped:', track);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
118
                        track.stop();
119
                    }
120
                });
121
            }
122
        });
123
    },
124
125
    /**
126
     * Add the buttons to the Atto toolbar.
127
     *
128
     * @method _addButton
129
     * @private
130
     */
131
    _addButton: function(type, callback) {
132
        this.addButton({
133
            buttonName: type,
134
            icon: this.get(type + 'rtcicon'),
135
            iconComponent: PLUGINNAME,
136
            callback: callback,
137
            title: type + 'rtc',
138
            tags: type + 'rtc',
139
            tagMatchRequiresAll: false
140
        });
141
    },
142
143
    /**
144
     * Toggle audiortc and normal display mode
145
     *
146
     * @method _audio
147
     * @private
148
     */
149
    _audio: function() {
150
        var dialogue = this.getDialogue();
151
152
        dialogue.set('height', 260);
153
        dialogue.set('headerContent', M.util.get_string('audiortc', 'atto_recordrtc'));
0 ignored issues
show
Bug introduced by
The variable M seems to be never declared. If this is a global, consider adding a /** global: M */ 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...
154
        dialogue.set('bodyContent', this._createContent('audio'));
155
156
        dialogue.show();
157
158
        M.atto_recordrtc.audiomodule.init(this);
159
    },
160
161
    /**
162
     * Toggle videortc and normal display mode
163
     *
164
     * @method _video
165
     * @private
166
     */
167
    _video: function() {
168
        var dialogue = this.getDialogue();
169
170
        dialogue.set('height', 700);
171
        dialogue.set('headerContent', M.util.get_string('videortc', 'atto_recordrtc'));
0 ignored issues
show
Bug introduced by
The variable M seems to be never declared. If this is a global, consider adding a /** global: M */ 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...
172
        dialogue.set('bodyContent', this._createContent('video'));
173
174
        dialogue.show();
175
176
        M.atto_recordrtc.videomodule.init(this);
177
    },
178
179
    /**
180
     * Create the HTML to be displayed in the dialogue box
181
     *
182
     * @method _createContent
183
     * @private
184
     */
185
    _createContent: function(type) {
186
        var audio = (type === 'audio'),
187
            bsRow = this.get('oldermoodle') ? 'row-fluid' : 'row',
188
            bsCol = this.get('oldermoodle') ? 'span' : 'col-xs-',
189
            bsAlWarn = this.get('oldermoodle') ? '' : 'alert-warning',
190
            bsAlDang = this.get('oldermoodle') ? 'alert-error' : 'alert-danger',
191
            bsSsBtn = this.get('oldermoodle') ? 'btn btn-large btn-danger btn-block'
192
                                              : 'btn btn-lg btn-outline-danger btn-block';
193
194
        var bodyContent = Y.Handlebars.compile(TEMPLATE)({
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...
195
            PLUGINNAME: PLUGINNAME,
196
            audio: audio,
197
            bs_row: bsRow,
198
            bs_col: bsCol,
199
            bs_al_warn: bsAlWarn,
200
            bs_al_dang: bsAlDang,
201
            bs_ss_btn: bsSsBtn,
202
            bs_ul_btn: 'btn btn-primary btn-block',
203
            browseralert_title: M.util.get_string('browseralert_title', 'atto_recordrtc'),
0 ignored issues
show
Bug introduced by
The variable M seems to be never declared. If this is a global, consider adding a /** global: M */ 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...
204
            browseralert: M.util.get_string('browseralert', 'atto_recordrtc'),
205
            startrecording: M.util.get_string('startrecording', 'atto_recordrtc'),
206
            attachrecording: M.util.get_string('attachrecording', 'atto_recordrtc')
207
        });
208
209
        return bodyContent;
210
    },
211
212
    /**
213
     * Close the dialogue without further action.
214
     *
215
     * @method closeDialogue
216
     * @param {Object} scope The "this" context of the editor.
217
     */
218
    closeDialogue: function(scope) {
219
        scope.getDialogue().hide();
220
221
        scope.editor.focus();
222
    },
223
224
    /**
225
     * Insert the annotation link in the editor.
226
     *
227
     * @method setLink
228
     * @param {Object} scope The "this" context of the editor.
229
     * @param {String} annotation The HTML link to the recording.
230
     */
231
    setLink: function(scope, annotation) {
232
        scope.getDialogue().hide();
233
234
        scope.editor.focus();
235
        scope.get('host').insertContentAtFocusPoint(annotation);
236
        scope.markUpdated();
237
    }
238
}, {
239
    ATTRS: {
240
        /**
241
         * The contextid to use when generating this recordrtc.
242
         *
243
         * @attribute contextid
244
         * @type String
245
         */
246
        contextid: {
247
            value: null
248
        },
249
250
        /**
251
         * The sesskey to use when generating this recordrtc.
252
         *
253
         * @attribute sesskey
254
         * @type String
255
         */
256
        sesskey: {
257
            value: null
258
        },
259
260
        /**
261
         * The root to use when loading the recordrtc.
262
         *
263
         * @attribute recordrtcroot
264
         * @type String
265
         */
266
        recordrtcroot: {
267
            value: null
268
        },
269
270
        /**
271
         * The allowedtypes to use when generating this recordrtc.
272
         *
273
         * @attribute allowedtypes
274
         * @type String
275
         */
276
        allowedtypes: {
277
            value: null
278
        },
279
280
        /**
281
         * The audiobitrate to use when generating this recordrtc.
282
         *
283
         * @attribute audiobitrate
284
         * @type String
285
         */
286
        audiobitrate: {
287
            value: null
288
        },
289
290
        /**
291
         * The videobitrate to use when generating this recordrtc.
292
         *
293
         * @attribute videobitrate
294
         * @type String
295
         */
296
        videobitrate: {
297
            value: null
298
        },
299
300
        /**
301
         * The timelimit to use when generating this recordrtc.
302
         *
303
         * @attribute timelimit
304
         * @type String
305
         */
306
        timelimit: {
307
            value: null
308
        },
309
310
        /**
311
         * The audiortcicon to use when generating this recordrtc.
312
         *
313
         * @attribute audiortcicon
314
         * @type String
315
         */
316
        audiortcicon: {
317
            value: null
318
        },
319
320
        /**
321
         * The videortcicon to use when generating this recordrtc.
322
         *
323
         * @attribute videortcicon
324
         * @type String
325
         */
326
        videortcicon: {
327
            value: null
328
        },
329
330
        /**
331
         * True if Moodle is version < 3.2.
332
         *
333
         * @attribute oldermoodle
334
         * @type Boolean
335
         */
336
        oldermoodle: {
337
            value: null
338
        },
339
340
        /**
341
         * Maximum upload size set on server, in MB.
342
         *
343
         * @attribute maxrecsize
344
         * @type String
345
         */
346
        maxrecsize: {
347
            value: null
348
        }
349
    }
350
});
351