main.js.coffee 2.45 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 12 13 14 15
window.errorMessage = (message) ->
  ehtml = $("<p>")
  ehtml.addClass("error_message")
  ehtml.html(message)
  ehtml

Nihad Abbasov's avatar
Nihad Abbasov committed
16
# Disable button if text field is empty
Robert Speicher's avatar
Robert Speicher committed
17 18 19 20
window.disableButtonIfEmptyField = (field_selector, button_selector) ->
  field = $(field_selector)
  closest_submit = field.closest("form").find(button_selector)

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

23
  field.on "input", ->
Nihad Abbasov's avatar
Nihad Abbasov committed
24
    if $(@).val() is ""
25
      closest_submit.disable()
Robert Speicher's avatar
Robert Speicher committed
26
    else
27
      closest_submit.enable()
Robert Speicher's avatar
Robert Speicher committed
28 29

$ ->
30
  # Click a .one_click_select field, select the contents
Nihad Abbasov's avatar
Nihad Abbasov committed
31
  $(".one_click_select").on 'click', -> $(@).select()
Robert Speicher's avatar
Robert Speicher committed
32

33 34 35
  # Initialize chosen selects
  $('select.chosen').chosen()

36 37 38
  # Initialize tooltips
  $('.has_tooltip').tooltip()

39 40 41
  # Bottom tooltip
  $('.has_bottom_tooltip').tooltip(placement: 'bottom')

Cyril's avatar
Cyril committed
42 43 44 45 46
  # Flash
  if (flash = $("#flash-container")).length > 0
    flash.click -> $(@).slideUp("slow")
    flash.slideDown "slow"
    setTimeout (-> flash.slideUp("slow")), 3000
47

48
  # Disable form buttons while a form is submitting
Robert Speicher's avatar
Robert Speicher committed
49
  $('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) ->
Nihad Abbasov's avatar
Nihad Abbasov committed
50
    buttons = $('[type="submit"]', @)
Robert Speicher's avatar
Robert Speicher committed
51 52 53

    switch e.type
      when 'ajax:beforeSend', 'submit'
54
        buttons.disable()
Robert Speicher's avatar
Robert Speicher committed
55
      else
56
        buttons.enable()
Robert Speicher's avatar
Robert Speicher committed
57 58

  # Show/Hide the profile menu when hovering the account box
Nihad Abbasov's avatar
Nihad Abbasov committed
59
  $('.account-box').hover -> $(@).toggleClass('hover')
Robert Speicher's avatar
Robert Speicher committed
60 61 62 63 64 65 66 67 68 69 70 71 72

  # 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
73 74
    $(@).next('table').show()
    $(@).remove()
Robert Speicher's avatar
Robert Speicher committed
75 76 77 78 79 80

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

83 84
  # Disable an element and add the 'disabled' Bootstrap class
  $.fn.extend disable: ->
Nihad Abbasov's avatar
Nihad Abbasov committed
85
    $(@).attr('disabled', 'disabled').addClass('disabled')
86 87 88

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

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