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

Complexity

Total Complexity 20
Complexity/F 2.22

Size

Lines of Code 92
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 92
rs 10
wmc 20
eloc 53
mnd 11
bc 11
fnc 9
bpm 1.2222
cpm 2.2222
noi 0
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 == "rooms" && action == "show" 
22
    || controller == "rooms" && action == "update" 
23
    || controller == "users" && action == "recordings" 
24
    || controller == "admins" && action == "server_recordings"){
25
26
    // Choose active header
27
    // (Name, Length or Users)
28
    $('th').each(function(){
29
      if($(this).data("header")){
30
        $(this).on('click', function(){
31
          set_active_header($(this).data("header"));
32
          sort_by($(this).data("header"), $(this).data('order'));
33
        });
34
      }
35
    });
36
37
    // Based on the header (Name, Length or Users) clicked,
38
    // Modify the ui for the tables
39
    var set_active_header = function(active_header){
40
      $('th').each(function(){
41
        if($(this).data("header") == active_header){
42
          configure_order($(this));
43
        }
44
        else{
45
          $(this).text($(this).data("header"));
46
          $(this).data('order', 'none');
47
        }
48
      });
49
    }
50
51
    // Based on the header (Name, Length or Users) clicked,
52
    // Modify the ui for the tables
53
    var configure_order = function(header_elem){
54
      if(header_elem.data('order') === 'asc'){ // asc
55
        header_elem.text(header_elem.data("header") + " ↓");
56
        header_elem.data('order', 'desc');
57
      }
58
      else if(header_elem.data('order') === 'desc'){ // desc
59
        header_elem.text(header_elem.data("header"));
60
        header_elem.data('order', 'none');
61
      }
62
      else{ // none
63
        header_elem.text(header_elem.data("header") + " ↑");
64
        header_elem.data('order', 'asc');
65
      }
66
    }
67
68
    // Given a label and an order, sort recordings by order
69
    // under a given label
70
    var sort_by = function(label, order){
71
      var recording_list_tbody = $('.table-responsive').find('tbody');
72
      if(label === "Name"){
73
        sort_recordings(recording_list_tbody, order, "#recording-title");
74
      }
75
      else if(label === "Length"){
76
        sort_recordings(recording_list_tbody, order, "#recording-length");
77
      }
78
      else if(label === "Users"){
79
        sort_recordings(recording_list_tbody, order, "#recording-users");
80
      }
81
    }
82
83
    // Generalized function for sorting recordings
84
    var sort_recordings = function(recording_list_tbody, order, recording_id){
85
      recording_list_tbody.find('tr').sort(function(a, b){
86
        var a_val, b_val;
87
        if (recording_id == "#recording-length") {
88
          a_val = $.trim($(a).find(recording_id).data("full-length"));
89
          b_val = $.trim($(b).find(recording_id).data("full-length"));
90
        } else {
91
          a_val = $.trim($(a).find(recording_id).text());
92
          b_val = $.trim($(b).find(recording_id).text());
93
        }
94
95
        if(order === "asc"){
96
          return a_val.localeCompare(b_val);
97
        }
98
        else if(order === "desc"){
99
          return b_val.localeCompare(a_val);
100
        } else {
101
          return undefined;
102
        }
103
104
105
      }).appendTo(recording_list_tbody);
106
    }
107
  }
108
});
109