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

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

    $.each({
      name:   'user_name',
      email:  'email',
      role:   'role_name',
    }, function(id, col) {
      $('#user-edit-' + id).val(row[col]);
    });
  });

  $('#user-edit-dialog').on('shown.bs.modal', function() {
    $('#user-edit-name').focus();
  });

  $('#user-edit-dialog').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),
        user_id = $('#user-edit-dialog').data('row').user_id;

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

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

    send('test/edit_user', {
      user_id:  user_id,
      name:     $('#user-edit-name').val(),
      email:    $('#user-edit-email').val(),
      role:     $('#user-edit-role').val(),
    }).always(function() {
      // toggle loading
      me.toggleClass('disabled').find('.loading').toggleClass('hidden');
    }).fail(function(r) {
      alert('Error ' + r.responseText);
    }).done(function(r) {
      console.log(r);
      $('#users-reload').click();
      $('#user-edit-dialog').modal('hide');
    });

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