application.js.coffee 8.2 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 9
#= require jquery-ui/autocomplete
#= require jquery-ui/datepicker
10
#= require jquery-ui/draggable
11 12
#= require jquery-ui/effect-highlight
#= require jquery-ui/sortable
13 14 15 16 17 18
#= require jquery_ujs
#= require jquery.cookie
#= require jquery.endless-scroll
#= require jquery.highlight
#= require jquery.waitforimages
#= require jquery.atwho
19
#= require jquery.scrollTo
20
#= require jquery.turbolinks
Robert Speicher's avatar
Robert Speicher committed
21 22
#= require d3
#= require cal-heatmap
MarkPochert's avatar
MarkPochert committed
23
#= require turbolinks
24
#= require autosave
25 26 27
#= require bootstrap
#= require select2
#= require raphael
28
#= require g.raphael
29
#= require g.bar
30
#= require Chart
31 32
#= require branch-graph
#= require ace/ace
33
#= require ace/ext-searchbox
34
#= require underscore
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 jquery.nicescroll
44
#= require_tree .
45
#= require fuzzaldrin-plus
46

Robert Speicher's avatar
Robert Speicher committed
47 48 49 50 51 52
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
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
  closest_submit = form.find(button_selector)
78 79
  updateButtons = ->
    filled = true
80
    form.find('input').filter(form_selector).each ->
81
      filled = rstrip($(this).val()) != "" || !$(this).attr('required')
82

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

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

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

94 95
window.unbindEvents = ->
  $(document).off('scroll')
96

97
window.shiftWindow = ->
98
  scrollBy 0, -100
99

100
document.addEventListener("page:fetch", unbindEvents)
101

102 103
window.addEventListener "hashchange", shiftWindow

104 105 106 107 108 109
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
110
$ ->
Phil Hughes's avatar
Phil Hughes committed
111
  bootstrapBreakpoint = bp.getBreakpointSize()
112

113 114
  $(".nicescroll").niceScroll(cursoropacitymax: '0.4', cursorcolor: '#FFF', cursorborder: "1px solid #FFF")

115
  # Click a .js-select-on-focus field, select the contents
116 117 118 119
  $(".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
120

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

124 125 126 127 128 129
  $('.js-remove-tr').bind 'ajax:before', ->
    $(this).hide()

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

161 162 163 164
  # Form submitter
  $('.trigger-submit').on 'change', ->
    $(@).parents('form').submit()

165
  $('abbr.timeago, .js-timeago').timeago()
166

Cyril's avatar
Cyril committed
167
  # Flash
168 169 170
  if (flash = $(".flash-container")).length > 0
    flash.click -> $(@).fadeOut()
    flash.show()
171

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

    switch e.type
      when 'ajax:beforeSend', 'submit'
178
        buttons.disable()
Robert Speicher's avatar
Robert Speicher committed
179
      else
180
        buttons.enable()
Robert Speicher's avatar
Robert Speicher committed
181 182

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

  # Commit show suppressed diff
186 187 188 189
  $(document).on 'click', '.diff-content .js-show-suppressed-diff', ->
    $container = $(@).parent()
    $container.next('table').show()
    $container.remove()
Robert Speicher's avatar
Robert Speicher committed
190

191 192 193
  $('.navbar-toggle').on 'click', ->
    $('.header-content .title').toggle()
    $('.header-content .navbar-collapse').toggle()
194
    $('.navbar-toggle').toggleClass('active')
195

196 197
  # Show/hide comments on diff
  $("body").on "click", ".js-toggle-diff-comments", (e) ->
198
    $(@).toggleClass('active')
199 200 201
    $(@).closest(".diff-file").find(".notes_holder").toggle()
    e.preventDefault()

202
  $(document).off "click", '.js-confirm-danger'
203 204 205 206 207 208 209
  $(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)

210 211 212 213
  $('input[type="search"]').each ->
    $this = $(this)
    $this.attr 'value', $this.val()
    return
214

215 216 217 218 219 220 221 222 223 224
  $(document)
    .off 'keyup', 'input[type="search"]'
    .on 'keyup', 'input[type="search"]' , (e) ->
      $this = $(this)
      $this.attr 'value', $this.val()

  $(document)
    .off 'breakpoint:change'
    .on 'breakpoint:change', (e, breakpoint) ->
      if breakpoint is 'sm' or breakpoint is 'xs'
225
        $gutterIcon = $('.js-sidebar-toggle').find('i')
226 227 228 229
        if $gutterIcon.hasClass('fa-angle-double-right')
          $gutterIcon.closest('a').trigger('click')

  $(document)
230 231
    .off 'click', '.js-sidebar-toggle'
    .on 'click', '.js-sidebar-toggle', (e, triggered) ->
232 233 234
      e.preventDefault()
      $this = $(this)
      $thisIcon = $this.find 'i'
235
      $allGutterToggleIcons = $('.js-sidebar-toggle i')
236
      if $thisIcon.hasClass('fa-angle-double-right')
237
        $allGutterToggleIcons
Jacob Schatz's avatar
Jacob Schatz committed
238
          .removeClass('fa-angle-double-right')
239
          .addClass('fa-angle-double-left')
240
        $('aside.right-sidebar')
241 242 243 244 245 246
          .removeClass('right-sidebar-expanded')
          .addClass('right-sidebar-collapsed')
        $('.page-with-sidebar')
          .removeClass('right-sidebar-expanded')
          .addClass('right-sidebar-collapsed')
      else
247
        $allGutterToggleIcons
Jacob Schatz's avatar
Jacob Schatz committed
248
          .removeClass('fa-angle-double-left')
249
          .addClass('fa-angle-double-right')
250
        $('aside.right-sidebar')
251 252 253 254 255
          .removeClass('right-sidebar-collapsed')
          .addClass('right-sidebar-expanded')
        $('.page-with-sidebar')
          .removeClass('right-sidebar-collapsed')
          .addClass('right-sidebar-expanded')
256
      if not triggered
257 258 259
        $.cookie("collapsed_gutter",
          $('.right-sidebar')
            .hasClass('right-sidebar-collapsed'), { path: '/' })
260

261
  fitSidebarForSize = ->
262
    oldBootstrapBreakpoint = bootstrapBreakpoint
Phil Hughes's avatar
Phil Hughes committed
263
    bootstrapBreakpoint = bp.getBreakpointSize()
264
    if bootstrapBreakpoint != oldBootstrapBreakpoint
265 266 267
      $(document).trigger('breakpoint:change', [bootstrapBreakpoint])

  checkInitialSidebarSize = ->
Phil Hughes's avatar
Phil Hughes committed
268
    bootstrapBreakpoint = bp.getBreakpointSize()
269 270 271
    if bootstrapBreakpoint is "xs" or "sm"
      $(document).trigger('breakpoint:change', [bootstrapBreakpoint])

272 273 274 275
  $(window)
    .off "resize"
    .on "resize", (e) ->
      fitSidebarForSize()
276

277
  checkInitialSidebarSize()
278
  new Aside()