application.js.coffee 5.82 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
yglukhov's avatar
yglukhov committed
19
#= require jquery.sticky-kit.min
MarkPochert's avatar
MarkPochert committed
20
#= require turbolinks
21
#= require autosave
22 23 24 25 26
#= require bootstrap
#= require select2
#= require raphael
#= require g.raphael-min
#= require g.bar-min
27
#= require chart-lib.min
28 29
#= require branch-graph
#= require ace/ace
30
#= require ace/ext-searchbox
31 32 33 34
#= require d3
#= require underscore
#= require nprogress
#= require nprogress-turbolinks
35
#= require dropzone
36
#= require mousetrap
37
#= require mousetrap/pause
38 39 40
#= require shortcuts
#= require shortcuts_navigation
#= require shortcuts_dashboard_navigation
41
#= require shortcuts_issuable
42
#= require shortcuts_network
43
#= require cal-heatmap
44 45
#= require_tree .

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

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

52 53
window.showAndHide = (selector) ->

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

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

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

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

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

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

# Disable button if any input field with given selector is empty
77
window.disableButtonIfAnyEmptyField = (form, form_selector, button_selector) ->
78
  closest_submit = form.find(button_selector)
79 80
  updateButtons = ->
    filled = true
81
    form.find('input').filter(form_selector).each ->
82
      filled = rstrip($(this).val()) != "" || !$(this).attr('required')
83

84
    if filled
85
      closest_submit.enable()
86 87 88 89 90
    else
      closest_submit.disable()

  updateButtons()
  form.keyup(updateButtons)
Robert Speicher's avatar
Robert Speicher committed
91

92 93 94 95 96 97 98
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>")

99 100 101
window.simpleFormat = (str) ->
  linkify(sanitize(str).replace(/\n/g, '<br />'))

102
window.unbindEvents = ->
103
  $(document).unbind('scroll')
104
  $(document).off('scroll')
105

106 107 108
window.shiftWindow = ->
  scrollBy 0, -50

109
document.addEventListener("page:fetch", unbindEvents)
110

111 112 113 114 115 116
# 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

117 118
$.timeago.settings.allowFuture = true

Robert Speicher's avatar
Robert Speicher committed
119
$ ->
120 121
  # Click a .js-select-on-focus field, select the contents
  $(".js-select-on-focus").on "focusin", -> $(this).select()
Robert Speicher's avatar
Robert Speicher committed
122

123 124 125
  $('.remove-row').bind 'ajax:success', ->
    $(this).closest('li').fadeOut()

126
  # Initialize select2 selects
127
  $('select.select2').select2(width: 'resolve', dropdownAutoWidth: true)
128

129 130 131 132 133 134 135
  # Close select2 on escape
  $('.js-select2').bind 'select2-close', ->
    setTimeout ( ->
      $('.select2-container-active').removeClass('select2-container-active')
      $(':focus').blur()
    ), 1

136
  # Initialize tooltips
137
  $('body').tooltip({
138
    selector: '.has_tooltip, [data-toggle="tooltip"], .page-sidebar-collapsed .nav-sidebar a'
139 140 141 142 143 144 145 146
    placement: (_, el) ->
      $el = $(el)
      if $el.attr('id') == 'js-shortcuts-home'
        # Place the logo tooltip on the right when collapsed, bottom when expanded
        $el.parents('header').hasClass('header-collapsed') and 'right' or 'bottom'
      else
        # Otherwise use the data-placement attribute like normal
        $el.data('placement')
147
  })
148

149 150 151 152
  # Form submitter
  $('.trigger-submit').on 'change', ->
    $(@).parents('form').submit()

153
  $("abbr.timeago").timeago()
154
  $('.js-timeago').timeago()
155

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

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

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

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

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

179 180
  # Show/hide comments on diff
  $("body").on "click", ".js-toggle-diff-comments", (e) ->
181
    $(@).toggleClass('active')
182 183 184
    $(@).closest(".diff-file").find(".notes_holder").toggle()
    e.preventDefault()

185
  $(document).off "click", '.js-confirm-danger'
186 187 188 189 190 191 192
  $(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)

193 194
  new Aside()

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

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

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