forked from reposense/RepoSense
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
292 lines (257 loc) · 7.68 KB
/
main.js
File metadata and controls
292 lines (257 loc) · 7.68 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
window.REPORT_ZIP = null;
window.REPOS = {};
window.isMacintosh = navigator.platform.includes('Mac');
window.hashParams = {};
window.addHash = function addHash(newKey, newVal) {
window.hashParams[newKey] = newVal;
};
window.removeHash = function removeHash(key) {
delete window.hashParams[key];
};
window.encodeHash = function encodeHash() {
const { hashParams } = window;
window.location.hash = Object.keys(hashParams)
.map((key) => `${key}=${encodeURIComponent(hashParams[key])}`)
.join('&');
};
window.decodeHash = function decodeHash() {
const hashParams = {};
window.location.hash.slice(1).split('&')
.forEach((param) => {
const [key, val] = param.split('=');
if (key) {
try {
hashParams[key] = decodeURIComponent(val);
} catch (error) {
this.userUpdated = false;
this.isLoading = false;
}
}
});
window.hashParams = hashParams;
};
const DRAG_BAR_WIDTH = 13.25;
const SCROLL_BAR_WIDTH = 17;
const GUIDE_BAR_WIDTH = 2;
const throttledEvent = (delay, handler) => {
let lastCalled = 0;
return (...args) => {
if (Date.now() - lastCalled > delay) {
lastCalled = Date.now();
handler(...args);
}
};
};
let guideWidth = (0.5 * window.innerWidth - (GUIDE_BAR_WIDTH / 2))
/ window.innerWidth;
let flexWidth = 0.5;
window.mouseMove = () => {};
window.registerMouseMove = () => {
const innerMouseMove = (event) => {
guideWidth = (
Math.min(
Math.max(
window.innerWidth - event.clientX,
SCROLL_BAR_WIDTH + DRAG_BAR_WIDTH,
),
window.innerWidth - SCROLL_BAR_WIDTH,
)
- (GUIDE_BAR_WIDTH / 2)
) / window.innerWidth;
window.$('tab-resize-guide').style.right = `${guideWidth * 100}%`;
};
window.$('tab-resize-guide').style.display = 'block';
window.$('app-wrapper').style['user-select'] = 'none';
window.mouseMove = throttledEvent(30, innerMouseMove);
};
window.deregisterMouseMove = () => {
flexWidth = (guideWidth * window.innerWidth + (GUIDE_BAR_WIDTH / 2))
/ window.innerWidth;
window.mouseMove = () => {};
if (window.$('tabs-wrapper')) {
window.$('tabs-wrapper').style.flex = `0 0 ${flexWidth * 100}%`;
}
window.$('tab-resize-guide').style.display = 'none';
window.$('app-wrapper').style['user-select'] = 'auto';
};
/* global Vue hljs */
Vue.directive('hljs', {
inserted(ele, binding) {
const element = ele;
element.className = binding.value.split('.').pop();
hljs.highlightBlock(element);
},
});
window.app = new window.Vue({
el: '#app',
data: {
repos: {},
users: [],
repoLength: 0,
loadedRepo: 0,
userUpdated: false,
isLoading: false,
isCollapsed: false,
isTabActive: true, // to force tab wrapper to load
tabType: 'empty',
tabInfo: {},
creationDate: '',
errorMessages: {},
},
methods: {
// model functions //
updateReportZip(evt) {
this.users = [];
window.JSZip.loadAsync(evt.target.files[0])
.then((zip) => {
window.REPORT_ZIP = zip;
}, () => {
window.alert('Either the .zip file is corrupted, or you uploaded a .zip file that is not generated '
+ 'by RepoSense.');
})
.then(() => this.updateReportView().then(() => this.renderTabHash()));
},
updateReportDir() {
window.REPORT_ZIP = null;
this.users = [];
this.updateReportView().then(() => this.renderTabHash());
},
async updateReportView() {
await window.api.loadSummary().then((names) => {
this.repos = window.REPOS;
this.repoLength = Object.keys(window.REPOS).length;
this.loadedRepo = 0;
this.userUpdated = false;
this.isLoading = true;
this.loadedRepo = 0;
return Promise.all(names.map((name) => (
window.api.loadCommits(name)
.then(() => { this.loadedRepo += 1; })
)));
}).then(() => {
this.userUpdated = true;
this.isLoading = false;
this.getUsers();
}).catch((error) => {
this.userUpdated = false;
this.isLoading = false;
window.alert(error);
});
},
getUsers() {
const full = [];
Object.keys(this.repos).forEach((repo) => {
if (this.repos[repo].users) {
full.push(this.repos[repo]);
}
});
this.users = full;
},
// handle opening of sidebar //
activateTab(tabName) {
// changing isTabActive to trigger redrawing of component
this.isTabActive = false;
if (document.getElementById('tabs-wrapper')) {
document.getElementById('tabs-wrapper').scrollTop = 0;
}
this.isTabActive = true;
this.isCollapsed = false;
this.tabType = tabName;
window.addHash('tabOpen', this.isTabActive);
window.addHash('tabType', this.tabType);
window.encodeHash();
},
deactivateTab() {
this.isTabActive = false;
window.addHash('tabOpen', this.isTabActive);
window.removeHash('tabAuthor');
window.removeHash('tabRepo');
window.removeHash('tabType');
window.encodeHash();
},
updateTabAuthorship(obj) {
this.tabInfo.tabAuthorship = Object.assign({}, obj);
this.activateTab('authorship');
},
updateTabZoom(obj) {
this.tabInfo.tabZoom = Object.assign({}, obj);
this.activateTab('zoom');
},
// updating summary view
updateSummaryDates(since, until) {
this.$refs.summary.updateDateRange(since, until);
},
renderAuthorShipTabHash(minDate, maxDate) {
const hash = window.hashParams;
const info = {
author: hash.tabAuthor,
repo: hash.tabRepo,
minDate,
maxDate,
};
const tabInfoLength = Object.values(info).filter((x) => x).length;
if (Object.keys(info).length === tabInfoLength) {
this.updateTabAuthorship(info);
} else if (hash.tabOpen === 'false' || tabInfoLength > 2) {
window.app.isTabActive = false;
}
},
renderTabHash() {
window.decodeHash();
const hash = window.hashParams;
if (!hash.tabOpen) {
return;
}
this.isTabActive = hash.tabOpen === 'true';
if (this.isTabActive) {
if (hash.tabType === 'authorship') {
let { since, until } = hash;
// get since and until dates from window.app if not found in hash
since = since || window.app.sinceDate;
until = until || window.app.untilDate;
this.renderAuthorShipTabHash(since, until);
} else {
// handle zoom tab if needed
}
}
},
generateKey(dataObj) {
return JSON.stringify(dataObj);
},
getRepoSenseHomeLink() {
return 'http://reposense.org';
},
getRepoSenseVersionLink() {
const version = window.app.repoSenseVersion;
if (!version) {
return 'https://github.com/reposense/RepoSense';
}
if (version.startsWith('v')) {
return `https://github.com/reposense/RepoSense/releases/tag/${version}`;
}
return `https://github.com/reposense/RepoSense/commits/${version}`;
},
receiveDates(dates) {
const [minDate, maxDate] = dates;
if (this.tabType === 'authorship') {
this.renderAuthorShipTabHash(minDate, maxDate);
}
},
},
components: {
v_zoom: window.vZoom,
v_summary: window.vSummary,
v_authorship: window.vAuthorship,
CircleSpinner: window.VueLoadingSpinner.Circle,
},
created() {
this.updateReportDir();
},
updated() {
this.$nextTick(() => {
if (window.$('tabs-wrapper')) {
window.$('tabs-wrapper').style.flex = `0 0 ${flexWidth * 100}%`;
}
});
},
});