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