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.chartjs;
020
021import freemarker.core.ParseException;
022import freemarker.template.MalformedTemplateNameException;
023import freemarker.template.TemplateNotFoundException;
024
025import java.io.IOException;
026
027import org.jgrapes.core.Channel;
028import org.jgrapes.core.Event;
029import org.jgrapes.core.Manager;
030import org.jgrapes.core.annotation.Handler;
031import org.jgrapes.portal.base.PageResourceProvider;
032import org.jgrapes.portal.base.PortalSession;
033import org.jgrapes.portal.base.events.AddPageResources;
034import org.jgrapes.portal.base.events.AddPageResources.ScriptResource;
035import org.jgrapes.portal.base.events.PortalReady;
036
037/**
038 * Provider for the [Chart.js](http://www.chartjs.org/) library.
039 */
040public class ChartJsProvider extends PageResourceProvider {
041
042    /**
043     * Creates a new component with its channel set to the given 
044     * channel.
045     * 
046     * @param componentChannel the channel that the component's 
047     * handlers listen on by default and that 
048     * {@link Manager#fire(Event, Channel...)} sends the event to 
049     */
050    public ChartJsProvider(Channel componentChannel) {
051        super(componentChannel);
052    }
053
054    /**
055     * On {@link PortalReady}, fire the appropriate {@link AddPageResources}.
056     *
057     * @param event the event
058     * @param portalSession the portal session
059     * @throws TemplateNotFoundException the template not found exception
060     * @throws MalformedTemplateNameException the malformed template name exception
061     * @throws ParseException the parse exception
062     * @throws IOException Signals that an I/O exception has occurred.
063     */
064    @Handler(priority = 100)
065    public void onPortalReady(PortalReady event, PortalSession portalSession)
066            throws TemplateNotFoundException, MalformedTemplateNameException,
067            ParseException, IOException {
068        String minExt = event.renderSupport()
069            .useMinifiedResources() ? ".min" : "";
070        portalSession.respond(new AddPageResources()
071            .addScriptResource(new ScriptResource()
072                .setProvides(new String[] { "chartjs.org" })
073                .setScriptUri(event.renderSupport().pageResource(
074                    "chart-js-2.7.0/Chart" + minExt + ".js"))));
075    }
076}