aboutsummaryrefslogtreecommitdiff
path: root/data/assets/js/admin/tabs/users.js
blob: b74b8a1f7bec6f576b23705b38e1f6ffea275a4b (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
jQuery(function($) {
  "use strict";

  var TEMPLATES = new LuigiTemplate.Cache({
    user: [
      "<a ",
        "href='#' ",
        "class='list-group-item %{css|h}' ",
        "title='Edit user \"%{name|h}\".' ",
        "data-user_id='%{user_id|h}' ",
        "data-q='%{q|h}' ",
      ">",
        "<i class='fa fa-fw fa-user'></i>",
        " ",
        "%{name|h} (%{email|h})",

        "<span class='%{badge_css} badge pull-right'>",
          "%{role|h}",
        "</span>",
      "</a>",
    ],

    loading: [
      "<span class='list-group-item disabled'>",
        "<i class='fa fa-spinner fa-spin'></i>",
        " ",
        "Loading...",
      "</span>",
    ],

    error: [
      "<span class='list-group-item list-group-item-danger disabled'>",
        "<i class='fa fa-exclamation-triangle'></i>",
        " ",
        "Error: %{error|h}",
      "</span>",
    ],
  });

  function filter() {
    var qs = $('#users-q').val().replace(/^\s+|\s+$/g, '').toLowerCase().split(/\s+/);

    if (qs.length > 0) {
      // hide all users
      $('#users .list-group-item').addClass('hidden');

      // show matching users
      $($.grep($('#users .list-group-item'), function(el) {
        var eq = $(el).data('q');

        return ($.grep(qs, function(q) {
          return eq.indexOf(q) !== -1;
        }).length == qs.length);
      })).removeClass('hidden');
    } else {
      // show all users
      $('#users .list-group-item').removeClass('hidden');
    }
  }

  var ROLE_BADGE_CSS = {
    viewer: 'hidden',
    editor: '',
    admin:  '',
  };

  function reload() {
    var btn = $('#users-reload'),
        list = $('#users');

    // show loading
    btn.toggleClass('disabled').find('.loading').toggleClass('hidden');
    list.html(TEMPLATES.run('loading'));

    send('user/get_users').always(function() {
      btn.toggleClass('disabled').find('.loading').toggleClass('hidden');
      list.html('');
    }).fail(function(r) {
      var error = r.responseText;

      try {
        var data = $.parseJSON(r.responseText);
        if (data.error)
          error = data.error;
      } catch (e) {}

      list.html(TEMPLATES.run('error', {
        error: error
      }));
    }).done(function(r) {
      list.html($.map(r, function(row) {
        return TEMPLATES.run('user', $.extend({}, row, {
          css: (row.is_active == '1') ? '' : 'list-group-item-danger',
          q:   [row.user_id, row.user_name, row.email, row.role_name].join('  ').toLowerCase(),
          badge_css: ROLE_BADGE_CSS[row.role],
        }));
      }).join(''));

      // refresh filters
      filter();
    });

    // stop event
    return false;
  }

  $('#users-q').on('search-update', function() {
    filter();
  });

  $('#users').on('click', 'a.list-group-item', function() {
    var user_id = $(this).data('user_id');

    // update highlight
    $('#users .active').removeClass('active');
    $(this).addClass('active');

    $('#user-edit-dialog').data('user_id', user_id).modal('show');

    // stop event
    return false;
  });

  $('#users-reload').click(reload);

  // load users
  $('#settings-tab-users').on('show.bs.tab', function() {
    reload();
  });
});