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.datatables;
020
021import freemarker.core.ParseException;
022import freemarker.template.MalformedTemplateNameException;
023import freemarker.template.TemplateNotFoundException;
024import java.io.IOException;
025import java.util.Locale;
026import java.util.Map;
027import java.util.ResourceBundle;
028import org.jgrapes.core.Channel;
029import org.jgrapes.core.Event;
030import org.jgrapes.core.Manager;
031import org.jgrapes.core.annotation.Handler;
032import org.jgrapes.webconsole.base.ConsoleSession;
033import org.jgrapes.webconsole.base.PageResourceProvider;
034import org.jgrapes.webconsole.base.StylingInfo;
035import org.jgrapes.webconsole.base.events.AddPageResources;
036import org.jgrapes.webconsole.base.events.AddPageResources.ScriptResource;
037import org.jgrapes.webconsole.base.events.ConsoleReady;
038
039/**
040 * Provider for the [Datatables](https://datatables.net/) library.
041 */
042public class DatatablesProvider extends PageResourceProvider {
043
044    private final StylingInfo stylingInfo;
045
046    /**
047     * Creates a new component with its channel set to the given 
048     * channel.
049     * 
050     * @param componentChannel the channel that the component's 
051     * handlers listen on by default and that 
052     * {@link Manager#fire(Event, Channel...)} sends the event to 
053     */
054    public DatatablesProvider(Channel componentChannel,
055            Map<Object, Object> properties) {
056        super(componentChannel);
057        stylingInfo = new StylingInfo(this, properties);
058    }
059
060    /**
061     * Provides a resource bundle for localization.
062     * The default implementation looks up a bundle using the
063     * package name plus "l10n" as base name.
064     * 
065     * @return the resource bundle
066     */
067    protected ResourceBundle resourceBundle(Locale locale) {
068        return ResourceBundle.getBundle(
069            getClass().getPackage().getName() + ".l10n", locale,
070            getClass().getClassLoader(),
071            ResourceBundle.Control.getNoFallbackControl(
072                ResourceBundle.Control.FORMAT_DEFAULT));
073    }
074
075    /**
076     * On {@link ConsoleReady}, fire the appropriate {@link AddPageResources}.
077     *
078     * @param event the event
079     * @param consoleSession the web console session
080     * @throws TemplateNotFoundException the template not found exception
081     * @throws MalformedTemplateNameException the malformed template name exception
082     * @throws ParseException the parse exception
083     * @throws IOException Signals that an I/O exception has occurred.
084     */
085    @Handler(priority = 100)
086    public void onConsoleReady(ConsoleReady event,
087            ConsoleSession consoleSession)
088            throws TemplateNotFoundException, MalformedTemplateNameException,
089            ParseException, IOException {
090        String minExt = event.renderSupport()
091            .useMinifiedResources() ? ".min" : "";
092        ResourceBundle bundle = resourceBundle(consoleSession.locale());
093        String script = "$.fn.dataTable.defaults.oLanguage._hungarianMap"
094            + "[\"lengthAll\"] = \"sLengthAll\";\n"
095            + "$.extend( $.fn.dataTable.defaults.oLanguage, {\n"
096            + " 'sLengthAll': 'all',\n"
097            + "} );\n"
098            + "$.extend( $.fn.dataTable.defaults.oLanguage, "
099            + bundle.getString("DataTablesL10n") + ");\n";
100        String baseDir = "datatables-20180804";
101        String styling = stylingInfo.get();
102        if (!"jqueryui".equals(styling) && !"bootstrap4".equals(styling)) {
103            styling = "standard";
104        }
105        AddPageResources addRequest = new AddPageResources()
106            .addCss(event.renderSupport()
107                .pageResource(baseDir + "/" + styling + "/datatables"
108                    + minExt + ".css"))
109            .addScriptResource(new ScriptResource()
110                .setProvides(new String[] { "datatables.net" })
111                .setScriptUri(event.renderSupport().pageResource(
112                    baseDir + "/" + styling + "/datatables" + minExt + ".js")))
113            .addScriptResource(new ScriptResource()
114                .setRequires(new String[] { "datatables.net" })
115                .setScriptUri(event.renderSupport().pageResource(
116                    baseDir + "/processing().js")))
117            .addScriptResource(new ScriptResource()
118                .setRequires(new String[] { "datatables.net" })
119                .setScriptSource(script));
120        if ("jqueryui".equals(styling)) {
121            addRequest.addCss(event.renderSupport().pageResource(
122                "jqueryui-overrides-1.0.0.css"));
123        }
124        consoleSession.respond(addRequest);
125    }
126
127}