tickets.js
9.91 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
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
400
/**
* Freshdesk ticket plugin. Drag tickets into the diagram. Tickets are
* updated on file open, page select and via Extras, Update Tickets.
*
* Drag freshdesk tickets into the diagram. Domain must match deskDomain.freshdesk.com.
*
* Use #C to configure the client as follows:
*
* https://www.draw.io/?p=tickets#C%7B"ticketsConfig"%3A %7B"deskApiKey"%3A"YOUR_API_KEY"%2C"deskDomain"%3A"YOUR_DOMAIN"%7D%7D
*
* Use an additional "open" variable in the config JSON to open a file after parsing as follows:
*
* ...#_TICKETS%7B"ticketsConfig"%3A %7B"deskApiKey"%3A"YOUR_API_KEY"%2C"deskDomain"%3A"YOUR_DOMAIN"%7D%2C"open"%3A"ID_WITH_PREFIX"%7D
*
* Required JSON parameters:
* - deskApiKey=api_key (see user profile)
* - deskDomain=subdomain (subdomain.freshdesk.com)
*
* Optional JSON parameters:
* - deskStatus: Lookup for status codes (code => string)
* - deskTypes: Lookup for ticket types (string => story, task, subTask, feature,
* bug, techTask, epic, improvement, fault, change, access, purchase or itHelp)
*
* The current configuration is stored in localStorage under ".tickets-config". Use
* https://jgraph.github.io/drawio-tools/tools/convert.html for URI encoding.
*/
Draw.loadPlugin(function(ui)
{
var config = null;
var deskDomain = null;
var deskApiKey = null;
var graph = ui.editor.graph;
var deskPriority = {'1': 'minor', '2': 'major',
'3': 'critical', '4': 'blocker'};
var deskTypes = {'Question': 'story', 'Incident': 'techTask', 'Problem': 'fault',
'Feature Request': 'feature', 'Lead': 'purchase'};
var deskStatus = {'2': 'Open', '3': 'Pending', '4': 'Resolved', '5': 'Closed',
'6': 'Waiting on Customer', '7': 'Waiting on Third Party',
'8': 'Resolved Internally'};
var deskStatusWidth = {};
function configure()
{
deskDomain = 'https://' + config.deskDomain + '.freshdesk.com';
deskApiKey = config.deskApiKey;
deskTypes = config.deskTypes || deskTypes;
deskStatus = config.deskStatus || deskStatus;
deskStatusWidth = {};
// Precomputes text widths for custom ticket status
var div = document.createElement('div');
div.style.fontFamily = 'Arial,Helvetica';
div.style.visibility = 'hidden';
div.style.position = 'absolute';
div.style.fontSize = '11px';
document.body.appendChild(div);
for (var key in deskStatus)
{
div.innerHTML = '';
mxUtils.write(div, deskStatus[key]);
deskStatusWidth[key] = div.clientWidth + 4;
}
document.body.removeChild(div);
};
if (window.location.hash != null && window.location.hash.substring(0, 9) == '#_TICKETS')
{
try
{
var temp = JSON.parse(decodeURIComponent(
window.location.hash.substring(9)));
if (temp != null && temp.ticketsConfig != null)
{
config = temp.ticketsConfig;
configure();
ui.fileLoaded(new LocalFile(ui, ui.emptyDiagramXml, this.defaultFilename, true));
ui.editor.setStatus('Drag tickets from <a href="' + deskDomain +
'/a/tickets/filters/all_tickets" target="_blank">' +
deskDomain + '</a>');
}
}
catch (e)
{
console.error(e);
}
}
function isDeskLink(link)
{
if (deskDomain != null)
{
var dl = deskDomain.length;
return config != null && link.substring(0, dl) == deskDomain &&
(link.substring(dl, dl + 18) == '/helpdesk/tickets/' ||
link.substring(dl, dl + 11) == '/a/tickets/');
}
else
{
return false;
}
};
function getIdForDeskLink(link)
{
return link.substring(link.lastIndexOf('/') + 1);
};
function getDeskTicket(id, fn)
{
var xhr = new XMLHttpRequest();
xhr.open('GET', deskDomain + '/api/v2/tickets/' + id);
xhr.setRequestHeader('Authorization', 'Basic ' + btoa(deskApiKey + ':x'));
xhr.onload = function ()
{
if (xhr.status >= 200 && xhr.status <= 299)
{
fn(JSON.parse(xhr.responseText), xhr);
}
else
{
fn(null, xhr);
}
};
xhr.onerror = function ()
{
fn(null, xhr);
};
xhr.send();
};
function updateStyle(cell, ticket)
{
var type = (ticket.type != null) ? deskTypes[ticket.type] : 'bug';
var status = deskStatus[ticket.status] || 'Unknown';
var priority = deskPriority[ticket.priority];
var sw = deskStatusWidth[ticket.status];
var prev = cell.style;
cell.style = mxUtils.setStyle(cell.style, 'issueType', type);
cell.style = mxUtils.setStyle(cell.style, 'issueStatus', status);
cell.style = mxUtils.setStyle(cell.style, 'issueStatusWidth', sw);
cell.style = mxUtils.setStyle(cell.style, 'issuePriority', priority);
return prev != cell.style;
};
function shortString(s, max)
{
if (s.length > max)
{
return s.substring(0, max) + '...';
}
else
{
return s;
}
}
function updateData(cell, ticket)
{
var changed = false;
function setAttr(key, value)
{
var prev = cell.value.getAttribute(key);
value = value || '';
if (prev != value)
{
cell.value.setAttribute(key, value);
return true;
}
else
{
return false;
}
};
changed = setAttr('abstract', shortString(ticket.description_text, 600)) |
setAttr('email_config_id', ticket.email_config_id) |
setAttr('requester_id', ticket.requester_id) |
setAttr('group_id', ticket.group_id) |
setAttr('created_at', ticket.created_at) |
setAttr('updated_at', ticket.updated_at) |
setAttr('due_by', ticket.due_by) |
setAttr('tags', ticket.tags.join(' '));
for (var key in ticket.custom_fields)
{
changed = changed | setAttr(key, ticket.custom_fields[key]);
}
return changed;
};
function updateTickets(spin)
{
if (config != null && (!spin || ui.spinner.spin(document.body, mxResources.get('loading') + '...')))
{
var validate = false;
var pending = 0;
graph.view.states.visit(function(id, state)
{
var link = graph.getLinkForCell(state.cell);
if (link != null && isDeskLink(link))
{
var id = getIdForDeskLink(link);
pending++;
getDeskTicket(id, function(ticket, req)
{
pending--;
if (ticket != null)
{
// Expression must execute both calls
if (updateStyle(state.cell, ticket) |
updateData(state.cell, ticket))
{
graph.view.invalidate(state.cell, true, false);
state.style = null;
validate = true;
}
}
if (pending == 0)
{
if (spin)
{
ui.spinner.stop();
}
if (validate)
{
graph.view.validate();
}
}
})
}
});
if (spin && pending == 0)
{
ui.spinner.stop();
}
}
};
function getCellForLink(link)
{
for (var key in graph.view.states.map)
{
var cell = graph.view.states.map[key].cell;
if (link == graph.getLinkForCell(cell))
{
return cell;
}
}
};
// Adds resource for action
mxResources.parse('updateTickets=Update Tickets...');
// Adds action
ui.actions.addAction('updateTickets', function()
{
updateTickets(true);
});
// Updates tickets in opened files
ui.editor.addListener('fileLoaded', function()
{
updateTickets(false);
});
// Updates tickets when page changes
ui.editor.addListener('pageSelected', function()
{
updateTickets(false);
});
// Adds menu item
var menu = ui.menus.get('extras');
var oldFunct = menu.funct;
menu.funct = function(menu, parent)
{
oldFunct.apply(this, arguments);
ui.menus.addMenuItems(menu, ['-', 'updateTickets'], parent);
};
// Intercepts ticket URLs
var uiInsertTextAt = ui.insertTextAt;
ui.insertTextAt = function(text, dx, dy, html, asImage, crop, resizeImages)
{
if (isDeskLink(text))
{
var cell = getCellForLink(text);
if (cell != null)
{
// Selects existing ticket with same link
graph.setSelectionCell(cell);
graph.scrollCellToVisible(graph.getSelectionCell());
}
else if (ui.spinner.spin(document.body, mxResources.get('loading') + '...'))
{
// Creates new shape
var id = getIdForDeskLink(text);
getDeskTicket(id, function(ticket, req)
{
ui.spinner.stop();
if (ticket != null)
{
var cell = null;
graph.getModel().beginUpdate();
try
{
cell = graph.insertVertex(graph.getDefaultParent(), null,
'%title%\n\n<b>Updated:</b> %updated_at% ' +
'(<a href="' + deskDomain + '/contacts/%requester_id%">From</a>)',
graph.snap(dx), graph.snap(dy), 200, 50,
'html=1;whiteSpace=wrap;overflow=hidden;shape=mxgraph.atlassian.issue;' +
'fontSize=12;verticalAlign=top;align=left;spacingTop=25;' +
'strokeColor=#A8ADB0;fillColor=#EEEEEE;backgroundOutline=1;');
graph.setLinkForCell(cell, text);
cell.value.setAttribute('title', shortString(ticket.subject, 40));
cell.value.setAttribute('subject', ticket.subject);
cell.value.setAttribute('placeholders', '1');
cell.value.setAttribute('ticket_id', id);
updateData(cell, ticket);
updateStyle(cell, ticket);
// Adds ticket ID label
var label1 = new mxCell('%ticket_id%', new mxGeometry(0, 0, 60, 20),
'strokeColor=none;fillColor=none;part=1;resizable=0;align=left;' +
'autosize=1;points=[];deletable=0;editable=0;connectable=0;');
graph.setAttributeForCell(label1, 'placeholders', '1');
label1.geometry.relative = true;
label1.geometry.offset = new mxPoint(20, 0);
label1.vertex = true;
cell.insert(label1);
graph.updateCellSize(cell);
cell.geometry.width = Math.max(220, cell.geometry.width);
cell.geometry.height += 10;
}
finally
{
graph.getModel().endUpdate();
}
graph.setSelectionCell(cell);
}
else
{
var err = req.status
try
{
err = JSON.parse(req.responseText);
}
catch (e)
{
// ignore
}
ui.handleError({message: err.message});
}
});
}
return null;
}
else
{
return uiInsertTextAt.apply(this, arguments);
}
};
});