main.js.coffee 2.6 KB
Newer Older
R
Robert Speicher 已提交
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

D
Dmitriy Zaporozhets 已提交
16 17 18 19 20 21
window.split = (val) ->
  return val.split( /,\s*/ )

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

N
Nihad Abbasov 已提交
22
# Disable button if text field is empty
R
Robert Speicher 已提交
23 24 25 26
window.disableButtonIfEmptyField = (field_selector, button_selector) ->
  field = $(field_selector)
  closest_submit = field.closest("form").find(button_selector)

27
  closest_submit.disable() if field.val() is ""
R
Robert Speicher 已提交
28

29
  field.on "input", ->
N
Nihad Abbasov 已提交
30
    if $(@).val() is ""
31
      closest_submit.disable()
R
Robert Speicher 已提交
32
    else
33
      closest_submit.enable()
R
Robert Speicher 已提交
34 35

$ ->
36
  # Click a .one_click_select field, select the contents
N
Nihad Abbasov 已提交
37
  $(".one_click_select").on 'click', -> $(@).select()
R
Robert Speicher 已提交
38

39 40 41
  # Initialize chosen selects
  $('select.chosen').chosen()

42 43 44
  # Initialize tooltips
  $('.has_tooltip').tooltip()

D
Dmitriy Zaporozhets 已提交
45 46 47
  # Bottom tooltip
  $('.has_bottom_tooltip').tooltip(placement: 'bottom')

C
Cyril 已提交
48 49 50 51 52
  # Flash
  if (flash = $("#flash-container")).length > 0
    flash.click -> $(@).slideUp("slow")
    flash.slideDown "slow"
    setTimeout (-> flash.slideUp("slow")), 3000
D
Dmitriy Zaporozhets 已提交
53

54
  # Disable form buttons while a form is submitting
R
Robert Speicher 已提交
55
  $('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) ->
N
Nihad Abbasov 已提交
56
    buttons = $('[type="submit"]', @)
R
Robert Speicher 已提交
57 58 59

    switch e.type
      when 'ajax:beforeSend', 'submit'
60
        buttons.disable()
R
Robert Speicher 已提交
61
      else
62
        buttons.enable()
R
Robert Speicher 已提交
63 64

  # Show/Hide the profile menu when hovering the account box
N
Nihad Abbasov 已提交
65
  $('.account-box').hover -> $(@).toggleClass('hover')
R
Robert Speicher 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78

  # 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", ->
N
Nihad Abbasov 已提交
79 80
    $(@).next('table').show()
    $(@).remove()
R
Robert Speicher 已提交
81 82 83 84 85 86

(($) ->
  _chosen = $.fn.chosen
  $.fn.extend chosen: (options) ->
    default_options = search_contains: "true"
    $.extend default_options, options
N
Nihad Abbasov 已提交
87
    _chosen.apply @, [default_options]
R
Robert Speicher 已提交
88

89 90
  # Disable an element and add the 'disabled' Bootstrap class
  $.fn.extend disable: ->
N
Nihad Abbasov 已提交
91
    $(@).attr('disabled', 'disabled').addClass('disabled')
92 93 94

  # Enable an element and remove the 'disabled' Bootstrap class
  $.fn.extend enable: ->
N
Nihad Abbasov 已提交
95
    $(@).removeAttr('disabled').removeClass('disabled')
96

R
Robert Speicher 已提交
97
)(jQuery)