summaryrefslogtreecommitdiff
path: root/users/app/assets/javascripts/users.js
blob: aaeba6e5ae70492bcecf31ed4afe47103b46b504 (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
(function() {
  //
  // LOCAL FUNCTIONS
  //

  var poll_users, 
      prevent_default, 
      form_failed, 
      form_passed, 
      clear_errors,
      update_user;

  prevent_default = function(event) {
    return event.preventDefault();
  };

  poll_users = function(query, process) {
    return $.get("/1/users.json", {
      query: query
    }).done(process);
  };

  clear_errors = function() {
    return $('#messages').empty();
  };

  update_user = function(submitEvent) {
    var form = submitEvent.target;
    var token = form.dataset.token;
    var url = form.action;
    var req = $.ajax({
      url: url,
      type: 'PUT',
      headers: { Authorization: 'Token token="' + token + '"' },
      data: $(form).serialize()
    });
    req.done( function() {
      $(form).find('input[type="submit"]').button('reset');
    });
  };

  markAsSubmitted = function(submitEvent) {
    var form = submitEvent.target;
    $(form).addClass('submitted')
    // bootstrap loading state:
    $(form).find('input[type="submit"]').button('loading');
  };

  //
  // PUBLIC FUNCTIONS
  //

  srp.session = new srp.Session();

  srp.signedUp = function() {
    return srp.login();
  };

  srp.loggedIn = function() {
    return window.location = '/';
  };

  srp.updated = function() {
    return window.location = '/users/' + srp.session.id();
  };

  //
  // if a json request returns an error, this function gets called and
  // decorates the appropriate fields with the error messages.
  //
  srp.error = function(message) {
    clear_errors();
    var element, error, field;
    if ($.isPlainObject(message) && message.errors) {
      for (field in message.errors) {
        if (field == 'base') {
          alert_message(message.errors[field]);
          continue;
        }
        error = message.errors[field];
        element = $('form input[name$="[' + field + ']"]');
        if (!element) {
          continue;
        }
        element.trigger('element:validate:fail.ClientSideValidations', error).data('valid', false);
      }
    } else if (message.error) {
      alert_message(message.error);
    } else {
      alert_message(JSON.stringify(message));
    }
  };

  //
  // INIT
  //

  $(document).ready(function() {
    $('form').submit(markAsSubmitted);
    $('#new_user').submit(prevent_default);
    $('#new_user').submit(srp.signup);
    $('#new_session').submit(prevent_default);
    $('#new_session').submit(srp.login);
    $('#update_login_and_password').submit(prevent_default);
    $('#update_login_and_password').submit(srp.update);
    $('#update_pgp_key').submit(prevent_default);
    $('#update_pgp_key').submit(update_user);
    return $('#user-typeahead').typeahead({
      source: poll_users
    });
  });

}).call(this);