aboutsummaryrefslogtreecommitdiff
path: root/data/assets/js/admin/dialogs/user-add.js
blob: 692d8a43c0f748920d285e299a9bf47777d5682b (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-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('test/add_user', {
      name:     $('#user-add-name').val(),
      email:    $('#user-add-email').val(),
      password: $('#user-add-password').val(),
      role:     $('#user-add-role').val(),
      active:   't',
    }).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-add-dialog').modal('hide');
    });

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