Commit 3b89f140905dd1e929d5bf587fa2509866ba5d32

Authored by Robert Speicher
1 parent 93daa8c5

Convert main.js to coffee

Remove duplicate 's' hotkey code
app/assets/javascripts/main.js.coffee
1 -$(document).ready(function(){  
2 -  
3 - $(".one_click_select").live("click", function(){  
4 - $(this).select();  
5 - });  
6 -  
7 - $('body').on('ajax:complete, ajax:beforeSend, submit', 'form', function(e){  
8 - var buttons = $('[type="submit"]', this);  
9 - switch( e.type ){  
10 - case 'ajax:beforeSend':  
11 - case 'submit':  
12 - buttons.attr('disabled', 'disabled');  
13 - break;  
14 - case ' ajax:complete':  
15 - default:  
16 - buttons.removeAttr('disabled');  
17 - break;  
18 - }  
19 - })  
20 -  
21 - $(".account-box").mouseenter(showMenu);  
22 - $(".account-box").mouseleave(resetMenu);  
23 -  
24 - $("#projects-list .project").live('click', function(e){  
25 - if(e.target.nodeName != "A" && e.target.nodeName != "INPUT") {  
26 - location.href = $(this).attr("url");  
27 - e.stopPropagation();  
28 - return false;  
29 - }  
30 - });  
31 -  
32 - /**  
33 - * Focus search field by pressing 's' key  
34 - */  
35 - $(document).keypress(function(e) {  
36 - if( $(e.target).is(":input") ) return;  
37 - switch(e.which) {  
38 - case 115: focusSearch();  
39 - e.preventDefault();  
40 - }  
41 - });  
42 -  
43 - /**  
44 - * Commit show suppressed diff  
45 - *  
46 - */  
47 - $(".supp_diff_link").bind("click", function() {  
48 - showDiff(this);  
49 - });  
50 -  
51 - /**  
52 - * Note markdown preview  
53 - *  
54 - */  
55 - $(document).on('click', '#preview-link', function(e) {  
56 - $('#preview-note').text('Loading...');  
57 -  
58 - var previewLinkText = ($(this).text() == 'Preview' ? 'Edit' : 'Preview');  
59 - $(this).text(previewLinkText);  
60 -  
61 - var note = $('#note_note').val();  
62 - if (note.trim().length === 0) { note = 'Nothing to preview'; }  
63 - $.post($(this).attr('href'), {note: note}, function(data) {  
64 - $('#preview-note').html(data);  
65 - });  
66 -  
67 - $('#preview-note, #note_note').toggle();  
68 - e.preventDefault();  
69 - });  
70 -});  
71 -  
72 -function focusSearch() {  
73 - $("#search").focus();  
74 -}  
75 -  
76 -function updatePage(data){  
77 - $.ajax({type: "GET", url: location.href, data: data, dataType: "script"});  
78 -}  
79 -  
80 -function showMenu() {  
81 - $(this).toggleClass('hover');  
82 -}  
83 -  
84 -function resetMenu() {  
85 - $(this).removeClass("hover");  
86 -}  
87 -  
88 -function slugify(text) {  
89 - return text.replace(/[^-a-zA-Z0-9]+/g, '_').toLowerCase();  
90 -}  
91 -  
92 -function showDiff(link) {  
93 - $(link).next('table').show();  
94 - $(link).remove();  
95 -}  
96 -  
97 -(function($){  
98 - var _chosen = $.fn.chosen;  
99 - $.fn.extend({  
100 - chosen: function(options) {  
101 - var default_options = {'search_contains' : 'true'};  
102 - $.extend(default_options, options);  
103 - return _chosen.apply(this, [default_options]);  
104 - }})  
105 -})(jQuery);  
106 -  
107 -  
108 -function ajaxGet(url) {  
109 - $.ajax({type: "GET", url: url, dataType: "script"});  
110 -}  
111 -  
112 -/**  
113 - * Disable button if text field is empty  
114 - */  
115 -function disableButtonIfEmtpyField(field_selector, button_selector) {  
116 - field = $(field_selector);  
117 - if(field.val() == "") {  
118 - field.closest("form").find(button_selector).attr("disabled", "disabled").addClass("disabled");  
119 - }  
120 -  
121 - field.on('keyup', function(){  
122 - var field = $(this);  
123 - var closest_submit = field.closest("form").find(button_selector);  
124 - if(field.val() == "") {  
125 - closest_submit.attr("disabled", "disabled").addClass("disabled");  
126 - } else {  
127 - closest_submit.removeAttr("disabled").removeClass("disabled");  
128 - }  
129 - })  
130 -} 1 +window.updatePage = (data) ->
  2 + $.ajax({type: "GET", url: location.href, data: data, dataType: "script"})
  3 +
  4 +window.slugify = (text) ->
  5 + text.replace(/[^-a-zA-Z0-9]+/g, '_').toLowerCase()
  6 +
  7 +window.ajaxGet = (url) ->
  8 + $.ajax({type: "GET", url: url, dataType: "script"})
  9 +
  10 + # Disable button if text field is empty
  11 +window.disableButtonIfEmptyField = (field_selector, button_selector) ->
  12 + field = $(field_selector)
  13 + closest_submit = field.closest("form").find(button_selector)
  14 +
  15 + closest_submit.attr("disabled", "disabled").addClass("disabled") if field.val() is ""
  16 +
  17 + field.on "keyup", ->
  18 + if $(this).val() is ""
  19 + closest_submit.attr("disabled", "disabled").addClass "disabled"
  20 + else
  21 + closest_submit.removeAttr("disabled").removeClass "disabled"
  22 +
  23 +$ ->
  24 + $(".one_click_select").live 'click', ->
  25 + $(this).select()
  26 +
  27 + $('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) ->
  28 + buttons = $('[type="submit"]', this)
  29 +
  30 + switch e.type
  31 + when 'ajax:beforeSend', 'submit'
  32 + buttons.attr('disabled', 'disabled')
  33 + else
  34 + buttons.removeAttr('disabled')
  35 +
  36 + # Show/Hide the profile menu when hovering the account box
  37 + $('.account-box').hover -> $(this).toggleClass('hover')
  38 +
  39 + $("#projects-list .project").live 'click', (e) ->
  40 + if e.target.nodeName isnt "A" and e.target.nodeName isnt "INPUT"
  41 + location.href = $(this).attr("url")
  42 + e.stopPropagation()
  43 + false
  44 +
  45 + # Focus search field by pressing 's' key
  46 + $(document).keypress (e) ->
  47 + # Don't do anything if typing in an input
  48 + return if $(e.target).is(":input")
  49 +
  50 + switch e.which
  51 + when 115
  52 + $("#search").focus()
  53 + e.preventDefault()
  54 +
  55 + # Commit show suppressed diff
  56 + $(".supp_diff_link").bind "click", ->
  57 + $(this).next('table').show()
  58 + $(this).remove()
  59 +
  60 + # Note markdown preview
  61 + $(document).on 'click', '#preview-link', (e) ->
  62 + $('#preview-note').text('Loading...')
  63 +
  64 + previewLinkText = if $(this).text() == 'Preview' then 'Edit' else 'Preview'
  65 + $(this).text(previewLinkText)
  66 +
  67 + note = $('#note_note').val()
  68 + note = 'Nothing to preview' if note.trim().length is 0
  69 + $.post($(this).attr('href'), {note: note}, (data) ->
  70 + $('#preview-note').html(data)
  71 + )
  72 +
  73 + $('#preview-note, #note_note').toggle()
  74 + e.preventDefault()
  75 + false
  76 +
  77 +(($) ->
  78 + _chosen = $.fn.chosen
  79 + $.fn.extend chosen: (options) ->
  80 + default_options = search_contains: "true"
  81 + $.extend default_options, options
  82 + _chosen.apply this, [default_options]
  83 +
  84 +)(jQuery)
app/views/layouts/_head_panel.html.haml
@@ -34,12 +34,4 @@ @@ -34,12 +34,4 @@
34 source: #{raw search_autocomplete_source}, 34 source: #{raw search_autocomplete_source},
35 select: function(event, ui) { location.href = ui.item.url } 35 select: function(event, ui) { location.href = ui.item.url }
36 }); 36 });
37 -  
38 - $(document).keypress(function(e) {  
39 - if($(e.target).is(":input")) return;  
40 - switch(e.which) {  
41 - case 115: focusSearch();  
42 - e.preventDefault();  
43 - }  
44 - });  
45 }); 37 });