app/assets/javascripts/user_edit.js   A
last analyzed

Complexity

Total Complexity 6
Complexity/F 2

Size

Lines of Code 62
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 62
rs 10
wmc 6
eloc 31
mnd 3
bc 3
fnc 3
bpm 1
cpm 2
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B user_edit.js ➔ clearRole 0 24 6
1
// BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
2
//
3
// Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
4
//
5
// This program is free software; you can redistribute it and/or modify it under the
6
// terms of the GNU Lesser General Public License as published by the Free Software
7
// Foundation; either version 3.0 of the License, or (at your option) any later
8
// version.
9
//
10
// BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
11
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
12
// PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
13
//
14
// You should have received a copy of the GNU Lesser General Public License along
15
// with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
16
17
$(document).on('turbolinks:load', function(){
18
  var controller = $("body").data('controller');
19
  var action = $("body").data('action');
20
  if ((controller == "admins" && action == "edit_user") || (controller == "users" && action == "edit")) {
21
    // Clear the role when the user clicks the x
22
    $(".clear-role").click(clearRole)
23
24
    // When the user selects an item in the dropdown add the role to the user
25
    $("#role-select-dropdown").change(function(data){
26
      var dropdown = $("#role-select-dropdown");
27
      var select_role_id = dropdown.val();
28
29
      if(select_role_id){
30
        // Disable the role in the dropdown
31
        var selected_role = dropdown.find('[value=\"' + select_role_id + '\"]');
32
        selected_role.prop("disabled", true)
33
34
        // Add the role tag
35
        var tag_container = $("#role-tag-container");
36
        tag_container.append("<span id=\"user-role-tag_" + select_role_id + "\" style=\"background-color:" + selected_role.data("colour") + ";\" class=\"tag user-role-tag\">" + 
37
          selected_role.text() + "<a data-role-id=\"" + select_role_id + "\" class=\"tag-addon clear-role\"><i data-role-id=\"" + select_role_id + "\" class=\"fas fa-times\"></i></a></span>");
38
39
        // Update the role ids input that gets submited on user update
40
        var role_ids = $("#user_role_ids").val()
41
        role_ids += " " + select_role_id
42
        $("#user_role_ids").val(role_ids)
43
        
44
        // Add the clear role function to the tag
45
        $("#user-role-tag_" + select_role_id).click(clearRole);
46
47
        // Reset the dropdown
48
        dropdown.val(null)
49
      }
50
    })
51
  }
52
})
53
54
// This function removes the specfied role from a user
55
function clearRole(data){
56
  // Get the role id
57
  var role_id = $(data.target).data("role-id");
58
  var role_tag = $("#user-role-tag_" + role_id);
59
60
  // Remove the role tag
61
  $(role_tag).remove()
62
  
63
  // Update the role ids input
64
  var role_ids = $("#user_role_ids").val()
65
  var parsed_ids = role_ids.split(' ')
66
  
67
  var index = parsed_ids.indexOf(role_id.toString());
68
  
69
  if (index > -1) {
70
    parsed_ids.splice(index, 1);
71
  }
72
  
73
  $("#user_role_ids").val(parsed_ids.join(' '))
74
  
75
  // Enable the role in the role select dropdown
76
  var selected_role = $("#role-select-dropdown").find('[value=\"' + role_id + '\"]');
77
  selected_role.prop("disabled", false)
78
}