main.js.coffee 3.29 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 47 48 49 50 51
window.startSpinner = ->
  $('.turbolink-spinner').fadeIn()

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

document.addEventListener("page:fetch", startSpinner)
document.addEventListener("page:receive", stopSpinner)
52

Robert Speicher's avatar
Robert Speicher committed
53
$ ->
54
  # Click a .one_click_select field, select the contents
Nihad Abbasov's avatar
Nihad Abbasov committed
55
  $(".one_click_select").on 'click', -> $(@).select()
Robert Speicher's avatar
Robert Speicher committed
56

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
57
  # Click a .appear-link, appear-data fadeout
58
  $(".appear-link").on 'click', (e) ->
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
59
    $('.appear-data').fadeIn()
60 61
    e.preventDefault()

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
62

63 64 65
  # Initialize chosen selects
  $('select.chosen').chosen()

66 67 68
  # Initialize tooltips
  $('.has_tooltip').tooltip()

69 70 71
  # Bottom tooltip
  $('.has_bottom_tooltip').tooltip(placement: 'bottom')

72 73 74 75
  # Form submitter
  $('.trigger-submit').on 'change', ->
    $(@).parents('form').submit()

76 77
  $("abbr.timeago").timeago()

Cyril's avatar
Cyril committed
78
  # Flash
79 80 81 82
  if (flash = $(".flash-container")).length > 0
    flash.click -> $(@).fadeOut()
    flash.show()
    setTimeout (-> flash.fadeOut()), 3000
83

84
  # Disable form buttons while a form is submitting
Robert Speicher's avatar
Robert Speicher committed
85
  $('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) ->
Nihad Abbasov's avatar
Nihad Abbasov committed
86
    buttons = $('[type="submit"]', @)
Robert Speicher's avatar
Robert Speicher committed
87 88 89

    switch e.type
      when 'ajax:beforeSend', 'submit'
90
        buttons.disable()
Robert Speicher's avatar
Robert Speicher committed
91
      else
92
        buttons.enable()
Robert Speicher's avatar
Robert Speicher committed
93 94

  # Show/Hide the profile menu when hovering the account box
Nihad Abbasov's avatar
Nihad Abbasov committed
95
  $('.account-box').hover -> $(@).toggleClass('hover')
Robert Speicher's avatar
Robert Speicher committed
96 97 98 99 100 101 102 103 104 105 106 107 108

  # 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
109 110
    $(@).next('table').show()
    $(@).remove()
Robert Speicher's avatar
Robert Speicher committed
111 112 113 114 115 116

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

119 120
  # Disable an element and add the 'disabled' Bootstrap class
  $.fn.extend disable: ->
Nihad Abbasov's avatar
Nihad Abbasov committed
121
    $(@).attr('disabled', 'disabled').addClass('disabled')
122 123 124

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

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