main.js.coffee 3.57 KB
Newer Older
Robert Speicher's avatar
Robert Speicher committed
1 2 3 4 5 6 7 8 9
window.updatePage = (data) ->
  $.ajax({type: "GET", url: location.href, data: data, dataType: "script"})

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

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

10 11
window.showAndHide = (selector) ->

12 13 14 15 16 17
window.errorMessage = (message) ->
  ehtml = $("<p>")
  ehtml.addClass("error_message")
  ehtml.html(message)
  ehtml

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
18 19 20 21 22 23
window.split = (val) ->
  return val.split( /,\s*/ )

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

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

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

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

37 38 39 40 41 42 43
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>")

44 45 46
window.simpleFormat = (str) ->
  linkify(sanitize(str).replace(/\n/g, '<br />'))

47 48 49 50 51 52
window.startSpinner = ->
  $('.turbolink-spinner').fadeIn()

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

53
window.unbindEvents = ->
54
  $(document).unbind('scroll')
55
  $(document).off('scroll')
56

57
document.addEventListener("page:fetch", startSpinner)
58
document.addEventListener("page:fetch", unbindEvents)
59
document.addEventListener("page:receive", stopSpinner)
60

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

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 71 72
  $('body').keydown (e) ->
    if e.which is 191
      new Shortcuts()
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
73

74 75 76
  # Initialize chosen selects
  $('select.chosen').chosen()

77 78 79
  # Initialize tooltips
  $('.has_tooltip').tooltip()

80 81 82
  # Bottom tooltip
  $('.has_bottom_tooltip').tooltip(placement: 'bottom')

83 84 85 86
  # Form submitter
  $('.trigger-submit').on 'change', ->
    $(@).parents('form').submit()

87 88
  $("abbr.timeago").timeago()

Cyril's avatar
Cyril committed
89
  # Flash
90 91 92 93
  if (flash = $(".flash-container")).length > 0
    flash.click -> $(@).fadeOut()
    flash.show()
    setTimeout (-> flash.fadeOut()), 3000
94

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

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

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

  # 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()

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

(($) ->
  _chosen = $.fn.chosen
  $.fn.extend chosen: (options) ->
    default_options = search_contains: "true"
    $.extend default_options, options
Nihad Abbasov's avatar
Nihad Abbasov committed
128
    _chosen.apply @, [default_options]
Robert Speicher's avatar
Robert Speicher committed
129

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

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

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