Completed
Branch pre-recording-server (439213)
by Jacob
01:39
created

editor_plugin.js ➔ addVideo   A

Complexity

Conditions 1
Paths 16

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
c 1
b 1
f 0
nc 16
nop 2
dl 0
loc 47
rs 9.0303

1 Function

Rating   Name   Duplication   Size   Complexity  
B editor_plugin.js ➔ ... ➔ ed.addCommand(ꞌmceVideoRTCꞌ) 0 38 5
1
/**
2
 * TinyMCE plugin RecordRTC - provides UI to annotate the content in the text editor with a WebRTC recording.
3
 *
4
 * @package    tinymce_recordrtc
5
 * @author     Jesus Federico  (jesus [at] blindsidenetworks [dt] com)
6
 * @author     Jacob Prud'homme (jacob [dt] prudhomme [at] blindsidenetworks [dt] com)
7
 * @copyright  2016 onwards, Blindside Networks Inc.
8
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
9
 */
10
11
// ESLint directives.
12
/* global tinyMCE */
13
/* eslint-disable camelcase */
14
15
// Scrutinizer CI directives.
16
/** global: M */
17
/** global: tinyMCE */
18
19
function addForceRepaint(ed) {
20
    ed.addCommand('mceForceRepaint', function() {
21
        var root = ed.dom.getRoot(),
22
            items = root.getElementsByTagName("img");
23
        for (var i = 0; i < items.length; i++) {
24
            var src = items[i].getAttribute('src').replace(/\?\d+$/, '');
25
            items[i].setAttribute('src', src + '?' + (new Date().getTime()));
26
        }
27
        ed.execCommand('mceRepaint');
28
        ed.focus();
29
    });
30
}
31
32
function addMaximizeWindow(ed) {
33
    ed.addCommand('mceMaximizeWindow', function(w) {
34
        // This function duplicates the TinyMCE windowManager code when 'maximize' button is pressed.
35
        var vp = ed.dom.getViewPort(),
36
            id = w.id;
37
        // Reduce viewport size to avoid scrollbars.
38
        vp.w -= 2;
39
        vp.h -= 2;
40
41
        w.oldPos = w.element.getXY();
42
        w.oldSize = w.element.getSize();
43
44
        w.element.moveTo(vp.x, vp.y);
45
        w.element.resizeTo(vp.w, vp.h);
46
        ed.dom.setStyles(id + '_ifr', {width: vp.w - w.deltaWidth, height: vp.h - w.deltaHeight});
47
        ed.dom.addClass(id + '_wrapper', 'mceMaximized');
48
    });
49
}
50
51
function addAudio(ed, url) {
52
    ed.addCommand('mceAudioRTC', function() {
53
        var audiortc = ed.getParam('audiortc', {});
54
        var viewparams = '';
55
        window.Object.keys(audiortc).forEach(function(key) {
56
            viewparams += (viewparams !== '' ? '&' : '') + window.encodeURIComponent(key);
57
            viewparams += '=' + window.encodeURIComponent(audiortc[key]);
58
        });
59
        var viewurl = ed.getParam("moodle_plugin_base") + 'recordrtc/audiortc.php';
60
        viewurl += (viewparams != '' ? '?' + viewparams : '');
61
62
        var onClose = function() {
63
            ed.windowManager.onClose.remove(onClose);
64
            ed.execCommand('mceForceRepaint');
65
        };
66
        ed.windowManager.onClose.add(onClose);
67
        var vp = ed.dom.getViewPort(),
68
            baseWidth = 640 + window.parseInt(ed.getLang('advimage.delta_width', 0)),
69
            percentOfViewportWidth = vp.w * 0.75,
70
            width = percentOfViewportWidth > baseWidth ? percentOfViewportWidth : baseWidth,
71
            height = 340 + window.parseInt(ed.getLang('advimage.delta_width', 0)),
72
            maximizedmode = (width >= vp.w - 2 || height >= vp.h - 2);
73
        if (maximizedmode) {
74
            width = vp.w;
75
            height = vp.h;
76
        }
77
        var w = ed.windowManager.open({
78
            file: viewurl,
79
            width: width,
80
            height: height,
81
            inline: 1,
82
            popup_css: ed.getParam("moodle_plugin_base") + 'recordrtc/tinymce/css/popup.css'
83
        }, {
84
            plugin_url: url // Plugin absolute URL.
85
        });
86
        if (maximizedmode) {
87
            ed.execCommand('mceMaximizeWindow', w);
88
        }
89
    });
90
91
    // Register AudioRTC button.
92
    ed.addButton('audiortc', {
93
        title: 'recordrtc.audiortc',
94
        cmd: 'mceAudioRTC',
95
        image: url + '/img/audiortc.png'
96
    });
97
}
98
99
function addVideo(ed, url) {
100
    ed.addCommand('mceVideoRTC', function() {
101
        var videortc = ed.getParam('videortc', {});
102
        var viewparams = '';
103
        window.Object.keys(videortc).forEach(function(key) {
104
            viewparams += (viewparams !== '' ? '&' : '') + window.encodeURIComponent(key);
105
            viewparams += '=' + window.encodeURIComponent(videortc[key]);
106
        });
107
        var viewurl = ed.getParam("moodle_plugin_base") + 'recordrtc/videortc.php';
108
        viewurl += (viewparams != '' ? '?' + viewparams : '');
109
110
        var onClose = function() {
111
            ed.windowManager.onClose.remove(onClose);
112
            ed.execCommand('mceForceRepaint');
113
        };
114
        ed.windowManager.onClose.add(onClose);
115
        var vp = ed.dom.getViewPort(),
116
            baseWidth = 720 + window.parseInt(ed.getLang('advimage.delta_width', 0)),
117
            percentOfViewportWidth = vp.w * 0.75,
118
            width = percentOfViewportWidth > baseWidth ? percentOfViewportWidth : baseWidth,
119
            height = 780 + window.parseInt(ed.getLang('advimage.delta_width', 0)),
120
            maximizedmode = (width >= vp.w - 2 || height >= vp.h - 2);
121
        if (maximizedmode) {
122
            width = vp.w;
123
            height = vp.h;
124
        }
125
        var w = ed.windowManager.open({
126
            file: viewurl,
127
            width: width,
128
            height: height,
129
            inline: 1,
130
            popup_css: ed.getParam("moodle_plugin_base") + 'recordrtc/tinymce/css/popup.css'
131
        }, {
132
            plugin_url: url // Plugin absolute URL.
133
        });
134
        if (maximizedmode) {
135
            ed.execCommand('mceMaximizeWindow', w);
136
        }
137
    });
138
139
    // Register VideoRTC button.
140
    ed.addButton('videortc', {
141
        title: 'recordrtc.videortc',
142
        cmd: 'mceVideoRTC',
143
        image: url + '/img/videortc.png'
144
    });
145
}
146
147
(function() {
148
    tinyMCE.PluginManager.requireLangPack('recordrtc');
149
150
    tinyMCE.create('tinymce.plugins.RecordRTC', {
151
        /**
152
         * Initializes the plugin, this will be executed after the plugin has been created.
153
         * This call is done before the editor instance has finished it's initialization so use the onInit event
154
         * of the editor instance to intercept that event.
155
         *
156
         * @param {tinyMCE.Editor} ed Editor instance that the plugin is initialized in.
157
         * @param {string} url Absolute URL to where the plugin is located.
158
         */
159
        init: function(ed, url) {
160
            // Add commands to the editor.
161
            addForceRepaint(ed);
162
            addMaximizeWindow(ed);
163
164
            if (M.editor_tinymce.filepicker_options[ed.id] &&
165
                M.editor_tinymce.filepicker_options[ed.id].hasOwnProperty('media')) {
166
                addAudio(ed, url);
167
                addVideo(ed, url);
168
            }
169
        },
170
171
        createControl: function() {
172
            return null;
173
        },
174
175
        /**
176
         * Returns information about the plugin as a name/value array.
177
         * The current keys are longname, author, authorurl, infourl and version.
178
         *
179
         * @return {Object} Name/value array containing information about the plugin.
180
         */
181
        getInfo: function() {
182
            return {
183
                longname: 'Moodle TinyMCE RecordRTC plugin',
184
                author: 'Jesus Federico',
185
                infourl: 'http://blindsidenetworks.com',
186
                version: "1.0"
187
            };
188
        },
189
    });
190
191
    // Register plugin.
192
    tinyMCE.PluginManager.add('recordrtc', tinyMCE.plugins.RecordRTC);
193
})();
194