diff --git a/frontend/ui/widget/filechooser.lua b/frontend/ui/widget/filechooser.lua index fc17fd1fd..d52f0c686 100644 --- a/frontend/ui/widget/filechooser.lua +++ b/frontend/ui/widget/filechooser.lua @@ -11,11 +11,16 @@ FileChooser = Menu:extend{ } function FileChooser:init() - self:updateItemTableFromPath(self.path) + self.item_table = self:genItemTableFromPath(self.path) Menu.init(self) -- call parent's init() end function FileChooser:compressPath(item_path) + if (item_path:sub(1, 1) == ".") then + -- ignore relative path + return item_path + end + -- compress paths like "test/pdf/../epub" into "test/epub" local path = item_path while path:match("/[^/]+[/][\\.][\\.]") do @@ -24,11 +29,9 @@ function FileChooser:compressPath(item_path) return path end -function FileChooser:updateItemTableFromPath(path) - path = self:compressPath(path) +function FileChooser:genItemTableFromPath(path) local dirs = {} local files = {} - self.path = path for f in lfs.dir(self.path) do if self.show_hidden or not string.match(f, "^%.[^.]") then @@ -46,21 +49,24 @@ function FileChooser:updateItemTableFromPath(path) end end table.sort(dirs) - if self.path ~= "/" then table.insert(dirs, 1, "..") end + if path ~= "/" then table.insert(dirs, 1, "..") end table.sort(files) - self.item_table = {} + local item_table = {} for _, dir in ipairs(dirs) do - table.insert(self.item_table, { text = dir.."/", path = self.path.."/"..dir }) + table.insert(item_table, { text = dir.."/", path = self.path.."/"..dir }) end for _, file in ipairs(files) do - table.insert(self.item_table, { text = file, path = self.path.."/"..file }) + table.insert(item_table, { text = file, path = self.path.."/"..file }) end + + return item_table end function FileChooser:changeToPath(path) - self:updateItemTableFromPath(path) - self:updateItems(1) + path = self:compressPath(path) + self.path = path + self:swithItemTable(nil, self:genItemTableFromPath(path)) end function FileChooser:onMenuSelect(item) diff --git a/frontend/ui/widget/menu.lua b/frontend/ui/widget/menu.lua index 41649daed..410aaeea8 100644 --- a/frontend/ui/widget/menu.lua +++ b/frontend/ui/widget/menu.lua @@ -477,7 +477,10 @@ function Menu:updateItems(select_number) end function Menu:swithItemTable(new_title, new_item_table) - self.menu_title.text = new_title + if self.menu_title then + self.menu_title.text = new_title + end + self.page = 1 self.item_table = new_item_table self:updateItems(1) end