001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2026 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.webconsole.provider.rcwebcomponents;
020
021import freemarker.core.ParseException;
022import freemarker.template.MalformedTemplateNameException;
023import freemarker.template.TemplateNotFoundException;
024import java.io.IOException;
025import java.util.Collections;
026import java.util.Map;
027import org.jgrapes.core.Channel;
028import org.jgrapes.core.Event;
029import org.jgrapes.core.Manager;
030import org.jgrapes.core.annotation.Handler;
031import org.jgrapes.webconsole.base.ConsoleConnection;
032import org.jgrapes.webconsole.base.PageResourceProvider;
033import org.jgrapes.webconsole.base.events.AddPageResources;
034import org.jgrapes.webconsole.base.events.AddPageResources.ScriptResource;
035import org.jgrapes.webconsole.base.events.ConsoleReady;
036
037/**
038 * Provider for the
039 * [rc-webcomoponents](https://github.com/richardcarls/rc-webcomponents)
040 * library.
041 */
042public class RcWebcomponentsProvider extends PageResourceProvider {
043
044    /**
045     * Creates a new component with its channel set to the given 
046     * channel.
047     * 
048     * @param componentChannel the channel that the component's 
049     * handlers listen on by default and that 
050     * {@link Manager#fire(Event, Channel...)} sends the event to 
051     */
052    public RcWebcomponentsProvider(Channel componentChannel) {
053        this(componentChannel, Collections.emptyMap());
054    }
055
056    /**
057     * Creates a new component with its channel set to the given 
058     * channel.
059     * 
060     * @param componentChannel the channel that the component's 
061     * handlers listen on by default and that 
062     * {@link Manager#fire(Event, Channel...)} sends the event to 
063     * @param properties the properties used to configure the component
064     */
065    public RcWebcomponentsProvider(Channel componentChannel,
066            Map<?, ?> properties) {
067        super(componentChannel);
068    }
069
070    /**
071     * On {@link ConsoleReady}, fire the appropriate {@link AddPageResources}.
072     *
073     * @param event the event
074     * @param connection the web console connection
075     * @throws TemplateNotFoundException the template not found exception
076     * @throws MalformedTemplateNameException the malformed template name exception
077     * @throws ParseException the parse exception
078     * @throws IOException Signals that an I/O exception has occurred.
079     */
080    @Handler(priority = 100)
081    public void onConsoleReady(ConsoleReady event, ConsoleConnection connection)
082            throws TemplateNotFoundException, MalformedTemplateNameException,
083            ParseException, IOException {
084        connection.respond(new AddPageResources()
085            .addScriptResource(new ScriptResource().setRequires("lit")
086                .setScriptType("module").setProvides("rc-webcomponents")
087                .setScriptSource("import '" + event.renderSupport()
088                    .pageResource(
089                        "rc-webcomponents/@rcarls/rc-webcomponents/define.js")
090                    .getPath() + "';\n")));
091    }
092}