aboutsummaryrefslogtreecommitdiff
path: root/js/microblog.js
blob: f7963e32b0dd2cc35e5ce350efd98fa86213579d (plain)
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
'use strict';

document.documentElement.classList.remove('no-js');

const textarea = document.querySelector('textarea[name="content"]');
const charCount = document.querySelector('#count');

if (textarea) {
	const maxCount = parseInt(textarea.getAttribute('maxlength'));

	if (textarea.value.length > 0) {
		const textLength = [...textarea.value].length;
		charCount.textContent = maxCount - textLength;
	} else {
		charCount.textContent = maxCount;
	}

	textarea.addEventListener('input', function () {
		const textLength = [...this.value].length;

		charCount.textContent = maxCount - textLength;
	}, false);
}

const postForm = document.querySelector('#post-new-form');
let useDragDrop = (!!window.FileReader && 'draggable' in document.createElement('div'));
useDragDrop = true;
if (postForm) {
	const droparea = postForm.querySelector('#post-droparea');
	const attachmentsInput = postForm.querySelector('#post-attachments');
	const data = {
		'attachments': []
	};


	if (droparea && attachmentsInput) {
		if (useDragDrop) {
			console.log('init with modern file attachments');

			const list = postForm.querySelector('#post-attachments-list');
			list.addEventListener('click', function (e) {
				e.preventDefault();

				// remove attachment
				if (e.target.nodeName.toLowerCase() == 'li') {
					const filename = e.target.textContent;

					data.attachments = data.attachments.filter(function (ele) {
						return ele.name !== filename;
					});

					e.target.remove();
				}
			});

			droparea.classList.remove('hidden');
			document.querySelector('#post-attachments-label').classList.add('hidden');

			droparea.ondragover = droparea.ondragenter = function (e) {
				e.stopPropagation();
				e.preventDefault();

				e.dataTransfer.dropEffect = 'copy';
				e.target.classList.add('drag');
			};

			droparea.ondragleave = function (e) {
				e.target.classList.remove('drag');
			};

			droparea.onclick = function (e) {
				e.preventDefault();

				// make a virtual file upload
				const input = document.createElement('input');
				input.type = 'file';
				input.setAttribute('multiple', '');
				input.setAttribute('accept', 'image/*'); // only images for now

				input.onchange = e => {
					processSelectedFiles(e.target.files);
				}

				input.click();
			};

			function processSelectedFiles(files) {
				if (!files || files.length < 1) return;

				for (const file of files) {
					const found = data.attachments.find(ele => ele.name === file.name);
					if(found) continue; // skip existing attachments

					data.attachments.push({
						'name': file.name,
						// todo: maybe some better form of dupe detection here?
						'file': file
					});

					const li = document.createElement('li');
					li.textContent = file.name;

					const reader = new FileReader();

					if(file.type.startsWith('image/')) {
						reader.onload = function (e) {
							var dataURL = e.target.result;

							const preview = document.createElement('img');
							preview.classList.add('file-preview');
							preview.setAttribute('src', dataURL);

							li.prepend(preview);
						};
						reader.onerror = function (e) {
							console.log('An error occurred during file input: '+e.target.error.code);
						};

						reader.readAsDataURL(file);
					}

					list.append(li);
				}
			}

			droparea.ondrop = function (e) {
				if (e.dataTransfer) {
					e.preventDefault();
					e.stopPropagation();

					processSelectedFiles(e.dataTransfer.files);
				}

				e.target.classList.remove('drag');
			};

			postForm.addEventListener('submit', async function (e) {
				e.preventDefault();

				const postFormData = new FormData();

				postFormData.append('content', postForm.querySelector('[name="content"]').value);

				for (const attachment of data.attachments) {
					postFormData.append('attachments[]', attachment.file);
				}

				/*
				for (const pair of postFormData.entries()) {
					console.log(`${pair[0]}, ${pair[1]}`);
				}
				*/

				const response = await fetch(postForm.getAttribute('action'), {
					body: postFormData,
					method: 'POST'
				});

				if (response.ok && response.status == 200) {
					const txt = await response.text();
					// console.log(response, txt);
					window.location.href = postForm.dataset.redirect;
				} else {
					console.warn('error during post submission!', response);
				}
			});
		} else {
			// use the native file input dialog
			// but enhanced
			if (attachmentsInput) {
				console.log('init with classic file attachments');

				attachmentsInput.addEventListener('change', function (e) {
					console.log(e.target.files);

					const list = postForm.querySelector('#post-attachments-list');
					list.replaceChildren();

					for (const file of e.target.files) {
						const li = document.createElement('li');
						li.textContent = file.name;
						list.append(li);
					}
				});
			}
		}
	}
}

// better rounded corners
if ('paintWorklet' in CSS) {
	// CSS.paintWorklet.addModule('./js/squircle.js');
}