BigW Consortium Gitlab

profile.js.coffee 2.38 KB
Newer Older
1
class @Profile
2 3 4 5 6
  constructor: (opts = {}) ->
    {
      @form = $('.edit-user')
    } = opts

7 8 9
    # 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()
10

11 12 13 14
    # Automatically submit email form when it changes
    $('#user_notification_email').on 'change', ->
      $(this).parents('form').submit()

Phil Hughes committed
15 16
    $('.update-username').on 'ajax:before', ->
      $('.loading-username').show()
17 18
      $(this).find('.update-success').hide()
      $(this).find('.update-failed').hide()
19

Phil Hughes committed
20 21
    $('.update-username').on 'ajax:complete', ->
      $('.loading-username').hide()
22
      $(this).find('.btn-save').enable()
23
      $(this).find('.loading-gif').hide()
24

25 26 27 28 29
    $('.update-notifications').on 'ajax:success', (e, data) ->
      if data.saved
        new Flash("Notification settings saved", "notice")
      else
        new Flash("Failed to save new settings", "alert")
30

31 32
    @bindEvents()

Alfredo Sumaran committed
33 34 35 36 37 38 39 40 41
    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'

    @avatarGlCrop = $('.js-user-avatar-input').glCrop(cropOpts).data 'glcrop'
42 43 44 45 46 47 48 49 50 51 52

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

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

  saveForm: ->
    self = @
    formData = new FormData(@form[0])
53 54 55

    avatarBlob = @avatarGlCrop.getBlob()
    formData.append('user[avatar]', avatarBlob, 'avatar.png') if avatarBlob?
56

57 58 59 60 61 62 63 64 65 66 67 68 69
    $.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
70
        # Enable submit button after requests ends
71
        self.form.find(':input[disabled]').enable()
72 73 74 75 76 77 78 79 80

$ ->
  # 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()