mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 16:36:37 +02:00
MDL-56792 core_charts: clean html content from title and labels
This commit add cleanData() and call it from all places that has labels or title to guarantee that html content will not be printed on the chart.
This commit is contained in:
parent
0cbfb7ff2a
commit
cff1f90adf
2 changed files with 25 additions and 8 deletions
2
lib/amd/build/chart_output_chartjs.min.js
vendored
2
lib/amd/build/chart_output_chartjs.min.js
vendored
|
@ -1 +1 @@
|
|||
define(["jquery","core/chartjs","core/chart_axis","core/chart_bar","core/chart_output_base","core/chart_line","core/chart_pie","core/chart_series"],function(a,b,c,d,e,f,g,h){function i(){e.prototype.constructor.apply(this,arguments),this._canvas=this._node,"CANVAS"!=this._canvas.prop("tagName")&&(this._canvas=a("<canvas>"),this._node.append(this._canvas)),this._build()}var j=function(a,b){return"axis-"+a+"-"+b};return i.prototype=Object.create(e.prototype),i.prototype._config=null,i.prototype._chartjs=null,i.prototype._canvas=null,i.prototype._build=function(){this._config=this._makeConfig(),this._chartjs=new b(this._canvas[0],this._config)},i.prototype._getChartType=function(){var a=this._chart.getType();return this._chart.getType()===d.prototype.TYPE&&this._chart.getHorizontal()===!0?a="horizontalBar":this._chart.getType()===g.prototype.TYPE&&this._chart.getDoughnut()===!0&&(a="doughnut"),a},i.prototype._makeAxisConfig=function(a,b,d){var e={id:j(b,d)};return a.getPosition()!==c.prototype.POS_DEFAULT&&(e.position=a.getPosition()),null!==a.getLabel()&&(e.scaleLabel={display:!0,labelString:a.getLabel()}),null!==a.getStepSize()&&(e.ticks=e.ticks||{},e.ticks.stepSize=a.getStepSize()),null!==a.getMax()&&(e.ticks=e.ticks||{},e.ticks.max=a.getMax()),null!==a.getMin()&&(e.ticks=e.ticks||{},e.ticks.min=a.getMin()),e},i.prototype._makeConfig=function(){var a={type:this._getChartType(),data:{labels:this._chart.getLabels(),datasets:this._makeDatasetsConfig()},options:{title:{display:null!==this._chart.getTitle(),text:this._chart.getTitle()}}};return this._chart.getXAxes().forEach(function(b,c){var d=b.getLabels();a.options.scales=a.options.scales||{},a.options.scales.xAxes=a.options.scales.xAxes||[],a.options.scales.xAxes[c]=this._makeAxisConfig(b,"x",c),null!==d&&(a.options.scales.xAxes[c].ticks.callback=function(a,b){return d[b]||""}),a.options.scales.xAxes[c].stacked=this._isStacked()}.bind(this)),this._chart.getYAxes().forEach(function(b,c){var d=b.getLabels();a.options.scales=a.options.scales||{},a.options.scales.yAxes=a.options.scales.yAxes||[],a.options.scales.yAxes[c]=this._makeAxisConfig(b,"y",c),null!==d&&(a.options.scales.yAxes[c].ticks.callback=function(a){return d[parseInt(a,10)]||""}),a.options.scales.yAxes[c].stacked=this._isStacked()}.bind(this)),a.options.tooltips={callbacks:{label:this._makeTooltip.bind(this)}},a},i.prototype._makeDatasetsConfig=function(){var a=this._chart.getSeries().map(function(a){var b=a.hasColoredValues()?a.getColors():a.getColor(),c={label:a.getLabel(),data:a.getValues(),type:a.getType(),fill:!1,backgroundColor:b,borderColor:this._chart.getType()==g.prototype.TYPE?null:b,lineTension:this._isSmooth(a)?.3:0};return null!==a.getXAxis()&&(c.xAxisID=j("x",a.getXAxis())),null!==a.getYAxis()&&(c.yAxisID=j("y",a.getYAxis())),c}.bind(this));return a},i.prototype._makeTooltip=function(a,b){var c=this._chart.getSeries()[a.datasetIndex],d=c.getLabel(),e=c.getLabels(),f=b.datasets[a.datasetIndex].data,g=f[a.index],h=d+": "+g;return null!==e&&(h=e[a.index]),h},i.prototype._isSmooth=function(a){var b=!1;return this._chart.getType()===f.prototype.TYPE?(b=a.getSmooth(),null===b&&(b=this._chart.getSmooth())):a.getType()===h.prototype.TYPE_LINE&&(b=a.getSmooth()),b},i.prototype._isStacked=function(){var a=!1;return this._chart.getType()===d.prototype.TYPE&&(a=this._chart.getStacked()),a},i.prototype.update=function(){a.extend(!0,this._config,this._makeConfig()),this._chartjs.update()},i});
|
||||
define(["jquery","core/chartjs","core/chart_axis","core/chart_bar","core/chart_output_base","core/chart_line","core/chart_pie","core/chart_series"],function(a,b,c,d,e,f,g,h){function i(){e.prototype.constructor.apply(this,arguments),this._canvas=this._node,"CANVAS"!=this._canvas.prop("tagName")&&(this._canvas=a("<canvas>"),this._node.append(this._canvas)),this._build()}var j=function(a,b){return"axis-"+a+"-"+b};return i.prototype=Object.create(e.prototype),i.prototype._config=null,i.prototype._chartjs=null,i.prototype._canvas=null,i.prototype._build=function(){this._config=this._makeConfig(),this._chartjs=new b(this._canvas[0],this._config)},i.prototype._cleanData=function(b){return b instanceof Array?b.map(function(b){return a("<span>").html(b).text()}):a("<span>").html(b).text()},i.prototype._getChartType=function(){var a=this._chart.getType();return this._chart.getType()===d.prototype.TYPE&&this._chart.getHorizontal()===!0?a="horizontalBar":this._chart.getType()===g.prototype.TYPE&&this._chart.getDoughnut()===!0&&(a="doughnut"),a},i.prototype._makeAxisConfig=function(a,b,d){var e={id:j(b,d)};return a.getPosition()!==c.prototype.POS_DEFAULT&&(e.position=a.getPosition()),null!==a.getLabel()&&(e.scaleLabel={display:!0,labelString:this._cleanData(a.getLabel())}),null!==a.getStepSize()&&(e.ticks=e.ticks||{},e.ticks.stepSize=a.getStepSize()),null!==a.getMax()&&(e.ticks=e.ticks||{},e.ticks.max=a.getMax()),null!==a.getMin()&&(e.ticks=e.ticks||{},e.ticks.min=a.getMin()),e},i.prototype._makeConfig=function(){var a={type:this._getChartType(),data:{labels:this._cleanData(this._chart.getLabels()),datasets:this._makeDatasetsConfig()},options:{title:{display:null!==this._chart.getTitle(),text:this._cleanData(this._chart.getTitle())}}};return this._chart.getXAxes().forEach(function(b,c){var d=b.getLabels();a.options.scales=a.options.scales||{},a.options.scales.xAxes=a.options.scales.xAxes||[],a.options.scales.xAxes[c]=this._makeAxisConfig(b,"x",c),null!==d&&(a.options.scales.xAxes[c].ticks.callback=function(a,b){return d[b]||""}),a.options.scales.xAxes[c].stacked=this._isStacked()}.bind(this)),this._chart.getYAxes().forEach(function(b,c){var d=b.getLabels();a.options.scales=a.options.scales||{},a.options.scales.yAxes=a.options.scales.yAxes||[],a.options.scales.yAxes[c]=this._makeAxisConfig(b,"y",c),null!==d&&(a.options.scales.yAxes[c].ticks.callback=function(a){return d[parseInt(a,10)]||""}),a.options.scales.yAxes[c].stacked=this._isStacked()}.bind(this)),a.options.tooltips={callbacks:{label:this._makeTooltip.bind(this)}},a},i.prototype._makeDatasetsConfig=function(){var a=this._chart.getSeries().map(function(a){var b=a.hasColoredValues()?a.getColors():a.getColor(),c={label:this._cleanData(a.getLabel()),data:a.getValues(),type:a.getType(),fill:!1,backgroundColor:b,borderColor:this._chart.getType()==g.prototype.TYPE?null:b,lineTension:this._isSmooth(a)?.3:0};return null!==a.getXAxis()&&(c.xAxisID=j("x",a.getXAxis())),null!==a.getYAxis()&&(c.yAxisID=j("y",a.getYAxis())),c}.bind(this));return a},i.prototype._makeTooltip=function(a,b){var c=this._chart.getSeries()[a.datasetIndex],d=c.getLabel(),e=c.getLabels(),f=b.datasets[a.datasetIndex].data,g=f[a.index],h=this._cleanData(d)+": "+g;return null!==e&&(h=this._cleanData(e[a.index])),h},i.prototype._isSmooth=function(a){var b=!1;return this._chart.getType()===f.prototype.TYPE?(b=a.getSmooth(),null===b&&(b=this._chart.getSmooth())):a.getType()===h.prototype.TYPE_LINE&&(b=a.getSmooth()),b},i.prototype._isStacked=function(){var a=!1;return this._chart.getType()===d.prototype.TYPE&&(a=this._chart.getStacked()),a},i.prototype.update=function(){a.extend(!0,this._config,this._makeConfig()),this._chartjs.update()},i});
|
|
@ -98,6 +98,23 @@ define([
|
|||
this._chartjs = new Chartjs(this._canvas[0], this._config);
|
||||
};
|
||||
|
||||
/**
|
||||
* Clean data.
|
||||
*
|
||||
* @param {(String|String[])} data A single string or an array of strings.
|
||||
* @returns {(String|String[])}
|
||||
* @protected
|
||||
*/
|
||||
Output.prototype._cleanData = function(data) {
|
||||
if (data instanceof Array) {
|
||||
return data.map(function(value) {
|
||||
return $('<span>').html(value).text();
|
||||
});
|
||||
} else {
|
||||
return $('<span>').html(data).text();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the chart type and handles the Chart.js specific chart types.
|
||||
*
|
||||
|
@ -143,7 +160,7 @@ define([
|
|||
if (axis.getLabel() !== null) {
|
||||
scaleData.scaleLabel = {
|
||||
display: true,
|
||||
labelString: axis.getLabel()
|
||||
labelString: this._cleanData(axis.getLabel())
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -176,13 +193,13 @@ define([
|
|||
var config = {
|
||||
type: this._getChartType(),
|
||||
data: {
|
||||
labels: this._chart.getLabels(),
|
||||
labels: this._cleanData(this._chart.getLabels()),
|
||||
datasets: this._makeDatasetsConfig()
|
||||
},
|
||||
options: {
|
||||
title: {
|
||||
display: this._chart.getTitle() !== null,
|
||||
text: this._chart.getTitle()
|
||||
text: this._cleanData(this._chart.getTitle())
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -236,7 +253,7 @@ define([
|
|||
var sets = this._chart.getSeries().map(function(series) {
|
||||
var colors = series.hasColoredValues() ? series.getColors() : series.getColor();
|
||||
var dataset = {
|
||||
label: series.getLabel(),
|
||||
label: this._cleanData(series.getLabel()),
|
||||
data: series.getValues(),
|
||||
type: series.getType(),
|
||||
fill: false,
|
||||
|
@ -276,11 +293,11 @@ define([
|
|||
var tooltipData = chartData[tooltipItem.index];
|
||||
|
||||
// Build default tooltip.
|
||||
var tooltip = serieLabel + ': ' + tooltipData;
|
||||
var tooltip = this._cleanData(serieLabel) + ': ' + tooltipData;
|
||||
|
||||
// Add serie labels to the tooltip if any.
|
||||
// Add series labels to the tooltip if any.
|
||||
if (serieLabels !== null) {
|
||||
tooltip = serieLabels[tooltipItem.index];
|
||||
tooltip = this._cleanData(serieLabels[tooltipItem.index]);
|
||||
}
|
||||
|
||||
return tooltip;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue