-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfreshdesk-ticket-note-agent.user.js
399 lines (329 loc) · 12 KB
/
freshdesk-ticket-note-agent.user.js
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// ==UserScript==
// @name Freshdesk Ticket Note Agent
// @version 0.1.0
// @description A userscript that adds agent assignment to note
// @license MIT
// @author Jim Cheung
// @namespace https://github.com/jinhucheung
// @include https://*.freshdesk.com/*
// @run-at document-idle
// @grant none
// @icon https://raw.githubusercontent.com/jinhucheung/freshdesk-userscripts/main/images/freshdesk.ico
// @updateURL https://raw.githubusercontent.com/jinhucheung/freshdesk-userscripts/main/freshdesk-ticket-note-agent.user.js
// @downloadURL https://raw.githubusercontent.com/jinhucheung/freshdesk-userscripts/main/freshdesk-ticket-note-agent.user.js
// ==/UserScript==
(() => {
'use strict'
// constants
const cssSelectors = {
basicDropdown: '#ember-basic-dropdown-wormhole',
noteAgentField: '#ticket-note-agent-field',
noteAgentInput: '#ticket-note-agent-input',
noteAgentDropdown: '#ticket-note-agent-dropdown-wormhole',
noteAgentDropdownOptionList: '#ticket-note-agent-dropdown-option-list',
noteAgentNotFound: '#ticket-note-agent-not-found',
noteEmailField: '[data-test-notify-to]',
noteEmailSelectField: '[data-test-notify-to] .ticket-action__email > .ember-view',
ticketProperties: '.__module-tickets__ticket-details__properties',
propertyAgentLabel: '[data-test-id="agent"] .label-field'
}
// data
let dropdownObserver = null
// entry
document.body.addEventListener('click', e => {
if (!isTicketPath() || !getTicket()) {
removeObserver()
return
}
if (isTargetOf('addNote', e.target)) {
setTimeout(renderNoteAgentField, 0)
addObserver()
}
if (isTargetOf('sendNote', e.target)) {
assignResponder()
}
if (!isTargetOf('noteAgent', e.target)) {
removeNoteAgentDropdown()
}
})
window.addEventListener('resize', () => {
if (!isTicketPath() || !getTicket()) return
locateNoteAgentDropdown()
})
// elements
function createNoteAgentField(name, value) {
const element = document.createElement('div')
element.id = 'ticket-note-agent-field'
element.className = 'ticket-action__fields can-edit'
element.innerHTML = `
<div class="ticket-action__field ticket-action__label text__infotext mt-11">${name}: </div>
<div class="ticket-action__field mt-10 ml-10" style="width: 300px;">
<input id="ticket-note-agent-input" data-group="note-agent" value="${value || ' -- '}"
style="border: none; box-shadow: none; outline: none; width: 100%; border-radius: 4px; padding: 4px 8px;" />
</div>
`
return element
}
function createNoteAgentDropdown(ticket, agents) {
const element = document.createElement('div')
element.id = 'ticket-note-agent-dropdown-wormhole'
element.innerHTML = `
<div class="dropdown-content ember-basic-dropdown-content ember-power-select-dropdown ember-view ember-basic-dropdown-content--left ember-basic-dropdown-content--above ember-basic-dropdown--transitioned-in"
style="max-height: 300px; width: 300px; overflow: auto;"
>
<ul class="ember-power-select-options ember-view" id="ticket-note-agent-dropdown-option-list">
<li class="ember-power-select-option" role="option"> -- </li>
${agents && agents.map(agent => {
return `
<li class="ember-power-select-option"
data-id="${agent.id}"
data-email="${agent.email}"
aria-selected="${agent.id == ticket.responderId}"
aria-current="${agent.id == ticket.responderId}"
role="option"
>${agent.name}</li>
`
}).join('')}
</ul>
</div>
`
return element
}
function createNoteAgentNotFound() {
const element = document.createElement('li')
element.id = 'not-found'
element.innerHTML = 'No results found'
element.style.padding = '7px 30px 7px 8px'
return element
}
function renderNoteAgentField() {
if (getNoteAgentField()) return
const noteEmailField = getNoteEmailField()
if (!noteEmailField) return
const responder = getResponder()
const propertyAgentLabel = getPropertyAgentLabel()
const noteAgentField = createNoteAgentField(propertyAgentLabel.title, responder.name)
noteEmailField.parentNode.insertBefore(noteAgentField, noteEmailField)
observeNoteAgentField()
}
function renderNoteAgentDropdown() {
if (getNoteAgentDropdown()) return
const dropdown = createNoteAgentDropdown(getTicket(), getAgents())
document.body.appendChild(dropdown)
locateNoteAgentDropdown()
observeNoteAgentDropdown()
}
function locateNoteAgentDropdown() {
const dropdown = getNoteAgentDropdown()
if (!dropdown) return
const dropdownContent = dropdown.querySelector('.dropdown-content')
if (!dropdownContent) return
const noteAgentInput = getNoteAgentInput()
if (!noteAgentInput) return
const noteAgentInputRect = noteAgentInput.getBoundingClientRect()
dropdownContent.style.top = `${noteAgentInputRect.top + window.scrollY + 35}px`
dropdownContent.style.left = `${noteAgentInputRect.left + window.scrollX}px`
}
function removeNoteAgentDropdown() {
const dropdown = getNoteAgentDropdown()
dropdown && dropdown.remove()
}
function filterNoteAgentOptions(value) {
const list = getNoteAgentDropdownOptionList()
if (!list) return true
const options = list.querySelectorAll('li')
if (!options) return true
const formattedValue = (value || '').toLowerCase()
let resultCount = options.length
options.forEach(option => {
const email = (option.dataset.email || '').toLowerCase()
if (!formattedValue || option.innerHTML.trim().toLowerCase().indexOf(formattedValue) >= 0 || email.indexOf(formattedValue) >= 0) {
option.style.display = 'block'
} else {
option.style.display = 'none'
resultCount--
}
})
return resultCount > 0
}
function observeNoteAgentField() {
const noteAgentInput = getNoteAgentInput()
if (!noteAgentInput) return
noteAgentInput.addEventListener('focus', () => {
noteAgentInput.style.boxShadow = '0 0 0 2px #2c5cc5'
noteAgentInput.select()
})
noteAgentInput.addEventListener('blur', () => {
noteAgentInput.style.boxShadow = ''
})
noteAgentInput.addEventListener('click', () => {
renderNoteAgentDropdown()
})
noteAgentInput.addEventListener('keyup', () => {
setTimeout(() => {
const hasResult = filterNoteAgentOptions(noteAgentInput.value)
const notFound = getNoteAgentNotFound()
if (hasResult) {
notFound && notFound.remove()
} else if (!notFound) {
const optionList = getNoteAgentDropdownOptionList()
optionList && optionList.appendChild(createNoteAgentNotFound())
}
}, 500)
})
}
function observeNoteAgentDropdown() {
const optionList = getNoteAgentDropdownOptionList()
optionList && optionList.addEventListener('click', e => {
if (e.target.getAttribute('role') === 'option') {
setResponder(e.target.dataset.id)
}
})
}
function addObserver() {
observeTicket()
observeDropdown()
}
function removeObserver() {
dropdownObserver && dropdownObserver.disconnect()
dropdownObserver = null
}
function observeTicket() {
const ticket = getTicket()
ticket && ticket.addObserver('responderId', handleResponderChanged)
}
function observeDropdown() {
if (dropdownObserver) return
const basicDropdown = getBasicDropdown()
if (!basicDropdown) return
dropdownObserver = new MutationObserver(() => {
setTimeout(() => {
basicDropdown.innerHTML && removeNoteAgentDropdown()
}, 0)
})
dropdownObserver.observe(basicDropdown, { childList: true, subtree: true })
}
function handleResponderChanged() {
const responder = getResponder()
const noteAgentInput = getNoteAgentInput()
if (noteAgentInput) noteAgentInput.value = responder.name || ' -- '
setEmailTo(responder)
}
function getBasicDropdown() {
return document.querySelector(cssSelectors.basicDropdown)
}
function getNoteAgentField() {
return document.querySelector(cssSelectors.noteAgentField)
}
function getNoteAgentInput() {
return document.querySelector(cssSelectors.noteAgentInput)
}
function getNoteAgentDropdown() {
return document.querySelector(cssSelectors.noteAgentDropdown)
}
function getNoteAgentDropdownOptionList() {
return document.querySelector(cssSelectors.noteAgentDropdownOptionList)
}
function getNoteAgentNotFound() {
return document.querySelector(cssSelectors.noteAgentNotFound)
}
function getNoteEmailField() {
return document.querySelector(cssSelectors.noteEmailField)
}
function getPropertyAgentLabel() {
return document.querySelector(cssSelectors.propertyAgentLabel)
}
// methods
function getApp() {
const apps = window.Ember.A(window.Ember.Namespace.NAMESPACES)
return apps[apps.length - 1]
}
function getPath() {
return getApp().__container__.lookup('controller:application').get('currentPath')
}
function getTicket() {
const view = lookupView(cssSelectors.ticketProperties)
return view && view.ticket
}
function getAccount() {
const ticket = getTicket()
return ticket && ticket.currentAccount
}
function getAgents() {
const account = getAccount()
return account && account.agents && account.agents.currentState.map(agent => agent.__data.contact)
}
function getResponder() {
const ticket = getTicket()
if (!ticket || !ticket.responderId) return {}
const agents = getAgents()
if (!agents) return {}
const responder = agents.find(agent => agent.id == ticket.responderId)
return responder || {}
}
function getMeta() {
const account = getAccount()
return account && account.meta
}
function lookupView(selector) {
const node = document.querySelector(selector)
if (!(node && node.id)) return
return getApp().__container__.lookup('-view-registry:main')[node.id]
}
function setEmailTo(responder) {
const emailTo = lookupView(cssSelectors.noteEmailSelectField)
if (!(emailTo && emailTo.options)) return
const selects = []
const option = emailTo.options.content.find(option => option.id == responder.id)
if (option && option._record) selects.push(option._record)
emailTo.set('selected', selects)
}
function setResponder(id) {
const ticket = getTicket()
ticket && ticket.set('responderId', parseInt(id))
}
function assignResponder() {
const ticket = getTicket()
if (!(ticket && ticket.data)) return
if (ticket.responderId == ticket.data.responderId) return
updateResponder(ticket.responderId).then(data => {
const responderId = (data.ticket || {}).responder_id
ticket.set('data.responderId', responderId)
// trigger to update submit button
ticket.set('responderId', `${responderId}`)
ticket.set('responderId', responderId)
})
}
function isTicketPath() {
return getPath() === 'helpdesk.tickets.show'
}
function isTargetOf(name, target) {
switch (name) {
case 'addNote':
// [add note] button
return target.dataset.testEmailActionBtn === 'note'
case 'sendNote':
// [Add note] button / [Add note and set as xxx] button
return target.id === 'send-and-set' || (target.getAttribute('class') || '').indexOf('send-and-set-item') >= 0
case 'noteAgent':
// [Agent] field in note box
return target.dataset.group === 'note-agent'
default:
return false
}
}
// api
async function updateResponder(id) {
const formattedId = id && parseInt(id) || null
return fetch(`/api/_/tickets/${getTicket().id}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': getMeta().csrf_token,
},
body: JSON.stringify({ responder_id: formattedId })
})
.then(response => response.json())
.catch(console.error)
}
})()