001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2017-2018 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.gridstack;
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 [Gridstack.js](http://gridstackjs.com/) library.
039 */
040public class GridstackProvider extends PageResourceProvider {
041
042    /**
043     * The Configuration.
044     * 
045     * @deprecated No longer needed because gridstack provides its own
046     * touch library now.
047     */
048    @SuppressWarnings("PMD.FieldNamingConventions")
049    @Deprecated
050    public enum Configuration {
051        CoreOnly, CoreWithJQUiPlugin, All
052    }
053
054    /**
055     * Creates a new component with its channel set to the given 
056     * channel.
057     *
058     * @param componentChannel the channel that the component's
059     * handlers listen on by default and that 
060     * {@link Manager#fire(Event, Channel...)} sends the event to
061     */
062    public GridstackProvider(Channel componentChannel) {
063        this(componentChannel, Collections.emptyMap());
064    }
065
066    /**
067     * Creates a new component with its channel set to the given 
068     * channel.
069     *
070     * @param componentChannel the channel that the component's
071     * handlers listen on by default and that 
072     * {@link Manager#fire(Event, Channel...)} sends the event to
073     * @param properties the properties used to configure the component
074     */
075    public GridstackProvider(Channel componentChannel, Map<?, ?> properties) {
076        super(componentChannel);
077    }
078
079    /**
080     * On {@link ConsoleReady}, fire the appropriate {@link AddPageResources}.
081     *
082     * @param event the event
083     * @param connection the web console connection
084     * @throws TemplateNotFoundException the template not found exception
085     * @throws MalformedTemplateNameException the malformed template name exception
086     * @throws ParseException the parse exception
087     * @throws IOException Signals that an I/O exception has occurred.
088     */
089    @Handler(priority = 100)
090    @SuppressWarnings("PMD.UnnecessaryVarargsArrayCreation")
091    public void onConsoleReady(ConsoleReady event, ConsoleConnection connection)
092            throws TemplateNotFoundException, MalformedTemplateNameException,
093            ParseException, IOException {
094        String minExt = event.renderSupport()
095            .useMinifiedResources() ? ".min" : "";
096        AddPageResources addRequest = new AddPageResources()
097            .addCss(event.renderSupport().pageResource(
098                "gridstack/gridstack" + minExt + ".css"));
099        connection.respond(new AddPageResources()
100            .addScriptResource(new ScriptResource()
101                .setProvides(new String[] { "gridstack" })));
102        connection.respond(addRequest);
103    }
104}