composer update
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
<?php
|
||||
|
||||
namespace Khill\Lavacharts\Javascript;
|
||||
|
||||
use \Khill\Lavacharts\Charts\Chart;
|
||||
|
||||
/**
|
||||
* ChartFactory Class
|
||||
*
|
||||
* This class takes Charts and uses all of the info to build the complete
|
||||
* javascript blocks for outputting into the page.
|
||||
*
|
||||
* @category Class
|
||||
* @package Khill\Lavacharts\Javascript
|
||||
* @since 3.0.0
|
||||
* @author Kevin Hill <kevinkhill@gmail.com>
|
||||
* @copyright (c) 2017, KHill Designs
|
||||
* @link http://github.com/kevinkhill/lavacharts GitHub Repository Page
|
||||
* @link http://lavacharts.com Official Docs Site
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
class ChartJsFactory extends JavascriptFactory
|
||||
{
|
||||
/**
|
||||
* Location of the output template.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const OUTPUT_TEMPLATE = 'chart.tmpl.js';
|
||||
|
||||
/**
|
||||
* Chart to create javascript from.
|
||||
*
|
||||
* @var \Khill\Lavacharts\Charts\Chart
|
||||
*/
|
||||
protected $chart;
|
||||
|
||||
/**
|
||||
* Event format template
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $eventTemplate;
|
||||
|
||||
/**
|
||||
* Format format template
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $formatTemplate;
|
||||
|
||||
/**
|
||||
* Creates a new ChartJsFactory with the javascript template.
|
||||
*
|
||||
* @param \Khill\Lavacharts\Charts\Chart $chart Chart to process
|
||||
*/
|
||||
public function __construct(Chart $chart)
|
||||
{
|
||||
$this->chart = $chart;
|
||||
|
||||
/**
|
||||
* In the scope of the events and formats, "this" is a reference to the lavachart class in question.
|
||||
*/
|
||||
$this->eventTemplate =
|
||||
'google.visualization.events.addListener(this.chart, "%s", function (event) {'.PHP_EOL.
|
||||
'return lava.event(event, this, %s);'.PHP_EOL.
|
||||
'}.bind(this));'.PHP_EOL;
|
||||
|
||||
$this->formatTemplate =
|
||||
'this.formats["col%1$s"] = new %2$s(%3$s);'.PHP_EOL.
|
||||
'this.formats["col%1$s"].format(this.data, %1$s);'.PHP_EOL;
|
||||
|
||||
$this->templateVars = $this->getTemplateVars();
|
||||
|
||||
parent::__construct(self::OUTPUT_TEMPLATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the template variables from the chart.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @access protected
|
||||
* @return array
|
||||
*/
|
||||
protected function getTemplateVars()
|
||||
{
|
||||
$vars = [
|
||||
'chartLabel' => $this->chart->getLabelStr(),
|
||||
'chartType' => $this->chart->getType(),
|
||||
'chartVer' => $this->chart->getVersion(),
|
||||
'chartClass' => $this->chart->getJsClass(),
|
||||
'chartPackage' => $this->chart->getJsPackage(),
|
||||
'chartData' => $this->chart->getDataTableJson(),
|
||||
'elemId' => $this->chart->getElementIdStr(),
|
||||
//'chartOptions' => $this->chart->toJson(),
|
||||
'pngOutput' => false,
|
||||
'formats' => '',
|
||||
'events' => ''
|
||||
];
|
||||
|
||||
if (method_exists($this->chart, 'getPngOutput')) {
|
||||
$vars['pngOutput'] = $this->chart->getPngOutput();
|
||||
}
|
||||
|
||||
if (method_exists($this->chart, 'getMaterialOutput') && $this->chart->getMaterialOutput() === true) {
|
||||
$vars['chartOptions'] = sprintf(
|
||||
$this->chart->getJsClass() . '.convertOptions(%s)',
|
||||
$this->chart->toJson()
|
||||
);
|
||||
} else {
|
||||
$vars['chartOptions'] = $this->chart->toJson();
|
||||
}
|
||||
|
||||
if ($this->chart->getDataTable()->hasFormattedColumns()) {
|
||||
$vars['formats'] = $this->buildFormatters();
|
||||
}
|
||||
|
||||
if ($this->chart->hasEvents()) {
|
||||
$vars['events'] = $this->buildEventCallbacks();
|
||||
}
|
||||
|
||||
return $vars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the javascript object of event callbacks.
|
||||
*
|
||||
* @access protected
|
||||
* @return string Javascript code block.
|
||||
*/
|
||||
protected function buildEventCallbacks()
|
||||
{
|
||||
$buffer = '';
|
||||
$events = $this->chart->getEvents();
|
||||
|
||||
foreach ($events as $event => $callback) {
|
||||
$buffer .= sprintf(
|
||||
$this->eventTemplate,
|
||||
$event,
|
||||
$callback
|
||||
).PHP_EOL.PHP_EOL;
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the javascript for the datatable column formatters.
|
||||
*
|
||||
* @access protected
|
||||
* @return string Javascript code block.
|
||||
*/
|
||||
protected function buildFormatters()
|
||||
{
|
||||
$buffer = '';
|
||||
$columns = $this->chart->getDataTable()->getFormattedColumns();
|
||||
|
||||
/**
|
||||
* @var int|string $index
|
||||
* @var \Khill\Lavacharts\DataTables\Columns\Column $column
|
||||
*/
|
||||
foreach ($columns as $index => $column) {
|
||||
$format = $column->getFormat();
|
||||
|
||||
$buffer .= sprintf(
|
||||
$this->formatTemplate,
|
||||
$index,
|
||||
$format->getJsClass(),
|
||||
$format->toJson()
|
||||
).PHP_EOL;
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
namespace Khill\Lavacharts\Javascript;
|
||||
|
||||
use Khill\Lavacharts\Lavacharts;
|
||||
use Khill\Lavacharts\Dashboards\Dashboard;
|
||||
use Khill\Lavacharts\Values\ElementId;
|
||||
|
||||
/**
|
||||
* DashboardFactory Class
|
||||
*
|
||||
* This class takes Chart and Control Wrappers and uses all of the info to build the complete
|
||||
* javascript blocks for outputting into the page.
|
||||
*
|
||||
* @category Class
|
||||
* @package Khill\Lavacharts\Javascript
|
||||
* @since 3.0.0
|
||||
* @author Kevin Hill <kevinkhill@gmail.com>
|
||||
* @copyright (c) 2017, KHill Designs
|
||||
* @link http://github.com/kevinkhill/lavacharts GitHub Repository Page
|
||||
* @link http://lavacharts.com Official Docs Site
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
class DashboardJsFactory extends JavascriptFactory
|
||||
{
|
||||
/**
|
||||
* Location of the output template.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const OUTPUT_TEMPLATE = 'dashboard.tmpl.js';
|
||||
|
||||
/**
|
||||
* Dashboard to generate javascript from.
|
||||
*
|
||||
* @var \Khill\Lavacharts\Dashboards\Dashboard
|
||||
*/
|
||||
private $dashboard;
|
||||
|
||||
/**
|
||||
* Creates a new DashboardFactory with the javascript template.
|
||||
*
|
||||
* @param \Khill\Lavacharts\Dashboards\Dashboard $dashboard
|
||||
*/
|
||||
public function __construct(Dashboard $dashboard)
|
||||
{
|
||||
$this->dashboard = $dashboard;
|
||||
|
||||
parent::__construct(self::OUTPUT_TEMPLATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the template variables from the chart.
|
||||
*
|
||||
* @since 3.1.0
|
||||
* @access protected
|
||||
* @return array
|
||||
*/
|
||||
protected function getTemplateVars()
|
||||
{
|
||||
$vars = [
|
||||
'elemId' => $this->dashboard->getElementIdStr(),
|
||||
'label' => $this->dashboard->getLabelStr(),
|
||||
'version' => Dashboard::VERSION,
|
||||
'class' => $this->dashboard->getJsClass(),
|
||||
'packages' => [
|
||||
$this->dashboard->getJsPackage()
|
||||
],
|
||||
'bindings' => $this->processBindings(),
|
||||
'chartData' =>$this->dashboard->getDataTableJson()
|
||||
];
|
||||
|
||||
/** @var \Khill\Lavacharts\Charts\Chart $chart */
|
||||
foreach ($this->dashboard->getBoundCharts() as $chart) {
|
||||
|
||||
array_push($vars['packages'], $chart->getJsPackage());
|
||||
}
|
||||
|
||||
$vars['packages'] = json_encode(array_unique($vars['packages']));
|
||||
|
||||
return $vars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process all the bindings for a Dashboard.
|
||||
*
|
||||
* Turns the chart and control wrappers into new Google Visualization Objects.
|
||||
*
|
||||
* @access private
|
||||
* @return string
|
||||
*/
|
||||
private function processBindings()
|
||||
{
|
||||
$buffer = '';
|
||||
$bindings = $this->dashboard->getBindings();
|
||||
|
||||
/** @var \Khill\Lavacharts\Dashboards\Bindings\Binding $binding */
|
||||
foreach ($bindings as $binding) {
|
||||
switch ($binding::TYPE) {
|
||||
case 'OneToOne':
|
||||
$controls = $binding->getControlWrappers()[0]->getJsConstructor();
|
||||
$charts = $binding->getChartWrappers()[0]->getJsConstructor();
|
||||
break;
|
||||
|
||||
case 'OneToMany':
|
||||
$controls = $binding->getControlWrappers()[0]->getJsConstructor();
|
||||
$charts = $this->mapWrapperArray($binding->getChartWrappers());
|
||||
break;
|
||||
|
||||
case 'ManyToOne':
|
||||
$controls = $this->mapWrapperArray($binding->getControlWrappers());
|
||||
$charts = $binding->getChartWrappers()[0]->getJsConstructor();
|
||||
break;
|
||||
|
||||
case 'ManyToMany':
|
||||
$controls = $this->mapWrapperArray($binding->getControlWrappers());
|
||||
$charts = $this->mapWrapperArray($binding->getChartWrappers());
|
||||
break;
|
||||
}
|
||||
|
||||
$buffer .= sprintf('this.dashboard.bind(%s, %s);', $controls, $charts);
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Map the wrapper values from the array to javascript notation.
|
||||
*
|
||||
* @access private
|
||||
* @param array $wrapperArray Array of control or chart wrappers
|
||||
* @return string Json notation for the wrappers
|
||||
*/
|
||||
private function mapWrapperArray($wrapperArray)
|
||||
{
|
||||
$wrappers = array_map(function ($wrapperArray) {
|
||||
return $wrapperArray->getJsConstructor();
|
||||
}, $wrapperArray);
|
||||
|
||||
return '[' . implode(', ', $wrappers) . ']';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace Khill\Lavacharts\Javascript;
|
||||
|
||||
use Khill\Lavacharts\Support\Buffer;
|
||||
|
||||
/**
|
||||
* JavascriptFactory Class
|
||||
*
|
||||
* This class takes charts and uses all the info to build the complete
|
||||
* javascript blocks for outputting into the page.
|
||||
*
|
||||
* @category Class
|
||||
* @package Khill\Lavacharts\Javascript
|
||||
* @since 2.0.0
|
||||
* @author Kevin Hill <kevinkhill@gmail.com>
|
||||
* @copyright (c) 2017, KHill Designs
|
||||
* @link http://github.com/kevinkhill/lavacharts GitHub Repository Page
|
||||
* @link http://lavacharts.com Official Docs Site
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
class JavascriptFactory
|
||||
{
|
||||
/**
|
||||
* Javascript output buffer.
|
||||
*
|
||||
* @var \Khill\Lavacharts\Support\Buffer
|
||||
*/
|
||||
protected $buffer;
|
||||
|
||||
/**
|
||||
* Javascript template for output.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $template;
|
||||
|
||||
/**
|
||||
* Array of variables and values to apply to the javascript template.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $templateVars;
|
||||
|
||||
/**
|
||||
* JavascriptFactory constructor.
|
||||
*
|
||||
* @param string $outputTemplate
|
||||
*/
|
||||
public function __construct($outputTemplate)
|
||||
{
|
||||
$this->template = file_get_contents(
|
||||
realpath(__DIR__ . '/../../javascript/templates/' . $outputTemplate)
|
||||
);
|
||||
|
||||
$this->buffer = new Buffer($this->template);
|
||||
|
||||
/** Replacing the template variables with values */
|
||||
foreach ($this->getTemplateVars() as $var => $value) {
|
||||
$this->buffer->replace('<'.$var.'>', $value);
|
||||
}
|
||||
|
||||
/** Converting string dates to date constructors */
|
||||
$this->buffer->pregReplace('/"Date\(((:?[0-9]+,?)+)\)"/', 'new Date(\1)');
|
||||
|
||||
/** Converting string nulls to actual nulls */
|
||||
$this->buffer->pregReplace('/"null"/', 'null');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the output buffer for the javascript.
|
||||
*
|
||||
* @return \Khill\Lavacharts\Support\Buffer
|
||||
*/
|
||||
public function getOutputBuffer()
|
||||
{
|
||||
return $this->buffer;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
namespace Khill\Lavacharts\Javascript;
|
||||
|
||||
use Khill\Lavacharts\Charts\Chart;
|
||||
use Khill\Lavacharts\Dashboards\Dashboard;
|
||||
use Khill\Lavacharts\Exceptions\ElementIdException;
|
||||
use Khill\Lavacharts\Support\Buffer;
|
||||
use Khill\Lavacharts\Support\Options;
|
||||
use Khill\Lavacharts\Support\Traits\HasOptionsTrait as HasOptions;
|
||||
use Khill\Lavacharts\Support\Contracts\RenderableInterface as Renderable;
|
||||
|
||||
/**
|
||||
* ScriptManager Class
|
||||
*
|
||||
* This class takes charts and uses all the info to build the complete
|
||||
* javascript blocks for outputting into the page. Also will output the lava.js module
|
||||
* and track if it is in page or not.
|
||||
*
|
||||
* @category Class
|
||||
* @package Khill\Lavacharts\Javascript
|
||||
* @since 3.0.5
|
||||
* @author Kevin Hill <kevinkhill@gmail.com>
|
||||
* @copyright (c) 2017, KHill Designs
|
||||
* @link http://github.com/kevinkhill/lavacharts GitHub Repository Page
|
||||
* @link http://lavacharts.com Official Docs Site
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
class ScriptManager
|
||||
{
|
||||
use HasOptions;
|
||||
|
||||
/**
|
||||
* Lava.js module location.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const LAVA_JS = '/../../javascript/dist/lava.js';
|
||||
|
||||
/**
|
||||
* Opening javascript tag.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const JS_OPEN = '<script type="text/javascript">';
|
||||
|
||||
/**
|
||||
* Closing javascript tag.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const JS_CLOSE = '</script>';
|
||||
|
||||
/**
|
||||
* Tracks if the lava.js module and jsapi have been rendered.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $lavaJsRendered = false;
|
||||
|
||||
/**
|
||||
* ScriptManager constructor.
|
||||
*
|
||||
* @param Options $options
|
||||
*/
|
||||
function __construct(Options $options)
|
||||
{
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calling this method before rendering will override the output of the lava.js
|
||||
* module into the page. The 'auto_run' option is also set to false, since we
|
||||
* are now relying on the user to load the lava.js module manually.
|
||||
*/
|
||||
public function bypassLavaJsOutput()
|
||||
{
|
||||
$this->lavaJsRendered = true;
|
||||
|
||||
$this->options->set('auto_run', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true|false depending on if the lava.js module
|
||||
* has be output to the page
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function lavaJsRendered()
|
||||
{
|
||||
return $this->lavaJsRendered;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the lava.js module.
|
||||
*
|
||||
* @param array $config
|
||||
* @return \Khill\Lavacharts\Support\Buffer
|
||||
*/
|
||||
public function getLavaJsModule(array $config = [])
|
||||
{
|
||||
$lavaJs = realpath(__DIR__ . self::LAVA_JS);
|
||||
$buffer = new Buffer(file_get_contents($lavaJs));
|
||||
|
||||
$this->options->merge($config);
|
||||
|
||||
$buffer->pregReplace('/OPTIONS_JSON/', $this->options->toJson());
|
||||
|
||||
$this->lavaJsRendered = true;
|
||||
|
||||
return $this->scriptTagWrap($buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a buffer with the javascript of a renderable resource.
|
||||
*
|
||||
*
|
||||
* @param \Khill\Lavacharts\Support\Contracts\RenderableInterface $renderable
|
||||
* @return \Khill\Lavacharts\Support\Buffer
|
||||
* @throws \Khill\Lavacharts\Exceptions\ElementIdException
|
||||
*/
|
||||
public function getOutputBuffer(Renderable $renderable)
|
||||
{
|
||||
if ($renderable->hasElementId() === false) {
|
||||
throw new ElementIdException($renderable);
|
||||
}
|
||||
|
||||
if ($renderable instanceof Dashboard) {
|
||||
$jsFactory = new DashboardJsFactory($renderable);
|
||||
}
|
||||
|
||||
if ($renderable instanceof Chart) {
|
||||
$jsFactory = new ChartJsFactory($renderable);
|
||||
}
|
||||
|
||||
$buffer = $jsFactory->getOutputBuffer();
|
||||
|
||||
return $this->scriptTagWrap($buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps a buffer with an html script tag
|
||||
*
|
||||
* @param \Khill\Lavacharts\Support\Buffer $buffer
|
||||
* @return \Khill\Lavacharts\Support\Buffer
|
||||
*/
|
||||
private function scriptTagWrap(Buffer $buffer)
|
||||
{
|
||||
return $buffer->prepend(PHP_EOL)
|
||||
->prepend(self::JS_OPEN)
|
||||
->prepend(PHP_EOL)
|
||||
->append(PHP_EOL)
|
||||
->append(self::JS_CLOSE);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user