application.js.coffee 7.81 KB
Newer Older
1 2 3 4 5 6
# 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.
#
Phil Hughes's avatar
Phil Hughes committed
7
#= require jquery2
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
MarkPochert's avatar
MarkPochert committed
21
#= require turbolinks
22
#= require autosave
connorshea's avatar
connorshea committed
23 24 25 26 27 28 29 30 31 32 33
#= require bootstrap/affix
#= require bootstrap/alert
#= require bootstrap/button
#= require bootstrap/collapse
#= require bootstrap/dropdown
#= require bootstrap/modal
#= require bootstrap/scrollspy
#= require bootstrap/tab
#= require bootstrap/transition
#= require bootstrap/tooltip
#= require bootstrap/popover
34 35
#= require select2
#= require raphael
36
#= require g.raphael
37
#= require g.bar
38 39
#= require branch-graph
#= require ace/ace
40
#= require ace/ext-searchbox
41
#= require underscore
42
#= require dropzone
43
#= require mousetrap
44
#= require mousetrap/pause
45 46 47
#= require shortcuts
#= require shortcuts_navigation
#= require shortcuts_dashboard_navigation
48
#= require shortcuts_issuable
49
#= require shortcuts_network
50
#= require jquery.nicescroll
51
#= require date.format
52 53 54 55 56 57
#= require_directory ./behaviors
#= require_directory ./blob
#= require_directory ./ci
#= require_directory ./commit
#= require_directory ./extensions
#= require_directory ./lib
58
#= require_directory ./u2f
59
#= require_directory .
60
#= require fuzzaldrin-plus
61
#= require cropper
62
#= require u2f
63

Robert Speicher's avatar
Robert Speicher committed
64 65 66 67 68 69
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
70 71 72 73 74 75
window.split = (val) ->
  return val.split( /,\s*/ )

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

76
window.rstrip = (val) ->
77
  return if val then val.replace(/\s+$/, '') else val
78

Nihad Abbasov's avatar
Nihad Abbasov committed
79
# Disable button if text field is empty
Robert Speicher's avatar
Robert Speicher committed
80 81
window.disableButtonIfEmptyField = (field_selector, button_selector) ->
  field = $(field_selector)
82
  closest_submit = field.closest('form').find(button_selector)
Robert Speicher's avatar
Robert Speicher committed
83

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

86
  field.on 'input', ->
87 88 89 90 91 92
    if rstrip($(@).val()) is ""
      closest_submit.disable()
    else
      closest_submit.enable()

# Disable button if any input field with given selector is empty
93
window.disableButtonIfAnyEmptyField = (form, form_selector, button_selector) ->
94
  closest_submit = form.find(button_selector)
95 96
  updateButtons = ->
    filled = true
97
    form.find('input').filter(form_selector).each ->
98
      filled = rstrip($(this).val()) != "" || !$(this).attr('required')
99

100
    if filled
101
      closest_submit.enable()
102 103 104 105 106
    else
      closest_submit.disable()

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

108 109 110
window.sanitize = (str) ->
  return str.replace(/<(?:.|\n)*?>/gm, '')

111 112
window.unbindEvents = ->
  $(document).off('scroll')
113

114
window.shiftWindow = ->
115
  scrollBy 0, -100
116

117
document.addEventListener("page:fetch", unbindEvents)
118

119 120
window.addEventListener "hashchange", shiftWindow

121 122 123 124 125 126
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
127
$ ->
Phil Hughes's avatar
Phil Hughes committed
128
  bootstrapBreakpoint = bp.getBreakpointSize()
129

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

132
  # Click a .js-select-on-focus field, select the contents
133 134 135 136
  $(".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
137

138 139 140
  $('.remove-row').bind 'ajax:success', ->
    $(this).closest('li').fadeOut()

141 142 143 144 145 146
  $('.js-remove-tr').bind 'ajax:before', ->
    $(this).hide()

  $('.js-remove-tr').bind 'ajax:success', ->
    $(this).closest('tr').fadeOut()

147
  # Initialize select2 selects
148
  $('select.select2').select2(width: 'resolve', dropdownAutoWidth: true)
149

150 151 152 153 154 155 156
  # Close select2 on escape
  $('.js-select2').bind 'select2-close', ->
    setTimeout ( ->
      $('.select2-container-active').removeClass('select2-container-active')
      $(':focus').blur()
    ), 1

157
  # Initialize tooltips
158
  $('body').tooltip(
159
    selector: '.has-tooltip, [data-toggle="tooltip"]'
160 161
    placement: (_, el) ->
      $el = $(el)
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
      $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'
  )
177

178 179 180 181
  # Form submitter
  $('.trigger-submit').on 'change', ->
    $(@).parents('form').submit()

182
  gl.utils.localTimeAgo($('abbr.timeago, .js-timeago'), true)
183

Cyril's avatar
Cyril committed
184
  # Flash
185 186 187
  if (flash = $(".flash-container")).length > 0
    flash.click -> $(@).fadeOut()
    flash.show()
188

189
  # Disable form buttons while a form is submitting
Robert Speicher's avatar
Robert Speicher committed
190
  $('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) ->
Nihad Abbasov's avatar
Nihad Abbasov committed
191
    buttons = $('[type="submit"]', @)
Robert Speicher's avatar
Robert Speicher committed
192 193 194

    switch e.type
      when 'ajax:beforeSend', 'submit'
195
        buttons.disable()
Robert Speicher's avatar
Robert Speicher committed
196
      else
197
        buttons.enable()
Robert Speicher's avatar
Robert Speicher committed
198 199

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

  # Commit show suppressed diff
203 204 205 206
  $(document).on 'click', '.diff-content .js-show-suppressed-diff', ->
    $container = $(@).parent()
    $container.next('table').show()
    $container.remove()
Robert Speicher's avatar
Robert Speicher committed
207

208 209 210
  $('.navbar-toggle').on 'click', ->
    $('.header-content .title').toggle()
    $('.header-content .navbar-collapse').toggle()
211
    $('.navbar-toggle').toggleClass('active')
Annabel Dunstone's avatar
Annabel Dunstone committed
212
    $('.navbar-toggle i').toggleClass("fa-angle-right fa-angle-left")
213

214 215
  # Show/hide comments on diff
  $("body").on "click", ".js-toggle-diff-comments", (e) ->
216
    $(@).toggleClass('active')
217 218 219
    $(@).closest(".diff-file").find(".notes_holder").toggle()
    e.preventDefault()

220
  $(document).off "click", '.js-confirm-danger'
221 222 223 224 225 226 227
  $(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)

228 229 230 231
  $('input[type="search"]').each ->
    $this = $(this)
    $this.attr 'value', $this.val()
    return
232

233 234 235 236 237 238
  $(document)
    .off 'keyup', 'input[type="search"]'
    .on 'keyup', 'input[type="search"]' , (e) ->
      $this = $(this)
      $this.attr 'value', $this.val()

239 240 241
  $sidebarGutterToggle = $('.js-sidebar-toggle')
  $navIconToggle = $('.toggle-nav-collapse')

242 243 244 245
  $(document)
    .off 'breakpoint:change'
    .on 'breakpoint:change', (e, breakpoint) ->
      if breakpoint is 'sm' or breakpoint is 'xs'
246
        $gutterIcon = $sidebarGutterToggle.find('i')
247
        if $gutterIcon.hasClass('fa-angle-double-right')
248 249 250 251 252
          $sidebarGutterToggle.trigger('click')

        $navIcon = $navIconToggle.find('.fa')
        if $navIcon.hasClass('fa-angle-left')
          $navIconToggle.trigger('click')
253

254
  fitSidebarForSize = ->
255
    oldBootstrapBreakpoint = bootstrapBreakpoint
Phil Hughes's avatar
Phil Hughes committed
256
    bootstrapBreakpoint = bp.getBreakpointSize()
257
    if bootstrapBreakpoint != oldBootstrapBreakpoint
258 259 260
      $(document).trigger('breakpoint:change', [bootstrapBreakpoint])

  checkInitialSidebarSize = ->
Phil Hughes's avatar
Phil Hughes committed
261
    bootstrapBreakpoint = bp.getBreakpointSize()
262 263 264
    if bootstrapBreakpoint is "xs" or "sm"
      $(document).trigger('breakpoint:change', [bootstrapBreakpoint])

265 266 267 268
  $(window)
    .off "resize"
    .on "resize", (e) ->
      fitSidebarForSize()
269

270
  checkInitialSidebarSize()
271
  new Aside()