From b1ed0107c7f61899c402b5e7a19710afd12feab0 Mon Sep 17 00:00:00 2001 From: sbosse Date: Mon, 21 Jul 2025 23:20:01 +0200 Subject: [PATCH] Mon 21 Jul 22:43:21 CEST 2025 --- js/ui/webui/file.js | 395 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 395 insertions(+) create mode 100644 js/ui/webui/file.js diff --git a/js/ui/webui/file.js b/js/ui/webui/file.js new file mode 100644 index 0000000..3ecdcdf --- /dev/null +++ b/js/ui/webui/file.js @@ -0,0 +1,395 @@ +FileDialog = { + locked : false, + lock : function () { + if (FileDialog.locked) return false; + FileDialog.locked=true; + return true; + }, + unlock : function () { FileDialog.locked=false }, + refreshFileDialog : function (handle) { + var lastselect; + console.log('refreshFileDialog',handle.dir); + handle.list(handle.dir, function (res) { + if (Utils.isError(res)) { + return handle.onerror(res); + } + var input=$('#fileDialogListFileName'); + if (res && res.status==0) { + var list = $('#fileDialogList'); + list.empty(); + var entries=res.dirs; + if (handle.mode=='Open' || handle.mode=='Load') entries=entries.concat(res.files); + $('#fileDialogListLabel').html(handle.dir); + entries.forEach(function (entry) { + if (entry.name[0]=='.' && entry.name!='..') return; + var listentry = $('

',{ + class:'nav-entry '+(entry.dir?'nav-entry-directory':'nav-entry-file'), + style:'cursor: pointer' + }).append(''+entry.name+'').appendTo(list); + if (entry.dir) + listentry.bind('click',function () { + if (entry.name=='..') + handle.dir = handle.dir.replace(/(\/[^\/]+)$/,''); + else + handle.dir += ((handle.dir=='/'?'':'/')+entry.name); + handle.dir=handle.dir||'/'; + handle.selected=entry.name; + FileDialog.refreshFileDialog(handle); + }); + else { + listentry.bind('click',function () { + if (lastselect) $(lastselect).removeClass('nav-selected'); + $(listentry).addClass('nav-selected'); + lastselect=listentry; + handle.selected=entry.name; + input.val(entry.name); + }); + } + return entry; + }) + } + }) + }, + dialog : function (title, mode, dir, filename, fs, callback) { + if (!FileDialog.lock()) return; + var handle={selected:filename, dir:dir, mode:mode, list:fs.list}; + var html='

'+title+ + '

'; + html += ''; + html += '
'; + html += '
'; + var input,options + handle.onerror=function (err) { + // Fallback: Browser file dialog + console.log('dialog',err); + options.close(); + FileDialog.unlock(); + if (callback) callback(null,null,err); + }; + setTimeout(function () { FileDialog.refreshFileDialog(handle); input=$('#fileDialogListFileName'); },1); + popup.custom(options={content:html,default_btns:{ok:mode}},function (reply) { + FileDialog.unlock(); + if (reply.proceed) { + filename=input.val(); + if (callback) callback(handle.dir,filename); + } + }); + }, +} + + +if (!Config.wex) Config.wex={http:'http://localhost:11111' }; +if (!Config.workdir) Config.workdir='/'; + +// FS APi using the WEX server +FS = { + list : async function (dir,cb) { + var result; + function handler (response) { + if (Utils.isError(response)) { + if (cb) cb(response); + return; + } + if (!Utils.isObject(response) || response.status!=0) { + result=response; + if (cb) cb(result); + return; + } + var dirs=response.reply.filter(function (entry) { return entry.dir }) + .sort(function (a,b) { return a.name