Completed
Push — develop ( 19d1c8...a45252 )
by Patrick
9s
created

users.js ➔ do_users_init   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
c 0
b 0
f 0
nc 5
nop 0
dl 0
loc 33
rs 8.439
1
function renderUID(data, type, row)
0 ignored issues
show
Unused Code introduced by
The parameter type is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter row is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
2
{
3
    return '<a href="user_edit.php?uid='+data+'">'+data+'</a>';
4
}
5
6
function renderName(data, type, row, meta)
0 ignored issues
show
Unused Code introduced by
The parameter meta is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
7
{
8
    if(row['givenName'] !== false)
9
    {
10
        return row['givenName']+' '+data;
11
    }
12
    return data;
13
}
14
15
function onUserTableBodyClick()
16
{
17
    if($(this).hasClass('selected')) 
18
    {
19
        $(this).removeClass('selected');
20
    }
21
    else 
22
    {
23
        $(this).addClass('selected');
24
    }
25
}
26
27
function storageAvailable(type)
28
{
29
    try
30
    {
31
        var storage = window[type],
32
            x = '__storage_test__';
33
        storage.setItem(x, x);
34
        storage.removeItem(x);
35
        return true;
36
    }
37
    catch(e)
38
    {
39
        return e instanceof DOMException && (
0 ignored issues
show
Bug introduced by
The variable DOMException seems to be never declared. If this is a global, consider adding a /** global: DOMException */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
40
            // everything except Firefox
41
            e.code === 22 ||
42
            // Firefox
43
            e.code === 1014 ||
44
            // test name field too, because code might not be present
45
            // everything except Firefox
46
            e.name === 'QuotaExceededError' ||
47
            // Firefox
48
            e.name === 'NS_ERROR_DOM_QUOTA_REACHED') &&
49
            // acknowledge QuotaExceededError only if there's something already stored
50
            storage.length !== 0;
51
    }
52
}
53
54
function populateTableNoStorage()
55
{
56
  $('#user_table').dataTable({
57
    'ajax': '../api/v1/users?fmt=data-table&$select=uid,displayName,sn,mail,givenName',
58
    'columns': [
59
      {'data': 'uid', 'render': renderUID},
60
      {'data': 'displayName'},
61
      {'data': 'sn'},
62
      {'data': 'mail'},
63
      {'data': 'givenName', 'visible': false}
64
    ]
65
  });
66
}
67
68
function populateTableFromArray(array)
69
{
70
  if(!$.fn.DataTable.isDataTable('#user_table'))
71
  {
72
    $('#user_table').dataTable({
73
      'data': array,
74
      'columns': [
75
        {'data': 'uid', 'render': renderUID},
76
        {'data': 'displayName'},
77
        {'data': 'sn'},
78
        {'data': 'mail'},
79
        {'data': 'givenName', 'visible': false}
80
      ],
81
      'deferRender': true
82
    });
83
  }
84
  else
85
  {
86
    var table = $('#user_table').DataTable();
87
    table.clear();
88
    for(var i = 0; i < array.length; i++)
89
    {
90
      table.row.add(array[i]);
91
    }
92
    table.draw();
93
  }
94
}
95
96
function gotUserList(jqXHR)
97
{
98
  if(jqXHR.status !== 200)
99
  {
100
    alert('Unable to obtain user list!');
101
    console.log(jqXHR);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
102
    return;
103
  }
104
  var cache = {users: jqXHR.responseJSON, time: Date.now()};
105
  window.localStorage.setItem('FlipsideUserCache', JSON.stringify(cache));
106
  populateTableFromArray(jqXHR.responseJSON);
107
}
108
109
function refreshCache()
110
{
111
  $.ajax({
112
    url: '../api/v1/users?$select=uid,displayName,sn,mail,givenName',
113
    type: 'get',
114
    dataType: 'json',
115
    complete: gotUserList
116
  });
117
}
118
119
function do_users_init()
120
{
121
    if($("#user_table").length > 0)
122
    {
123
      if(storageAvailable('localStorage'))
124
      {
125
        var cache = window.localStorage.getItem('FlipsideUserCache');
126
        if(!cache)
127
        {
128
          refreshCache();
129
        }
130
        else
131
        {
132
          cache = JSON.parse(cache);
133
          if(cache.time < Date.now()-1200000)
134
          {
135
            //Cache is 2 minutes old... refresh
136
            refreshCache();
137
          }
138
          else
139
          {
140
            populateTableFromArray(cache.users);
141
          }
142
        }
143
      }
144
      else
145
      {
146
        $('.fa-refresh').hide();
147
        populateTableNoStorage();
148
      }
149
      $("#user_table tbody").on('click', 'tr', onUserTableBodyClick);
150
    }
151
}
152
153
$(do_users_init);
154