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.ConsoleConnection;
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 * 
042 * @deprected Because of its dependency on JQuery.
043 */
044@Deprecated
045public class DatatablesProvider extends PageResourceProvider {
046
047    private final StylingInfo stylingInfo;
048
049    /**
050     * Creates a new component with its channel set to the given 
051     * channel.
052     * 
053     * @param componentChannel the channel that the component's 
054     * handlers listen on by default and that 
055     * {@link Manager#fire(Event, Channel...)} sends the event to 
056     */
057    public DatatablesProvider(Channel componentChannel, Map<?, ?> 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 ConsoleReady}, fire the appropriate {@link AddPageResources}.
079     *
080     * @param event the event
081     * @param connection the web console connection
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 onConsoleReady(ConsoleReady event, ConsoleConnection connection)
089            throws TemplateNotFoundException, MalformedTemplateNameException,
090            ParseException, IOException {
091        String minExt = event.renderSupport()
092            .useMinifiedResources() ? ".min" : "";
093        ResourceBundle bundle = resourceBundle(connection.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                .setRequires("jquery")
112                .setProvides(new String[] { "datatables.net" })
113                .setScriptUri(event.renderSupport().pageResource(
114                    baseDir + "/" + styling + "/datatables" + minExt + ".js")))
115            .addScriptResource(new ScriptResource()
116                .setRequires(new String[] { "datatables.net" })
117                .setScriptUri(event.renderSupport().pageResource(
118                    baseDir + "/processing().js")))
119            .addScriptResource(new ScriptResource()
120                .setRequires(new String[] { "datatables.net" })
121                .setScriptSource(script));
122        if ("jqueryui".equals(styling)) {
123            addRequest.addCss(event.renderSupport().pageResource(
124                "jqueryui-overrides-1.0.0.css"));
125        }
126        connection.respond(addRequest);
127    }
128
129}