summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/profile/profile.js
blob: ed1d87abafed50b7f42e28d07cbf227b456c8e39 (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
(function() {
  var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

  this.Profile = (function() {
    function Profile(opts) {
      var cropOpts, ref;
      if (opts == null) {
        opts = {};
      }
      this.onSubmitForm = bind(this.onSubmitForm, this);
      this.form = (ref = opts.form) != null ? ref : $('.edit-user');
      $('.js-preferences-form').on('change.preference', 'input[type=radio]', function() {
        return $(this).parents('form').submit();
      });
      $('#user_notification_email').on('change', function() {
        return $(this).parents('form').submit();
      });
      $('.update-username').on('ajax:before', function() {
        $('.loading-username').show();
        $(this).find('.update-success').hide();
        return $(this).find('.update-failed').hide();
      });
      $('.update-username').on('ajax:complete', function() {
        $('.loading-username').hide();
        $(this).find('.btn-save').enable();
        return $(this).find('.loading-gif').hide();
      });
      $('.update-notifications').on('ajax:success', function(e, data) {
        if (data.saved) {
          return new Flash("Notification settings saved", "notice");
        } else {
          return new Flash("Failed to save new settings", "alert");
        }
      });
      this.bindEvents();
      cropOpts = {
        filename: '.js-avatar-filename',
        previewImage: '.avatar-image .avatar',
        modalCrop: '.modal-profile-crop',
        pickImageEl: '.js-choose-user-avatar-button',
        uploadImageBtn: '.js-upload-user-avatar',
        modalCropImg: '.modal-profile-crop-image'
      };
      this.avatarGlCrop = $('.js-user-avatar-input').glCrop(cropOpts).data('glcrop');
    }

    Profile.prototype.bindEvents = function() {
      return this.form.on('submit', this.onSubmitForm);
    };

    Profile.prototype.onSubmitForm = function(e) {
      e.preventDefault();
      return this.saveForm();
    };

    Profile.prototype.saveForm = function() {
      var avatarBlob, formData, self;
      self = this;
      formData = new FormData(this.form[0]);
      avatarBlob = this.avatarGlCrop.getBlob();
      if (avatarBlob != null) {
        formData.append('user[avatar]', avatarBlob, 'avatar.png');
      }
      return $.ajax({
        url: this.form.attr('action'),
        type: this.form.attr('method'),
        data: formData,
        dataType: "json",
        processData: false,
        contentType: false,
        success: function(response) {
          return new Flash(response.message, 'notice');
        },
        error: function(jqXHR) {
          return new Flash(jqXHR.responseJSON.message, 'alert');
        },
        complete: function() {
          window.scrollTo(0, 0);
          return self.form.find(':input[disabled]').enable();
        }
      });
    };

    return Profile;

  })();

  $(function() {
    $(document).on('focusout.ssh_key', '#key_key', function() {
      var $title, comment;
      $title = $('#key_title');
      comment = $(this).val().match(/^\S+ \S+ (.+)\n?$/);
      if (comment && comment.length > 1 && $title.val() === '') {
        return $title.val(comment[1]).change();
      }
    });
    if (gl.utils.getPagePath() === 'profiles') {
      return new Profile();
    }
  });

}).call(this);