blob_gitignore_selector.js.coffee 1.36 KB
Newer Older
Alfredo Sumaran's avatar
Alfredo Sumaran committed
1
class @BlobGitignoreSelector
2 3 4 5
  constructor: (opts) ->
    {
      @dropdown
      @editor
Alfredo Sumaran's avatar
Alfredo Sumaran committed
6 7
      @$wrapper        = @dropdown.closest('.gitignore-selector')
      @$filenameInput  = $('#file_name')
8 9 10 11 12 13 14 15
      @data           = @dropdown.data('filenames')
    } = opts

    @dropdown.glDropdown(
      data: @data,
      filterable: true,
      selectable: true,
      search:
Alfredo Sumaran's avatar
Alfredo Sumaran committed
16 17 18 19
        fields: ['name']
      clicked: @onClick
      text: (gitignore) ->
        gitignore.name
20 21
    )

Alfredo Sumaran's avatar
Alfredo Sumaran committed
22
    @toggleGitignoreSelector()
23 24 25
    @bindEvents()

  bindEvents: ->
Alfredo Sumaran's avatar
Alfredo Sumaran committed
26
    @$filenameInput
27
      .on 'keyup blur', (e) =>
Alfredo Sumaran's avatar
Alfredo Sumaran committed
28
        @toggleGitignoreSelector()
29

Alfredo Sumaran's avatar
Alfredo Sumaran committed
30 31 32
  toggleGitignoreSelector: ->
    filename = @$filenameInput.val() or $('.editor-file-name').text().trim()
    @$wrapper.toggleClass 'hidden', filename isnt '.gitignore'
33

Alfredo Sumaran's avatar
Alfredo Sumaran committed
34
  onClick: (item, el, e) =>
Alfredo Sumaran's avatar
Alfredo Sumaran committed
35
    e.preventDefault()
Alfredo Sumaran's avatar
Alfredo Sumaran committed
36
    @requestIgnoreFile(item.name)
37 38

  requestIgnoreFile: (name) ->
Alfredo Sumaran's avatar
Alfredo Sumaran committed
39
    Api.gitignoreText name, @requestIgnoreFileSuccess.bind(@)
40 41

  requestIgnoreFileSuccess: (gitignore) ->
Alfredo Sumaran's avatar
Alfredo Sumaran committed
42
    @editor.setValue(gitignore.content, 1)
43 44
    @editor.focus()

Alfredo Sumaran's avatar
Alfredo Sumaran committed
45
class @BlobGitignoreSelectors
46 47
  constructor: (opts) ->
    {
Alfredo Sumaran's avatar
Alfredo Sumaran committed
48
      @$dropdowns = $('.js-gitignore-selector')
49 50 51
      @editor
    } = opts

Alfredo Sumaran's avatar
Alfredo Sumaran committed
52 53
    @$dropdowns.each (i, dropdown) =>
      $dropdown = $(dropdown)
54

Alfredo Sumaran's avatar
Alfredo Sumaran committed
55
      new BlobGitignoreSelector(
56
        dropdown: $dropdown,
Alfredo Sumaran's avatar
Alfredo Sumaran committed
57
        editor: @editor
58
      )