forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsidebarApp.js
More file actions
181 lines (162 loc) · 3.67 KB
/
sidebarApp.js
File metadata and controls
181 lines (162 loc) · 3.67 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
176
177
178
179
180
181
/**@type {HTMLElement} */
let $apps;
/**@type {HTMLElement} */
let $sidebar;
/**@type {HTMLElement} */
let $contaienr;
export default class SidebarApp {
/**@type {HTMLSpanElement} */
#icon;
/**@type {string} */
#id;
/**@type {string} */
#init;
/**@type {string} */
#title;
/**@type {boolean} */
#active;
/**@type {(el:HTMLElement)=>void} */
#onselect;
/**@type {HTMLElement} */
#container;
/**
* Creates a new sidebar app.
* @param {string} icon
* @param {string} id
* @param {string} title
* @param {(el:HTMLElement)=>void} init
* @param {(el:HTMLElement)=>void} onselect
*/
constructor(icon, id, title, init, onselect) {
const emptyFunc = () => {};
this.#container = <div className="container"></div>;
this.#icon = <Icon icon={icon} id={id} title={title} />;
this.#id = id;
this.#title = title;
this.#init = init || emptyFunc;
this.#onselect = onselect || emptyFunc;
this.#init(this.#container);
}
/**
* Installs the app in the sidebar.
* @param {boolean} prepend
* @returns {void}
*/
install(prepend = false) {
if (prepend) {
$apps.prepend(this.#icon);
return;
}
$apps.append(this.#icon);
}
/**
* Initialize the sidebar element.
* @param {HTMLElement} $el sidebar element
* @param {HTMLElement} $el2 apps element
*/
static init($el, $el2) {
$sidebar = $el;
$apps = $el2;
}
/**@type {HTMLSpanElement} */
get icon() {
return this.#icon;
}
/**@type {string} */
get id() {
return this.#id;
}
/**@type {string} */
get title() {
return this.#title;
}
/**@type {boolean} */
get active() {
return !!this.#active;
}
/**@param {boolean} value */
set active(value) {
const nextValue = !!value;
if (this.#active === nextValue) return;
this.#active = nextValue;
this.#icon.classList.toggle("active", this.#active);
if (this.#active) {
const oldContainer = getContainer(this.#container);
// Try to replace the old container, or append if it's not in the DOM
try {
if (oldContainer && oldContainer.parentNode === $sidebar) {
$sidebar.replaceChild($contaienr, oldContainer);
} else {
// Old container not in sidebar, just append the new one
const existingContainer = $sidebar.get(".container");
if (existingContainer) {
$sidebar.replaceChild($contaienr, existingContainer);
} else {
$sidebar.appendChild($contaienr);
}
}
} catch (error) {
// Fallback: append the new container
console.warn("Error switching sidebar container:", error);
const existingContainer = $sidebar.get(".container");
if (existingContainer) {
existingContainer.remove();
}
$sidebar.appendChild($contaienr);
}
this.#onselect(this.#container);
}
}
/**@type {HTMLElement} */
get container() {
return this.#container;
}
/**@type {(el:HTMLElement)=>void} */
get init() {
return this.#init;
}
/**@type {(el:HTMLElement)=>void} */
get onselect() {
return this.#onselect;
}
remove() {
if (this.#icon) {
this.#icon.remove();
this.#icon = null;
}
if (this.#container) {
this.#container.remove();
this.#container = null;
}
}
}
/**
* Creates a icon element for a sidebar app.
* @param {object} param0
* @param {string} param0.icon
* @param {string} param0.id
* @returns {HTMLElement}
*/
function Icon({ icon, id, title }) {
const className = `icon ${icon}`;
return (
<span
data-action="sidebar-app"
data-id={id}
title={title}
className={className}
></span>
);
}
/**
* Gets the container or sets it if it's not set.
* @param {HTMLElement} $el
* @returns {HTMLElement}
*/
function getContainer($el) {
const res = $contaienr;
if ($el) {
$contaienr = $el;
}
return res || $sidebar.get(".container");
}