applications: add freifunk widgets app to customize the index page
[project/luci.git] / applications / luci-freifunk-widgets / luasrc / model / cbi / freifunk / widgets / widgets_overview.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2012 Manuel Munz <freifunk at somakoma dot de>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11 ]]--
12
13 local uci = require "luci.model.uci".cursor()
14 local fs = require "luci.fs"
15 local utl = require "luci.util"
16 m = Map("freifunk-widgets", translate("Widgets"),
17 translate("Configure installed widgets."))
18
19 wdg = m:section(TypedSection, "widget", translate("Widgets"))
20 wdg.addremove = true
21 wdg.extedit = luci.dispatcher.build_url("admin/freifunk/widgets/widget/%s")
22 wdg.template = "cbi/tblsection"
23 wdg.sortable = true
24
25 --[[
26 function wdg.create(...)
27 local sid = TypedSection.create(...)
28 luci.http.redirect(wdg.extedit % sid)
29 end
30 ]]--
31
32 local en = wdg:option(Flag, "enabled", translate("Enable"))
33 en.rmempty = false
34 --en.default = "0"
35 function en.cfgvalue(self, section)
36 return Flag.cfgvalue(self, section) or "0"
37 end
38
39 local tmpl = wdg:option(ListValue, "template", translate("Template"))
40 for k, v in ipairs(fs.dir('/usr/lib/lua/luci/view/freifunk/widgets/')) do
41 if v ~= "." and v ~= ".." then
42 tmpl:value(v)
43 end
44 end
45
46 local title = wdg:option(Value, "title", translate("Title"))
47 --title.rmempty = true
48
49
50 local order = wdg:option(Value, "order", translate("Order"))
51 order.default = "100"
52 order.placeholder = "100"
53 order.datatype = "integer"
54
55 local width = wdg:option(Value, "width", translate("Width"))
56 --width.rmempty = true
57
58 local height = wdg:option(Value, "height", translate("Height"))
59 --height.rmempty = true
60
61 local pr = wdg:option(Value, "paddingright", translate("Padding right"))
62 pr.rmempty = true
63
64 function m.on_commit(self)
65 -- clean custom text files for elements which may have been deleted
66 local dir = "/usr/share/customtext/"
67 local active = {}
68 uci:foreach("freifunk-widgets", "widget", function(s)
69 if s["template"] == "html" then
70 table.insert(active, s[".name"])
71 end
72 end )
73 for k, v in ipairs(fs.dir(dir)) do
74 filename = string.gsub(v, ".html", "")
75 if not utl.contains(active, filename) then
76 fs.unlink(dir .. v)
77 end
78 end
79 end
80
81 return m
82