application.js.coffee 4.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 19 20 21 22 23 24 25
#= require jquery.blockUI
#= require turbolinks
#= require jquery.turbolinks
#= require bootstrap
#= require select2
#= require raphael
#= require g.raphael-min
#= require g.bar-min
#= require branch-graph
26
#= require highlight.pack
27 28 29 30 31
#= require ace/ace
#= require d3
#= require underscore
#= require nprogress
#= require nprogress-turbolinks
32
#= require dropzone
Job van der Voort's avatar
Job van der Voort committed
33
#= require semantic-ui/sidebar
34 35
#= require_tree .

Robert Speicher's avatar
Robert Speicher committed
36 37 38 39 40 41
window.slugify = (text) ->
  text.replace(/[^-a-zA-Z0-9]+/g, '_').toLowerCase()

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

42 43
window.showAndHide = (selector) ->

44 45 46 47 48 49
window.errorMessage = (message) ->
  ehtml = $("<p>")
  ehtml.addClass("error_message")
  ehtml.html(message)
  ehtml

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
50 51 52 53 54 55
window.split = (val) ->
  return val.split( /,\s*/ )

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

Nihad Abbasov's avatar
Nihad Abbasov committed
56
# Disable button if text field is empty
Robert Speicher's avatar
Robert Speicher committed
57 58 59 60
window.disableButtonIfEmptyField = (field_selector, button_selector) ->
  field = $(field_selector)
  closest_submit = field.closest("form").find(button_selector)

61
  closest_submit.disable() if field.val().replace(/\s+$/, "") is ""
Robert Speicher's avatar
Robert Speicher committed
62

63
  field.on "input", ->
64
    if $(@).val().replace(/\s+$/, "") is ""
65
      closest_submit.disable()
Robert Speicher's avatar
Robert Speicher committed
66
    else
67
      closest_submit.enable()
Robert Speicher's avatar
Robert Speicher committed
68

69 70 71 72 73 74 75
window.sanitize = (str) ->
  return str.replace(/<(?:.|\n)*?>/gm, '')

window.linkify = (str) ->
  exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig
  return str.replace(exp,"<a href='$1'>$1</a>")

76 77 78
window.simpleFormat = (str) ->
  linkify(sanitize(str).replace(/\n/g, '<br />'))

79
window.unbindEvents = ->
80
  $(document).unbind('scroll')
81
  $(document).off('scroll')
82

83
document.addEventListener("page:fetch", unbindEvents)
84

Robert Speicher's avatar
Robert Speicher committed
85
$ ->
86
  # Click a .one_click_select field, select the contents
Nihad Abbasov's avatar
Nihad Abbasov committed
87
  $(".one_click_select").on 'click', -> $(@).select()
Robert Speicher's avatar
Robert Speicher committed
88

89 90 91
  $('.remove-row').bind 'ajax:success', ->
    $(this).closest('li').fadeOut()

92
  # Initialize select2 selects
93
  $('select.select2').select2(width: 'resolve', dropdownAutoWidth: true)
94

95 96 97
  # Initialize tooltips
  $('.has_tooltip').tooltip()

98 99 100
  # Bottom tooltip
  $('.has_bottom_tooltip').tooltip(placement: 'bottom')

101 102 103 104
  # Form submitter
  $('.trigger-submit').on 'change', ->
    $(@).parents('form').submit()

105
  $("abbr.timeago").timeago()
106
  $('.js-timeago').timeago()
107

Cyril's avatar
Cyril committed
108
  # Flash
109 110 111
  if (flash = $(".flash-container")).length > 0
    flash.click -> $(@).fadeOut()
    flash.show()
112
    setTimeout (-> flash.fadeOut()), 5000
113

114
  # Disable form buttons while a form is submitting
Robert Speicher's avatar
Robert Speicher committed
115
  $('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) ->
Nihad Abbasov's avatar
Nihad Abbasov committed
116
    buttons = $('[type="submit"]', @)
Robert Speicher's avatar
Robert Speicher committed
117 118 119

    switch e.type
      when 'ajax:beforeSend', 'submit'
120
        buttons.disable()
Robert Speicher's avatar
Robert Speicher committed
121
      else
122
        buttons.enable()
Robert Speicher's avatar
Robert Speicher committed
123 124

  # Show/Hide the profile menu when hovering the account box
Nihad Abbasov's avatar
Nihad Abbasov committed
125
  $('.account-box').hover -> $(@).toggleClass('hover')
Robert Speicher's avatar
Robert Speicher committed
126 127 128 129 130 131 132 133 134 135

  # Focus search field by pressing 's' key
  $(document).keypress (e) ->
    # Don't do anything if typing in an input
    return if $(e.target).is(":input")

    switch e.which
      when 115
        $("#search").focus()
        e.preventDefault()
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
136 137 138 139
      when 63
        new Shortcuts()
        e.preventDefault()

Robert Speicher's avatar
Robert Speicher committed
140 141

  # Commit show suppressed diff
142
  $(".diff-content").on "click", ".supp_diff_link", ->
Nihad Abbasov's avatar
Nihad Abbasov committed
143 144
    $(@).next('table').show()
    $(@).remove()
Robert Speicher's avatar
Robert Speicher committed
145

146 147 148 149 150 151 152 153
  # Show/hide comments on diff
  $("body").on "click", ".js-toggle-diff-comments", (e) ->
    $(@).find('i').
      toggleClass('icon-chevron-down').
      toggleClass('icon-chevron-up')
    $(@).closest(".diff-file").find(".notes_holder").toggle()
    e.preventDefault()

Robert Speicher's avatar
Robert Speicher committed
154
(($) ->
155 156
  # Disable an element and add the 'disabled' Bootstrap class
  $.fn.extend disable: ->
Nihad Abbasov's avatar
Nihad Abbasov committed
157
    $(@).attr('disabled', 'disabled').addClass('disabled')
158 159 160

  # Enable an element and remove the 'disabled' Bootstrap class
  $.fn.extend enable: ->
Nihad Abbasov's avatar
Nihad Abbasov committed
161
    $(@).removeAttr('disabled').removeClass('disabled')
162

Robert Speicher's avatar
Robert Speicher committed
163
)(jQuery)