-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipes.coffee
69 lines (60 loc) · 2.27 KB
/
recipes.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
$(document).on('ready turbolinks:load', () ->
$('.new-recipe-form, .edit-recipe-form').on('submit', (ev) ->
unless $(this).data('calculated')
ev.preventDefault()
hours = $('.hours-input').val()
mins = $('.minutes-input').val()
seconds = (hours * 3600) + (mins * 60)
$('.calc-time').val(seconds)
$(this).data('calculated', true)
$(this).submit()
else
return true
)
$('.category-select').select2({
theme: 'bootstrap'
placeholder: "click and type to search for a category..."
ajax:
url: "/categories/find"
dataType: 'json'
delay: 100
data: (params) ->
return {q: params.term, page: params.page}
processResults: (data, params) ->
results = []
$.each(data.items, (index, item) ->
results.push({id: item.id, text: item.name})
)
return {results: results, pagination: {more: data.has_more}}
cache: true
})
$('.access-select').on('change', () ->
$('.access-desc').text($(this).find('option:selected').data('description'))
)
$('.category-select').on('change', () ->
$('.category-desc').text($(this).find('option:selected').data('description'))
)
$('.favorite-toggle').on('ajax:success', (ev, data, status, xhr) ->
$('.favorite-icon').removeClass(data['remove_class']).addClass(data['add_class'])
$('.favorite-count').text(data['favorite_count'])
).on('ajax:error', (ev, xhr, status, error) ->
chef.createNotification('danger', "Can't toggle your favorite right now - try again later.", $(this))
)
if ($('.mde-enabled').length)
$('.mde-field').each(() ->
mde = new SimpleMDE({
element: this,
forceSync: true,
indentWithTabs: false,
tabSize: 4
})
).removeClass("mde-field")
$('.new-comment-form').on('ajax:success', (ev, data, status, xhr) ->
$('.comments').append(data['insert'])
).on('ajax:error', (ev, xhr, status, error) ->
chef.createNotification('danger', "Can't create a comment right now - try again later.", $('input[type=submit]', $(this)))
)
)