| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
local widgets_by_type = { |
| 7 |
graph = require 'obvious.lib.widget.graph', |
| 8 |
progressbar = require 'obvious.lib.widget.progressbar', |
| 9 |
textbox = require 'obvious.lib.widget.textbox', |
| 10 |
} |
| 11 |
|
| 12 |
local setmetatable = setmetatable |
| 13 |
local getmetatable = getmetatable |
| 14 |
local pairs = pairs |
| 15 |
local type = type |
| 16 |
local lib = { |
| 17 |
hooks = require("obvious.lib.hooks") |
| 18 |
} |
| 19 |
|
| 20 |
local defaults = { } |
| 21 |
|
| 22 |
|
| 23 |
local funcs = { } |
| 24 |
|
| 25 |
funcs.set_type = function (obj, widget_type) |
| 26 |
local widget_type = widgets_by_type[widget_type] |
| 27 |
if not widget_type or not widget_type.create then |
| 28 |
return |
| 29 |
end |
| 30 |
|
| 31 |
local meta = getmetatable(obj) |
| 32 |
|
| 33 |
local widget = widget_type.create(meta.data) |
| 34 |
obj[1] = widget |
| 35 |
obj.update() |
| 36 |
return obj |
| 37 |
end |
| 38 |
|
| 39 |
local function from_data_source(data) |
| 40 |
local ret = { } |
| 41 |
|
| 42 |
for k, v in pairs(funcs) do |
| 43 |
ret[k] = v |
| 44 |
end |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
ret[1] = widgets_by_type.graph.create(data) |
| 49 |
|
| 50 |
ret.update = function() |
| 51 |
|
| 52 |
|
| 53 |
ret[1]:update() |
| 54 |
end |
| 55 |
|
| 56 |
|
| 57 |
lib.hooks.timer.register(10, 60, ret.update) |
| 58 |
lib.hooks.timer.start(ret.update) |
| 59 |
ret.update() |
| 60 |
|
| 61 |
local meta = { } |
| 62 |
|
| 63 |
meta.data = data |
| 64 |
|
| 65 |
|
| 66 |
meta.__index = function (obj, key) |
| 67 |
local ret = obj[1][key] |
| 68 |
if key ~= "layout" and type(ret) == "function" then |
| 69 |
|
| 70 |
return function(_, ...) |
| 71 |
|
| 72 |
return ret(obj[1], ...) |
| 73 |
end |
| 74 |
end |
| 75 |
return ret |
| 76 |
end |
| 77 |
|
| 78 |
setmetatable(ret, meta) |
| 79 |
return ret |
| 80 |
end |
| 81 |
|
| 82 |
return { |
| 83 |
from_data_source = from_data_source, |
| 84 |
} |
| 85 |
|
| 86 |
|