main.js (2.3 KB)


 1 // Main: initialization and global event listeners
 2 
 3 document.addEventListener('click', function(e) {
 4 	if (!e.target.closest('.task-container')) {
 5 		var activeTask = document.querySelector('.task-container.active');
 6 		if (activeTask) {
 7 			var taskInput = activeTask.querySelector('input[type="text"]');
 8 			taskInput.focus();
 9 		} else {
10 			selectFirstSubtask();
11 		}
12 	}
13 });
14 
15 document.addEventListener('DOMContentLoaded', function() {
16 	state.appContainer = document.getElementById('app-container');
17 	state.rootTask = loadTasksFromLocalStorage();
18 	state.currentTask = state.rootTask;
19 	state.taskPath = [state.currentTask];
20 
21 	// Scroll handler to block default scroll behavior
22 	function handleScroll(e) {
23 		e.preventDefault();
24 	}
25 	window.addEventListener('wheel', handleScroll, { passive: false });
26 
27 	// Print handlers
28 	function handleBeforePrint() {
29 		var printContent = document.getElementById('print-content');
30 		if (!printContent) {
31 			printContent = document.createElement('div');
32 			printContent.id = 'print-content';
33 			document.body.appendChild(printContent);
34 		}
35 		var serializedTasks = serializeTaskTree(state.currentTask);
36 		printContent.textContent = serializedTasks;
37 	}
38 
39 	function handleAfterPrint() {
40 		var printContent = document.getElementById('print-content');
41 		if (printContent) {
42 			printContent.remove();
43 		}
44 	}
45 
46 	window.addEventListener('beforeprint', handleBeforePrint);
47 	window.addEventListener('afterprint', handleAfterPrint);
48 
49 	// Theme cycling with F2
50 	document.addEventListener('keydown', function(event) {
51 		if (event.key === 'F2' && !state.isF2Pressed) {
52 			event.preventDefault();
53 			state.isF2Pressed = true;
54 			cycleTheme();
55 		}
56 	});
57 
58 	document.addEventListener('keyup', function(event) {
59 		if (event.key === 'F2') {
60 			state.isF2Pressed = false;
61 		}
62 	});
63 
64 	// File save/open handlers
65 	document.addEventListener('keydown', handleSave);
66 	document.addEventListener('keydown', handleOpen);
67 
68 	// Reset cursor on non-focused inputs
69 	state.appContainer.addEventListener('focusin', function(e) {
70 		if (e.target.tagName === 'INPUT' && e.target.type === 'text') {
71 			document.querySelectorAll('input[type="text"]').forEach(input => {
72 				if (input !== e.target) {
73 					placeCursorAtBeginning(input);
74 				}
75 			});
76 		}
77 	});
78 
79 	// Initialize
80 	setInitialTheme();
81 	renderCurrentView();
82 	selectFirstSubtask();
83 });