Convert main.js to coffee

Remove duplicate 's' hotkey code
This commit is contained in:
Robert Speicher 2012-09-09 05:59:15 -04:00
parent 93daa8c568
commit 3b89f14090
2 changed files with 65 additions and 119 deletions

View file

@ -1,130 +1,84 @@
$(document).ready(function(){ window.updatePage = (data) ->
$.ajax({type: "GET", url: location.href, data: data, dataType: "script"})
$(".one_click_select").live("click", function(){ window.slugify = (text) ->
$(this).select(); text.replace(/[^-a-zA-Z0-9]+/g, '_').toLowerCase()
});
$('body').on('ajax:complete, ajax:beforeSend, submit', 'form', function(e){ window.ajaxGet = (url) ->
var buttons = $('[type="submit"]', this); $.ajax({type: "GET", url: url, dataType: "script"})
switch( e.type ){
case 'ajax:beforeSend':
case 'submit':
buttons.attr('disabled', 'disabled');
break;
case ' ajax:complete':
default:
buttons.removeAttr('disabled');
break;
}
})
$(".account-box").mouseenter(showMenu); # Disable button if text field is empty
$(".account-box").mouseleave(resetMenu); window.disableButtonIfEmptyField = (field_selector, button_selector) ->
field = $(field_selector)
closest_submit = field.closest("form").find(button_selector)
$("#projects-list .project").live('click', function(e){ closest_submit.attr("disabled", "disabled").addClass("disabled") if field.val() is ""
if(e.target.nodeName != "A" && e.target.nodeName != "INPUT") {
location.href = $(this).attr("url");
e.stopPropagation();
return false;
}
});
/** field.on "keyup", ->
* Focus search field by pressing 's' key if $(this).val() is ""
*/ closest_submit.attr("disabled", "disabled").addClass "disabled"
$(document).keypress(function(e) { else
if( $(e.target).is(":input") ) return; closest_submit.removeAttr("disabled").removeClass "disabled"
switch(e.which) {
case 115: focusSearch();
e.preventDefault();
}
});
/** $ ->
* Commit show suppressed diff $(".one_click_select").live 'click', ->
* $(this).select()
*/
$(".supp_diff_link").bind("click", function() {
showDiff(this);
});
/** $('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) ->
* Note markdown preview buttons = $('[type="submit"]', this)
*
*/
$(document).on('click', '#preview-link', function(e) {
$('#preview-note').text('Loading...');
var previewLinkText = ($(this).text() == 'Preview' ? 'Edit' : 'Preview'); switch e.type
$(this).text(previewLinkText); when 'ajax:beforeSend', 'submit'
buttons.attr('disabled', 'disabled')
else
buttons.removeAttr('disabled')
var note = $('#note_note').val(); # Show/Hide the profile menu when hovering the account box
if (note.trim().length === 0) { note = 'Nothing to preview'; } $('.account-box').hover -> $(this).toggleClass('hover')
$.post($(this).attr('href'), {note: note}, function(data) {
$('#preview-note').html(data);
});
$('#preview-note, #note_note').toggle(); $("#projects-list .project").live 'click', (e) ->
e.preventDefault(); if e.target.nodeName isnt "A" and e.target.nodeName isnt "INPUT"
}); location.href = $(this).attr("url")
}); e.stopPropagation()
false
function focusSearch() { # Focus search field by pressing 's' key
$("#search").focus(); $(document).keypress (e) ->
} # Don't do anything if typing in an input
return if $(e.target).is(":input")
function updatePage(data){ switch e.which
$.ajax({type: "GET", url: location.href, data: data, dataType: "script"}); when 115
} $("#search").focus()
e.preventDefault()
function showMenu() { # Commit show suppressed diff
$(this).toggleClass('hover'); $(".supp_diff_link").bind "click", ->
} $(this).next('table').show()
$(this).remove()
function resetMenu() { # Note markdown preview
$(this).removeClass("hover"); $(document).on 'click', '#preview-link', (e) ->
} $('#preview-note').text('Loading...')
function slugify(text) { previewLinkText = if $(this).text() == 'Preview' then 'Edit' else 'Preview'
return text.replace(/[^-a-zA-Z0-9]+/g, '_').toLowerCase(); $(this).text(previewLinkText)
}
function showDiff(link) { note = $('#note_note').val()
$(link).next('table').show(); note = 'Nothing to preview' if note.trim().length is 0
$(link).remove(); $.post($(this).attr('href'), {note: note}, (data) ->
} $('#preview-note').html(data)
)
(function($){ $('#preview-note, #note_note').toggle()
var _chosen = $.fn.chosen; e.preventDefault()
$.fn.extend({ false
chosen: function(options) {
var default_options = {'search_contains' : 'true'};
$.extend(default_options, options);
return _chosen.apply(this, [default_options]);
}})
})(jQuery);
(($) ->
_chosen = $.fn.chosen
$.fn.extend chosen: (options) ->
default_options = search_contains: "true"
$.extend default_options, options
_chosen.apply this, [default_options]
function ajaxGet(url) { )(jQuery)
$.ajax({type: "GET", url: url, dataType: "script"});
}
/**
* Disable button if text field is empty
*/
function disableButtonIfEmtpyField(field_selector, button_selector) {
field = $(field_selector);
if(field.val() == "") {
field.closest("form").find(button_selector).attr("disabled", "disabled").addClass("disabled");
}
field.on('keyup', function(){
var field = $(this);
var closest_submit = field.closest("form").find(button_selector);
if(field.val() == "") {
closest_submit.attr("disabled", "disabled").addClass("disabled");
} else {
closest_submit.removeAttr("disabled").removeClass("disabled");
}
})
}

View file

@ -34,12 +34,4 @@
source: #{raw search_autocomplete_source}, source: #{raw search_autocomplete_source},
select: function(event, ui) { location.href = ui.item.url } select: function(event, ui) { location.href = ui.item.url }
}); });
$(document).keypress(function(e) {
if($(e.target).is(":input")) return;
switch(e.which) {
case 115: focusSearch();
e.preventDefault();
}
});
}); });