From b618361bf1c7ea160f3e0ef18aa8f409f7a21eff Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Fri, 10 Oct 2008 21:52:22 +0000 Subject: [PATCH] * luci/libs/cbi: implement file browse fields * luci/modules/admin-full: add file browse controller --- libs/cbi/htdocs/luci-static/resources/cbi.js | 10 ++ libs/cbi/luasrc/cbi.lua | 14 ++ libs/cbi/luasrc/view/cbi/browser.htm | 23 ++++ libs/cbi/luasrc/view/cbi/filebrowser.htm | 122 ++++++++++++++++++ .../luasrc/controller/admin/filebrowser.lua | 20 +++ 5 files changed, 189 insertions(+) create mode 100644 libs/cbi/luasrc/view/cbi/browser.htm create mode 100644 libs/cbi/luasrc/view/cbi/filebrowser.htm create mode 100644 modules/admin-full/luasrc/controller/admin/filebrowser.lua diff --git a/libs/cbi/htdocs/luci-static/resources/cbi.js b/libs/cbi/htdocs/luci-static/resources/cbi.js index d7bc74bc5..4b1849bea 100644 --- a/libs/cbi/htdocs/luci-static/resources/cbi.js +++ b/libs/cbi/htdocs/luci-static/resources/cbi.js @@ -167,3 +167,13 @@ function cbi_combobox_init(id, values, def, man) { }); cbi_combobox(id, values, def, man); } + +function cbi_filebrowser(id, url, defpath) { + var field = document.getElementById(id); + var browser = window.open( + url + ( field.value || defpath || '' ) + '?field=' + id, + "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes" + ); + + browser.focus(); +} diff --git a/libs/cbi/luasrc/cbi.lua b/libs/cbi/luasrc/cbi.lua index 176fa453a..eed2b64d3 100644 --- a/libs/cbi/luasrc/cbi.lua +++ b/libs/cbi/luasrc/cbi.lua @@ -1395,6 +1395,12 @@ function FileUpload.__init__(self, ...) end end +function FileUpload.formcreated(self, section) + return AbstractValue.formcreated(self, section) or + luci.http.formvalue("cbi.rlf."..section.."."..self.option) or + luci.http.formvalue("cbi.rlf."..section.."."..self.option..".x") +end + function FileUpload.cfgvalue(self, section) local val = AbstractValue.cfgvalue(self, section) if val and luci.fs.access(val) then @@ -1422,3 +1428,11 @@ function FileUpload.remove(self, section) if val and luci.fs.access(val) then luci.fs.unlink(val) end return AbstractValue.remove(self, section) end + + +FileBrowser = class(AbstractValue) + +function FileBrowser.__init__(self, ...) + AbstractValue.__init__(self, ...) + self.template = "cbi/browser" +end diff --git a/libs/cbi/luasrc/view/cbi/browser.htm b/libs/cbi/luasrc/view/cbi/browser.htm new file mode 100644 index 000000000..1982fcb05 --- /dev/null +++ b/libs/cbi/luasrc/view/cbi/browser.htm @@ -0,0 +1,23 @@ +<%# +LuCI - Lua Configuration Interface +Copyright 2008 Steven Barth +Copyright 2008 Jo-Philipp Wich + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +$Id$ + +-%> + +<% + local t = require("luci.tools.webadmin") + local v = self:cfgvalue(section) +-%> +<%+cbi/valueheader%> + /> + '<%=self.default_path and ", '"..self.default_path.."'"%>);return false" alt="<%:cbi_browser%>" title="<%:cbi_browser%>" src="<%=resource%>/cbi/folder.png" style="vertical-align:bottom" /> +<%+cbi/valuefooter%> diff --git a/libs/cbi/luasrc/view/cbi/filebrowser.htm b/libs/cbi/luasrc/view/cbi/filebrowser.htm new file mode 100644 index 000000000..0e223a933 --- /dev/null +++ b/libs/cbi/luasrc/view/cbi/filebrowser.htm @@ -0,0 +1,122 @@ +<%# +LuCI - Lua Configuration Interface +Copyright 2008 Steven Barth +Copyright 2008 Jo-Philipp Wich + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +$Id$ + +-%> + + + + + + Filebrowser - LuCI + + + + + + <% + require("luci.fs") + require("luci.http") + require("luci.dispatcher") + + local field = luci.http.formvalue('field') + local request = luci.dispatcher.context.path + local path = { '' } + + for i = 3, #request do + if request[i] ~= '..' and #request[i] > 0 then + path[#path+1] = request[i] + end + end + + local filepath = table.concat( path, '/' ) + local filestat = luci.fs.stat( filepath ) + local baseurl = luci.dispatcher.build_url('admin', 'filebrowser') + + if filestat and filestat.type == "regular" then + table.remove( path, #path ) + filepath = table.concat( path, '/' ) .. '/' + elseif not ( filestat and filestat.type == "directory" ) then + path = { '' } + filepath = '/' + else + filepath = filepath .. '/' + end + + local entries = luci.fs.dir(filepath) + -%> +
+ Location: + <% for i, dir in ipairs(path) do %> + <% if i == 1 then %> + (root) + <% elseif next(path, i) then %> + <% baseurl = baseurl .. '/' .. dir %> + / <%=dir%> + <% else %> + <% baseurl = baseurl .. '/' .. dir %> + / <%=dir%> + <% end %> + <% end %> +
+ +
+ +
+
    + <% for _, e in luci.util.vspairs(entries) do + local stat = luci.fs.stat(filepath..e) + if e ~= '.' and e ~= '..' and stat and stat.type == 'directory' then + -%> +
  • + Directory + <%=e%>/ +
  • + <% end end -%> + + <% for _, e in luci.util.vspairs(entries) do + local stat = luci.fs.stat(filepath..e) + if stat and stat.type ~= 'directory' then + -%> +
  • + File + <%=e%> +
  • + <% end end -%> +
+
+ + diff --git a/modules/admin-full/luasrc/controller/admin/filebrowser.lua b/modules/admin-full/luasrc/controller/admin/filebrowser.lua new file mode 100644 index 000000000..d83139fd7 --- /dev/null +++ b/modules/admin-full/luasrc/controller/admin/filebrowser.lua @@ -0,0 +1,20 @@ +--[[ +LuCI - Lua Configuration Interface + +Copyright 2008 Steven Barth +Copyright 2008 Jo-Philipp Wich + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +$Id$ +]]-- + +module("luci.controller.admin.filebrowser", package.seeall) + +function index() + entry( {"admin", "filebrowser"}, template("cbi/filebrowser") ).leaf = true +end -- 2.25.1