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.chartjs; 020 021import freemarker.core.ParseException; 022import freemarker.template.MalformedTemplateNameException; 023import freemarker.template.TemplateNotFoundException; 024import java.io.IOException; 025import java.util.Collections; 026import java.util.Map; 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.ConsoleConnection; 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 [Chart.js](http://www.chartjs.org/) library. 039 * 040 * The package cannot be used as distributed in node_modules 041 * because of imports that aren't browser compliant. It is made 042 * available for import as page resource "chart.js/auto.js". 043 */ 044public class ChartJsProvider extends PageResourceProvider { 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 ChartJsProvider(Channel componentChannel) { 055 this(componentChannel, Collections.emptyMap()); 056 } 057 058 /** 059 * Creates a new component with its channel set to the given 060 * channel. 061 * 062 * @param componentChannel the channel that the component's 063 * handlers listen on by default and that 064 * {@link Manager#fire(Event, Channel...)} sends the event to 065 * @param properties the properties used to configure the component 066 */ 067 public ChartJsProvider(Channel componentChannel, Map<?, ?> properties) { 068 super(componentChannel); 069 } 070 071 /** 072 * On {@link ConsoleReady}, fire the appropriate {@link AddPageResources}. 073 * 074 * @param event the event 075 * @param connection the web console connection 076 * @throws TemplateNotFoundException the template not found exception 077 * @throws MalformedTemplateNameException the malformed template name exception 078 * @throws ParseException the parse exception 079 * @throws IOException Signals that an I/O exception has occurred. 080 */ 081 @Handler(priority = 100) 082 public void onConsoleReady(ConsoleReady event, ConsoleConnection connection) 083 throws TemplateNotFoundException, MalformedTemplateNameException, 084 ParseException, IOException { 085 var pageResources = new AddPageResources() 086 .addScriptResource(new ScriptResource().setProvides("chart.js")) 087 .addScriptResource(new ScriptResource().setScriptType("module") 088 .setScriptUri(event.renderSupport().pageResource( 089 "chart.js/adapters/chartjs-adapter-simple.js"))); 090 connection.respond(pageResources); 091 } 092}