aboutsummaryrefslogtreecommitdiff
path: root/gn2/wqflask/static/new/javascript/table_functions.js
blob: a648778e4479400a4d6be4027cd166417322e5f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
recheckRows = function(theTable, checkedRows){
  // This is meant to recheck checkboxes after columns are resized
  checkCells = theTable.column(0).nodes().to$();
  for (let i = 0; i < checkCells.length; i++) {
    if (checkedRows.includes(i)){
      checkCells[i].childNodes[0].checked = true;
    }
  }

  checkRows = traitTable.rows().nodes();
  for (let i =0; i < checkRows.length; i++) {
    if (checkedRows.includes(i)){
      checkRows[i].classList.add("selected")
    }
  }
}

getCheckedRows = function(tableId){
  let checkedRows = []
  $("#" + tableId + " input.checkbox").each(function(index){
    if ($(this).prop("checked") == true){
      checkedRows.push(index);
    }
  });

  return checkedRows
}

function setUserColumnsDefWidths(tableId, columnDefs) {
  var userColumnDef;

  // Get the settings for this table from localStorage
  var userColumnDefs = JSON.parse(localStorage.getItem(tableId)) || [];

  if (userColumnDefs.length === 0 ) return;

  columnDefs.forEach( function(columnDef) {
    // Check if there is a width specified for this column
    userColumnDef = userColumnDefs.find( function(column) {
      return column.targets === columnDef.targets;
    });

    // If there is, set the width of this columnDef in px
    if ( userColumnDef ) {

      columnDef.sWidth = userColumnDef.width + 'px';
      columnDef.width = userColumnDef.width + 'px';

      $('.toggle-vis').each(function(){
        if ($(this).attr('data-column') == columnDef.targets){
          if ($(this).hasClass("active")){
            columnDef.bVisible = false
          } else {
            columnDef.bVisible = true
          }
        }
      })
    }
  });

  return columnDefs
}

function saveColumnSettings(tableId, traitTable) {
  var userColumnDefs = JSON.parse(localStorage.getItem(tableId)) || [];
  var width, header, existingSetting; 

  traitTable.columns().every( function ( targets ) {
    // Check if there is a setting for this column in localStorage
    existingSetting = userColumnDefs.findIndex( function(column) { return column.targets === targets;});

    // Get the width of this column
    header = this.header();
    width = $(header).width();

    if ( existingSetting !== -1 ) {
    // Update the width
    userColumnDefs[existingSetting].width = width;
    } else {
      // Add the width for this column
      userColumnDefs.push({
          targets: targets,
          width:  width,
      });
    }
  });

  // Save (or update) the settings in localStorage
  localStorage.setItem(tableId, JSON.stringify(userColumnDefs));
}

let naturalAsc = $.fn.dataTableExt.oSort["natural-ci-asc"]
let naturalDesc = $.fn.dataTableExt.oSort["natural-ci-desc"]

let na_equivalent_vals = ["N/A", "--", "", "NULL"]; //ZS: Since there are multiple values that should be treated the same as N/A

function extractInnerText(the_string){
  var span = document.createElement('span');
  span.innerHTML = the_string;
  return span.textContent || span.innerText;
}

function sortNAs(a, b, sort_function){
  if ( na_equivalent_vals.includes(a) && na_equivalent_vals.includes(b)) {
    return 0;
  }
  if (na_equivalent_vals.includes(a)){
    return 1
  }
  if (na_equivalent_vals.includes(b)) {
    return -1;
  }
  return sort_function(a, b)
}

$.extend( $.fn.dataTableExt.oSort, {
  "natural-minus-na-asc": function (a, b) {
    return sortNAs(extractInnerText(a), extractInnerText(b), naturalAsc)
  },
  "natural-minus-na-desc": function (a, b) {
    return sortNAs(extractInnerText(a), extractInnerText(b), naturalDesc)
  }
});

$.fn.dataTable.ext.order['dom-checkbox'] = function  ( settings, col )
{
    return this.api().column( col, {order:'index'} ).nodes().map( function ( td, i ) {
        return $('input', td).prop('checked') ? '1' : '0';
    } );
};

$.fn.dataTable.ext.order['dom-inner-text'] = function  ( settings, col )
{
    return this.api().column( col, {order:'index'} ).nodes().map( function ( td, i ) {
        return $(td).text();
    } );
}