-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathinput-field-tweaks.js
96 lines (75 loc) · 1.99 KB
/
input-field-tweaks.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
import fitTextarea from 'fit-textarea';
import {newReplyTextareasObserver} from '../libs/dom-utils';
import {paths} from '../libs/paths';
// Dynamically change textarea height
function watchTextareas() {
const textareas = document.querySelectorAll('textarea');
for (const textarea of textareas) {
fitTextarea.watch(textarea);
}
newReplyTextareasObserver(event => {
fitTextarea.watch(event.target);
});
return true;
}
// Dynamically increase / decrease title field length
function dynamicallyChangeWidth(path) {
if (['/reply', '/user', ...paths.comments].includes(path)) {
return false;
}
const titleInput = document.querySelector('input[name="title"]');
if (!titleInput) {
return false;
}
titleInput.style.width = '49ch';
titleInput.addEventListener('input', () => {
const {length} = titleInput.value;
titleInput.style.width = length < 49 ? '49ch' : (length + 1) + 'ch';
});
return true;
}
// Show characters remaining beside title field
function charactersRemainging(path) {
if (['/reply', '/newpoll', '/user', ...paths.comments].includes(path)) {
return false;
}
const titleInput = document.querySelector('input[name="title"]');
if (!titleInput) {
return false;
}
const titleLengthLimit = 80;
const span = document.createElement('span');
span.classList.add('__rhn__characters-under');
titleInput.nextElementSibling.classList.add('__rhn__characters-over');
titleInput.parentElement.append(span);
titleInput.addEventListener('input', () => {
const {length} = titleInput.value;
span.innerHTML = length <= titleLengthLimit ?
`${titleLengthLimit - length} remaining` :
'';
});
return true;
}
function init(metadata) {
watchTextareas();
dynamicallyChangeWidth(metadata.path);
charactersRemainging(metadata.path);
return true;
}
const details = {
id: 'input-field-tweaks',
pages: {
include: [
...paths.forms,
...paths.comments,
'/user'
],
exclude: [
'/delete-confirm',
'/changepw'
]
},
loginRequired: false,
init
};
export default details;