summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/profile.js.coffee
blob: f81d7dd6e69a632cfee43bcf02c011a2af15b2c6 (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
class @Profile
  constructor: (opts = {}) ->
    {
      @form = $('.edit-user')
    } = opts

    # Automatically submit the Preferences form when any of its radio buttons change
    $('.js-preferences-form').on 'change.preference', 'input[type=radio]', ->
      $(this).parents('form').submit()

    $('.update-username').on 'ajax:before', ->
      $('.loading-username').show()
      $(this).find('.update-success').hide()
      $(this).find('.update-failed').hide()

    $('.update-username').on 'ajax:complete', ->
      $('.loading-username').hide()
      $(this).find('.btn-save').enable()
      $(this).find('.loading-gif').hide()

    $('.update-notifications').on 'ajax:complete', ->
      $(this).find('.btn-save').enable()

    @bindEvents()

    @avatarGlCrop = $('.js-user-avatar-input').glCrop().data 'glcrop'

  bindEvents: ->
    @form.on 'submit', @onSubmitForm

  onSubmitForm: (e) =>
    e.preventDefault()
    @saveForm()

  saveForm: ->
    self = @

    formData = new FormData(@form[0])
    formData.append('user[avatar]', @avatarGlCrop.getBlob(), 'avatar.png')

    $.ajax
      url: @form.attr('action')
      type: @form.attr('method')
      data: formData
      dataType: "json"
      processData: false
      contentType: false
      success: (response) ->
        new Flash(response.message, 'notice')
      error: (jqXHR) ->
        new Flash(jqXHR.responseJSON.message, 'alert')
      complete: ->
        window.scrollTo 0, 0
        self.form.find(':input[disabled]').enable()

$ ->
  # Extract the SSH Key title from its comment
  $(document).on 'focusout.ssh_key', '#key_key', ->
    $title  = $('#key_title')
    comment = $(@).val().match(/^\S+ \S+ (.+)\n?$/)

    if comment && comment.length > 1 && $title.val() == ''
      $title.val(comment[1]).change()