-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform-controller.ts
More file actions
178 lines (151 loc) · 4.8 KB
/
form-controller.ts
File metadata and controls
178 lines (151 loc) · 4.8 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { Controller } from '@hotwired/stimulus'
import { DateTime } from 'luxon'
import { FormData } from '@interfaces/form-data'
export default class extends Controller {
declare readonly startTarget: HTMLInputElement
declare readonly hasStopButtonTarget: boolean
declare readonly endTarget: HTMLInputElement
declare readonly absolutInputTarget: HTMLInputElement
declare readonly descriptionTarget: HTMLInputElement
declare readonly issueTargets: Element[]
declare readonly shareCopiedValue: string
declare readonly shareIgnoredValue: string
declare readonly sessionActiveValue: boolean
private connected = false
static targets = [
'description',
'start',
'stopButton',
'end',
'issue',
'absolutInput',
]
static values = {
shareCopied: String,
shareIgnored: String,
sessionActive: Boolean,
}
public connect() {
this.connected = true
this.showShareIgnoredNotice()
}
public disconnect() {
this.connected = false
}
public absoluteTime(_event: Event) {
try {
const value = parseFloat(this.absolutInputTarget.value)
const startDate = this.convertToDateTime(this.startTarget.value)
if (value && startDate.isValid) {
const newEndDate = startDate.plus({ hours: value })
this.endTarget.value = newEndDate.toFormat('dd.LL.yyyy HH:mm')
this.endTarget.dispatchEvent(new Event('change'))
}
} finally {
this.absolutInputTarget.value = ''
}
}
public issueTargetConnected(_: Element) {
if (this.connected) {
this.change()
}
}
public issueTargetDisconnected(_: Element) {
if (this.connected) {
this.change()
}
}
public change() {
const form: FormData = {
timer_start: this.startTarget.value,
timer_end: this.endTarget.value,
comments: this.descriptionTarget.value,
issue_ids: this.extractIssueIds() || [],
}
this.dispatchUpdate(form)
}
public share(_event: Event) {
const parts: string[] = []
const issueIds = this.extractIssueIds()
const comments = this.descriptionTarget.value
const timerStart = this.startTarget.value
const timerEnd = this.endTarget.value
issueIds.forEach((id) => parts.push(`issue_ids[]=${encodeURIComponent(id)}`))
if (comments) parts.push(`comments=${encodeURIComponent(comments)}`)
if (timerStart) parts.push(`timer_start=${encodeURIComponent(timerStart)}`)
if (timerEnd) parts.push(`timer_end=${encodeURIComponent(timerEnd)}`)
const query = parts.length > 0 ? `?${parts.join('&')}` : ''
const url = `${window.location.origin}${window.location.pathname}${query}`
this.copyToClipboard(url).then(() => {
this.showFlashNotice(this.shareCopiedValue)
})
}
private copyToClipboard(text: string): Promise<void> {
if (navigator.clipboard?.writeText) {
return navigator.clipboard.writeText(text).catch(() => {
this.copyToClipboardFallback(text)
})
}
this.copyToClipboardFallback(text)
return Promise.resolve()
}
private copyToClipboardFallback(text: string) {
const textarea = document.createElement('textarea')
textarea.value = text
textarea.style.position = 'fixed'
textarea.style.opacity = '0'
document.body.appendChild(textarea)
textarea.select()
document.execCommand('copy')
document.body.removeChild(textarea)
}
private showShareIgnoredNotice() {
if (!this.sessionActiveValue) return
const urlParams = new URLSearchParams(window.location.search)
const hasShareParams = urlParams.has('comments') ||
urlParams.has('timer_start') ||
urlParams.has('timer_end') ||
urlParams.getAll('issue_ids[]').some((v) => v !== '')
if (hasShareParams) {
this.showFlashNotice(this.shareIgnoredValue)
}
}
private showFlashNotice(message: string) {
const flash = document.getElementById('flash_notice')
if (flash) {
flash.textContent = message
flash.style.display = ''
return
}
const container = document.getElementById('content')
if (!container) return
const notice = document.createElement('div')
notice.id = 'flash_notice'
notice.className = 'flash notice'
notice.textContent = message
container.prepend(notice)
}
private extractIssueIds(): string[] {
return (
this.issueTargets
.map((element) => element.getAttribute('data-issue-id') || '')
.filter((value) => value !== null) || []
)
}
private dispatchUpdate(form: FormData) {
if (this.hasStopButtonTarget) {
$.ajax({
type: 'POST',
url: window.RedmineTracky.trackerUpdatePath,
data: { timer_session: form },
async: true,
})
}
}
private convertToDateTime(value: string) {
return DateTime.fromFormat(
value,
window.RedmineTracky.datetimeFormatJavascript,
)
}
}