Changeset

4171:85fa8c9e992a

mod_register_apps: Add support for module:provides("site-app", app_info)
author Matthew Wild <mwild1@gmail.com>
date Tue, 06 Oct 2020 16:03:38 +0100
parents 4170:2a655577fbcb
children 4172:740ca44eba06
files mod_register_apps/mod_register_apps.lua
diffstat 1 files changed, 45 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/mod_register_apps/mod_register_apps.lua	Tue Oct 06 16:02:31 2020 +0100
+++ b/mod_register_apps/mod_register_apps.lua	Tue Oct 06 16:03:38 2020 +0100
@@ -113,21 +113,45 @@
 
 local site_apps = module:shared("apps");
 
-for k, v in pairs(site_apps) do
-	if v._source == module.name then
-		site_apps[k] = nil;
+local function add_app(app_info, source)
+	local app_id = app_info.id or app_info.name:gsub("%W+", "-"):lower();
+	if (not show_apps or show_apps:contains(app_id))
+	and not (hide_apps and hide_apps:contains(app_id))
+	and not site_apps[app_id] then
+		app_info.id = app_id;
+		app_info.image = relurl(app_info.image);
+		site_apps[app_id] = app_info;
+		app_info._source = source;
+		table.insert(site_apps, app_info);
 	end
 end
 
-for _, app_info in ipairs(app_config) do
+local function remove_app(app_info)
 	local app_id = app_info.id or app_info.name:gsub("%W+", "-"):lower();
-	if (not show_apps or show_apps:contains(app_id))
-	and not (hide_apps and hide_apps:contains(app_id)) then
-		app_info.id = app_id;
-		app_info.image = relurl(app_info.image);
-		site_apps[app_id] = app_info;
-		app_info._source = module.name;
-		table.insert(site_apps, app_info);
+	site_apps[app_id] = nil;
+end
+
+local function add_config_apps()
+	for _, app_info in ipairs(app_config) do
+		add_app(app_info, module.name);
+	end
+end
+
+local function module_app_added(event)
+	module:log("warn", "ADDING %s", event.item.name)
+	add_app(event.item, module.name);
+end
+
+local function module_app_removed(event)
+	remove_app(event.item);
+end
+
+-- Remove all apps added by this module
+local function remove_all_apps()
+	for k, v in pairs(site_apps) do
+		if v._source == module.name then
+			remove_app(k);
+		end
 	end
 end
 
@@ -144,3 +168,13 @@
 		});
 	};
 });
+
+function module.load()
+	add_config_apps();
+	module:handle_items("site-app-provider", module_app_added, module_app_removed, true);
+end
+
+function module.unload()
+	remove_all_apps();
+end
+