Passed
Push — master ( 90e07f...5b48b2 )
by Jacob
02:00
created

yui/src/recordingcommon/js/commonmodule.js   A

Complexity

Total Complexity 12
Complexity/F 1.71

Size

Lines of Code 89
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
c 2
b 0
f 0
nc 44
dl 0
loc 89
rs 10
wmc 12
mnd 1
bc 14
fnc 7
bpm 2
cpm 1.7142
noi 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A M.atto_recordrtc.commonmodule.insert_annotation 0 11 2
A M.atto_recordrtc.commonmodule.check_secure 0 8 2
A M.atto_recordrtc.commonmodule.show_alert 0 12 1
A M.atto_recordrtc.commonmodule.capture_user_media 0 3 1
A M.atto_recordrtc.commonmodule.check_browser 0 7 2
A M.atto_recordrtc.commonmodule.create_annotation 0 12 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
 * 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
// JSHint directives.
28
/*jshint es5: true */
29
/*jshint onevar: false */
30
/*jshint shadow: true */
31
/*global M */
32
33
// Scrutinizer CI directives.
34
/** global: M */
35
/** global: Y */
36
37
M.atto_recordrtc = M.atto_recordrtc || {};
38
39
// Shorten access to module namespaces.
40
var cm = M.atto_recordrtc.commonmodule;
41
42
M.atto_recordrtc.commonmodule = {
43
    // Unitialized variables to be used by the other modules.
44
    editorScope: null,
45
    alertWarning: null,
46
    alertDanger: null,
47
    player: null,
48
    playerDOM: null, // Used to manipulate DOM directly.
49
    startStopBtn: null,
50
    uploadBtn: null,
51
    recType: null,
52
    stream: null,
53
    mediaRecorder: null,
54
    olderMoodle: null,
55
56
    // A helper for making a Moodle alert appear.
57
    // Subject is the content of the alert (which error ther alert is for).
58
    // Possibility to add on-alert-close event.
59
    show_alert: function(subject, onCloseEvent) {
60
        Y.use('moodle-core-notification-alert', function() {
61
            var dialogue = new M.core.alert({
62
                title: M.util.get_string(subject + '_title', 'atto_recordrtc'),
63
                message: M.util.get_string(subject, 'atto_recordrtc')
64
            });
65
66
            if (onCloseEvent) {
67
                dialogue.after('complete', onCloseEvent);
68
            }
69
        });
70
    },
71
72
    // Notify and redirect user if plugin is used from insecure location.
73
    check_secure: function() {
74
        var isSecureOrigin = (window.location.protocol === 'https:') ||
75
                             (window.location.host.indexOf('localhost') !== -1);
76
77
        if (!isSecureOrigin) {
78
            cm.alertDanger.ancestor().ancestor().removeClass('hide');
79
        }
80
    },
81
82
    // Display "consider switching browsers" message if not using:
83
    // - Firefox 29+;
84
    // - Chrome 49+;
85
    // - Opera 36+.
86
    check_browser: function() {
87
        if (!((window.bowser.firefox && window.bowser.version >= 29) ||
88
              (window.bowser.chrome && window.bowser.version >= 49) ||
89
              (window.bowser.opera && window.bowser.version >= 36))) {
90
            cm.alertWarning.ancestor().ancestor().removeClass('hide');
91
        }
92
    },
93
94
    // Capture webcam/microphone stream.
95
    capture_user_media: function(mediaConstraints, successCallback, errorCallback) {
96
        window.navigator.mediaDevices.getUserMedia(mediaConstraints).then(successCallback).catch(errorCallback);
97
    },
98
99
    // Generates link to recorded annotation to be inserted.
100
    create_annotation: function(type, recording_url) {
101
        var linkText = window.prompt(M.util.get_string('annotationprompt', 'atto_recordrtc'),
102
                                     M.util.get_string('annotation:' + type, 'atto_recordrtc'));
103
104
        // Return HTML for annotation link, if user did not press "Cancel".
105
        if (!linkText) {
106
            return undefined;
107
        } else {
108
            var annotation = '<a target="_blank" href="' + recording_url + '">' + linkText + '</a>';
109
            return annotation;
110
        }
111
    },
112
113
    // Inserts link to annotation in editor text area.
114
    insert_annotation: function(type, recording_url) {
115
        var annotation = cm.create_annotation(type, recording_url);
116
117
        // Insert annotation link.
118
        // If user pressed "Cancel", just go back to main recording screen.
119
        if (!annotation) {
120
            cm.uploadBtn.set('textContent', M.util.get_string('attachrecording', 'atto_recordrtc'));
121
        } else {
122
            cm.editorScope.setLink(cm.editorScope, annotation);
123
        }
124
    }
125
};
126