BigW Consortium Gitlab

application.js.coffee 5.43 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 committed
19
#= require jquery.sticky-kit.min
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 .

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 97 98
window.shiftWindow = ->
  scrollBy 0, -50

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

101 102 103 104 105 106
# 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

107
$ ->
108
  # Click a .js-select-on-focus field, select the contents
109 110 111 112
  $(".js-select-on-focus").on "focusin", ->
    # Prevent a mouseup event from deselecting the input
    $(this).select().one 'mouseup', (e) ->
      e.preventDefault()
113

114 115 116
  $('.remove-row').bind 'ajax:success', ->
    $(this).closest('li').fadeOut()

117
  # Initialize select2 selects
118
  $('select.select2').select2(width: 'resolve', dropdownAutoWidth: true)
119

120 121 122 123 124 125 126
  # Close select2 on escape
  $('.js-select2').bind 'select2-close', ->
    setTimeout ( ->
      $('.select2-container-active').removeClass('select2-container-active')
      $(':focus').blur()
    ), 1

127
  # Initialize tooltips
128
  $('body').tooltip({
129
    selector: '.has_tooltip, [data-toggle="tooltip"], .page-sidebar-collapsed .nav-sidebar a'
130 131 132 133 134 135
    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
136 137
        # Otherwise use the data-placement attribute, or 'bottom' if undefined
        $el.data('placement') or 'bottom'
138
  })
139

140 141 142 143
  # Form submitter
  $('.trigger-submit').on 'change', ->
    $(@).parents('form').submit()

144
  $("abbr.timeago").timeago()
145
  $('.js-timeago').timeago()
146

Cyril committed
147
  # Flash
148 149 150
  if (flash = $(".flash-container")).length > 0
    flash.click -> $(@).fadeOut()
    flash.show()
151

152
  # Disable form buttons while a form is submitting
153
  $('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) ->
Nihad Abbasov committed
154
    buttons = $('[type="submit"]', @)
155 156 157

    switch e.type
      when 'ajax:beforeSend', 'submit'
158
        buttons.disable()
159
      else
160
        buttons.enable()
161 162

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

  # Commit show suppressed diff
166
  $(".diff-content").on "click", ".supp_diff_link", ->
Nihad Abbasov committed
167 168
    $(@).next('table').show()
    $(@).remove()
169

170 171 172 173
  $('.navbar-toggle').on 'click', ->
    $('.header-content .title').toggle()
    $('.header-content .navbar-collapse').toggle()

174 175
  # Show/hide comments on diff
  $("body").on "click", ".js-toggle-diff-comments", (e) ->
176
    $(@).toggleClass('active')
177 178 179
    $(@).closest(".diff-file").find(".notes_holder").toggle()
    e.preventDefault()

180
  $(document).off "click", '.js-confirm-danger'
181 182 183 184 185 186 187
  $(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)

188
  new Aside()