Completed
Push — master ( 0ba9a1...0759d1 )
by Jesus
02:18
created

yui/src/modform/js/modform.js   C

Complexity

Total Complexity 54
Complexity/F 2.57

Size

Lines of Code 300
Function Count 21

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 13
Bugs 1 Features 0
Metric Value
cc 0
nc 393216
dl 0
loc 300
rs 6.8539
c 13
b 1
f 0
wmc 54
mnd 4
bc 48
fnc 21
bpm 2.2857
cpm 2.5714
noi 0

21 Functions

Rating   Name   Duplication   Size   Complexity  
A M.mod_bigbluebuttonbn.modform.init 0 11 1
B M.mod_bigbluebuttonbn.modform.participantSelectionSet 0 23 6
A M.mod_bigbluebuttonbn.modform.showFieldset 0 12 3
A M.mod_bigbluebuttonbn.modform.updateInstanceTypeProfile 0 9 2
A M.mod_bigbluebuttonbn.modform.isFeatureEnabled 0 4 1
A M.mod_bigbluebuttonbn.modform.showInput 0 13 3
B M.mod_bigbluebuttonbn.modform.applyInstanceTypeProfile 0 34 1
B M.mod_bigbluebuttonbn.modform.participantListRoleUpdate 0 13 5
A M.mod_bigbluebuttonbn.modform.selectEnable 0 4 1
A M.mod_bigbluebuttonbn.modform.participantAdd 0 17 4
A M.mod_bigbluebuttonbn.modform.selectClear 0 6 2
B M.mod_bigbluebuttonbn.modform.participantAddToForm 0 43 6
A M.mod_bigbluebuttonbn.modform.participantListUpdate 0 4 1
B M.mod_bigbluebuttonbn.modform.participantRemoveFromMemory 0 9 5
A M.mod_bigbluebuttonbn.modform.participantListInit 0 19 4
A M.mod_bigbluebuttonbn.modform.participantAddToMemory 0 7 1
A M.mod_bigbluebuttonbn.modform.participantRemoveFromForm 0 9 3
A M.mod_bigbluebuttonbn.modform.participantRemove 0 10 1
A M.mod_bigbluebuttonbn.modform.selectAddOption 0 7 1
A M.mod_bigbluebuttonbn.modform.selectDisable 0 4 1
A M.mod_bigbluebuttonbn.modform.participantListClear 0 8 2

How to fix   Complexity   

Complexity

Complex classes like yui/src/modform/js/modform.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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
/** global: M */
17
/** global: Y */
18
19
M.mod_bigbluebuttonbn = M.mod_bigbluebuttonbn || {};
20
21
M.mod_bigbluebuttonbn.modform = {
22
23
    bigbluebuttonbn: {},
24
    strings: {},
25
26
    /**
27
     * Initialise the broker code.
28
     *
29
     * @method init
30
     * @param {object} bigbluebuttonbn
31
     */
32
    init: function(bigbluebuttonbn) {
33
        this.bigbluebuttonbn = bigbluebuttonbn;
34
        this.strings = {
35
            as: M.util.get_string('mod_form_field_participant_list_text_as', 'bigbluebuttonbn'),
36
            viewer: M.util.get_string('mod_form_field_participant_bbb_role_viewer', 'bigbluebuttonbn'),
37
            moderator: M.util.get_string('mod_form_field_participant_bbb_role_moderator', 'bigbluebuttonbn'),
38
            remove: M.util.get_string('mod_form_field_participant_list_action_remove', 'bigbluebuttonbn')
39
        };
40
        this.updateInstanceTypeProfile();
41
        this.participantListInit();
42
    },
43
44
    updateInstanceTypeProfile: function() {
45
        var selectedType, profileType;
46
        selectedType = Y.one('#id_type');
47
        profileType = this.bigbluebuttonbn.instanceTypeRoomOnly;
48
        if (selectedType !== null) {
49
            profileType = selectedType.get('value');
50
        }
51
        this.applyInstanceTypeProfile(profileType);
52
    },
53
54
    applyInstanceTypeProfile: function(profileType) {
55
        var showAll = this.isFeatureEnabled(profileType, 'all');
56
        // Show room settings validation.
57
        this.showFieldset('id_room', showAll ||
58
                          this.isFeatureEnabled(profileType, 'showroom'));
59
        this.showInput('id_record', showAll ||
60
                       this.isFeatureEnabled(profileType, 'showrecordings'));
61
        // Show recordings settings validation.
62
        this.showFieldset('id_recordings', showAll ||
63
                          this.isFeatureEnabled(profileType, 'showrecordings'));
64
        // Show recordings imported settings validation.
65
        this.showInput('id_recordings_imported', showAll ||
66
                       this.isFeatureEnabled(profileType, 'showrecordings'));
67
        // Preuploadpresentation feature validation.
68
        this.showFieldset('id_preuploadpresentation', showAll ||
69
                          this.isFeatureEnabled(profileType, 'preuploadpresentation'));
70
        // Participants feature validation.
71
        this.showFieldset('id_permissions', showAll ||
72
                          this.isFeatureEnabled(profileType, 'permissions'));
73
        // Schedule feature validation.
74
        this.showFieldset('id_schedule', showAll ||
75
                          this.isFeatureEnabled(profileType, 'schedule'));
76
        // Common module settings validation.
77
        this.showFieldset('id_modstandardelshdr', showAll ||
78
                          this.isFeatureEnabled(profileType, 'modstandardelshdr'));
79
        // Restrict access validation.
80
        this.showFieldset('id_availabilityconditionsheader', showAll ||
81
                          this.isFeatureEnabled(profileType, 'availabilityconditionsheader'));
82
        // Tags validation.
83
        this.showFieldset('id_tagshdr', showAll || this.isFeatureEnabled(profileType, 'tagshdr'));
84
        // Competencies validation.
85
        this.showFieldset('id_competenciessection', showAll ||
86
                          this.isFeatureEnabled(profileType, 'competenciessection'));
87
    },
88
89
    isFeatureEnabled: function(profileType, feature) {
90
        var features = this.bigbluebuttonbn.instanceTypeProfiles[profileType].features;
91
        return (features.indexOf(feature) != -1);
92
    },
93
94
    showFieldset: function(id, show) {
95
        // Show room settings validation.
96
        var fieldset = Y.DOM.byId(id);
97
        if (!fieldset) {
98
            return;
99
        }
100
        if (show) {
101
            Y.DOM.setStyle(fieldset, 'display', 'block');
102
            return;
103
        }
104
        Y.DOM.setStyle(fieldset, 'display', 'none');
105
    },
106
107
    showInput: function(id, show) {
108
        // Show room settings validation.
109
        var inputset = Y.DOM.byId(id);
110
        if (!inputset) {
111
            return;
112
        }
113
        var node = Y.one(inputset).ancestor('div').ancestor('div');
114
        if (show) {
115
            node.setStyle('display', 'block');
116
            return;
117
        }
118
        node.setStyle('display', 'none');
119
    },
120
121
    participantSelectionSet: function() {
122
        this.selectClear('bigbluebuttonbn_participant_selection');
123
        var type = document.getElementById('bigbluebuttonbn_participant_selection_type');
124
        for (var i = 0; i < type.options.length; i++) {
125
            if (type.options[i].selected) {
126
                var options = this.bigbluebuttonbn.participantData[type.options[i].value].children;
127
                for (var option in options) {
128
                    if (options.hasOwnProperty(option)) {
129
                        this.selectAddOption(
130
                            'bigbluebuttonbn_participant_selection', options[option].name, options[option].id
131
                        );
132
                    }
133
                }
134
                if (type.options[i].value === 'all') {
135
                    this.selectAddOption('bigbluebuttonbn_participant_selection',
136
                        '---------------', 'all');
137
                    this.selectDisable('bigbluebuttonbn_participant_selection');
138
                } else {
139
                    this.selectEnable('bigbluebuttonbn_participant_selection');
140
                }
141
            }
142
        }
143
    },
144
145
    participantListInit: function() {
146
        var selectionTypeValue, selectionValue, selectionRole, participantSelectionTypes;
147
        this.participantListClear();
148
        for (var i = 0; i < this.bigbluebuttonbn.participantList.length; i++) {
149
            selectionTypeValue = this.bigbluebuttonbn.participantList[i].selectiontype;
150
            selectionValue = this.bigbluebuttonbn.participantList[i].selectionid;
151
            selectionRole = this.bigbluebuttonbn.participantList[i].role;
152
            participantSelectionTypes = this.bigbluebuttonbn.participantData[selectionTypeValue];
153
            if (selectionTypeValue != 'all' && typeof participantSelectionTypes.children[selectionValue] == 'undefined') {
154
                // Remove from memory.
155
                this.participantRemoveFromMemory(selectionTypeValue, selectionValue);
156
                continue;
157
            }
158
            // Add it to the form.
159
            this.participantAddToForm(selectionTypeValue, selectionValue, selectionRole);
160
        }
161
        // Update in the form.
162
        this.participantListUpdate();
163
    },
164
165
    participantListClear: function() {
166
        var table, rows;
167
        table = document.getElementById('participant_list_table');
168
        rows = table.getElementsByTagName('tr');
169
        for (var i = rows.length; i > 0; i--) {
170
            table.deleteRow(0);
171
        }
172
    },
173
174
    participantListUpdate: function() {
175
        var participantList = document.getElementsByName('participants')[0];
176
        participantList.value = JSON.stringify(this.bigbluebuttonbn.participantList).replace(/"/g, '&quot;');
177
    },
178
179
    participantRemove: function(selectionTypeValue, selectionValue) {
180
        // Remove from memory.
181
        this.participantRemoveFromMemory(selectionTypeValue, selectionValue);
182
183
        // Remove from the form.
184
        this.participantRemoveFromForm(selectionTypeValue, selectionValue);
185
186
        // Update in the form.
187
        this.participantListUpdate();
188
    },
189
190
    participantRemoveFromMemory: function(selectionTypeValue, selectionValue) {
191
        var selectionid = (selectionValue === '' ? null : selectionValue);
192
        for (var i = 0; i < this.bigbluebuttonbn.participantList.length; i++) {
193
            if (this.bigbluebuttonbn.participantList[i].selectiontype == selectionTypeValue &&
194
                this.bigbluebuttonbn.participantList[i].selectionid == selectionid) {
195
                this.bigbluebuttonbn.participantList.splice(i, 1);
196
            }
197
        }
198
    },
199
200
    participantRemoveFromForm: function(selectionTypeValue, selectionValue) {
201
        var id = 'participant_list_tr_' + selectionTypeValue + '-' + selectionValue;
202
        var participantListTable = document.getElementById('participant_list_table');
203
        for (var i = 0; i < participantListTable.rows.length; i++) {
204
            if (participantListTable.rows[i].id == id) {
205
                participantListTable.deleteRow(i);
206
            }
207
        }
208
    },
209
210
    participantAdd: function() {
211
        var selectionType = document.getElementById('bigbluebuttonbn_participant_selection_type');
212
        var selection = document.getElementById('bigbluebuttonbn_participant_selection');
213
        // Lookup to see if it has been added already.
214
        for (var i = 0; i < this.bigbluebuttonbn.participantList.length; i++) {
215
            if (this.bigbluebuttonbn.participantList[i].selectiontype == selectionType.value &&
216
                this.bigbluebuttonbn.participantList[i].selectionid == selection.value) {
217
                return;
218
            }
219
        }
220
        // Add it to memory.
221
        this.participantAddToMemory(selectionType.value, selection.value);
222
        // Add it to the form.
223
        this.participantAddToForm(selectionType.value, selection.value, 'viewer');
224
        // Update in the form.
225
        this.participantListUpdate();
226
    },
227
228
    participantAddToMemory: function(selectionTypeValue, selectionValue) {
229
        this.bigbluebuttonbn.participantList.push({
230
            "selectiontype": selectionTypeValue,
231
            "selectionid": selectionValue,
232
            "role": "viewer"
233
        });
234
    },
235
236
    participantAddToForm: function(selectionTypeValue, selectionValue, selectionRole) {
237
        var listTable, innerHTML, selectedHtml, removeHtml, removeClass, bbbRoles, i, row, cell0, cell1, cell2, cell3;
238
        listTable = document.getElementById('participant_list_table');
239
        row = listTable.insertRow(listTable.rows.length);
240
        row.id = "participant_list_tr_" + selectionTypeValue + "-" + selectionValue;
241
        cell0 = row.insertCell(0);
242
        cell0.width = "125px";
243
        cell0.innerHTML = '<b><i>' + this.bigbluebuttonbn.participantData[selectionTypeValue].name;
244
        cell0.innerHTML += (selectionTypeValue !== 'all' ? ':&nbsp;' : '') + '</i></b>';
245
        cell1 = row.insertCell(1);
246
        cell1.innerHTML = '';
247
        if (selectionTypeValue !== 'all') {
248
            cell1.innerHTML = this.bigbluebuttonbn.participantData[selectionTypeValue].children[selectionValue].name;
249
        }
250
        innerHTML = '&nbsp;<i>' + this.strings.as + '</i>&nbsp;';
251
        innerHTML += '<select id="participant_list_role_' + selectionTypeValue + '-' + selectionValue + '"';
252
        innerHTML += ' onchange="M.mod_bigbluebuttonbn.modform.participantListRoleUpdate(\'';
253
        innerHTML += selectionTypeValue + '\', \'' + selectionValue;
254
        innerHTML += '\'); return 0;" class="select custom-select">';
255
        bbbRoles = ['viewer', 'moderator'];
256
        for (i = 0; i < bbbRoles.length; i++) {
257
            selectedHtml = '';
258
            if (bbbRoles[i] === selectionRole) {
259
                selectedHtml = ' selected="selected"';
260
            }
261
            innerHTML += '<option value="' + bbbRoles[i] + '"' + selectedHtml + '>' + this.strings[bbbRoles[i]] + '</option>';
262
        }
263
        innerHTML += '</select>';
264
        cell2 = row.insertCell(2);
265
        cell2.innerHTML = innerHTML;
266
        cell3 = row.insertCell(3);
267
        cell3.width = "20px";
268
        removeHtml = this.strings.remove;
269
        removeClass = "btn btn-secondary btn-sm";
270
        if (this.bigbluebuttonbn.iconsEnabled) {
271
            removeHtml = this.bigbluebuttonbn.pixIconDelete;
272
            removeClass = "btn btn-link";
273
        }
274
        innerHTML = '<a class="' + removeClass + '" onclick="M.mod_bigbluebuttonbn.modform.participantRemove(\'';
275
        innerHTML += selectionTypeValue + '\', \'' + selectionValue;
276
        innerHTML += '\'); return 0;" title="' + this.strings.remove + '">' + removeHtml + '</a>';
277
        cell3.innerHTML = innerHTML;
278
    },
279
280
    participantListRoleUpdate: function(type, id) {
281
        // Update in memory.
282
        var participantListRoleSelection = document.getElementById('participant_list_role_' + type + '-' + id);
283
        for (var i = 0; i < this.bigbluebuttonbn.participantList.length; i++) {
284
            if (this.bigbluebuttonbn.participantList[i].selectiontype == type &&
285
                this.bigbluebuttonbn.participantList[i].selectionid == (id === '' ? null : id)) {
286
                this.bigbluebuttonbn.participantList[i].role = participantListRoleSelection.value;
287
            }
288
        }
289
290
        // Update in the form.
291
        this.participantListUpdate();
292
    },
293
294
    selectClear: function(id) {
295
        var select = document.getElementById(id);
296
        while (select.length > 0) {
297
            select.remove(select.length - 1);
298
        }
299
    },
300
301
    selectEnable: function(id) {
302
        var select = document.getElementById(id);
303
        select.disabled = false;
304
    },
305
306
    selectDisable: function(id) {
307
        var select = document.getElementById(id);
308
        select.disabled = true;
309
    },
310
311
    selectAddOption: function(id, text, value) {
312
        var select = document.getElementById(id);
313
        var option = document.createElement('option');
314
        option.text = text;
315
        option.value = value;
316
        select.add(option, option.length);
317
    }
318
};
319