BigW Consortium Gitlab

application.js.coffee 8.77 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 committed
21 22
#= require d3
#= require cal-heatmap
23
#= require turbolinks
24
#= require autosave
connorshea committed
25 26 27 28 29 30 31 32 33 34 35
#= 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
36 37
#= require select2
#= require raphael
38
#= require g.raphael
39
#= require g.bar
40
#= require Chart
41 42
#= require branch-graph
#= require ace/ace
43
#= require ace/ext-searchbox
44
#= require underscore
45
#= require dropzone
46
#= require mousetrap
47
#= require mousetrap/pause
48 49 50
#= require shortcuts
#= require shortcuts_navigation
#= require shortcuts_dashboard_navigation
51
#= require shortcuts_issuable
52
#= require shortcuts_network
53
#= require jquery.nicescroll
54
#= require date.format
55
#= require_tree .
56
#= require fuzzaldrin-plus
57
#= require cropper
58

59 60 61 62 63 64
window.slugify = (text) ->
  text.replace(/[^-a-zA-Z0-9]+/g, '_').toLowerCase()

window.ajaxGet = (url) ->
  $.ajax({type: "GET", url: url, dataType: "script"})

65 66 67 68 69 70
window.split = (val) ->
  return val.split( /,\s*/ )

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

71
window.rstrip = (val) ->
72
  return if val then val.replace(/\s+$/, '') else val
73

Nihad Abbasov committed
74
# Disable button if text field is empty
75 76
window.disableButtonIfEmptyField = (field_selector, button_selector) ->
  field = $(field_selector)
77
  closest_submit = field.closest('form').find(button_selector)
78

79
  closest_submit.disable() if rstrip(field.val()) is ""
80

81
  field.on 'input', ->
82 83 84 85 86 87
    if rstrip($(@).val()) is ""
      closest_submit.disable()
    else
      closest_submit.enable()

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

95
    if filled
96
      closest_submit.enable()
97 98 99 100 101
    else
      closest_submit.disable()

  updateButtons()
  form.keyup(updateButtons)
102

103 104 105
window.sanitize = (str) ->
  return str.replace(/<(?:.|\n)*?>/gm, '')

106 107
window.unbindEvents = ->
  $(document).off('scroll')
108

109
window.shiftWindow = ->
110
  scrollBy 0, -100
111

112
document.addEventListener("page:fetch", unbindEvents)
113

114 115
window.addEventListener "hashchange", shiftWindow

116 117 118 119 120 121
window.onload = ->
  # Scroll the window to avoid the topnav bar
  # https://github.com/twitter/bootstrap/issues/1768
  if location.hash
    setTimeout shiftWindow, 100

122
$ ->
Phil Hughes committed
123
  bootstrapBreakpoint = bp.getBreakpointSize()
124

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

127
  # Click a .js-select-on-focus field, select the contents
128 129 130 131
  $(".js-select-on-focus").on "focusin", ->
    # Prevent a mouseup event from deselecting the input
    $(this).select().one 'mouseup', (e) ->
      e.preventDefault()
132

133 134 135
  $('.remove-row').bind 'ajax:success', ->
    $(this).closest('li').fadeOut()

136 137 138 139 140 141
  $('.js-remove-tr').bind 'ajax:before', ->
    $(this).hide()

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

142
  # Initialize select2 selects
143
  $('select.select2').select2(width: 'resolve', dropdownAutoWidth: true)
144

145 146 147 148 149 150 151
  # Close select2 on escape
  $('.js-select2').bind 'select2-close', ->
    setTimeout ( ->
      $('.select2-container-active').removeClass('select2-container-active')
      $(':focus').blur()
    ), 1

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

173 174 175 176
  # Form submitter
  $('.trigger-submit').on 'change', ->
    $(@).parents('form').submit()

177
  gl.utils.localTimeAgo($('abbr.timeago, .js-timeago'), true)
178

Cyril committed
179
  # Flash
180 181 182
  if (flash = $(".flash-container")).length > 0
    flash.click -> $(@).fadeOut()
    flash.show()
183

184
  # Disable form buttons while a form is submitting
185
  $('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) ->
Nihad Abbasov committed
186
    buttons = $('[type="submit"]', @)
187 188 189

    switch e.type
      when 'ajax:beforeSend', 'submit'
190
        buttons.disable()
191
      else
192
        buttons.enable()
193 194

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

  # Commit show suppressed diff
198 199 200 201
  $(document).on 'click', '.diff-content .js-show-suppressed-diff', ->
    $container = $(@).parent()
    $container.next('table').show()
    $container.remove()
202

203 204 205
  $('.navbar-toggle').on 'click', ->
    $('.header-content .title').toggle()
    $('.header-content .navbar-collapse').toggle()
206
    $('.navbar-toggle').toggleClass('active')
207

208 209
  # Show/hide comments on diff
  $("body").on "click", ".js-toggle-diff-comments", (e) ->
210
    $(@).toggleClass('active')
211 212 213
    $(@).closest(".diff-file").find(".notes_holder").toggle()
    e.preventDefault()

214
  $(document).off "click", '.js-confirm-danger'
215 216 217 218 219 220 221
  $(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)

222 223 224 225
  $('input[type="search"]').each ->
    $this = $(this)
    $this.attr 'value', $this.val()
    return
226

227 228 229 230 231 232
  $(document)
    .off 'keyup', 'input[type="search"]'
    .on 'keyup', 'input[type="search"]' , (e) ->
      $this = $(this)
      $this.attr 'value', $this.val()

233 234 235
  $sidebarGutterToggle = $('.js-sidebar-toggle')
  $navIconToggle = $('.toggle-nav-collapse')

236 237 238 239
  $(document)
    .off 'breakpoint:change'
    .on 'breakpoint:change', (e, breakpoint) ->
      if breakpoint is 'sm' or breakpoint is 'xs'
240
        $gutterIcon = $sidebarGutterToggle.find('i')
241
        if $gutterIcon.hasClass('fa-angle-double-right')
242 243 244 245 246
          $sidebarGutterToggle.trigger('click')

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

  $(document)
249 250
    .off 'click', '.js-sidebar-toggle'
    .on 'click', '.js-sidebar-toggle', (e, triggered) ->
251 252 253
      e.preventDefault()
      $this = $(this)
      $thisIcon = $this.find 'i'
254
      $allGutterToggleIcons = $('.js-sidebar-toggle i')
255
      if $thisIcon.hasClass('fa-angle-double-right')
256
        $allGutterToggleIcons
Jacob Schatz committed
257
          .removeClass('fa-angle-double-right')
258
          .addClass('fa-angle-double-left')
259
        $('aside.right-sidebar')
260 261 262 263 264 265
          .removeClass('right-sidebar-expanded')
          .addClass('right-sidebar-collapsed')
        $('.page-with-sidebar')
          .removeClass('right-sidebar-expanded')
          .addClass('right-sidebar-collapsed')
      else
266
        $allGutterToggleIcons
Jacob Schatz committed
267
          .removeClass('fa-angle-double-left')
268
          .addClass('fa-angle-double-right')
269
        $('aside.right-sidebar')
270 271 272 273 274
          .removeClass('right-sidebar-collapsed')
          .addClass('right-sidebar-expanded')
        $('.page-with-sidebar')
          .removeClass('right-sidebar-collapsed')
          .addClass('right-sidebar-expanded')
275
      if not triggered
276 277 278
        $.cookie("collapsed_gutter",
          $('.right-sidebar')
            .hasClass('right-sidebar-collapsed'), { path: '/' })
279

280
  fitSidebarForSize = ->
281
    oldBootstrapBreakpoint = bootstrapBreakpoint
Phil Hughes committed
282
    bootstrapBreakpoint = bp.getBreakpointSize()
283
    if bootstrapBreakpoint != oldBootstrapBreakpoint
284 285 286
      $(document).trigger('breakpoint:change', [bootstrapBreakpoint])

  checkInitialSidebarSize = ->
Phil Hughes committed
287
    bootstrapBreakpoint = bp.getBreakpointSize()
288 289 290
    if bootstrapBreakpoint is "xs" or "sm"
      $(document).trigger('breakpoint:change', [bootstrapBreakpoint])

291 292 293 294
  $(window)
    .off "resize"
    .on "resize", (e) ->
      fitSidebarForSize()
295

296
  checkInitialSidebarSize()
297
  new Aside()