forked from jlous/NaviSane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNaviSane.user.js
382 lines (326 loc) · 11.4 KB
/
NaviSane.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
// ==UserScript==
// @name NaviSane
// @version 2.8.1
// @namespace https://github.com/steria/NaviSane
// @homepage https://github.com/steria/NaviSane
// @downloadURL https://github.com/steria/NaviSane/raw/master/NaviSane.user.js
// @description GUI-tweaks for timeføring
// @match https://naviwep.steria.no/*
// @require https://code.jquery.com/jquery-3.2.1.min.js
// ==/UserScript==
// Pre-commit sjekkliste:
// * Justere @version
// * Beskrive feature i readme.md (og justere "NY"-markering/er der)
// TODO/WISHLIST:
// responsiveColumnWidths() - incl. responsive day names?
// finish saneArrowKeys() (right/left navigation)
// reopenButton()
// highlightLineUnderCursor()
// highlightFocusedLine()
// style -> class
// menuHoverIntent()
// UTILS
if (!String.prototype.contains) {
String.prototype.contains = function () {
return this.indexOf(arguments[0]) !== -1;
};
}
String.prototype.appearsIn = function () {
return arguments[0].indexOf(this) !== -1;
};
function ignore() {
//pass item as param to get rid of 'unused' warnings
}
// FEATURES
function saneColumnHeaders() {
const monthName = ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
$("a[title^='Date']").each(function () {
const title = $(this).attr("title");
const month = Number(title.substr(10, 2));
const day = Number(title.substr(12, 2));
const date = monthName[month] + " " + day;
$(this).append("<br>" + date);
});
}
function sanePeriodHeader() {
const headerSpan = $("#ctl00_ContentPlaceHolder1_LBL_CurrentPeriod");
const oldTitle = headerSpan.text();
const groups = /^(\d\d\.\d\d\.\d\d\d\d - \d\d\.\d\d\.\d\d\d\d) .Week(\d\d?).\d\d\d\d ?(\d?)/.exec(oldTitle);
const dateRange = groups[1];
const weekNo = groups[2];
const weekPart = groups[3];
const weekSep = weekPart.length > 0 ? "." : "";
const newText = " <b>Week " + weekNo + weekSep + weekPart + "</b> <span style='color:silver;font-size:smaller'>" + dateRange + "</span>";
headerSpan.html(newText);
}
function currentPeriod() {
const header = $("#ctl00_ContentPlaceHolder1_LBL_CurrentPeriod").text();
return header.replace(/^.*(\d\d\.\d\d\.\d\d\d\d - \d\d\.\d\d\.\d\d\d\d).*$/, "$1");
}
function sanePeriodNavigation() {
$(".CurrentPeriod")
.prepend("<button type='button' id='prevPeriod'>◀</button>")
.append("<button type='button' id='nextPeriod'>▶</button>")
;
$("#prevPeriod").click(function () {
const period = currentPeriod();
const dropdown = $("#ctl00_ContentPlaceHolder1_PeriodDropdownList_Arrow").get(0);
dropdown.click();
const thisItem = $("li.rcbItem:contains('" + period + "')");
thisItem.next().click();
});
$("#nextPeriod").click(function () {
const period = currentPeriod();
const dropdown = $("#ctl00_ContentPlaceHolder1_PeriodDropdownList_Arrow").get(0);
dropdown.click();
const thisItem = $("li.rcbItem:contains('" + period + "')");
thisItem.prev().click();
});
}
function killThoseEffingMenuAnimations() {
Telerik.Web.UI.AnimationSettings.prototype.get_type = function () {
return 0;
};
Telerik.Web.UI.AnimationSettings.prototype.get_duration = function () {
return 0;
};
Telerik.Web.UI.RadMenu.prototype.get_collapseDelay = function () {
return 0;
};
}
function saneTableStyle() {
$("head").append("<style>" +
// Regneark-celler: hele cellen _er_ et input-felt
".riSingle {width:auto !important;} " +
"input.myclass { width: 100% !important; background-color: transparent !important; border-width:0 !important; } " +
// Konsistente tallkolonner
".rgMasterTable td[align=right]{font-family: Arial, sans-serif !important;} " + // like brede siffer i bold som normal
"input.myclass {font-family: Arial, sans-serif !important } " + // samme font i input
".RadGrid_WebBlue .rgFooter td{padding-right: 5px; border:unset} " + // samme høyremarg for input og utregnede verdier
// Flat stil
".RadGrid_WebBlue { border: unset } "+
".RadMenu_WebBlue .rmRootGroup {background-image: unset} "+
".RadGrid_WebBlue .rgHeader { background-image: unset; border: unset} " +
".RadGrid_WebBlue .rgRow>td, .RadGrid_WebBlue .rgAltRow>td " + " { border-width:0 0 0 1px;} " +
".RadGrid_WebBlue .rgCommandCell {border:unset} "+
".RadGrid_WebBlue .rgCommandRow {background: unset} " +
".RadGrid_WebBlue .rgPager {background:unset} "+
".RadGrid_WebBlue td.rgPagerCell {border:unset} "+
// Kolonnefarger
".rgMasterTable>tbody td[align=right]{padding: 1px; background-color:transparent;} " + // hvit for input
".rgMasterTable>tbody td{ background-color:rgba(0,64,128, 0.05);} " + // blågrå for read-only
".rgMasterTable>tbody td:last-child {background-color:rgba(0,64,128, 0.05);} " +
".RadGrid_WebBlue .rgAltRow { background-color: #f7f7f7;} " + // sebra-striper som funker med kolonnefargene
// Fjern øvre knapper
"thead .rgCommandRow {display: none} "+
// Flytt nedre knapper til høyre og på samme linje:
".RadGrid_WebBlue .rgCommandCell tr{display: inline} "+
".RadGrid_WebBlue .rgCommandCell table {width: 100%} "+
".RadGrid_WebBlue .rgCommandCell table tbody {text-align: right} "+
// Vanlig knappeutseende på periode-dropdown
".RadComboBox_WebBlue tr{display: block}"+
".RadComboBox_WebBlue .rcbArrowCell, .RadComboBox_WebBlue .rcbInputCell{-webkit-appearance: button; background-image: unset}"+
".RadComboBox_WebBlue .rcbInputCell{width: auto !important}"+
".RadComboBox .rcbInputCell input {width: auto}"+
".RadComboBox .rcbInputCell:after {content: '▼'}"+
".RadComboBox .rcbArrowCell {display:none}"+
"</style>");
}
function inputsInSameRow($input) {
const row = $input.closest('tr');
return $(row).find('input.riTextBox');
}
function inputsInSameColumn($input) {
const column = $input.closest('td').index() + 1;
return $input.closest('table').find('td:nth-child(' + column + ') input.riTextBox');
}
function fullDay($input) {
$input.val("7,5");
rightCell($input);
}
function inputLikeYesterday($input) {
const $inputToLeft = $input.closest("td").prev().find("input.riTextBox");
if ($inputToLeft.length === 1) {
$input.val($inputToLeft.val());
}
}
function columnLikeYesterday($input) {
const $inputToLeft = $input.closest("td").prev().find("input.riTextBox");
if ($inputToLeft.length === 0) {
return;
}
inputsInSameColumn($input).each(function () {
inputLikeYesterday($(this));
});
}
function autoFillShortcuts() {
$(document).keyup(function (keyEvent) {
switch (keyEvent.key) {
case " ":
fullDay($(keyEvent.target));
break;
case "=":
columnLikeYesterday($(keyEvent.target));
break;
}
});
$("input.riTextBox").attr("title", "Shortcuts: \n<space>: 7,5 \n'=': Entire day like yesterday");
}
function saneSaveShortcut() {
$(document).keydown(function (keyEvent) {
if (keyEvent.ctrlKey && keyEvent.key === "s") {
keyEvent.preventDefault();
keyEvent.target.blur();
$("#ctl00_ContentPlaceHolder1_Grid_TimeSheet_ctl00_ctl02_ctl00_BTN_SaveRegistrations").click();
return false;
}
});
$("input[id$=SaveRegistrations]").prop("title", "Ctrl+S");
}
function replaceSelection(input, content){
const old = $(input).val();
$(input).val(old.slice(0,input.selectionStart) + content + old.slice(input.selectionEnd));
}
function saneDecimalPoint() {
$("#TimeSheetTableDiv").find(".riTextBox").keydown(function (e1) {
if (e1.key === ".") {
replaceSelection(e1.target, ",");
return false;
}
});
}
function upCell($input) {
const columnNo = $($input).closest('td').index();
const $row = $input.closest('tr');
$row.prev().children().eq(columnNo).find('input.riTextBox').focus();
}
function downCell($input) {
const columnNo = $($input).closest('td').index();
const $row = $input.closest('tr');
$row.next().children().eq(columnNo).find('input.riTextBox').focus();
}
function rightCell($input) {
const $column = $($input).closest('td').next().find('input.riTextBox').focus();
}
function leftCell($input) {
//TODO: move focus left ONLY if cursor is at start of content.
}
function saneArrowKeys() {
$("input.riTextBox").each(function () {
this.control._incrementSettings.InterceptArrowKeys = false;
});
}
function arrowKeyNavigation() {
$(document).keydown(function (keyEvent) { // only keyup/keydown is generated for arrow keys (in Chrome)
switch (keyEvent.key) {
case "ArrowUp":
upCell($(keyEvent.target));
break;
case "ArrowDown":
downCell($(keyEvent.target));
break;
case "ArrowRight":
//rightCell($(keyEvent.target));
break;
case "ArrowLeft":
//leftCell($(keyEvent.target));
break;
}
});
}
function highlightDayOff($cell){
const column = $cell.index() + 1;
$cell.closest('table').find('tbody td:nth-child(' + column + ')').each( function(){
$(this).attr("style", "background-color:rgba(255,0,0, 0.05);"); //TODO: use class in stead
});
}
function highlightTimeDiffs() {
$("tr.rgFooter:nth-child(2)>td[align=right]").each(function () {
$(this).attr("style", "color:#999; font-weight:normal");
if ($(this).text().includes("0,00")) {
highlightDayOff($(this));
}
});
$("tr.rgFooter:last>td[align=right]").each(function () {
if ($(this).text().includes("-")) {
$(this).attr("style", "color:#e00; font-weight:bold");
}
else if ($(this).text().includes("0,00")) {
$(this).attr("style", "color:#0b0; font-weight:normal;");
}
else {
$(this).attr("style", "color:#00d; font-weight:bold");
}
});
}
function saneTableFocus() {
$("#ctl00_ContentPlaceHolder1_Grid_TimeSheet").removeAttr('tabIndex');
}
function conditionalPager(){
if ($(".rgPager .rgInfoPart").text().contains("in 1 pages")){
$(".rgPager").hide();
}
}
function toDecimal(hoursMinutes){
const [both, hours, minutes] = hoursMinutes.match(/(\d+):(\d+)/);
const hundredths = Math.round(minutes*100/60);
return `${hours},${hundredths}`;
}
function parseTime(src){
if (src.match(/\d+:\d+/)) return toDecimal(src);
return src.replace('.', ',');
}
function smartPaste(){
$("#TimeSheetTableDiv").find(".riTextBox").on('paste', function(e) {
const payload = e.originalEvent.clipboardData.getData('text');
const rowCells = inputsInSameRow(e.target);
const focusColumn = rowCells.index(e.target);
const newValues = payload.split("\t").map(s => parseTime(s));
if (newValues && newValues[0]){
for (let i = 0; i < newValues.length && i + focusColumn < rowCells.length; i++){
$(rowCells[i+focusColumn]).val(newValues[i]);
}
return false;
}
});
}
// SETUP
function onPeriodChange(handler) {
$(".CurrentPeriod").on("DOMNodeInserted", function (e) {
if (e.target.id === "ctl00_ContentPlaceHolder1_LBL_Approved") {
handler();
}
});
}
function afterNativePeriodInit() {
saneArrowKeys();
saneTableFocus();
}
function initPeriod() {
saneColumnHeaders();
sanePeriodHeader();
smartPaste();
highlightTimeDiffs();
conditionalPager();
setTimeout(afterNativePeriodInit, 100);
}
function initPeriodDirectView() {
sanePeriodNavigation();
arrowKeyNavigation();
saneSaveShortcut();
saneDecimalPoint();
autoFillShortcuts();
initPeriod();
onPeriodChange(initPeriod);
}
function initCommon() {
saneTableStyle();
killThoseEffingMenuAnimations();
}
function initPage() {
initCommon();
if ("/timereg_direct.aspx".appearsIn(document.location.pathname)) {
initPeriodDirectView();
}
}
initPage();