-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjira-plus.user.js
231 lines (212 loc) · 8.45 KB
/
jira-plus.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
// ==UserScript==
// @name Jira Plus
// @version 0.3.2
// @match */secure/Tempo.jspa
// @require https://cdn.jsdelivr.net/npm/vue@2.6.11/dist/vue.js
// @downloadURL https://github.com/henczi/userscripts/raw/master/jira-plus.user.js
// ==/UserScript==
const vueRootId = 'jira-plus-plugin';
// XHR response hook
(function(open) {
XMLHttpRequest.prototype.open = function(method, url) {
if (method.toLowerCase() === 'post' && url === '/rest/tempo-timesheets/4/worklogs/search') {
this.addEventListener('load', function() {
var styleEl = document.getElementById('overide-tempo-style');
if (!styleEl) {
styleEl = document.createElement('style');
styleEl.setAttribute('id', 'overide-tempo-style');
document.head.appendChild(styleEl);
}
var responseJSON = JSON.parse(this.responseText);
if (responseJSON) {
styleEl.innerHTML = responseJSON.map(x => {
var rules = [];
if (x.attributes?.['_Overtime_']?.value === 'true') rules.push('border: 2px solid red;');
if (x.attributes?.['_On-site_']?.value === 'true') rules.push('background-color: #aaa;');
return rules.length ? `#WORKLOG-${x.tempoWorklogId} { ${ rules.join(';') } }` : '';
}).filter(Boolean).join('\n')
}
});
}
open.apply(this, arguments);
};
})(XMLHttpRequest.prototype.open);
function logInfo() {
console.log("%cJira+ extension \n%cversion: " + GM_info.script.version, "font-size: 1.6rem; font-weight: bold;", "");
}
function registerGlobalHelpers() {
window.__reactGetInternalInstance = function (domEl) {
var iis = Object.keys(domEl).filter(x => x.startsWith('__reactInternalInstance'));
if (iis.length) {
var iikey = iis[0];
return domEl[iikey]
}
return null;
}
window.__reactGetEventHandlers = function (domEl) {
var ehs = Object.keys(domEl).filter(x => x.startsWith('__reactEventHandlers'));
if (ehs.length) {
var ehkey = ehs[0];
return domEl[ehkey]
}
return null;
}
window.__reactInputSetContent = function (selector = '#comment', text = 'komment') {
var commentEl = document.querySelector(selector);
const ehs = window.__reactGetEventHandlers(commentEl);
ehs.onChange({ target: { value: text } })
ehs.onBlur && ehs.onBlur();
}
}
function injectGlobalStyle() {
var sheet = document.createElement('style');
sheet.innerHTML = `
.jira-plus-plugin .cursor-pointer { cursor: pointer; }
.jira-tempo-worklog-viewer { position: fixed; z-index: 10000; max-height: 500px; overflow-y: auto; display: none; }
.jira-tempo-worklog-viewer.dropdown-container { border: 1px solid rgb(204, 204, 204); border-radius: .21428571em; box-shadow: rgba(0, 0, 0, 0.1) 0px 2px 10px 4px; }
.jira-tempo-worklog-viewer.dropdown-container { padding: 10px; background: white; }
.jira-tempo-worklog-viewer.dropdown-container .item { padding: 10px; }
.jira-tempo-worklog-viewer.dropdown-container .item:hover { background: rgb(220, 240, 253); }
`;
document.head.appendChild(sheet);
}
function getIssueKeyFromCurrentWorklogForm() {
let issueKey;
const worklogForm = (document.querySelector('#worklogForm'));
if (worklogForm) {
const isAdd = !!worklogForm.querySelector('#issuePickerInput');
try {
if (isAdd) {
issueKey = ((worklogForm.querySelectorAll('.tuiForm__col, span[name^="selected_issue"')[0] || {}).textContent || '').split(':')[0]
} else {
issueKey = window.__reactGetInternalInstance(worklogForm.parentNode).memoizedProps.children.props.issue.key;
}
} catch (e) {
console.log("Error: get issueKey", e);
}
}
return issueKey;
}
function registerComponents(Vue) {
Vue.component('tempo-worklog-viewer-dropdown', {
template: `
<div ref="container" class="jira-tempo-worklog-viewer dropdown-container">
<div>
<strong>{{name}}</strong>
</div>
<div class="item cursor-pointer" v-for="item in data" @click.stop.prevent="select(item)" :title="item.authorName + ' - ' + item.date">
<strong>{{item.comment}}</strong> ({{item.timeSpent}})
</div>
</div>
`,
data: () => ({
name: '',
commentBoxId: 'comment',
data: []
}),
methods: {
async load(worklogId) {
this.data = [];
this.name = worklogId;
if (!worklogId) { return; }
const res = await fetch(`/rest/api/2/issue/${worklogId}/worklog`, { "credentials": "include", "headers": {}, "body": null, "method": "GET" })
.then(x => x.json())
.then(x => x.worklogs.map(y => ({ comment: y.comment, timeSpent: convertTimeSpent(y.timeSpentSeconds), authorName: y.author.displayName, date: y.started.split('T')[0] })).reverse())
this.data = res.filter(x => x.comment.toLowerCase().indexOf('working on issue') < 0);
if (this.data.length > 0) {
this.show();
}
},
select(t) {
window.__reactInputSetContent('#comment', t.comment);
window.__reactInputSetContent('#timeSpentSeconds', t.timeSpent);
this.hide();
},
hide() {
this.$refs.container.style.display = 'none';
},
show() {
this.$refs.container.style.display = 'block';
},
onFocus(event) {
const target = event.target
if (target.id === this.commentBoxId) {
const rect = target.getBoundingClientRect();
this.$refs.container.style.top = `${rect.bottom}px`;
this.$refs.container.style.left = `${rect.left}px`;
this.$refs.container.style.width = `${(rect.right - rect.left)}px`;
const issueKey = getIssueKeyFromCurrentWorklogForm();
if (issueKey) {
this.load(issueKey);
}
} else {
this.hide();
}
},
onBlur(event) {
if (event.target.id === this.commentBoxId) {
setTimeout(() => this.hide(), 150);
}
}
},
created() {
window.addEventListener('focus', this.onFocus.bind(this), true);
window.addEventListener('blur', this.onBlur.bind(this), true);
}
});
}
// 1d 2h 30m formátumban jöhet a timeSpent propertyben, de az 1d-t nem fogadja el logoláskor :(
function convertTimeSpent(timeSpentSeconds) {
const hours = ~~(timeSpentSeconds / 3600);
const minutes = ~~((timeSpentSeconds - (hours * 3600)) / 60)
if (!minutes) {
return `${hours}h`;
}
return `${hours}h ${minutes}m`;
}
function addAutoDescriptionHandler() {
// JIRA_PLUS_PERSONAL_AUTODESCRIPTION_MAP -- '{ "ISSUE-KEY": ["text1", "text2"] }'
var PERSONAL_AUTODESCRIPTION_MAP = JSON.parse(localStorage.getItem('JIRA_PLUS_PERSONAL_AUTODESCRIPTION_MAP')) ?? {};
window.addEventListener('click', function(event) {
if (event.target.name === 'submitWorklogButton') {
if (document.getElementById('comment').value === '') {
const issueKey = getIssueKeyFromCurrentWorklogForm();
if (issueKey) {
const issueTexts = PERSONAL_AUTODESCRIPTION_MAP[issueKey];
if (issueTexts && issueTexts.length) {
const minCount = Math.floor(issueTexts.length / 2);
const description = issueTexts
.sort(() => 0.5 - Math.random())
.slice(0, minCount + Math.random() * (issueTexts.length - minCount + 1))
.join(', ')
window.__reactInputSetContent('#comment', description);
}
}
}
}
}, true);
}
function main() {
const Vue = window.Vue;
logInfo();
injectGlobalStyle();
registerComponents(Vue);
// Extension container
var extensionContainer = document.createElement('div');
extensionContainer.setAttribute('class', 'jira-plus-plugin')
// Vue app element
var vAppRoot = document.createElement('div');
vAppRoot.setAttribute('id', vueRootId);
extensionContainer.appendChild(vAppRoot);
document.body.appendChild(extensionContainer);
var app = new Vue({
el: '#' + vueRootId,
template: '<tempo-worklog-viewer-dropdown/>'
});
}
(function () {
'use strict';
registerGlobalHelpers();
addAutoDescriptionHandler();
window.onload = main;
})();