OSDN Git Service

Improve themes. Load highlight.js properly
[wvm/gitlab.git] / app / assets / javascripts / main.js.coffee
1 window.slugify = (text) ->
2   text.replace(/[^-a-zA-Z0-9]+/g, '_').toLowerCase()
3
4 window.ajaxGet = (url) ->
5   $.ajax({type: "GET", url: url, dataType: "script"})
6
7 window.showAndHide = (selector) ->
8
9 window.errorMessage = (message) ->
10   ehtml = $("<p>")
11   ehtml.addClass("error_message")
12   ehtml.html(message)
13   ehtml
14
15 window.split = (val) ->
16   return val.split( /,\s*/ )
17
18 window.extractLast = (term) ->
19   return split( term ).pop()
20
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)
25
26   closest_submit.disable() if field.val() is ""
27
28   field.on "input", ->
29     if $(@).val() is ""
30       closest_submit.disable()
31     else
32       closest_submit.enable()
33
34 window.sanitize = (str) ->
35   return str.replace(/<(?:.|\n)*?>/gm, '')
36
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>")
40
41 window.simpleFormat = (str) ->
42   linkify(sanitize(str).replace(/\n/g, '<br />'))
43
44 window.startSpinner = ->
45   $('.turbolink-spinner').fadeIn()
46
47 window.stopSpinner = ->
48   $('.turbolink-spinner').fadeOut()
49
50 window.unbindEvents = ->
51   $(document).unbind('scroll')
52   $(document).off('scroll')
53
54 document.addEventListener("page:fetch", startSpinner)
55 document.addEventListener("page:fetch", unbindEvents)
56 document.addEventListener("page:change", stopSpinner)
57
58 $ ->
59   $('pre code').each (i, e) ->
60     hljs.highlightBlock(e)
61
62   # Click a .one_click_select field, select the contents
63   $(".one_click_select").on 'click', -> $(@).select()
64
65   $('.remove-row').bind 'ajax:success', ->
66     $(this).closest('li').fadeOut()
67
68   # Click a .appear-link, appear-data fadeout
69   $(".appear-link").on 'click', (e) ->
70     $('.appear-data').fadeIn()
71     e.preventDefault()
72
73   # Initialize select2 selects
74   $('select.select2').select2(width: 'resolve', dropdownAutoWidth: true)
75
76   # Initialize tooltips
77   $('.has_tooltip').tooltip()
78
79   # Bottom tooltip
80   $('.has_bottom_tooltip').tooltip(placement: 'bottom')
81
82   # Form submitter
83   $('.trigger-submit').on 'change', ->
84     $(@).parents('form').submit()
85
86   $("abbr.timeago").timeago()
87   $('.js-timeago').timeago()
88
89   # Flash
90   if (flash = $(".flash-container")).length > 0
91     flash.click -> $(@).fadeOut()
92     flash.show()
93     setTimeout (-> flash.fadeOut()), 5000
94
95   # Disable form buttons while a form is submitting
96   $('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) ->
97     buttons = $('[type="submit"]', @)
98
99     switch e.type
100       when 'ajax:beforeSend', 'submit'
101         buttons.disable()
102       else
103         buttons.enable()
104
105   # Show/Hide the profile menu when hovering the account box
106   $('.account-box').hover -> $(@).toggleClass('hover')
107
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")
112
113     switch e.which
114       when 115
115         $("#search").focus()
116         e.preventDefault()
117       when 63
118         new Shortcuts()
119         e.preventDefault()
120
121
122   # Commit show suppressed diff
123   $(".content").on "click", ".supp_diff_link", ->
124     $(@).next('table').show()
125     $(@).remove()
126
127   $(".content").on "click", ".js-details-expand", ->
128     $(@).next('.js-details-contain').removeClass("hide")
129     $(@).remove()
130
131 (($) ->
132   # Disable an element and add the 'disabled' Bootstrap class
133   $.fn.extend disable: ->
134     $(@).attr('disabled', 'disabled').addClass('disabled')
135
136   # Enable an element and remove the 'disabled' Bootstrap class
137   $.fn.extend enable: ->
138     $(@).removeAttr('disabled').removeClass('disabled')
139
140 )(jQuery)