/* ── File Manager Module ── */
window.FilesModule = {
currentPath: '',
contextTarget: null,
toDelete: null,
pickerMode: false,
pickerCallback: null,
init: function() {
var self = this;
var params = new URLSearchParams(window.location.search);
if (params.get('picker') === '1') {
self.pickerMode = true;
self.pickerMode = params.get('mode') || 'dir';
}
window.addEventListener('message', function(e) {
try {
var d = JSON.parse(e.data);
if (d.action === 'fm-picker-open') {
self.pickerMode = true;
self.pickerMode = d.mode || 'dir';
self.pickerCallback = d.callback_id || null;
self.refresh();
}
} catch(ex) {}
});
// Path input: Enter to jump
var input = document.getElementById('fm-path-input');
input.onkeydown = function(e) {
if (e.key === 'Enter') {
var p = input.value.trim();
if (p) self._jumpTo(p);
}
};
self.refresh();
},
/* ── API helpers ── */
_api: function(url, opts) {
opts = opts || {};
opts.credentials = 'include';
return fetch(url, opts).then(function(r) {
if (!r.ok) throw new Error(r.status + ' ' + r.statusText);
return r.json();
});
},
_get: function(url) { return this._api(url); },
_post: function(url, body) {
return this._api(url, {method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify(body)});
},
/* ── Jump to path (with existence check) ── */
_jumpTo: function(path) {
var self = this;
// Probe: try to list the target path
this._get('./api/files/info?path=' + encodeURIComponent(path))
.then(function(info) {
if (info.error) { self._pathError(info.error); return; }
if (info.is_dir) {
self.currentPath = info.path;
self.refresh();
} else {
self._pathError('路径是文件,不是目录');
}
}).catch(function(e) {
self._pathError('目录不存在或无权限访问: ' + path);
});
},
_pathError: function(msg) {
var list = document.getElementById('fm-list');
list.innerHTML = '
' +
'⚠ ' + this._esc(msg) + '
';
// Still update the input and breadcrumb to show what was attempted
document.getElementById('fm-breadcrumb').innerHTML =
'' + this._esc(msg) + '';
},
navigate: function(path) {
this.currentPath = path || '';
this.refresh();
},
refresh: function() {
var self = this;
var showHidden = document.getElementById('fm-hidden')?.checked ? '1' : '0';
self._get('./api/files/list?path=' + encodeURIComponent(self.currentPath) + '&show_hidden=' + showHidden)
.then(function(d) {
self._render(d);
}).catch(function(e) {
document.getElementById('fm-list').innerHTML =
'
加载失败: ' + self._esc(e.message) + '
';
});
},
/* ── Render ── */
_render: function(d) {
var self = this;
// Update editable path input
var input = document.getElementById('fm-path-input');
input.value = d.current || '';
// Breadcrumbs (logical path)
var bc = document.getElementById('fm-breadcrumb');
var html = self._buildBreadcrumbHTML(d.breadcrumbs);
// Resolved breadcrumbs — show as second row when symlink redirect
if (d.resolved_breadcrumbs) {
html += '
';
}
bc.innerHTML = html;
bc.querySelectorAll('.fm-crumb').forEach(function(el) {
el.onclick = function() { self.navigate(this.dataset.path); };
});
// File list
var list = document.getElementById('fm-list');
var rows = '';
// ".." row for parent (unless at filesystem root)
if (d.parent && d.parent !== d.current) {
rows += '
' +
'📂' +
'..' +
'' +
'' +
'
';
}
if (d.items && d.items.length) {
d.items.forEach(function(item) {
if (item.broken) {
// Broken symlink or unreadable entry — show as disabled
rows += '