Completed
Branch pre-recording-server (e6fae9)
by Jacob
01:27
created

editor_plugin.js ➔ addMaximizeWindow   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 18
rs 9.4285

1 Function

Rating   Name   Duplication   Size   Complexity  
A editor_plugin.js ➔ ... ➔ ed.addCommand(ꞌmceMaximizeWindowꞌ) 0 16 1
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() {
20
    ed.addCommand('mceForceRepaint', function() {
0 ignored issues
show
Bug introduced by
The variable ed seems to be never declared. If this is a global, consider adding a /** global: ed */ 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...
21
        var root = ed.dom.getRoot(),
0 ignored issues
show
Bug introduced by
The variable ed seems to be never declared. If this is a global, consider adding a /** global: ed */ 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...
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(w) {
0 ignored issues
show
Unused Code introduced by
The parameter w is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
33
    ed.addCommand('mceMaximizeWindow', function(w) {
0 ignored issues
show
Bug introduced by
The variable ed seems to be never declared. If this is a global, consider adding a /** global: ed */ 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...
34
        // This function duplicates the TinyMCE windowManager code when 'maximize' button is pressed.
35
        var vp = ed.dom.getViewPort(),
0 ignored issues
show
Bug introduced by
The variable ed seems to be never declared. If this is a global, consider adding a /** global: ed */ 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...
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() {
52
    tinyMCE.PluginManager.requireLangPack('recordrtc');
53
54
    tinyMCE.create('tinymce.plugins.RecordRTC', {
55
        /**
56
         * Initializes the plugin, this will be executed after the plugin has been created.
57
         * This call is done before the editor instance has finished it's initialization so use the onInit event
58
         * of the editor instance to intercept that event.
59
         *
60
         * @param {tinyMCE.Editor} ed Editor instance that the plugin is initialized in.
61
         * @param {string} url Absolute URL to where the plugin is located.
62
         */
63
        init: function(ed, url) {
64
            // Add commands to the editor.
65
            addForceRepaint();
66
            addMaximizeWindow(w);
0 ignored issues
show
Bug introduced by
The variable w seems to be never declared. If this is a global, consider adding a /** global: w */ 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...
67
68
            if (M.editor_tinymce.filepicker_options[ed.id] &&
69
                M.editor_tinymce.filepicker_options[ed.id].hasOwnProperty('media')) {
70
                ed.addCommand('mceAudioRTC', function() {
71
                    var audiortc = ed.getParam('audiortc', {});
72
                    var viewparams = '';
73
                    window.Object.keys(audiortc).forEach(function(key) {
74
                        viewparams += (viewparams !== '' ? '&' : '') + window.encodeURIComponent(key);
75
                        viewparams += '=' + window.encodeURIComponent(audiortc[key]);
76
                    });
77
                    var viewurl = ed.getParam("moodle_plugin_base") + 'recordrtc/audiortc.php';
78
                    viewurl += (viewparams != '' ? '?' + viewparams : '');
79
80
                    var onClose = function() {
81
                        ed.windowManager.onClose.remove(onClose);
82
                        ed.execCommand('mceForceRepaint');
83
                    };
84
                    ed.windowManager.onClose.add(onClose);
85
                    var vp = ed.dom.getViewPort(),
86
                        baseWidth = 640 + window.parseInt(ed.getLang('advimage.delta_width', 0)),
87
                        percentOfViewportWidth = vp.w * 0.75,
88
                        width = percentOfViewportWidth > baseWidth ? percentOfViewportWidth : baseWidth,
89
                        height = 340 + window.parseInt(ed.getLang('advimage.delta_width', 0)),
90
                        maximizedmode = (width >= vp.w - 2 || height >= vp.h - 2);
91
                    if (maximizedmode) {
92
                        width = vp.w;
93
                        height = vp.h;
94
                    }
95
                    var w = ed.windowManager.open({
96
                        file: viewurl,
97
                        width: width,
98
                        height: height,
99
                        inline: 1,
100
                        popup_css: ed.getParam("moodle_plugin_base") + 'recordrtc/tinymce/css/popup.css'
101
                    }, {
102
                        plugin_url: url // Plugin absolute URL.
103
                    });
104
                    if (maximizedmode) {
105
                        ed.execCommand('mceMaximizeWindow', w);
106
                    }
107
                });
108
109
                // Register AudioRTC button.
110
                ed.addButton('audiortc', {
111
                    title: 'recordrtc.audiortc',
112
                    cmd: 'mceAudioRTC',
113
                    image: url + '/img/audiortc.png'
114
                });
115
116
                ed.addCommand('mceVideoRTC', function() {
117
                    var videortc = ed.getParam('videortc', {});
118
                    var viewparams = '';
119
                    window.Object.keys(videortc).forEach(function(key) {
120
                        viewparams += (viewparams !== '' ? '&' : '') + window.encodeURIComponent(key);
121
                        viewparams += '=' + window.encodeURIComponent(videortc[key]);
122
                    });
123
                    var viewurl = ed.getParam("moodle_plugin_base") + 'recordrtc/videortc.php';
124
                    viewurl += (viewparams != '' ? '?' + viewparams : '');
125
126
                    var onClose = function() {
127
                        ed.windowManager.onClose.remove(onClose);
128
                        ed.execCommand('mceForceRepaint');
129
                    };
130
                    ed.windowManager.onClose.add(onClose);
131
                    var vp = ed.dom.getViewPort(),
132
                        baseWidth = 720 + window.parseInt(ed.getLang('advimage.delta_width', 0)),
133
                        percentOfViewportWidth = vp.w * 0.75,
134
                        width = percentOfViewportWidth > baseWidth ? percentOfViewportWidth : baseWidth,
135
                        height = 780 + window.parseInt(ed.getLang('advimage.delta_width', 0)),
136
                        maximizedmode = (width >= vp.w - 2 || height >= vp.h - 2);
137
                    if (maximizedmode) {
138
                        width = vp.w;
139
                        height = vp.h;
140
                    }
141
                    var w = ed.windowManager.open({
142
                        file: viewurl,
143
                        width: width,
144
                        height: height,
145
                        inline: 1,
146
                        popup_css: ed.getParam("moodle_plugin_base") + 'recordrtc/tinymce/css/popup.css'
147
                    }, {
148
                        plugin_url: url // Plugin absolute URL.
149
                    });
150
                    if (maximizedmode) {
151
                        ed.execCommand('mceMaximizeWindow', w);
152
                    }
153
                });
154
155
                // Register VideoRTC button.
156
                ed.addButton('videortc', {
157
                    title: 'recordrtc.videortc',
158
                    cmd: 'mceVideoRTC',
159
                    image: url + '/img/videortc.png'
160
                });
161
            }
162
        },
163
        
164
        createControl: function() {
165
            return null;
166
        },
167
168
        /**
169
         * Returns information about the plugin as a name/value array.
170
         * The current keys are longname, author, authorurl, infourl and version.
171
         *
172
         * @return {Object} Name/value array containing information about the plugin.
173
         */
174
        getInfo: function() {
175
            return {
176
                longname: 'Moodle TinyMCE RecordRTC plugin',
177
                author: 'Jesus Federico',
178
                infourl: 'http://blindsidenetworks.com',
179
                version: "1.0"
180
            };
181
        },
182
    });
183
184
    // Register plugin.
185
    tinyMCE.PluginManager.add('recordrtc', tinyMCE.plugins.RecordRTC);
186
})();
187