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

  $('#user-add-dialog').on('show.bs.modal', function() {
    $.each(['name', 'email', 'password'], function(i, id) {
      $('#user-add-' + id).val('');
    });
    $('#user-add-role').val('viewer');
    $('#user-add-active').val('inactive');
  });

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

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

      // stop event
      return false;
    }
  });

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

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

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

    send('user/add', {
      name:     $('#user-add-name').val(),
      email:    $('#user-add-email').val(),
      password: $('#user-add-password').val(),
      role:     $('#user-add-role').val(),
      active:   ($('#user-add-active').val() == 'active') ? 't' : 'f',
    }).always(function() {
      // toggle loading
      me.toggleClass('disabled').find('.loading').toggleClass('hidden');
    }).fail(function(r) {
      gripe(r, 'add user');
    }).done(function(r) {
      $('#users-reload').click();
      $('#user-add-dialog').modal('hide');
    });

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