1 window.slugify = (text) ->
2 text.replace(/[^-a-zA-Z0-9]+/g, '_').toLowerCase()
4 window.ajaxGet = (url) ->
5 $.ajax({type: "GET", url: url, dataType: "script"})
7 window.showAndHide = (selector) ->
9 window.errorMessage = (message) ->
11 ehtml.addClass("error_message")
15 window.split = (val) ->
16 return val.split( /,\s*/ )
18 window.extractLast = (term) ->
19 return split( term ).pop()
21 # Disable button if text field is empty
22 window.disableButtonIfEmptyField = (field_selector, button_selector) ->
23 field = $(field_selector)
24 closest_submit = field.closest("form").find(button_selector)
26 closest_submit.disable() if field.val() is ""
30 closest_submit.disable()
32 closest_submit.enable()
34 window.sanitize = (str) ->
35 return str.replace(/<(?:.|\n)*?>/gm, '')
37 window.linkify = (str) ->
38 exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig
39 return str.replace(exp,"<a href='$1'>$1</a>")
41 window.simpleFormat = (str) ->
42 linkify(sanitize(str).replace(/\n/g, '<br />'))
44 window.startSpinner = ->
45 $('.turbolink-spinner').fadeIn()
47 window.stopSpinner = ->
48 $('.turbolink-spinner').fadeOut()
50 window.unbindEvents = ->
51 $(document).unbind('scroll')
52 $(document).off('scroll')
54 document.addEventListener("page:fetch", startSpinner)
55 document.addEventListener("page:fetch", unbindEvents)
56 document.addEventListener("page:change", stopSpinner)
59 $('pre code').each (i, e) ->
60 hljs.highlightBlock(e)
62 # Click a .one_click_select field, select the contents
63 $(".one_click_select").on 'click', -> $(@).select()
65 $('.remove-row').bind 'ajax:success', ->
66 $(this).closest('li').fadeOut()
68 # Click a .appear-link, appear-data fadeout
69 $(".appear-link").on 'click', (e) ->
70 $('.appear-data').fadeIn()
73 # Initialize select2 selects
74 $('select.select2').select2(width: 'resolve', dropdownAutoWidth: true)
77 $('.has_tooltip').tooltip()
80 $('.has_bottom_tooltip').tooltip(placement: 'bottom')
83 $('.trigger-submit').on 'change', ->
84 $(@).parents('form').submit()
86 $("abbr.timeago").timeago()
87 $('.js-timeago').timeago()
90 if (flash = $(".flash-container")).length > 0
91 flash.click -> $(@).fadeOut()
93 setTimeout (-> flash.fadeOut()), 5000
95 # Disable form buttons while a form is submitting
96 $('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) ->
97 buttons = $('[type="submit"]', @)
100 when 'ajax:beforeSend', 'submit'
105 # Show/Hide the profile menu when hovering the account box
106 $('.account-box').hover -> $(@).toggleClass('hover')
108 # Focus search field by pressing 's' key
109 $(document).keypress (e) ->
110 # Don't do anything if typing in an input
111 return if $(e.target).is(":input")
122 # Commit show suppressed diff
123 $(".content").on "click", ".supp_diff_link", ->
124 $(@).next('table').show()
127 $(".content").on "click", ".js-details-expand", ->
128 $(@).next('.js-details-contain').removeClass("hide")
132 # Disable an element and add the 'disabled' Bootstrap class
133 $.fn.extend disable: ->
134 $(@).attr('disabled', 'disabled').addClass('disabled')
136 # Enable an element and remove the 'disabled' Bootstrap class
137 $.fn.extend enable: ->
138 $(@).removeAttr('disabled').removeClass('disabled')