-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathissue-completion-controller.ts
More file actions
102 lines (86 loc) · 2.94 KB
/
issue-completion-controller.ts
File metadata and controls
102 lines (86 loc) · 2.94 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
import { Controller } from '@hotwired/stimulus'
import { CompletionResult } from '@interfaces/completion-result'
import ListController from '@controllers/list-controller'
// https://github.com/redmine/redmine/blob/4.2-stable/public/javascripts/application.js#L637-L648
declare function observeAutocompleteField(
id: string,
selector: Function,
options: { select: Function },
): any
export default class extends Controller {
declare readonly inputTarget: HTMLInputElement
declare readonly listAnchorTarget: Element
static targets = ['input', 'listAnchor']
static values = { update: Boolean }
connect() {
this.listenForInput()
this.prefillFromURL()
}
private listenForInput() {
this.cleanup()
observeAutocompleteField(
this.inputTarget.id,
function (request: any, callback: any) {
const url = window.RedmineTracky.issueCompletionPath
const data = { term: request.term, scope: 'all' }
$.get(url, data, null, 'json')
.done((data: CompletionResult[]) => callback(data))
.fail(() => callback([]))
},
{
select: (_event: Event, item: { item: CompletionResult }) => {
this.addIssue(item)
this.cleanup()
return false
},
},
)
}
private prefillFromURL() {
const urlParams = new URLSearchParams(window.location.search)
this.prefillIssuesFromURL(urlParams)
this.prefillFieldFromURL(urlParams, 'comments', '#timer_session_comments')
this.prefillFieldFromURL(urlParams, 'timer_start', '#timer_session_timer_start')
this.prefillFieldFromURL(urlParams, 'timer_end', '#timer_session_timer_end')
}
private prefillIssuesFromURL(urlParams: URLSearchParams) {
const issueIds = urlParams.getAll('issue_ids[]')
issueIds.filter(v => v !== "").forEach((id) => {
const url = window.RedmineTracky.issueCompletionPath
const data = { term: id, scope: 'all' }
$.get(url, data, null, 'json')
.done((results: CompletionResult[]) => {
const [result] = results
this.addIssue({ item: result })
})
.fail(() => {
console.error(`Failed to fetch issue with ID: ${id}`)
})
})
}
private prefillFieldFromURL(urlParams: URLSearchParams, param: string, selector: string) {
const value = urlParams.get(param)
if (!value) return
const field = document.querySelector<HTMLInputElement>(selector)
if (field) {
field.value = value
field.dispatchEvent(new Event('change'))
}
}
private addIssue(issue: { item: CompletionResult }) {
const listController =
this.application.getControllerForElementAndIdentifier(
this.listAnchorTarget,
'list',
) as ListController
listController?.addItem(issue.item)
}
private clearInput() {
$(this.inputTarget).val('')
}
private cleanup() {
this.clearInput()
$('.ui-autocomplete').hide()
$('.ui-helper-hidden-accessible').hide()
}
}