forked from HeyPuter/puter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUITaskbar.js
304 lines (280 loc) · 12.4 KB
/
UITaskbar.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* Copyright (C) 2024 Puter Technologies Inc.
*
* This file is part of Puter.
*
* Puter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import UITaskbarItem from './UITaskbarItem.js'
import UIPopover from './UIPopover.js'
async function UITaskbar(options){
global_element_id++;
options = options ?? {};
options.content = options.content ?? '';
// get launch apps
$.ajax({
url: api_origin + "/get-launch-apps",
type: 'GET',
async: true,
contentType: "application/json",
headers: {
"Authorization": "Bearer "+auth_token
},
success: function (apps){
window.launch_apps = apps;
}
});
let h = '';
h += `<div id="ui-taskbar_${global_element_id}" class="taskbar" style="height:${window.taskbar_height}px;"><span id='clock'></span></div>`;
$('.desktop').append(h);
//---------------------------------------------
// add `Start` to taskbar
//---------------------------------------------
UITaskbarItem({
icon: window.icons['start.svg'],
name: 'Start',
sortable: false,
keep_in_taskbar: true,
disable_context_menu: true,
onClick: async function(item){
// skip if popover already open
if($(item).hasClass('has-open-popover'))
return;
// show popover
let popover = UIPopover({
content: `<div class="launch-popover hide-scrollbar"><span class="close-launch-popover">✕</span></div>`,
snapToElement: item,
parent_element: item,
width: 500,
height: 500,
center_horizontally: true,
});
// In the rare case that launch_apps is not populated yet, get it from the server
// then populate the popover
if(!launch_apps || !launch_apps.recent || launch_apps.recent.length === 0){
// get launch apps
launch_apps = await $.ajax({
url: api_origin + "/get-launch-apps",
type: 'GET',
async: true,
contentType: "application/json",
headers: {
"Authorization": "Bearer "+auth_token
},
});
}
let apps_str = '';
apps_str += `<div style="margin-bottom: 10px; padding: 5px; position: relative;">`
apps_str += `<input style="background-image:url(${window.icons['magnifier-outline.svg']});" class="launch-search">`;
apps_str += `<img class="launch-search-clear" src="${window.icons['close.svg']}">`;
apps_str += `</div>`;
// -------------------------------------------
// Recent apps
// -------------------------------------------
if(launch_apps.recent.length > 0){
// heading
apps_str += `<h1 class="start-section-heading start-section-heading-recent">Recent</h1>`;
// apps
apps_str += `<div class="launch-apps-recent">`;
for (let index = 0; index < window.launch_recent_apps_count && index < launch_apps.recent.length; index++) {
const app_info = launch_apps.recent[index];
apps_str += `<div title="${html_encode(app_info.title)}" data-name="${html_encode(app_info.name)}" class="start-app-card">`;
apps_str += `<div class="start-app" data-app-name="${html_encode(app_info.name)}" data-app-uuid="${html_encode(app_info.uuid)}" data-app-icon="${html_encode(app_info.icon)}" data-app-title="${html_encode(app_info.title)}">`;
apps_str += `<img class="start-app-icon" src="${html_encode(app_info.icon ? app_info.icon : window.icons['app.svg'])}">`;
apps_str += `<span class="start-app-title">${html_encode(app_info.title)}</span>`;
apps_str += `</div>`;
apps_str += `</div>`;
}
apps_str += `</div>`;
}
// -------------------------------------------
// Reccomended apps
// -------------------------------------------
if(launch_apps.recommended.length > 0){
// heading
apps_str += `<h1 class="start-section-heading start-section-heading-recommended" style="${launch_apps.recent.length > 0 ? 'padding-top: 30px;' : ''}">Recommended</h1>`;
// apps
apps_str += `<div class="launch-apps-recommended">`;
for (let index = 0; index < launch_apps.recommended.length; index++) {
const app_info = launch_apps.recommended[index];
apps_str += `<div title="${html_encode(app_info.title)}" data-name="${html_encode(app_info.name)}" class="start-app-card">`;
apps_str += `<div class="start-app" data-app-name="${html_encode(app_info.name)}" data-app-uuid="${html_encode(app_info.uuid)}" data-app-icon="${html_encode(app_info.icon)}" data-app-title="${html_encode(app_info.title)}">`;
apps_str += `<img class="start-app-icon" src="${html_encode(app_info.icon ? app_info.icon : window.icons['app.svg'])}">`;
apps_str += `<span class="start-app-title">${html_encode(app_info.title)}</span>`;
apps_str += `</div>`;
apps_str += `</div>`;
}
apps_str += `</div>`;
}
// add apps to popover
$(popover).find('.launch-popover').append(apps_str);
// focus on search input only if not on mobile
if(!isMobile.phone)
$(popover).find('.launch-search').focus();
// make apps draggable
$(popover).find('.start-app').draggable({
appendTo: "body",
revert: "invalid",
connectToSortable: ".taskbar",
//containment: "document",
zIndex: parseInt($(popover).css('z-index')) + 1,
scroll: false,
distance: 5,
revertDuration: 100,
helper: 'clone',
cursorAt: { left: 18, top: 20 },
start: function(event, ui){
},
drag: function(event, ui){
},
stop: function(){
}
});
}
});
//---------------------------------------------
// add `Explorer` to the taskbar
//---------------------------------------------
UITaskbarItem({
icon: window.icons['folders.svg'],
app: 'explorer',
name: 'Explorer',
sortable: false,
keep_in_taskbar: true,
lock_keep_in_taskbar: true,
onClick: function(){
let open_window_count = parseInt($(`.taskbar-item[data-app="explorer"]`).attr('data-open-windows'));
if(open_window_count === 0){
launch_app({ name: 'explorer', path: window.home_path});
}else{
return false;
}
}
})
//---------------------------------------------
// Add other useful apps to the taskbar
//---------------------------------------------
if(window.user.taskbar_items && window.user.taskbar_items.length > 0){
for (let index = 0; index < window.user.taskbar_items.length; index++) {
const app_info = window.user.taskbar_items[index];
// add taskbar item for each app
UITaskbarItem({
icon: app_info.icon,
app: app_info.name,
name: app_info.title,
keep_in_taskbar: true,
onClick: function(){
let open_window_count = parseInt($(`.taskbar-item[data-app="${app_info.name}"]`).attr('data-open-windows'));
if(open_window_count === 0){
launch_app({
name: app_info.name,
})
}else{
return false;
}
}
});
}
}
//---------------------------------------------
// add `Trash` to the taskbar
//---------------------------------------------
const trash = await puter.fs.stat(trash_path);
if(window.socket){
window.socket.emit('trash.is_empty', {is_empty: trash.is_empty});
}
UITaskbarItem({
icon: trash.is_empty ? window.icons['trash.svg'] : window.icons['trash-full.svg'],
app: 'trash',
name: 'Trash',
sortable: false,
keep_in_taskbar: true,
lock_keep_in_taskbar: true,
onClick: function(){
let open_windows = $(`.window[data-path="${html_encode(trash_path)}"]`);
if(open_windows.length === 0){
launch_app({ name: 'explorer', path: window.trash_path});
}else{
open_windows.focusWindow();
}
},
onItemsDrop: function(items){
move_items(items, trash_path);
}
})
make_taskbar_sortable();
}
window.make_taskbar_sortable = function(){
//-------------------------------------------
// Taskbar is sortable
//-------------------------------------------
$('.taskbar').sortable({
axis: "x",
items: '.taskbar-item-sortable:not(.has-open-contextmenu)',
cancel: '.has-open-contextmenu',
placeholder: "taskbar-item-sortable-placeholder",
helper : 'clone',
distance: 5,
revert: 10,
receive: function(event, ui){
if(!$(ui.item).hasClass('taskbar-item')){
// if app is already in taskbar, cancel
if($(`.taskbar-item[data-app="${$(ui.item).attr('data-app-name')}"]`).length !== 0){
$(this).sortable('cancel');
$('.taskbar .start-app').remove();
return;
}else{
}
}
},
update: function(event, ui){
if(!$(ui.item).hasClass('taskbar-item')){
// if app is already in taskbar, cancel
if($(`.taskbar-item[data-app="${$(ui.item).attr('data-app-name')}"]`).length !== 0){
$(this).sortable('cancel');
$('.taskbar .start-app').remove();
return;
}
let item = UITaskbarItem({
icon: $(ui.item).attr('data-app-icon'),
app: $(ui.item).attr('data-app-name'),
name: $(ui.item).attr('data-app-title'),
append_to_taskbar: false,
keep_in_taskbar: true,
onClick: function(){
let open_window_count = parseInt($(`.taskbar-item[data-app="${$(ui.item).attr('data-app-name')}"]`).attr('data-open-windows'));
if(open_window_count === 0){
launch_app({
name: $(ui.item).attr('data-app-name'),
})
}else{
return false;
}
}
});
let el = ($(item).detach())
$(el).insertAfter(ui.item);
// $(ui.item).insertBefore(`<h1>Hello!</h1>`);
$(el).show();
$(ui.item).removeItems();
update_taskbar();
}
// only proceed to update DB if the item sorted was a pinned item otherwise no point in updating the taskbar in DB
else if($(ui.item).attr('data-keep-in-taskbar') === 'true'){
update_taskbar();
}
},
});
}
export default UITaskbar;