application.js.coffee 6.97 KB
Newer Older
1 2 3 4 5 6 7
# 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
8
#= require jquery-ui
9 10 11 12 13 14
#= require jquery_ujs
#= require jquery.cookie
#= require jquery.endless-scroll
#= require jquery.highlight
#= require jquery.waitforimages
#= require jquery.atwho
15
#= require jquery.scrollTo
16
#= require jquery.turbolinks
Robert Speicher's avatar
Robert Speicher committed
17 18
#= require d3
#= require cal-heatmap
MarkPochert's avatar
MarkPochert committed
19
#= require turbolinks
20
#= require autosave
21 22 23
#= require bootstrap
#= require select2
#= require raphael
24
#= require g.raphael
25
#= require g.bar
26
#= require Chart
27 28
#= require branch-graph
#= require ace/ace
29
#= require ace/ext-searchbox
30 31 32
#= require underscore
#= require nprogress
#= require nprogress-turbolinks
33
#= require dropzone
34
#= require mousetrap
35
#= require mousetrap/pause
36 37 38
#= require shortcuts
#= require shortcuts_navigation
#= require shortcuts_dashboard_navigation
39
#= require shortcuts_issuable
40
#= require shortcuts_network
41
#= require jquery.nicescroll
42
#= require_tree .
43
#= require fuzzaldrin-plus
44

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"})

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

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

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

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

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

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

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

81
    if filled
82
      closest_submit.enable()
83 84 85 86 87
    else
      closest_submit.disable()

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

89 90 91
window.sanitize = (str) ->
  return str.replace(/<(?:.|\n)*?>/gm, '')

92 93
window.unbindEvents = ->
  $(document).off('scroll')
94

95
window.shiftWindow = ->
96
  scrollBy 0, -100
97

98
document.addEventListener("page:fetch", unbindEvents)
99

100 101
window.addEventListener "hashchange", shiftWindow

102 103 104 105 106 107
window.onload = ->
  # Scroll the window to avoid the topnav bar
  # https://github.com/twitter/bootstrap/issues/1768
  if location.hash
    setTimeout shiftWindow, 100

Robert Speicher's avatar
Robert Speicher committed
108
$ ->
109 110
  $(".nicescroll").niceScroll(cursoropacitymax: '0.4', cursorcolor: '#FFF', cursorborder: "1px solid #FFF")

111
  # Click a .js-select-on-focus field, select the contents
112 113 114 115
  $(".js-select-on-focus").on "focusin", ->
    # Prevent a mouseup event from deselecting the input
    $(this).select().one 'mouseup', (e) ->
      e.preventDefault()
Robert Speicher's avatar
Robert Speicher committed
116

117 118 119
  $('.remove-row').bind 'ajax:success', ->
    $(this).closest('li').fadeOut()

120 121 122 123 124 125
  $('.js-remove-tr').bind 'ajax:before', ->
    $(this).hide()

  $('.js-remove-tr').bind 'ajax:success', ->
    $(this).closest('tr').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 138
  $('body').tooltip(
    selector: '.has_tooltip, [data-toggle="tooltip"]'
139 140
    placement: (_, el) ->
      $el = $(el)
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
      $el.data('placement') || 'bottom'
  )

  $('.header-logo .home').tooltip(
    placement: (_, el) ->
      $el = $(el)
      if $('.page-with-sidebar').hasClass('page-sidebar-collapsed') then 'right' else 'bottom'
    container: 'body'
  )

  $('.page-with-sidebar').tooltip(
    selector: '.sidebar-collapsed .nav-sidebar a, .sidebar-collapsed a.sidebar-user'
    placement: 'right'
    container: 'body'
  )
156

157 158 159 160
  # Form submitter
  $('.trigger-submit').on 'change', ->
    $(@).parents('form').submit()

161
  $('abbr.timeago, .js-timeago').timeago()
162

Cyril's avatar
Cyril committed
163
  # Flash
164 165 166
  if (flash = $(".flash-container")).length > 0
    flash.click -> $(@).fadeOut()
    flash.show()
167

168
  # Disable form buttons while a form is submitting
Robert Speicher's avatar
Robert Speicher committed
169
  $('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) ->
Nihad Abbasov's avatar
Nihad Abbasov committed
170
    buttons = $('[type="submit"]', @)
Robert Speicher's avatar
Robert Speicher committed
171 172 173

    switch e.type
      when 'ajax:beforeSend', 'submit'
174
        buttons.disable()
Robert Speicher's avatar
Robert Speicher committed
175
      else
176
        buttons.enable()
Robert Speicher's avatar
Robert Speicher committed
177 178

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

  # Commit show suppressed diff
182 183 184 185
  $(document).on 'click', '.diff-content .js-show-suppressed-diff', ->
    $container = $(@).parent()
    $container.next('table').show()
    $container.remove()
Robert Speicher's avatar
Robert Speicher committed
186

187 188 189
  $('.navbar-toggle').on 'click', ->
    $('.header-content .title').toggle()
    $('.header-content .navbar-collapse').toggle()
190
    $('.navbar-toggle').toggleClass('active')
191

192 193
  # Show/hide comments on diff
  $("body").on "click", ".js-toggle-diff-comments", (e) ->
194
    $(@).toggleClass('active')
195 196 197
    $(@).closest(".diff-file").find(".notes_holder").toggle()
    e.preventDefault()

198
  $(document).off "click", '.js-confirm-danger'
199 200 201 202 203 204 205
  $(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)

206 207 208 209 210 211 212 213 214
  $('input[type="search"]').each ->
    $this = $(this)
    $this.attr 'value', $this.val()
    return
    
  $(document).on 'keyup', 'input[type="search"]' , (e) ->
    $this = $(this)
    $this.attr 'value', $this.val()

215 216 217 218 219 220 221 222 223 224 225
  $('.right-sidebar').on 'click', '.gutter-toggle', (e) ->
    e.preventDefault()
    $this = $(this)
    $thisIcon = $this.find 'i'
    if $thisIcon.hasClass('fa-angle-double-right')
      $thisIcon.removeClass('fa-angle-double-right')
        .addClass('fa-angle-double-left')
      $this
        .closest('aside')
        .removeClass('right-sidebar-expanded')
        .addClass('right-sidebar-collapsed')
226 227 228
      $('.page-with-sidebar')
        .removeClass('right-sidebar-expanded')
        .addClass('right-sidebar-collapsed')
229 230 231 232 233 234 235
    else
      $thisIcon.removeClass('fa-angle-double-left')
        .addClass('fa-angle-double-right')
      $this
        .closest('aside')
        .removeClass('right-sidebar-collapsed')
        .addClass('right-sidebar-expanded')
236 237 238 239 240 241
      $('.page-with-sidebar')
        .removeClass('right-sidebar-collapsed')
        .addClass('right-sidebar-expanded')
    $.cookie("collapsed_gutter", 
      $('.right-sidebar')
        .hasClass('right-sidebar-collapsed'), { path: '/' })
242

243
  new Aside()