application.js.coffee 5.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# This is a manifest file that'll be compiled into including all the files listed below.
# Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
# be included in the compiled file accessible from http://example.com/assets/application.js
# It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
# the compiled file.
#
#= require jquery
#= require jquery.ui.all
#= require jquery_ujs
#= require jquery.cookie
#= require jquery.endless-scroll
#= require jquery.highlight
#= require jquery.history
#= require jquery.waitforimages
#= require jquery.atwho
16
#= require jquery.scrollTo
17 18
#= require jquery.blockUI
#= require jquery.turbolinks
MarkPochert's avatar
MarkPochert committed
19
#= require turbolinks
20
#= require bootstrap
21
#= require password_strength
22 23 24 25
#= require select2
#= require raphael
#= require g.raphael-min
#= require g.bar-min
26
#= require chart-lib.min
27 28
#= require branch-graph
#= require ace/ace
29
#= require ace/ext-searchbox
30 31 32 33
#= require d3
#= require underscore
#= require nprogress
#= require nprogress-turbolinks
34
#= require dropzone
Job van der Voort's avatar
Job van der Voort committed
35
#= require semantic-ui/sidebar
36
#= require mousetrap
37
#= require mousetrap/pause
38 39 40 41 42
#= require shortcuts
#= require shortcuts_navigation
#= require shortcuts_dashboard_navigation
#= require shortcuts_issueable
#= require shortcuts_network
43 44
#= require_tree .

Robert Speicher's avatar
Robert Speicher committed
45 46 47 48 49 50
window.slugify = (text) ->
  text.replace(/[^-a-zA-Z0-9]+/g, '_').toLowerCase()

window.ajaxGet = (url) ->
  $.ajax({type: "GET", url: url, dataType: "script"})

51 52
window.showAndHide = (selector) ->

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
53 54 55 56 57 58
window.split = (val) ->
  return val.split( /,\s*/ )

window.extractLast = (term) ->
  return split( term ).pop()

59
window.rstrip = (val) ->
60
  return if val then val.replace(/\s+$/, '') else val
61

Nihad Abbasov's avatar
Nihad Abbasov committed
62
# Disable button if text field is empty
Robert Speicher's avatar
Robert Speicher committed
63 64
window.disableButtonIfEmptyField = (field_selector, button_selector) ->
  field = $(field_selector)
65
  closest_submit = field.closest('form').find(button_selector)
Robert Speicher's avatar
Robert Speicher committed
66

67
  closest_submit.disable() if rstrip(field.val()) is ""
Robert Speicher's avatar
Robert Speicher committed
68

69
  field.on 'input', ->
70 71 72 73 74 75
    if rstrip($(@).val()) is ""
      closest_submit.disable()
    else
      closest_submit.enable()

# Disable button if any input field with given selector is empty
76
window.disableButtonIfAnyEmptyField = (form, form_selector, button_selector) ->
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  closest_submit = form.find(button_selector)
  empty = false
  form.find('input').filter(form_selector).each ->
    empty = true if rstrip($(this).val()) is ""

  if empty
    closest_submit.disable()
  else
    closest_submit.enable()

  form.keyup ->
    empty = false
    form.find('input').filter(form_selector).each ->
      empty = true if rstrip($(this).val()) is ""

    if empty
93
      closest_submit.disable()
Robert Speicher's avatar
Robert Speicher committed
94
    else
95
      closest_submit.enable()
Robert Speicher's avatar
Robert Speicher committed
96

97 98 99 100 101 102 103
window.sanitize = (str) ->
  return str.replace(/<(?:.|\n)*?>/gm, '')

window.linkify = (str) ->
  exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig
  return str.replace(exp,"<a href='$1'>$1</a>")

104 105 106
window.simpleFormat = (str) ->
  linkify(sanitize(str).replace(/\n/g, '<br />'))

107
window.unbindEvents = ->
108
  $(document).unbind('scroll')
109
  $(document).off('scroll')
110

111 112 113
window.shiftWindow = ->
  scrollBy 0, -50

114
document.addEventListener("page:fetch", unbindEvents)
115

116 117 118 119 120 121
# Scroll the window to avoid the topnav bar
# https://github.com/twitter/bootstrap/issues/1768
if location.hash
  setTimeout shiftWindow, 1
window.addEventListener "hashchange", shiftWindow

Robert Speicher's avatar
Robert Speicher committed
122
$ ->
123

124
  # Click a .one_click_select field, select the contents
Nihad Abbasov's avatar
Nihad Abbasov committed
125
  $(".one_click_select").on 'click', -> $(@).select()
Robert Speicher's avatar
Robert Speicher committed
126

127 128 129
  $('.remove-row').bind 'ajax:success', ->
    $(this).closest('li').fadeOut()

130
  # Initialize select2 selects
131
  $('select.select2').select2(width: 'resolve', dropdownAutoWidth: true)
132

133 134 135 136 137 138 139
  # Close select2 on escape
  $('.js-select2').bind 'select2-close', ->
    setTimeout ( ->
      $('.select2-container-active').removeClass('select2-container-active')
      $(':focus').blur()
    ), 1

140 141 142
  # Initialize tooltips
  $('.has_tooltip').tooltip()

143 144 145
  # Bottom tooltip
  $('.has_bottom_tooltip').tooltip(placement: 'bottom')

146 147 148 149
  # Form submitter
  $('.trigger-submit').on 'change', ->
    $(@).parents('form').submit()

150
  $("abbr.timeago").timeago()
151
  $('.js-timeago').timeago()
152

Cyril's avatar
Cyril committed
153
  # Flash
154 155 156
  if (flash = $(".flash-container")).length > 0
    flash.click -> $(@).fadeOut()
    flash.show()
157

158
  # Disable form buttons while a form is submitting
Robert Speicher's avatar
Robert Speicher committed
159
  $('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) ->
Nihad Abbasov's avatar
Nihad Abbasov committed
160
    buttons = $('[type="submit"]', @)
Robert Speicher's avatar
Robert Speicher committed
161 162 163

    switch e.type
      when 'ajax:beforeSend', 'submit'
164
        buttons.disable()
Robert Speicher's avatar
Robert Speicher committed
165
      else
166
        buttons.enable()
Robert Speicher's avatar
Robert Speicher committed
167 168

  # Show/Hide the profile menu when hovering the account box
Nihad Abbasov's avatar
Nihad Abbasov committed
169
  $('.account-box').hover -> $(@).toggleClass('hover')
Robert Speicher's avatar
Robert Speicher committed
170 171

  # Commit show suppressed diff
172
  $(".diff-content").on "click", ".supp_diff_link", ->
Nihad Abbasov's avatar
Nihad Abbasov committed
173 174
    $(@).next('table').show()
    $(@).remove()
Robert Speicher's avatar
Robert Speicher committed
175

176 177 178
  # Show/hide comments on diff
  $("body").on "click", ".js-toggle-diff-comments", (e) ->
    $(@).find('i').
179 180
      toggleClass('fa fa-chevron-down').
      toggleClass('fa fa-chevron-up')
181 182 183
    $(@).closest(".diff-file").find(".notes_holder").toggle()
    e.preventDefault()

184 185 186 187 188 189 190
  $(document).on "click", '.js-confirm-danger', (e) ->
    e.preventDefault()
    btn = $(e.target)
    text = btn.data("confirm-danger-message")
    form = btn.closest("form")
    new ConfirmDangerModal(form, text)

Robert Speicher's avatar
Robert Speicher committed
191
(($) ->
192 193
  # Disable an element and add the 'disabled' Bootstrap class
  $.fn.extend disable: ->
Nihad Abbasov's avatar
Nihad Abbasov committed
194
    $(@).attr('disabled', 'disabled').addClass('disabled')
195 196 197

  # Enable an element and remove the 'disabled' Bootstrap class
  $.fn.extend enable: ->
Nihad Abbasov's avatar
Nihad Abbasov committed
198
    $(@).removeAttr('disabled').removeClass('disabled')
199

Robert Speicher's avatar
Robert Speicher committed
200
)(jQuery)