main.js.coffee 3.57 KB
Newer Older
Robert Speicher's avatar
Robert Speicher committed
1 2 3 4 5 6
window.slugify = (text) ->
  text.replace(/[^-a-zA-Z0-9]+/g, '_').toLowerCase()

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

7 8
window.showAndHide = (selector) ->

9 10 11 12 13 14
window.errorMessage = (message) ->
  ehtml = $("<p>")
  ehtml.addClass("error_message")
  ehtml.html(message)
  ehtml

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
15 16 17 18 19 20
window.split = (val) ->
  return val.split( /,\s*/ )

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

Nihad Abbasov's avatar
Nihad Abbasov committed
21
# Disable button if text field is empty
Robert Speicher's avatar
Robert Speicher committed
22 23 24 25
window.disableButtonIfEmptyField = (field_selector, button_selector) ->
  field = $(field_selector)
  closest_submit = field.closest("form").find(button_selector)

26
  closest_submit.disable() if field.val() is ""
Robert Speicher's avatar
Robert Speicher committed
27

28
  field.on "input", ->
Nihad Abbasov's avatar
Nihad Abbasov committed
29
    if $(@).val() is ""
30
      closest_submit.disable()
Robert Speicher's avatar
Robert Speicher committed
31
    else
32
      closest_submit.enable()
Robert Speicher's avatar
Robert Speicher committed
33

34 35 36 37 38 39 40
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>")

41 42 43
window.simpleFormat = (str) ->
  linkify(sanitize(str).replace(/\n/g, '<br />'))

44 45 46 47 48 49
window.startSpinner = ->
  $('.turbolink-spinner').fadeIn()

window.stopSpinner = ->
  $('.turbolink-spinner').fadeOut()

50
window.unbindEvents = ->
51
  $(document).unbind('scroll')
52
  $(document).off('scroll')
53

54
document.addEventListener("page:fetch", startSpinner)
55
document.addEventListener("page:fetch", unbindEvents)
56
document.addEventListener("page:change", stopSpinner)
57

Robert Speicher's avatar
Robert Speicher committed
58
$ ->
59
  # Click a .one_click_select field, select the contents
Nihad Abbasov's avatar
Nihad Abbasov committed
60
  $(".one_click_select").on 'click', -> $(@).select()
Robert Speicher's avatar
Robert Speicher committed
61

62 63 64
  $('.remove-row').bind 'ajax:success', ->
    $(this).closest('li').fadeOut()

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
65
  # Click a .appear-link, appear-data fadeout
66
  $(".appear-link").on 'click', (e) ->
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
67
    $('.appear-data').fadeIn()
68 69
    e.preventDefault()

70
  # Initialize select2 selects
71
  $('select.select2').select2(width: 'resolve', dropdownAutoWidth: true)
72

73 74 75
  # Initialize tooltips
  $('.has_tooltip').tooltip()

76 77 78
  # Bottom tooltip
  $('.has_bottom_tooltip').tooltip(placement: 'bottom')

79 80 81 82
  # Form submitter
  $('.trigger-submit').on 'change', ->
    $(@).parents('form').submit()

83
  $("abbr.timeago").timeago()
84
  $('.js-timeago').timeago()
85

Cyril's avatar
Cyril committed
86
  # Flash
87 88 89
  if (flash = $(".flash-container")).length > 0
    flash.click -> $(@).fadeOut()
    flash.show()
90
    setTimeout (-> flash.fadeOut()), 5000
91

92
  # Disable form buttons while a form is submitting
Robert Speicher's avatar
Robert Speicher committed
93
  $('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) ->
Nihad Abbasov's avatar
Nihad Abbasov committed
94
    buttons = $('[type="submit"]', @)
Robert Speicher's avatar
Robert Speicher committed
95 96 97

    switch e.type
      when 'ajax:beforeSend', 'submit'
98
        buttons.disable()
Robert Speicher's avatar
Robert Speicher committed
99
      else
100
        buttons.enable()
Robert Speicher's avatar
Robert Speicher committed
101 102

  # Show/Hide the profile menu when hovering the account box
Nihad Abbasov's avatar
Nihad Abbasov committed
103
  $('.account-box').hover -> $(@).toggleClass('hover')
Robert Speicher's avatar
Robert Speicher committed
104 105 106 107 108 109 110 111 112 113

  # 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
114 115 116 117
      when 63
        new Shortcuts()
        e.preventDefault()

Robert Speicher's avatar
Robert Speicher committed
118 119

  # Commit show suppressed diff
120
  $(".content").on "click", ".supp_diff_link", ->
Nihad Abbasov's avatar
Nihad Abbasov committed
121 122
    $(@).next('table').show()
    $(@).remove()
Robert Speicher's avatar
Robert Speicher committed
123

124 125 126 127
  $(".content").on "click", ".js-details-expand", ->
    $(@).next('.js-details-contain').removeClass("hide")
    $(@).remove()

Robert Speicher's avatar
Robert Speicher committed
128
(($) ->
129 130
  # Disable an element and add the 'disabled' Bootstrap class
  $.fn.extend disable: ->
Nihad Abbasov's avatar
Nihad Abbasov committed
131
    $(@).attr('disabled', 'disabled').addClass('disabled')
132 133 134

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

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