BigW Consortium Gitlab

application.js.coffee 5.8 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
19
#= require turbolinks
20
#= require autosave
21 22 23 24 25
#= require bootstrap
#= 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
35
#= require mousetrap
36
#= require mousetrap/pause
37 38 39
#= require shortcuts
#= require shortcuts_navigation
#= require shortcuts_dashboard_navigation
40
#= require shortcuts_issuable
41
#= require shortcuts_network
42
#= require cal-heatmap
43
#= require jquery.nicescroll.min
44 45
#= require_tree .

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 54 55 56 57
window.split = (val) ->
  return val.split( /,\s*/ )

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

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

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

66
  closest_submit.disable() if rstrip(field.val()) is ""
67

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

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

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

  updateButtons()
  form.keyup(updateButtons)
89

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

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

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

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

101 102
window.addEventListener "hashchange", shiftWindow

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

109
$ ->
110 111
  $(".nicescroll").niceScroll(cursoropacitymax: '0.4', cursorcolor: '#FFF', cursorborder: "1px solid #FFF")

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

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

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

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

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

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

137
  # Initialize tooltips
138
  $('body').tooltip({
139
    selector: '.has_tooltip, [data-toggle="tooltip"], .page-sidebar-collapsed .nav-sidebar a'
140 141 142 143 144 145
    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
146 147
        # Otherwise use the data-placement attribute, or 'bottom' if undefined
        $el.data('placement') or 'bottom'
148
  })
149

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

154
  $('abbr.timeago, .js-timeago').timeago()
155

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
162
  $('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) ->
Nihad Abbasov committed
163
    buttons = $('[type="submit"]', @)
164 165 166

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

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

  # Commit show suppressed diff
175 176 177 178
  $(document).on 'click', '.diff-content .js-show-suppressed-diff', ->
    $container = $(@).parent()
    $container.next('table').show()
    $container.remove()
179

180 181 182
  $('.navbar-toggle').on 'click', ->
    $('.header-content .title').toggle()
    $('.header-content .navbar-collapse').toggle()
183
    $('.navbar-toggle').toggleClass('active')
184

185 186
  # Show/hide comments on diff
  $("body").on "click", ".js-toggle-diff-comments", (e) ->
187
    $(@).toggleClass('active')
188 189 190
    $(@).closest(".diff-file").find(".notes_holder").toggle()
    e.preventDefault()

191
  $(document).off "click", '.js-confirm-danger'
192 193 194 195 196 197 198
  $(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)

199
  new Aside()