composer update
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -0,0 +1,17 @@
|
||||
module.exports = function (config) {
|
||||
config.set({
|
||||
frameworks: ['jasmine','sinon'],
|
||||
files: [
|
||||
'./node_modules/jasmine-sinon/lib/jasmine-sinon.js',
|
||||
'./dist/lava.js',
|
||||
'./tests/lava.spec.js'
|
||||
],
|
||||
singleRun: false,
|
||||
reporters: ['dots'],
|
||||
port: 9876,
|
||||
colors: true,
|
||||
logLevel: config.LOG_ERROR,
|
||||
autoWatch: true,
|
||||
browsers: [(process.env.TRAVIS ? 'PhantomJS' : 'Chrome')]
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
/* jshint undef: true */
|
||||
/* globals module, require, phantom, window */
|
||||
"use strict";
|
||||
|
||||
var resemble = require('node-resemble-js');
|
||||
|
||||
var rendersDir = './javascript/phantomjs/renders/';
|
||||
var args = process.argv;
|
||||
var chart = args[2];
|
||||
|
||||
resemble(rendersDir + chart + '.png').compareTo(rendersDir + chart + '.png').onComplete(function (data) {
|
||||
//console.log(data);
|
||||
|
||||
if (Number(data.misMatchPercentage) <= 0.01) {
|
||||
console.log('Pass!');
|
||||
} else {
|
||||
console.log('Fail!');
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
/* jshint undef: true */
|
||||
/* globals module, require, phantom, window */
|
||||
"use strict";
|
||||
|
||||
var page = require('webpage').create();
|
||||
var args = require('system').args;
|
||||
|
||||
var renderOutputDir = './javascript/phantomjs/renders';
|
||||
var url = 'http://127.0.0.1:5000/';
|
||||
var chart = args[1];
|
||||
|
||||
page.onConsoleMessage = function(msg) {
|
||||
console.log(msg);
|
||||
};
|
||||
|
||||
page.open(url + chart, function (status) {
|
||||
if (status !== "success") {
|
||||
console.log('Error loading page.');
|
||||
} else {
|
||||
console.log('Page loaded, waiting on chart to render.');
|
||||
|
||||
page.onCallback = function (data) {
|
||||
page.render(renderOutputDir + '/' + chart + '.png');
|
||||
|
||||
console.log('Saved to disk.');
|
||||
|
||||
phantom.exit();
|
||||
|
||||
//console.log('CALLBACK: ' + JSON.stringify(data));
|
||||
// Prints 'CALLBACK: { "hello": "world" }'
|
||||
};
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
/* jshint undef: true, unused: true */
|
||||
/* globals window, require */
|
||||
|
||||
/**
|
||||
* Lava.js entry point for Browserify
|
||||
*/
|
||||
(function(){
|
||||
"use strict";
|
||||
|
||||
var window = this;
|
||||
var debounced = null;
|
||||
var debounceTimeout = 250;
|
||||
var bind = require('lodash').bind;
|
||||
var ready = require('document-ready');
|
||||
var addResizeEvent = require('./lava/Utils').addResizeEvent;
|
||||
|
||||
window.lava = require('./lava/Lava.js');
|
||||
|
||||
/**
|
||||
* Once the window is ready...
|
||||
*/
|
||||
ready(function() {
|
||||
/**
|
||||
* Adding the resize event listener for redrawing charts.
|
||||
*/
|
||||
addResizeEvent(function (event) {
|
||||
var redraw = bind(event.target.lava.redrawCharts, window.lava);
|
||||
|
||||
console.log('Window resized, redrawing charts');
|
||||
|
||||
clearTimeout(debounced);
|
||||
|
||||
debounced = setTimeout(redraw, debounceTimeout);
|
||||
});
|
||||
|
||||
/**
|
||||
* Let's go!
|
||||
*/
|
||||
|
||||
if (window.lava.options.auto_run === true) {
|
||||
window.lava.run();
|
||||
}
|
||||
});
|
||||
}.apply(window));
|
||||
@@ -0,0 +1,162 @@
|
||||
/* jshint undef: true */
|
||||
/* globals document, google, require, module */
|
||||
|
||||
/**
|
||||
* Chart class used for storing all the needed configuration for rendering.
|
||||
*
|
||||
* @typedef {Function} Chart
|
||||
* @property {string} label - Label for the chart.
|
||||
* @property {string} type - Type of chart.
|
||||
* @property {Object} element - Html element in which to render the chart.
|
||||
* @property {Object} chart - Google chart object.
|
||||
* @property {string} package - Type of Google chart package to load.
|
||||
* @property {boolean} pngOutput - Should the chart be displayed as a PNG.
|
||||
* @property {Object} data - Datatable for the chart.
|
||||
* @property {Object} options - Configuration options for the chart.
|
||||
* @property {Array} formats - Formatters to apply to the chart data.
|
||||
* @property {Object} promises - Promises used in the rendering chain.
|
||||
* @property {Function} init - Initializes the chart.
|
||||
* @property {Function} configure - Configures the chart.
|
||||
* @property {Function} render - Renders the chart.
|
||||
* @property {Function} uuid - Creates identification string for the chart.
|
||||
* @property {Object} _errors - Collection of errors to be thrown.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Chart module
|
||||
*
|
||||
* @class Chart
|
||||
* @module lava/Chart
|
||||
* @author Kevin Hill <kevinkhill@gmail.com>
|
||||
* @copyright (c) 2017, KHill Designs
|
||||
* @license MIT
|
||||
*/
|
||||
module.exports = (function() {
|
||||
'use strict';
|
||||
|
||||
var Q = require('q');
|
||||
|
||||
/**
|
||||
* Chart Class
|
||||
*
|
||||
* This is the javascript version of a lavachart with methods for interacting with
|
||||
* the google chart and the PHP lavachart output.
|
||||
*
|
||||
* @param {string} type
|
||||
* @param {string} label
|
||||
* @constructor
|
||||
*/
|
||||
function Chart (type, label) {
|
||||
this.label = label;
|
||||
this.type = type;
|
||||
this.element = null;
|
||||
this.chart = null;
|
||||
this.package = null;
|
||||
this.pngOutput = false;
|
||||
this.data = {};
|
||||
this.options = {};
|
||||
this.formats = [];
|
||||
this.promises = {
|
||||
configure: Q.defer(),
|
||||
rendered: Q.defer()
|
||||
};
|
||||
this.init = function(){};
|
||||
this.configure = function(){};
|
||||
this.render = function(){};
|
||||
this.uuid = function() {
|
||||
return this.type+'::'+this.label;
|
||||
};
|
||||
this._errors = require('./Errors.js');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the data for the chart by creating a new DataTable
|
||||
*
|
||||
* @public
|
||||
* @external "google.visualization.DataTable"
|
||||
* @see {@link https://developers.google.com/chart/interactive/docs/reference#DataTable|DataTable Class}
|
||||
* @param {object} data Json representation of a DataTable
|
||||
* @param {Array.<Array>} data.cols Array of column definitions
|
||||
* @param {Array.<Array>} data.rows Array of row definitions
|
||||
*/
|
||||
Chart.prototype.setData = function (data) {
|
||||
this.data = new google.visualization.DataTable(data);
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets the options for the chart.
|
||||
*
|
||||
* @public
|
||||
* @param {object} options
|
||||
*/
|
||||
Chart.prototype.setOptions = function (options) {
|
||||
this.options = options;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets whether the chart is to be rendered as PNG or SVG
|
||||
*
|
||||
* @public
|
||||
* @param {string|int} png
|
||||
*/
|
||||
Chart.prototype.setPngOutput = function (png) {
|
||||
this.pngOutput = Boolean(typeof png == 'undefined' ? false : png);
|
||||
};
|
||||
|
||||
/**
|
||||
* Set the ID of the output element for the Dashboard.
|
||||
*
|
||||
* @public
|
||||
* @param {string} elemId
|
||||
* @throws ElementIdNotFound
|
||||
*/
|
||||
Chart.prototype.setElement = function (elemId) {
|
||||
this.element = document.getElementById(elemId);
|
||||
|
||||
if (! this.element) {
|
||||
throw new this._errors.ElementIdNotFound(elemId);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Redraws the chart.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
Chart.prototype.redraw = function() {
|
||||
this.chart.draw(this.data, this.options);
|
||||
};
|
||||
|
||||
/**
|
||||
* Draws the chart as a PNG instead of the standard SVG
|
||||
*
|
||||
* @public
|
||||
* @external "chart.getImageURI"
|
||||
* @see {@link https://developers.google.com/chart/interactive/docs/printing|Printing PNG Charts}
|
||||
*/
|
||||
Chart.prototype.drawPng = function() {
|
||||
var img = document.createElement('img');
|
||||
img.src = this.chart.getImageURI();
|
||||
|
||||
this.element.innerHTML = '';
|
||||
this.element.appendChild(img);
|
||||
};
|
||||
|
||||
/**
|
||||
* Formats columns of the DataTable.
|
||||
*
|
||||
* @public
|
||||
* @param {Array.<Object>} formatArr Array of format definitions
|
||||
*/
|
||||
Chart.prototype.applyFormats = function (formatArr) {
|
||||
for(var a=0; a < formatArr.length; a++) {
|
||||
var formatJson = formatArr[a];
|
||||
var formatter = new google.visualization[formatJson.type](formatJson.config);
|
||||
|
||||
formatter.format(this.data, formatJson.index);
|
||||
}
|
||||
};
|
||||
|
||||
return Chart;
|
||||
|
||||
}());
|
||||
@@ -0,0 +1,74 @@
|
||||
/* jshint undef: true */
|
||||
/* globals document, google, require, module */
|
||||
|
||||
/**
|
||||
* Dashboard module
|
||||
*
|
||||
* @class Dashboard
|
||||
* @module lava/Dashboard
|
||||
* @author Kevin Hill <kevinkhill@gmail.com>
|
||||
* @copyright (c) 2017, KHill Designs
|
||||
* @license MIT
|
||||
*/
|
||||
module.exports = (function() {
|
||||
'use strict';
|
||||
|
||||
var Q = require('q');
|
||||
|
||||
/**
|
||||
* Dashboard Class
|
||||
*
|
||||
* This is the javascript version of a dashboard with methods for interacting with
|
||||
* the google chart and the PHP lavachart output.
|
||||
*
|
||||
* @param {String} label
|
||||
* @constructor
|
||||
*/
|
||||
function Dashboard (label) {
|
||||
this.label = label;
|
||||
this.type = 'Dashboard';
|
||||
this.element = null;
|
||||
this.data = null;
|
||||
this.bindings = [];
|
||||
this.dashboard = null;
|
||||
this.deferred = Q.defer();
|
||||
this.init = function(){};
|
||||
this.configure = function(){};
|
||||
this.render = function(){};
|
||||
this.uuid = function() {
|
||||
return this.type+'::'+this.label;
|
||||
};
|
||||
this._errors = require('./Errors.js');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the data for the chart by creating a new DataTable
|
||||
*
|
||||
* @external "google.visualization.DataTable"
|
||||
* @see {@link https://developers.google.com/chart/interactive/docs/reference#DataTable|DataTable}
|
||||
* @param {Object} data Json representation of a DataTable
|
||||
* @param {Array.<Array>} data.cols Array of column definitions
|
||||
* @param {Array.<Array>} data.rows Array of row definitions
|
||||
*/
|
||||
Dashboard.prototype.setData = function (data) {
|
||||
this.data = new google.visualization.DataTable(data);
|
||||
};
|
||||
|
||||
/**
|
||||
* Set the ID of the output element for the Dashboard.
|
||||
*
|
||||
* @public
|
||||
* @param {string} elemId
|
||||
* @throws ElementIdNotFound
|
||||
*/
|
||||
Dashboard.prototype.setElement = function (elemId) {
|
||||
this.element = document.getElementById(elemId);
|
||||
|
||||
if (! this.element) {
|
||||
throw new this._errors.ElementIdNotFound(elemId);
|
||||
}
|
||||
};
|
||||
|
||||
return Dashboard;
|
||||
|
||||
}());
|
||||
@@ -0,0 +1,82 @@
|
||||
/* jshint undef: true */
|
||||
/* globals module, require */
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Errors module
|
||||
*
|
||||
* @module lava/Errors
|
||||
* @author Kevin Hill <kevinkhill@gmail.com>
|
||||
* @copyright (c) 2017, KHill Designs
|
||||
* @license MIT
|
||||
*/
|
||||
var LavachartsError = function (message) {
|
||||
this.name = 'LavachartsError';
|
||||
this.message = (message || '');
|
||||
};
|
||||
LavachartsError.prototype = Error.prototype;
|
||||
|
||||
/**
|
||||
* InvalidCallback Error
|
||||
*
|
||||
* thrown when when anything but a function is given as a callback
|
||||
* @type {function}
|
||||
*/
|
||||
var InvalidCallback = function (callback) {
|
||||
this.name = 'InvalidCallback';
|
||||
this.message = '[Lavacharts] ' + typeof callback + ' is not a valid callback.';
|
||||
};
|
||||
InvalidCallback.prototype = LavachartsError.prototype;
|
||||
module.exports.InvalidCallback = InvalidCallback;
|
||||
|
||||
/**
|
||||
* InvalidLabel Error
|
||||
*
|
||||
* Thrown when when anything but a string is given as a label.
|
||||
*
|
||||
* @type {function}
|
||||
*/
|
||||
module.exports.InvalidLabel = function (label) {
|
||||
this.name = 'InvalidLabel';
|
||||
this.message = '[Lavacharts] "' + typeof label + '" is not a valid label.';
|
||||
};
|
||||
module.exports.InvalidLabel.prototype = Error.prototype;
|
||||
|
||||
/**
|
||||
* ElementIdNotFound Error
|
||||
*
|
||||
* Thrown when when anything but a string is given as a label.
|
||||
*
|
||||
* @type {function}
|
||||
*/
|
||||
module.exports.ElementIdNotFound = function (elemId) {
|
||||
this.name = 'ElementIdNotFound';
|
||||
this.message = '[Lavacharts] DOM node #' + elemId + ' was not found.';
|
||||
};
|
||||
module.exports.ElementIdNotFound.prototype = Error.prototype;
|
||||
|
||||
/**
|
||||
* ChartNotFound Error
|
||||
*
|
||||
* Thrown when when the getChart() method cannot find a chart with the given label.
|
||||
*
|
||||
* @type {function}
|
||||
*/
|
||||
module.exports.ChartNotFound = function (label) {
|
||||
this.name = 'ChartNotFound';
|
||||
this.message = '[Lavacharts] Chart with label "' + label + '" was not found.';
|
||||
};
|
||||
module.exports.ChartNotFound.prototype = Error.prototype;
|
||||
|
||||
/**
|
||||
* DashboardNotFound Error
|
||||
*
|
||||
* Thrown when when the getDashboard() method cannot find a chart with the given label.
|
||||
*
|
||||
* @type {function}
|
||||
*/
|
||||
module.exports.DashboardNotFound = function (label) {
|
||||
this.name = 'DashboardNotFound';
|
||||
this.message = '[Lavacharts] Dashboard with label "' + label + '" was not found.';
|
||||
};
|
||||
module.exports.DashboardNotFound.prototype = Error.prototype;
|
||||
+540
@@ -0,0 +1,540 @@
|
||||
/* jshint undef: true, unused: true */
|
||||
/* globals window, document, console, google, module, require */
|
||||
|
||||
/**
|
||||
* lava.js module
|
||||
*
|
||||
* @module lava/Lava
|
||||
* @author Kevin Hill <kevinkhill@gmail.com>
|
||||
* @copyright (c) 2017, KHill Designs
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
module.exports = (function() {
|
||||
'use strict';
|
||||
|
||||
var Q = require('q');
|
||||
// var Promise = require('bluebird');
|
||||
var _ = require('lodash');
|
||||
var util = require('util');
|
||||
var EventEmitter = require('events');
|
||||
|
||||
function Lava() {
|
||||
/**
|
||||
* Defining the Chart class within the module.
|
||||
*
|
||||
* @type {Chart}
|
||||
*/
|
||||
this.Chart = require('./Chart.js');
|
||||
|
||||
/**
|
||||
* Defining the Dashboard class within the module.
|
||||
*
|
||||
* @type {Dashboard}
|
||||
*/
|
||||
this.Dashboard = require('./Dashboard.js');
|
||||
|
||||
/**
|
||||
* Urls to Google's static loader
|
||||
*
|
||||
* @type {string}
|
||||
* @public
|
||||
*/
|
||||
this.gstaticUrl = 'https://www.gstatic.com/charts/loader.js';
|
||||
|
||||
/**
|
||||
* JSON object of config items.
|
||||
*
|
||||
* @type {Object}
|
||||
* @private
|
||||
*/
|
||||
this.options = (function () {
|
||||
if (typeof OPTIONS_JSON !== 'object') {
|
||||
return {};
|
||||
}
|
||||
|
||||
return OPTIONS_JSON;
|
||||
}());
|
||||
|
||||
/**
|
||||
* Array of charts stored in the module.
|
||||
*
|
||||
* @type {Array.<Chart>}
|
||||
* @private
|
||||
*/
|
||||
this._charts = [];
|
||||
|
||||
/**
|
||||
* Array of dashboards stored in the module.
|
||||
*
|
||||
* @type {Array.<Dashboard>}
|
||||
* @private
|
||||
*/
|
||||
this._dashboards = [];
|
||||
|
||||
/**
|
||||
* Ready callback to be called when the module is finished running.
|
||||
*
|
||||
* @callback _readyCallback
|
||||
* @private
|
||||
*/
|
||||
this._readyCallback = _.noop();
|
||||
|
||||
/**
|
||||
* Error definitions for the module.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
this._errors = require('./Errors.js');
|
||||
|
||||
/**
|
||||
* Apply the EventEmitter methods to Lava
|
||||
*/
|
||||
EventEmitter.call(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inherit from the EventEmitter
|
||||
*/
|
||||
util.inherits(Lava, EventEmitter);
|
||||
|
||||
/**
|
||||
* Initialize the Lava.js module by attaching the event listeners
|
||||
* and calling the charts' and dashboards' init methods
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
Lava.prototype.init = function () {
|
||||
console.log('[lava.js] Initializing');
|
||||
|
||||
var $lava = this;
|
||||
var readyCount = 0;
|
||||
|
||||
this.on('ready', function (renderable) {
|
||||
console.log('[lava.js] ' + renderable.uuid() + ' -> ready');
|
||||
|
||||
readyCount++;
|
||||
|
||||
if (readyCount === $lava._getRenderables().length) {
|
||||
console.log('[lava.js] Loading Google');
|
||||
|
||||
$lava._loadGoogle().then(function() {
|
||||
return $lava._mapRenderables(function (renderable) {
|
||||
console.log('[lava.js] ' + renderable.uuid() + ' -> configuring');
|
||||
|
||||
return renderable.configure();
|
||||
});
|
||||
}).then(function() {
|
||||
return $lava._mapRenderables(function (renderable) {
|
||||
console.log('[lava.js] ' + renderable.uuid() + ' -> rendering');
|
||||
|
||||
return renderable.render();
|
||||
});
|
||||
}).then(function() {
|
||||
console.log('[lava.js] Ready, firing ready callback');
|
||||
|
||||
$lava._readyCallback();
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Runs the Lava.js module by calling all the renderables' init methods
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
Lava.prototype.run = function () {
|
||||
this.init();
|
||||
|
||||
this._forEachRenderable(function (renderable) {
|
||||
console.log('[lava.js] ' + renderable.uuid() + ' -> initializing');
|
||||
|
||||
renderable.init();
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Stores a renderable lava object within the module.
|
||||
*
|
||||
* @param {Chart|Dashboard} renderable
|
||||
*/
|
||||
Lava.prototype.store = function (renderable) {
|
||||
if (renderable instanceof this.Chart) {
|
||||
this.storeChart(renderable);
|
||||
}
|
||||
|
||||
if (renderable instanceof this.Dashboard) {
|
||||
this.storeDashboard(renderable);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Assigns a callback for when the charts are ready to be interacted with.
|
||||
*
|
||||
* This is used to wrap calls to lava.loadData() or lava.loadOptions()
|
||||
* to protect against accessing charts that aren't loaded yet
|
||||
*
|
||||
* @public
|
||||
* @param {Function} callback
|
||||
*/
|
||||
Lava.prototype.ready = function (callback) {
|
||||
if (typeof callback !== 'function') {
|
||||
throw new this._errors.InvalidCallback(callback);
|
||||
}
|
||||
|
||||
this._readyCallback = callback;
|
||||
};
|
||||
|
||||
/**
|
||||
* Event wrapper for chart events.
|
||||
*
|
||||
*
|
||||
* Used internally when events are applied so the user event function has
|
||||
* access to the chart within the event callback.
|
||||
*
|
||||
* @param {Object} event
|
||||
* @param {Object} lavachart
|
||||
* @param {Function} callback
|
||||
* @return {Function}
|
||||
*/
|
||||
Lava.prototype.event = function (event, lavachart, callback) {
|
||||
if (typeof callback !== 'function') {
|
||||
throw new this._errors.InvalidCallback(callback);
|
||||
}
|
||||
|
||||
return callback(event, lavachart.chart, lavachart.data);
|
||||
};
|
||||
|
||||
/**
|
||||
* Loads new data into the chart and redraws.
|
||||
*
|
||||
*
|
||||
* Used with an AJAX call to a PHP method returning DataTable->toJson(),
|
||||
* a chart can be dynamically update in page, without reloads.
|
||||
*
|
||||
* @public
|
||||
* @param {string} label
|
||||
* @param {string} json
|
||||
* @param {Function} callback
|
||||
*/
|
||||
Lava.prototype.loadData = function (label, json, callback) {
|
||||
if (typeof callback === 'undefined') {
|
||||
callback = _.noop;
|
||||
}
|
||||
|
||||
if (typeof callback !== 'function') {
|
||||
throw new this._errors.InvalidCallback(callback);
|
||||
}
|
||||
|
||||
this.getChart(label, function (chart) {
|
||||
if (typeof json.data !== 'undefined') {
|
||||
chart.setData(json.data);
|
||||
} else {
|
||||
chart.setData(json);
|
||||
}
|
||||
|
||||
if (typeof json.formats !== 'undefined') {
|
||||
chart.applyFormats(json.formats);
|
||||
}
|
||||
|
||||
chart.redraw();
|
||||
|
||||
callback(chart);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Loads new options into a chart and redraws.
|
||||
*
|
||||
*
|
||||
* Used with an AJAX call, or javascript events, to load a new array of options into a chart.
|
||||
* This can be used to update a chart dynamically, without reloads.
|
||||
*
|
||||
* @public
|
||||
* @param {string} label
|
||||
* @param {string} json
|
||||
* @param {Function} callback
|
||||
*/
|
||||
Lava.prototype.loadOptions = function (label, json, callback) {
|
||||
if (typeof callback === 'undefined') {
|
||||
callback = callback || _.noop;
|
||||
}
|
||||
|
||||
if (typeof callback !== 'function') {
|
||||
throw new this._errors.InvalidCallback(callback);
|
||||
}
|
||||
|
||||
this.getChart(label, function (chart) {
|
||||
chart.setOptions(json);
|
||||
|
||||
chart.redraw();
|
||||
|
||||
callback(chart);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Redraws all of the registered charts on screen.
|
||||
*
|
||||
* This method is attached to the window resize event with a 300ms debounce
|
||||
* to make the charts responsive to the browser resizing.
|
||||
*/
|
||||
Lava.prototype.redrawCharts = function() {
|
||||
this._forEachRenderable(function (renderable) {
|
||||
console.log('[lava.js] ' + renderable.uuid() + ' -> redrawing');
|
||||
|
||||
var redraw = _.bind(renderable.redraw, renderable);
|
||||
|
||||
redraw();
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a new Chart.
|
||||
*
|
||||
* @public
|
||||
* @param {string} type Type of chart to create
|
||||
* @param {string} label Label for the chart
|
||||
* @return {Chart}
|
||||
*/
|
||||
Lava.prototype.createChart = function (type, label) {
|
||||
return new this.Chart(type, label);
|
||||
};
|
||||
|
||||
/**
|
||||
* Stores a chart within the module.
|
||||
*
|
||||
* @public
|
||||
* @param {Chart} chart
|
||||
*/
|
||||
Lava.prototype.storeChart = function (chart) {
|
||||
this._charts.push(chart);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the LavaChart javascript objects
|
||||
*
|
||||
*
|
||||
* The LavaChart object holds all the user defined properties such as data, options, formats,
|
||||
* the GoogleChart object, and relative methods for internal use.
|
||||
*
|
||||
* The GoogleChart object is available as ".chart" from the returned LavaChart.
|
||||
* It can be used to access any of the available methods such as
|
||||
* getImageURI() or getChartLayoutInterface().
|
||||
* See https://google-developers.appspot.com/chart/interactive/docs/gallery/linechart#methods
|
||||
* for some examples relative to LineCharts.
|
||||
*
|
||||
* @public
|
||||
* @param {string} label
|
||||
* @param {Function} callback
|
||||
* @throws InvalidLabel
|
||||
* @throws InvalidCallback
|
||||
* @throws ChartNotFound
|
||||
*/
|
||||
Lava.prototype.getChart = function (label, callback) {
|
||||
if (typeof label !== 'string') {
|
||||
throw new this._errors.InvalidLabel(label);
|
||||
}
|
||||
|
||||
if (typeof callback !== 'function') {
|
||||
throw new this._errors.InvalidCallback(callback);
|
||||
}
|
||||
|
||||
var chart = _.find(this._charts, {label: label});
|
||||
|
||||
if (!chart) {
|
||||
throw new this._errors.ChartNotFound(label);
|
||||
}
|
||||
|
||||
callback(chart);
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a new Dashboard with a given label.
|
||||
*
|
||||
* @public
|
||||
* @param {string} label
|
||||
* @return {Dashboard}
|
||||
*/
|
||||
Lava.prototype.createDashboard = function (label) {
|
||||
return new this.Dashboard(label);
|
||||
};
|
||||
|
||||
/**
|
||||
* Stores a dashboard within the module.
|
||||
*
|
||||
* @public
|
||||
* @param {Dashboard} dash
|
||||
*/
|
||||
Lava.prototype.storeDashboard = function (dash) {
|
||||
this._dashboards.push(dash);
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve a Dashboard from Lava.js
|
||||
*
|
||||
* @public
|
||||
* @param {string} label Dashboard label
|
||||
* @param {Function} callback Callback function
|
||||
* @throws InvalidLabel
|
||||
* @throws InvalidCallback
|
||||
* @throws DashboardNotFound
|
||||
*/
|
||||
Lava.prototype.getDashboard = function (label, callback) {
|
||||
if (typeof label !== 'string') {
|
||||
throw new this._errors.InvalidLabel(label);
|
||||
}
|
||||
|
||||
if (typeof callback !== 'function') {
|
||||
throw new this._errors.InvalidCallback(callback);
|
||||
}
|
||||
|
||||
var dash = _.find(this._dashboards, {label: label});
|
||||
|
||||
if (dash instanceof this.Dashboard === false) {
|
||||
throw new this._errors.DashboardNotFound(label);
|
||||
}
|
||||
|
||||
callback(dash);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns an array with the charts and dashboards.
|
||||
*
|
||||
* @private
|
||||
* @return {Array}
|
||||
*/
|
||||
Lava.prototype._getRenderables = function () {
|
||||
return _.concat(this._charts, this._dashboards);
|
||||
};
|
||||
|
||||
/**
|
||||
* Applies the callback to each of the charts and dashboards.
|
||||
*
|
||||
* @private
|
||||
* @param {Function} callback
|
||||
*/
|
||||
Lava.prototype._forEachRenderable = function (callback) {
|
||||
_.forEach(this._getRenderables(), callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Applies the callback and builds an array of return values
|
||||
* for each of the charts and dashboards.
|
||||
*
|
||||
* @private
|
||||
* @param {Function} callback
|
||||
* @return {Array}
|
||||
*/
|
||||
Lava.prototype._mapRenderables = function (callback) {
|
||||
return _.map(this._getRenderables(), callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns an array of the google packages to load.
|
||||
*
|
||||
* @private
|
||||
* @return {Array}
|
||||
*/
|
||||
Lava.prototype._getPackages = function () {
|
||||
return _.union(
|
||||
_.map(this._charts, 'package'),
|
||||
_.flatten(_.map(this._dashboards, 'packages'))
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if Google's Static Loader is in page.
|
||||
*
|
||||
* @private
|
||||
* @returns {boolean}
|
||||
*/
|
||||
Lava.prototype._googleIsLoaded = function () {
|
||||
var scripts = document.getElementsByTagName('script');
|
||||
var loaded = false;
|
||||
|
||||
for (var i = scripts.length; i--;) {
|
||||
if (scripts[i].src === this.gstaticUrl) {
|
||||
loaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
return loaded;
|
||||
};
|
||||
|
||||
/**
|
||||
* Load the Google Static Loader and resolve the promise when ready.
|
||||
*
|
||||
* @private
|
||||
* @returns {Promise}
|
||||
*/
|
||||
Lava.prototype._loadGoogle = function () {
|
||||
var $lava = this;
|
||||
var deferred = Q.defer();
|
||||
var script = this._createScriptTag(deferred);
|
||||
|
||||
if (this._googleIsLoaded()) {
|
||||
console.log('[lava.js] Static loader found, initializing window.google');
|
||||
|
||||
$lava._googleChartLoader(deferred);
|
||||
} else {
|
||||
console.log('[lava.js] Static loader not found, appending to head');
|
||||
|
||||
document.head.appendChild(script);
|
||||
}
|
||||
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a new script tag for the Google Static Loader.
|
||||
*
|
||||
* @private
|
||||
* @param {Promise} deferred
|
||||
* @returns {Element}
|
||||
*/
|
||||
Lava.prototype._createScriptTag = function (deferred) {
|
||||
var script = document.createElement('script');
|
||||
var $lava = this;
|
||||
|
||||
script.type = 'text/javascript';
|
||||
script.async = true;
|
||||
script.src = this.gstaticUrl;
|
||||
script.onload = script.onreadystatechange = function (event) {
|
||||
event = event || window.event;
|
||||
|
||||
if (event.type === 'load' || (/loaded|complete/.test(this.readyState))) {
|
||||
this.onload = this.onreadystatechange = null;
|
||||
|
||||
$lava._googleChartLoader(deferred);
|
||||
}
|
||||
};
|
||||
|
||||
return script;
|
||||
};
|
||||
|
||||
/**
|
||||
* Runs the Google chart loader and resolves the promise.
|
||||
*
|
||||
* @param {Promise} deferred
|
||||
* @private
|
||||
*/
|
||||
Lava.prototype._googleChartLoader = function (deferred) {
|
||||
var config = {
|
||||
packages: this._getPackages(),
|
||||
language: this.options.locale
|
||||
};
|
||||
|
||||
if (this.options.maps_api_key !== '') {
|
||||
config.mapsApiKey = this.options.maps_api_key;
|
||||
}
|
||||
|
||||
console.log('[lava.js] Google loaded with options:', config);
|
||||
|
||||
google.charts.load('current', config);
|
||||
|
||||
google.charts.setOnLoadCallback(deferred.resolve);
|
||||
};
|
||||
|
||||
return new Lava();
|
||||
}());
|
||||
@@ -0,0 +1,22 @@
|
||||
/* jshint undef: true, unused: true */
|
||||
/* globals exports, window */
|
||||
|
||||
/**
|
||||
* Slightly modified event attachment handler.
|
||||
*
|
||||
* Credit to Alex V.
|
||||
*
|
||||
* @link https://stackoverflow.com/users/327934/alex-v
|
||||
* @link http://stackoverflow.com/a/3150139
|
||||
* @param {Function} callback
|
||||
*/
|
||||
exports.addResizeEvent = function (callback) {
|
||||
if (window === null || typeof(window) === 'undefined') return;
|
||||
if (window.addEventListener) {
|
||||
window.addEventListener('resize', callback, false);
|
||||
} else if (window.attachEvent) {
|
||||
window.attachEvent('onresize', callback);
|
||||
} else {
|
||||
window['onresize'] = callback;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,43 @@
|
||||
/* jshint undef: true, unused: true */
|
||||
/* globals lava, google */
|
||||
|
||||
(function(){
|
||||
"use strict";
|
||||
|
||||
var $chart = lava.createChart('<chartType>', '<chartLabel>');
|
||||
|
||||
$chart.init = function() {
|
||||
$chart.package = '<chartPackage>';
|
||||
$chart.setElement('<elemId>');
|
||||
$chart.setPngOutput(<pngOutput>);
|
||||
|
||||
$chart.configure = function () {
|
||||
$chart.render = function (data) {
|
||||
$chart.setData(<chartData>);
|
||||
|
||||
$chart.options = <chartOptions>;
|
||||
|
||||
$chart.chart = new <chartClass>($chart.element);
|
||||
|
||||
<formats>
|
||||
<events>
|
||||
|
||||
$chart.chart.draw($chart.data, $chart.options);
|
||||
|
||||
if ($chart.pngOutput === true) {
|
||||
$chart.drawPng();
|
||||
}
|
||||
|
||||
$chart.promises.rendered.resolve();
|
||||
return $chart.promises.rendered.promise;
|
||||
};
|
||||
|
||||
$chart.promises.configure.resolve();
|
||||
return $chart.promises.configure.promise;
|
||||
};
|
||||
|
||||
lava.emit('ready', $chart);
|
||||
};
|
||||
|
||||
lava.store($chart);
|
||||
})();
|
||||
@@ -0,0 +1,34 @@
|
||||
/* jshint undef: true, unused: true */
|
||||
/* globals lava, google */
|
||||
|
||||
(function(){
|
||||
"use strict";
|
||||
|
||||
var $dash = lava.createDashboard('<label>');
|
||||
|
||||
$dash.init = function () {
|
||||
$dash.setElement('<elemId>');
|
||||
$dash.packages = <packages>;
|
||||
|
||||
$dash.configure = function () {
|
||||
$dash.render = function (data) {
|
||||
$dash.dashboard = new <class>($dash.element);
|
||||
|
||||
$dash.setData(<chartData>);
|
||||
|
||||
<bindings>
|
||||
|
||||
$dash.dashboard.draw($dash.data);
|
||||
|
||||
lava.emit('rendered', $dash);
|
||||
};
|
||||
|
||||
$dash.deferred.resolve();
|
||||
return $dash.deferred.promise;
|
||||
};
|
||||
|
||||
lava.emit('ready', $dash);
|
||||
};
|
||||
|
||||
lava.store($dash);
|
||||
})();
|
||||
Reference in New Issue
Block a user