1# Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
2# See the LICENCE file in the repository root for full licence text.
3
4import { isClickable } from 'utils/html'
5
6# Avoid triggering submit when pressing enter since both
7# submit and change will be triggered.
8# Moreover, blurring the input will trigger the 'change' event.
9# That part is handled by storing the submitted value in the DOM.
10$(document).on 'keypress', '.js-auto-submit', (e) ->
11 # 13 == enter key
12 return unless e.which == 13
13 e.preventDefault()
14 $(e.target).trigger 'change'
15
16# automatically submit on change
17$(document).on 'change', '.js-auto-submit', (e) ->
18 $target = $(e.target)
19 return if $target.val() == $target.data('last-submitted-value')
20
21 $target.data('last-submitted-value', $target.val())
22 $target.parents('form').submit()
23
24# A class to make contenteditable fields useful inside of forms.
25$(document).on 'keypress', '.content-editable-submit', (e) ->
26 # 13 == enter key
27 return unless e.which == 13
28 e.preventDefault()
29 $(e.target).trigger 'blur'
30
31$(document).on 'blur', '.content-editable-submit', (e) ->
32 $target = $(e.target)
33 return if $target.html() == $target.data('last-submitted-value')
34
35 $target.data('last-submitted-value', $target.html())
36
37 $form = $target.parents('form')
38
39 el = $(document.createElement('input'))
40 el.attr('type', 'hidden')
41 el.attr('name', $target.attr('data-name'))
42 el.attr('value', $target.html())
43
44 # temporarily add a hidden form element for this contenteditable field.
45 $form.append(el);
46
47 $form.submit()
48
49 # remove now that we're done submitting.
50 el.remove()
51
52#populate last-submitted values
53$(document).on 'turbo:load', ->
54 $('.content-editable-submit').each (_i, el) ->
55 $el = $(el)
56 $el.data('last-submitted-value', $el.html())
57
58# fadeOut effect for popup
59$(document).on 'click', '#popup-container, #overlay', (e) ->
60 $('#overlay').fadeOut()
61 $popup = $(e.target).closest('.popup-active')
62 $popup.fadeOut null, -> $popup.remove()
63
64
65###
66# Click anywhere on row to click the main link!
67# Usage:
68# 1. add class `clickable-row` to the row
69# 2. add class `clickable-row-link` to the link that should be
70# clicked when the row is clicked
71# 3. ???
72# 4. profit!
73# May contain caveats.
74###
75$(document).on 'click', '.clickable-row', (e) ->
76 target = e.target
77
78 return if isClickable target
79
80 row = e.currentTarget
81 if row.classList.contains 'clickable-row-link'
82 row.click()
83 else
84 row.getElementsByClassName('clickable-row-link')[0]?.click()
85
86
87# submit form on ctrl-enter (or cmd-enter).
88$(document).on 'keydown', '.js-quick-submit', (e) ->
89 return unless (e.ctrlKey || e.metaKey) && e.key == 'Enter'
90
91 e.preventDefault()
92 $(e.target).closest('form').submit()
93
94
95$(document).on 'ajax:beforeSend', (e) ->
96 # currentTarget is document
97 form = e.target
98
99 return false if form._submitting
100
101 form._submitting = true
102
103 form._ujsSubmitDisabled = []
104 for el in form.querySelectorAll('.js-ujs-submit-disable')
105 continue if el.disabled
106
107 el.blur() if el.dataset.blurOnSubmitDisable == '1'
108 el.disabled = true
109 form._ujsSubmitDisabled.push el
110
111
112$(document).on 'ajax:complete', (e) ->
113 form = e.target
114
115 form._submitting = false
116 el.disabled = false while el = form._ujsSubmitDisabled.pop()