Passed
Push — master ( ec4722...b2f2e7 )
by Jesus
04:39 queued 15s
created

search.js ➔ searchPage   D

Complexity

Conditions 12

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
dl 0
loc 12
rs 4.8
c 0
b 0
f 0
eloc 6

How to fix   Complexity   

Complexity

Complex classes like search.js ➔ searchPage 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
// 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
21
  if ((controller == "admins" && action == "index") || 
22
      (controller == "rooms" && action == "show") || 
23
      (controller == "rooms" && action == "update") ||
24
      (controller == "rooms" && action == "join") || 
25
      (controller == "users" && action == "recordings") ||
26
      (controller == "admins" && action == "server_recordings")) {
27
    // Submit search if the user hits enter
28
    $("#search-input").keypress(function(key) {
29
      if (key.which == 13) {
30
        searchPage()
31
      }
32
    })
33
34
    // Add listeners for sort
35
    $("th[data-order]").click(function(data){
36
      var header_elem = $(data.target)
37
38
      if(header_elem.data('order') === 'asc'){ // asc
39
        header_elem.data('order', 'desc');
40
      }
41
      else if(header_elem.data('order') === 'desc'){ // desc
42
        header_elem.data('order', 'none');
43
      }
44
      else{ // none
45
        header_elem.data('order', 'asc');
46
      }
47
48
      var search = $("#search-input").val();
49
50
      var url = window.location.pathname + "?page=1&search=" + search + "&column=" + header_elem.data("header") +
51
       "&direction=" + header_elem.data('order')
52
53
      window.location.replace(addRecordingTable(url))
54
    })
55
56
    if(controller === "rooms" && action === "show"){
57
      $(".page-item > a").each(function(){
58
        if(!$(this).attr('href').endsWith("#")){
59
          $(this).attr('href', $(this).attr('href') + "#recordings-table")
60
        }
61
      })
62
    }
63
  }
64
})
65
66
// Searches the user table for the given string
67
function searchPage() {
68
  var search = $("#search-input").val();
69
70
  // Check if the user filtered by role
71
  var role = new URL(location.href).searchParams.get('role')
72
73
  var url = window.location.pathname + "?page=1&search=" + search
74
75
  if (role) { url += "&role=" + role } 
76
77
  window.location.replace(addRecordingTable(url));
78
}
79
80
// Clears the search bar
81
function clearSearch() {
82
  var role = new URL(location.href).searchParams.get('role')
83
84
  var url = window.location.pathname + "?page=1"
85
86
  if (role) { url += "&role=" + role } 
87
  
88
  window.location.replace(addRecordingTable(url));
89
}
90
91
function addRecordingTable(url) {
92
  if($("body").data('controller') === "rooms" && $("body").data('action') === "show") { 
93
    url += "#recordings-table"
94
  }
95
  return url
96
}
97