-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathdomHandler.js
More file actions
175 lines (155 loc) · 5.41 KB
/
domHandler.js
File metadata and controls
175 lines (155 loc) · 5.41 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
if (typeof window.domHandlerLoaded === "undefined") {
console.log("domHandler.js loaded");
window.domHandlerLoaded = true;
// DOM manipulation utilities
const DOMHandler = {
// Create checkbox with consistent styling
createCheckbox(index) {
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.className = CSS_CLASSES.CHECKBOX;
checkbox.dataset.index = index;
checkbox.style.cssText = `
margin-right: 8px;
margin-left: 4px;
position: relative;
top: 1px;
`;
return checkbox;
},
// Create flex container for conversation layout
createFlexContainer() {
const container = document.createElement("div");
container.style.cssText = `
display: flex;
align-items: center;
width: 100%;
padding: 0;
`;
return container;
},
// Get conversation title safely
getConversationTitle(conversationElement) {
const titleElement = conversationElement.querySelector(UI_CONFIG.SELECTORS.TITLE_SELECTOR);
return titleElement ? titleElement.textContent.trim() : "this conversation";
},
// Find interactive element in conversation
findInteractiveElement(conversationElement) {
return conversationElement.querySelector(UI_CONFIG.SELECTORS.INTERACTIVE_ELEMENT_SELECTOR);
},
// Dispatch hover event
dispatchHoverEvent(element) {
const hoverEvent = new MouseEvent("mouseover", {
view: window,
bubbles: true,
cancelable: true,
});
element.dispatchEvent(hoverEvent);
},
// Dispatch pointer down event
dispatchPointerDownEvent(element) {
const pointerDownEvent = new PointerEvent("pointerdown", {
bubbles: true,
cancelable: true,
pointerType: "mouse",
});
element.dispatchEvent(pointerDownEvent);
},
// Get all conversations from history
getAllConversations() {
const history = document.querySelector(UI_CONFIG.SELECTORS.HISTORY);
if (!history) {
throw new Error("History container not found");
}
return history.querySelectorAll(UI_CONFIG.SELECTORS.CONVERSATION_SELECTOR);
},
// Toggle conversation link interaction
toggleConversationInteraction(conversation, disable = true) {
const link = conversation.querySelector("a");
if (link) {
if (disable) {
link.style.pointerEvents = "none";
link.style.cursor = "default";
} else {
link.style.pointerEvents = "auto";
link.style.cursor = "pointer";
}
}
}
};
// Event handling utilities
const EventHandler = {
// Handle checkbox click with shift selection
handleCheckboxClick(event, checkbox) {
event.stopPropagation();
this.handleShiftSelection(checkbox);
GlobalState.setLastCheckedCheckbox(checkbox);
},
// Handle shift key selection
handleShiftSelection(clickedCheckbox) {
if (GlobalState.isShiftPressed() && GlobalState.getLastCheckedCheckbox()) {
const allCheckboxes = Array.from(
document.querySelectorAll(`.${CSS_CLASSES.CHECKBOX}`)
);
const start = allCheckboxes.indexOf(GlobalState.getLastCheckedCheckbox());
const end = allCheckboxes.indexOf(clickedCheckbox);
if (start !== -1 && end !== -1) {
const [lower, upper] = start < end ? [start, end] : [end, start];
for (let i = lower; i <= upper; i++) {
allCheckboxes[i].checked = true;
}
}
}
},
// Toggle checkbox in conversation
toggleCheckboxInConversation(conversation, event) {
event.preventDefault();
event.stopPropagation();
const checkbox = conversation.querySelector(`.${CSS_CLASSES.CHECKBOX}`);
if (checkbox) {
checkbox.checked = !checkbox.checked;
this.handleShiftSelection(checkbox);
if (checkbox.checked) {
GlobalState.setLastCheckedCheckbox(checkbox);
}
}
},
// Add keyboard event listeners
addKeyboardListeners() {
console.log("Adding keyboard event listeners...");
document.addEventListener("keydown", (event) => {
if (event.key === "Shift") {
console.log("Shift key pressed");
GlobalState.setShiftPressed(true);
}
});
document.addEventListener("keyup", (event) => {
if (event.key === "Shift") {
console.log("Shift key released");
GlobalState.setShiftPressed(false);
}
});
}
};
// Export to global scope (for backward compatibility)
window.DOMHandler = DOMHandler;
window.EventHandler = EventHandler;
// Register modules with the core system
if (window.ChatGPTBulkDelete && window.ChatGPTBulkDelete.registerModule) {
window.ChatGPTBulkDelete.registerModule('DOMHandler', DOMHandler);
window.ChatGPTBulkDelete.registerModule('EventHandler', EventHandler);
} else {
// Fallback: wait for core system to be ready
const registerModules = () => {
if (window.ChatGPTBulkDelete && window.ChatGPTBulkDelete.registerModule) {
window.ChatGPTBulkDelete.registerModule('DOMHandler', DOMHandler);
window.ChatGPTBulkDelete.registerModule('EventHandler', EventHandler);
} else {
setTimeout(registerModules, 50);
}
};
registerModules();
}
} else {
console.log("domHandler.js already loaded, skipping re-initialization");
}