gitlabhq/app/assets/javascripts/main.js.coffee

93 lines
2.5 KiB
CoffeeScript
Raw Normal View History

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"})
2012-10-10 15:41:20 +02:00
# Disable button if text field is empty
window.disableButtonIfEmptyField = (field_selector, button_selector) ->
field = $(field_selector)
closest_submit = field.closest("form").find(button_selector)
closest_submit.disable() if field.val() is ""
field.on "keyup", ->
2012-10-10 15:41:20 +02:00
if $(@).val() is ""
closest_submit.disable()
else
closest_submit.enable()
$ ->
2012-09-09 13:17:01 +02:00
# Click a .one_click_select field, select the contents
2012-10-10 15:41:20 +02:00
$(".one_click_select").on 'click', -> $(@).select()
# Initialize chosen selects
$('select.chosen').chosen()
# Disable form buttons while a form is submitting
$('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) ->
2012-10-10 15:41:20 +02:00
buttons = $('[type="submit"]', @)
switch e.type
when 'ajax:beforeSend', 'submit'
buttons.disable()
else
buttons.enable()
# Show/Hide the profile menu when hovering the account box
2012-10-10 15:41:20 +02:00
$('.account-box').hover -> $(@).toggleClass('hover')
# 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", ->
2012-10-10 15:41:20 +02:00
$(@).next('table').show()
$(@).remove()
# Note markdown preview
$(document).on 'click', '#preview-link', (e) ->
2012-10-10 15:41:20 +02:00
$('#preview-note').text 'Loading...'
2012-10-10 15:41:20 +02:00
previewLinkText = if $(@).text() is 'Preview' then 'Edit' else 'Preview'
$(@).text previewLinkText
note = $('#note_note').val()
2012-09-09 13:17:01 +02:00
2012-10-10 15:41:20 +02:00
if note.trim().length is 0
$('#preview-note').text 'Nothing to preview.'
2012-09-09 13:17:01 +02:00
else
2012-10-10 15:41:20 +02:00
$.post $(@).attr('href'), {note: note}, (data) ->
2012-09-09 13:17:01 +02:00
$('#preview-note').html(data)
$('#preview-note, #note_note').toggle()
e.preventDefault()
false
(($) ->
_chosen = $.fn.chosen
$.fn.extend chosen: (options) ->
default_options = search_contains: "true"
$.extend default_options, options
2012-10-10 15:41:20 +02:00
_chosen.apply @, [default_options]
# Disable an element and add the 'disabled' Bootstrap class
$.fn.extend disable: ->
2012-10-10 15:41:20 +02:00
$(@).attr('disabled', 'disabled').addClass('disabled')
# Enable an element and remove the 'disabled' Bootstrap class
$.fn.extend enable: ->
2012-10-10 15:41:20 +02:00
$(@).removeAttr('disabled').removeClass('disabled')
)(jQuery)