Explorar o código

Add file selector full path enter support

Toby Chui hai 1 día
pai
achega
3ac926a335

+ 24 - 6
src/system/locale/file_selector.json

@@ -89,7 +89,10 @@
                 "overwrite/replace": "取代",
                 "overwrite/cancel": "取消",
                 "ctx/newfolder": "新增資料夾",
-                "ctx/folderproperties": "資料夾內容"
+                "ctx/folderproperties": "資料夾內容",
+                "footer/folder": "資料夾:",
+                "footer/folderhint": "目前資料夾,或輸入路徑",
+                "message/cannotopenfolder": "找不到或無法開啟資料夾"
             },
             "titles": {
                 "Back": "上一頁",
@@ -199,7 +202,10 @@
                 "overwrite/replace": "取代",
                 "overwrite/cancel": "取消",
                 "ctx/newfolder": "新增資料夾",
-                "ctx/folderproperties": "資料夾內容"
+                "ctx/folderproperties": "資料夾內容",
+                "footer/folder": "資料夾:",
+                "footer/folderhint": "目前資料夾,或輸入路徑",
+                "message/cannotopenfolder": "找不到或無法開啟資料夾"
             },
             "titles": {
                 "Back": "上一頁",
@@ -309,7 +315,10 @@
                 "overwrite/replace": "替换",
                 "overwrite/cancel": "取消",
                 "ctx/newfolder": "新建文件夹",
-                "ctx/folderproperties": "文件夹属性"
+                "ctx/folderproperties": "文件夹属性",
+                "footer/folder": "文件夹:",
+                "footer/folderhint": "当前文件夹,或输入路径",
+                "message/cannotopenfolder": "找不到或无法打开文件夹"
             },
             "titles": {
                 "Back": "上一页",
@@ -419,7 +428,10 @@
                 "overwrite/replace": "Replace",
                 "overwrite/cancel": "Cancel",
                 "ctx/newfolder": "New Folder",
-                "ctx/folderproperties": "Folder Properties"
+                "ctx/folderproperties": "Folder Properties",
+                "footer/folder": "Folder:",
+                "footer/folderhint": "Current folder, or type a path",
+                "message/cannotopenfolder": "Folder not found or cannot be opened"
             },
             "titles": {
                 "Back": "Back",
@@ -529,7 +541,10 @@
                 "overwrite/replace": "置き換える",
                 "overwrite/cancel": "キャンセル",
                 "ctx/newfolder": "新しいフォルダー",
-                "ctx/folderproperties": "フォルダーのプロパティ"
+                "ctx/folderproperties": "フォルダーのプロパティ",
+                "footer/folder": "フォルダー:",
+                "footer/folderhint": "現在のフォルダー、またはパスを入力",
+                "message/cannotopenfolder": "フォルダーが見つからないか開けません"
             },
             "titles": {
                 "Back": "戻る",
@@ -639,7 +654,10 @@
                 "overwrite/replace": "바꾸기",
                 "overwrite/cancel": "취소",
                 "ctx/newfolder": "새 폴더",
-                "ctx/folderproperties": "폴더 속성"
+                "ctx/folderproperties": "폴더 속성",
+                "footer/folder": "폴더:",
+                "footer/folderhint": "현재 폴더 또는 경로 입력",
+                "message/cannotopenfolder": "폴더를 찾을 수 없거나 열 수 없습니다"
             },
             "titles": {
                 "Back": "뒤로",

+ 203 - 15
src/web/SystemAO/file_system/file_selector.html

@@ -765,7 +765,28 @@
                 border-top: 1px solid var(--fs-line);
                 flex-shrink: 0;
                 flex-wrap: wrap;
+                position: relative;
+            }
+            /* A typed folder path that could not be opened, floated above the footer */
+            .fsPathError{
+                display: none;
+                position: absolute;
+                left: 14px;
+                right: 14px;
+                bottom: 100%;
+                margin-bottom: 6px;
+                z-index: 120;
+                padding: 8px 12px;
+                border-radius: 8px;
+                background: var(--fs-toast-error-bg);
+                /* !important: ao.css repaints every div under a themed body */
+                color: var(--fs-toast-error-fg) !important;
+                box-shadow: 0 4px 14px var(--fs-shadow);
+                font-size: 13px;
+                word-break: break-all;
             }
+            .fsPathError.show{ display: block; }
+            .fsFooter #filename.invalid{ border-color: var(--fs-toast-error-bg); }
             .fsFooterLabel{
                 font-size: 14px;
                 color: var(--fs-text);
@@ -1087,6 +1108,7 @@
 
             <!-- Footer -->
             <div class="fsFooter">
+                <div class="fsPathError" id="pathError" role="alert"></div>
                 <span class="fsFooterLabel" id="filenameLabel" locale="footer/filename">File name(N):</span>
                 <input id="filename" type="text" spellcheck="false" autocomplete="off" placeholder="New Filename">
                 <select id="filterSelect" onchange="renderFileList();"></select>
@@ -1145,6 +1167,10 @@
 
                 The result is handed back either through the float window parent callback
                 (inside the ArozOS desktop) or through localStorage[listenerUUID].
+
+                A folder path may also be typed into the name field: "file" and "new"
+                pickers open that folder, a "folder" picker returns it, see
+                openTypedPath().
             */
 
             /* ------------------------------------------------------------------ */
@@ -1341,6 +1367,7 @@
                     }else{
                         $("#filename").val("newfile.txt");
                     }
+                    lastPlainFilename = $("#filename").val();
                     /*
                         This picker writes a new file rather than opening an
                         existing one, so the confirm button says so.
@@ -1353,9 +1380,11 @@
                     $("#confirmBtn").attr("locale", "footer/create")
                                     .text(applocale.getString("footer/create", "Create"));
                 }else if (type == "folder"){
-                    //A filename has no meaning when picking a folder
-                    $("#filename").hide();
-                    $("#filenameLabel").hide();
+                    //The name field takes a folder name or path here. Left empty,
+                    //the folder being browsed is the one picked
+                    $("#filenameLabel").attr("locale", "footer/folder")
+                                       .text(applocale.getString("footer/folder", "Folder:"));
+                    $("#filename").attr("placeholder", applocale.getString("footer/folderhint", "Current folder, or type a path"));
                 }
 
                 buildFilterOptions();
@@ -1561,10 +1590,16 @@
             /* ------------------------------------------------------------------ */
             /*  Directory listing                                                  */
             /* ------------------------------------------------------------------ */
-            function listDir(dir, pushHistory){
+            //preloaded: the listing of "dir" when the caller has already fetched it
+            function listDir(dir, pushHistory, preloaded){
                 if (pushHistory === undefined){
                     pushHistory = true;
                 }
+                //A folder picker's name field belongs to the folder it was typed in
+                if (type == "folder" && dir != currentDir){
+                    $("#filename").val("");
+                }
+                clearPathError();
                 currentDir = dir;
                 if (pushHistory){
                     //Drop anything ahead of the cursor before appending, so that
@@ -1598,6 +1633,13 @@
                 $("#loadingPlaceholder").addClass("show");
                 clearDetails();
 
+                if (preloaded !== undefined){
+                    $("#loadingPlaceholder").removeClass("show");
+                    currentFileList = preloaded || [];
+                    renderFileList();
+                    return;
+                }
+
                 $.ajax({
                     url: "../../system/file_system/listDir",
                     data: {dir: dir, sort: "default", showHidden: showHidden ? "true" : "false"},
@@ -2380,6 +2422,16 @@
             }
 
             function confirmSelection(){
+                var typedPath = getTypedPath();
+                if (typedPath != ""){
+                    openTypedPath(typedPath);
+                    return;
+                }
+                confirmListedSelection();
+            }
+
+            //Confirm what is highlighted, or the name typed for it
+            function confirmListedSelection(){
                 var files = [];
                 $("#fileArea").find(".fileObject.selected").each(function(){
                     files.push({
@@ -2415,17 +2467,7 @@
                 //Check for special cases
                 if (files.length == 0 && type == "folder"){
                     //Select the current path as target instead
-                    var currentPathname = currentDir.split("/");
-                    currentPathname.pop();
-                    currentPathname = currentPathname.pop();
-                    if (currentPathname == ""){
-                        currentPathname = currentDir;
-                    }
-
-                    files.push({
-                        filename: currentPathname,
-                        filepath: currentDir
-                    });
+                    files.push(folderResult(currentDir));
                 }else if (files.length == 0 && type == "new"){
                     //Push this new file into the return structure
                     var newFilename = $("#filename").val();
@@ -2470,6 +2512,152 @@
                 }
             }
 
+            //The result entry for a whole folder, "dir" ending with "/"
+            function folderResult(dir){
+                var segments = dir.split("/");
+                segments.pop();
+                return {
+                    filename: segments.pop() || dir,
+                    filepath: dir
+                };
+            }
+
+            /* ------------------------------------------------------------------ */
+            /*  Typed paths                                                        */
+            /* ------------------------------------------------------------------ */
+            /*
+                The name field also takes a location: a full path such as
+                "user:/Document/", a path relative to the folder on screen ("../Photo")
+                or the name of a folder in it. A file picker ("file" and "new") goes
+                to that folder. A folder picker returns it as the result.
+            */
+            var typedPathPending = false;
+            var lastPlainFilename = "";     //Put back in "new" mode after a path replaced it
+
+            function looksLikePath(value){
+                return /[\/\\]/.test(value) || /^[^:]+:$/.test(value) || value == "." || value == "..";
+            }
+
+            //What the name field asks to open as a folder, "" when it is to be
+            //read as the name of a selection
+            function getTypedPath(){
+                var value = $("#filename").val().trim();
+                if (value == ""){
+                    return "";
+                }
+                var listed = getFileObjectByName(value);
+                if (type == "folder"){
+                    //A highlighted folder filled the field itself, and a name that is
+                    //on screen is picked from the listing without asking the server
+                    var highlighted = $("#fileArea").find(".fileObject.selected").length > 0;
+                    return (highlighted || (listed != null && listed.IsDir)) ? "" : value;
+                }
+                if (looksLikePath(value)){
+                    return value;
+                }
+                return (listed != null && listed.IsDir) ? value : "";
+            }
+
+            //Turn typed text into a virtual directory path ending with "/".
+            //Relative text is read from baseDir, a leading "/" from its drive root
+            function resolveTypedPath(value, baseDir){
+                var path = value.replace(/\\/g, "/");
+                var root;
+                var rest;
+                var absolute = path.match(/^([^\/:]+:)(\/.*)?$/);
+                if (absolute != null){
+                    root = absolute[1] + "/";
+                    rest = absolute[2] || "";
+                }else{
+                    var base = baseDir.match(/^([^\/:]+:)\/?(.*)$/);
+                    root = base[1] + "/";
+                    rest = path.charAt(0) == "/" ? path : base[2] + "/" + path;
+                }
+
+                var segments = [];
+                rest.split("/").forEach(function(segment){
+                    if (segment == ".."){
+                        segments.pop();
+                    }else if (segment != "" && segment != "."){
+                        segments.push(segment);
+                    }
+                });
+                return segments.length == 0 ? root : root + segments.join("/") + "/";
+            }
+
+            function openTypedPath(typed){
+                if (typedPathPending){
+                    return;
+                }
+                var fromDir = currentDir;
+                var target = resolveTypedPath(typed, fromDir);
+                typedPathPending = true;
+
+                //Listing it is what tells whether it is a folder this user can open
+                $.ajax({
+                    url: "../../system/file_system/listDir",
+                    data: {dir: target, sort: "default", showHidden: showHidden ? "true" : "false"},
+                    success: function(data){
+                        typedPathPending = false;
+                        if (currentDir != fromDir || $("#filename").val().trim() != typed){
+                            //The user has moved on while this was being asked
+                            return;
+                        }
+                        if (data != null && data.error !== undefined){
+                            //"subfolder/name.ext" still saves into the subfolder
+                            var fullPath = /^[^\/\\:]+:/.test(typed);
+                            var folderOnly = /[\/\\]$/.test(typed);
+                            if (type == "new" && !fullPath && !folderOnly){
+                                confirmListedSelection();
+                            }else{
+                                showPathError(target);
+                            }
+                            return;
+                        }
+                        if (type == "folder"){
+                            deliverSelection([folderResult(target)]);
+                            return;
+                        }
+                        $("#filename").removeClass("invalid").val(type == "new" ? lastPlainFilename : "");
+                        var here = fromDir.replace(/\/?$/, "/");
+                        listDir(target, target != here, data);
+                    },
+                    error: function(){
+                        typedPathPending = false;
+                        showPathError(target);
+                    }
+                });
+            }
+
+            function showPathError(path){
+                $("#pathError").text(applocale.getString("message/cannotopenfolder", "Folder not found or cannot be opened") +
+                    ": " + path.replace(/([^:])\/$/, "$1")).addClass("show");
+                $("#filename").addClass("invalid").focus();
+            }
+
+            function clearPathError(){
+                $("#pathError").removeClass("show");
+                $("#filename").removeClass("invalid");
+            }
+
+            $("#filename").on("focus", function(){
+                var value = $(this).val();
+                if (value.trim() != "" && !looksLikePath(value.trim())){
+                    lastPlainFilename = value;
+                }
+            });
+
+            //What is typed takes over from whatever was highlighted
+            $("#filename").on("input", function(){
+                clearPathError();
+                var selected = $("#fileArea").find(".fileObject.selected");
+                if (selected.length > 0){
+                    selected.removeClass("selected");
+                    setWindowTitleForSelection(0);
+                    clearDetails();
+                }
+            });
+
             /* ------------------------------------------------------------------ */
             /*  Replace prompt (new file mode)                                     */
             /* ------------------------------------------------------------------ */