Completed
Push — master ( 46959c...a001d3 )
by Jacob
01:35
created

tinymce/js/commonmodule.js   A

Complexity

Total Complexity 18
Complexity/F 2

Size

Lines of Code 105
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 13
Bugs 1 Features 0
Metric Value
cc 0
c 13
b 1
f 0
nc 176
dl 0
loc 105
rs 10
wmc 18
mnd 3
bc 18
fnc 9
bpm 2
cpm 2
noi 0

8 Functions

Rating   Name   Duplication   Size   Complexity  
B 0 21 5
A commonmodule.js ➔ d 0 3 1
A M.tinymce_recordrtc.insert_annotation 0 12 2
A M.tinymce_recordrtc.check_browser 0 7 2
A M.tinymce_recordrtc.show_alert 0 12 1
A M.tinymce_recordrtc.capture_user_media 0 3 1
A M.tinymce_recordrtc.create_annotation 0 12 2
A M.tinymce_recordrtc.check_secure 0 8 2
1
// TinyMCE recordrtc library functions.
2
// @package    tinymce_recordrtc.
3
// @author     Jesus Federico  (jesus [at] blindsidenetworks [dt] com).
4
// @copyright  2016 to present, Blindside Networks Inc.
5
// @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
6
7
// Scrutinizer CI directives.
8
/** global: M */
9
/** global: Y */
10
/** global: tinyMCEPopup */
11
12
M.tinymce_recordrtc = M.tinymce_recordrtc || {};
13
14
// Extract plugin settings to params hash.
15
(function() {
16
    var params = {};
17
    var r = /([^&=]+)=?([^&]*)/g;
18
19
    var d = function(s) {
20
        return window.decodeURIComponent(s.replace(/\+/g, ' '));
21
    };
22
23
    var search = window.location.search;
24
    var match = r.exec(search.substring(1));
25
    while (match) {
26
        params[d(match[1])] = d(match[2]);
27
28
        if (d(match[2]) === 'true' || d(match[2]) === 'false') {
29
            params[d(match[1])] = d(match[2]) === 'true' ? true : false;
30
        }
31
        match = r.exec(search.substring(1));
32
    }
33
34
    window.params = params;
35
})();
36
37
// Initialize some variables.
38
var alertWarning = null;
39
var alertDanger = null;
40
var mediaRecorder = null;
41
var player = null;
42
var playerDOM = null;
43
var recType = null;
44
var startStopBtn = null;
45
var uploadBtn = null;
46
47
// A helper for making a Moodle alert appear.
48
// Subject is the content of the alert (which error ther alert is for).
49
// Possibility to add on-alert-close event.
50
M.tinymce_recordrtc.show_alert = function(subject, onCloseEvent) {
51
    Y.use('moodle-core-notification-alert', function() {
52
        var dialogue = new M.core.alert({
53
            title: M.util.get_string(subject + '_title', 'tinymce_recordrtc'),
54
            message: M.util.get_string(subject, 'tinymce_recordrtc')
55
        });
56
57
        if (onCloseEvent) {
58
            dialogue.after('complete', onCloseEvent);
59
        }
60
    });
61
};
62
63
// Notify and redirect user if plugin is used from insecure location.
64
M.tinymce_recordrtc.check_secure = function() {
65
    var isSecureOrigin = (window.location.protocol === 'https:') ||
66
                         (window.location.host.indexOf('localhost') !== -1);
67
68
    if (!isSecureOrigin) {
69
        alertDanger.ancestor().ancestor().removeClass('hide');
70
    }
71
};
72
73
// Display "consider switching browsers" message if not using:
74
// - Firefox 29+;
75
// - Chrome 49+;
76
// - Opera 36+.
77
M.tinymce_recordrtc.check_browser = function() {
78
    if (!((window.bowser.firefox && window.bowser.version >= 29) ||
79
          (window.bowser.chrome && window.bowser.version >= 49) ||
80
          (window.bowser.opera && window.bowser.version >= 36))) {
81
        alertWarning.ancestor().ancestor().removeClass('hide');
82
    }
83
};
84
85
// Capture webcam/microphone stream.
86
M.tinymce_recordrtc.capture_user_media = function(mediaConstraints, successCallback, errorCallback) {
87
    window.navigator.mediaDevices.getUserMedia(mediaConstraints).then(successCallback).catch(errorCallback);
88
};
89
90
// Generates link to recorded annotation to be inserted.
91
M.tinymce_recordrtc.create_annotation = function(type, recording_url) {
92
    var linkText = window.prompt(M.util.get_string('annotationprompt', 'tinymce_recordrtc'),
93
                                 M.util.get_string('annotation:' + type, 'tinymce_recordrtc'));
94
95
    // Return HTML for annotation link, if user did not press "Cancel".
96
    if (!linkText) {
97
        return undefined;
98
    } else {
99
        var annotation = '<div><a target="_blank" href="' + recording_url + '">' + linkText + '</a></div>';
100
        return annotation;
101
    }
102
};
103
104
// Inserts link to annotation in editor text area.
105
M.tinymce_recordrtc.insert_annotation = function(type, recording_url) {
106
    var annotation = M.tinymce_recordrtc.create_annotation(type, recording_url);
107
108
    // Insert annotation link.
109
    // If user pressed "Cancel", just go back to main recording screen.
110
    if (!annotation) {
111
        uploadBtn.set('textContent', M.util.get_string('attachrecording', 'tinymce_recordrtc'));
112
    } else {
113
        tinyMCEPopup.editor.execCommand('mceInsertContent', false, annotation);
114
        tinyMCEPopup.close();
115
    }
116
};
117