aboutsummaryrefslogtreecommitdiff
path: root/data/assets/js/admin/dialogs/user-edit.js
blob: 038a58773202364ff457c2af679ef316b351451a (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
jQuery(function($) {
  "use strict";

  $('#user-edit-dialog').on('show.bs.modal', function() {
    var me = $(this);

    me.find('.modal-body').addClass('hidden');
    me.find('.modal-body.loading-text').removeClass('hidden');

    send('user/get', {
      user_id: me.data('user_id'),
    }).always(function() {
      me.find('.modal-body.loading-text').addClass('hidden');
    }).fail(function(r) {
      var error = r.responseText;

      try {
        // try to extract error message from json response
        var data = $.parseJSON(r.responseText);
        if ('error' in data)
          error = data.error;
      } catch (e) {}

      me.find('.modal-body.loading-error').removeClass('hidden')
        .find('.error-text').text(error);
    }).done(function(r) {
      $.each({
        name:   'name',
        email:  'email',
        role:   'role',
      }, function(id, col) {
        $('#user-edit-' + id).val(r[col]);
      });

      $('#user-edit-password').val('');
      $('#user-edit-active').val((r.is_active == '1') ? 'active' : 'inactive');

      me.find('.modal-body.loading-done').removeClass('hidden');
    });
  }).on('shown.bs.modal', function() {
    $('#user-edit-name').focus();
  }).on('hide.bs.modal', function() {
    // clear highlight
    $('#users .active').removeClass('active');
  });

  $('#user-edit-name, #user-edit-email').keydown(function(ev) {
    if (ev.which == 13) {
      setTimeout(function() {
        $('#user-edit-confirm').click();
      }, 10);

      // stop event
      return false;
    }
  });

  $('#user-edit-confirm').click(function() {
    var me = $(this);

    if (me.hasClass('disabled'))
      return false;

    // toggle loading
    me.toggleClass('disabled').find('.loading').toggleClass('hidden');

    send('user/set', {
      user_id:  $('#user-edit-dialog').data('user_id'),
      name:     $('#user-edit-name').val(),
      email:    $('#user-edit-email').val(),
      password: $('#user-edit-password').val(),
      role:     $('#user-edit-role').val(),
      active:   ($('#user-edit-active').val() == 'active') ? 't' : 'f',
    }).always(function() {
      // toggle loading
      me.toggleClass('disabled').find('.loading').toggleClass('hidden');
    }).fail(function(r) {
      var error = r.responseText;

      try {
        // try to extract error message from json response
        var data = $.parseJSON(r.responseText);
        if ('error' in data)
          error = data.error;
      } catch (e) {}

      alert('Error: ' + error);
    }).done(function(r) {
      $('#users-reload').click();
      $('#user-edit-dialog').modal('hide');
    });

    // stop event
    return false;
  });
});