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.Map;
026import java.util.logging.Logger;
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.ConsoleSession;
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    private static Logger LOG
043        = Logger.getLogger(GridstackProvider.class.getName());
044
045    public enum Configuration {
046        CoreOnly, CoreWithJQUiPlugin, All
047    }
048
049    private Configuration configuration = Configuration.All;
050    private boolean requireTouchPunch;
051
052    /**
053     * Creates a new component with its channel set to the given 
054     * channel.
055     *
056     * Supported properties are:
057     * 
058     *  * *configuration*: the string representation of one of the
059     *    {@link Configuration} values. Determines the gridstack 
060     *    configuration to provide. defaults to "All".
061     *    
062     *  * *requireTouchPunch*: a boolean indicating whether
063     *    "jquery-ui.touch-punch" should be required to ensure that
064     *    it is loaded before gridstack (see {@link AddPageResources}).
065     *    Defaults to `false`. If set to `true`, forces configuration
066     *    `All` to be replaced by `CoreWithJQUiPlugin` because the 
067     *    touch punch provider requires "jquery" and thus the full 
068     *    JQuery library is available anyway.
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,
076            Map<Object, Object> properties) {
077        super(componentChannel);
078        requireTouchPunch
079            = (boolean) properties.getOrDefault("requireTouchPunch", false);
080        if (properties.containsKey("configuration")) {
081            String config = (String) properties.get("configuration");
082            try {
083                configuration = Configuration.valueOf(config);
084            } catch (IllegalArgumentException e) {
085                LOG.severe(
086                    "Illegal value for \"configuration\": " + config);
087            }
088        }
089        if (requireTouchPunch && configuration == Configuration.All) {
090            configuration = Configuration.CoreWithJQUiPlugin;
091        }
092    }
093
094    /**
095     * On {@link ConsoleReady}, fire the appropriate {@link AddPageResources}.
096     *
097     * @param event the event
098     * @param consoleSession the web console session
099     * @throws TemplateNotFoundException the template not found exception
100     * @throws MalformedTemplateNameException the malformed template name exception
101     * @throws ParseException the parse exception
102     * @throws IOException Signals that an I/O exception has occurred.
103     */
104    @Handler(priority = 100)
105    public void onConsoleReady(ConsoleReady event,
106            ConsoleSession consoleSession)
107            throws TemplateNotFoundException, MalformedTemplateNameException,
108            ParseException, IOException {
109        String minExt = event.renderSupport()
110            .useMinifiedResources() ? ".min" : "";
111        AddPageResources addRequest = new AddPageResources()
112            .addCss(event.renderSupport().pageResource(
113                "gridstack/gridstack" + minExt + ".css"));
114        switch (configuration) {
115        case CoreOnly:
116            addRequest
117                .addScriptResource(new ScriptResource()
118                    .setRequires(requireTouchPunch
119                        ? "jquery-ui.touch-punch"
120                        : "jquery")
121                    .setProvides("gridstack")
122                    .setScriptUri(event.renderSupport().pageResource(
123                        "gridstack/gridstack" + minExt + ".js")));
124            break;
125        case CoreWithJQUiPlugin:
126            addRequest.addScriptResource(new ScriptResource()
127                .setRequires(requireTouchPunch
128                    ? "jquery-ui.touch-punch"
129                    : "jquery")
130                .setProvides("gridstack")
131                .setScriptUri(event.renderSupport().pageResource(
132                    "gridstack/gridstack" + minExt + ".js")));
133            addRequest.addScriptResource(new ScriptResource()
134                .setRequires("gridstack")
135                .setProvides("gridstack.jQueryUI")
136                .setScriptUri(event.renderSupport().pageResource(
137                    "gridstack/gridstack.jQueryUI" + minExt + ".js")));
138            break;
139        case All:
140            addRequest.addScriptResource(new ScriptResource()
141                .setRequires("jquery")
142                .setProvides("gridstack", "gridstack.all")
143                .setScriptUri(event.renderSupport().pageResource(
144                    "gridstack/gridstack.all.js")));
145        }
146        consoleSession.respond(addRequest);
147    }
148}