Add a helper function to reduce code size

This commit is contained in:
etimberg 2015-10-18 16:31:18 -04:00
parent 180209e55c
commit ceaa4ff03f

View File

@ -60,6 +60,19 @@
}, },
}; };
// Helper to push or concat based on if the 2nd parameter is an array or not
function pushOrConcat(base, toPush) {
if (toPush) {
if (helpers.isArray(toPush)) {
base = base.concat(toPush);
} else {
base.push(toPush);
}
}
return base;
}
Chart.Tooltip = Chart.Element.extend({ Chart.Tooltip = Chart.Element.extend({
initialize: function() { initialize: function() {
var options = this._options; var options = this._options;
@ -106,34 +119,17 @@
}); });
}, },
// Get the title
getTitle: function() { getTitle: function() {
var beforeTitle = this._options.tooltips.callbacks.beforeTitle.apply(this, arguments), var beforeTitle = this._options.tooltips.callbacks.beforeTitle.apply(this, arguments),
title = this._options.tooltips.callbacks.title.apply(this, arguments), title = this._options.tooltips.callbacks.title.apply(this, arguments),
afterTitle = this._options.tooltips.callbacks.afterTitle.apply(this, arguments); afterTitle = this._options.tooltips.callbacks.afterTitle.apply(this, arguments);
var lines = []; var lines = [];
lines = pushOrConcat(lines, beforeTitle);
lines = pushOrConcat(lines, title);
lines = pushOrConcat(lines, afterTitle);
if (beforeTitle) {
if (helpers.isArray(beforeTitle)) {
lines = lines.concat(beforeTitle);
} else {
lines.push(beforeTitle);
}
}
if (title) {
if (helpers.isArray(title)) {
lines = lines.concat(title);
} else {
lines.push(title);
}
}
if (afterTitle) {
if (helpers.isArray(afterTitle)) {
lines = lines.concat(afterTitle);
} else {
lines.push(afterTitle);
}
}
return lines; return lines;
}, },
@ -189,34 +185,16 @@
return helpers.isArray(lines) ? lines : [lines]; return helpers.isArray(lines) ? lines : [lines];
}, },
// Get the footer and beforeFooter and afterFooter lines
getFooter: function() { getFooter: function() {
var beforeFooter = this._options.tooltips.callbacks.beforeFooter.apply(this, arguments), var beforeFooter = this._options.tooltips.callbacks.beforeFooter.apply(this, arguments);
footer = this._options.tooltips.callbacks.footer.apply(this, arguments), var footer = this._options.tooltips.callbacks.footer.apply(this, arguments);
afterFooter = this._options.tooltips.callbacks.afterFooter.apply(this, arguments); var afterFooter = this._options.tooltips.callbacks.afterFooter.apply(this, arguments);
var lines = []; var lines = [];
lines = pushOrConcat(lines, beforeFooter);
if (beforeFooter) { lines = pushOrConcat(lines, footer);
if (helpers.isArray(beforeFooter)) { lines = pushOrConcat(lines, afterFooter);
lines = lines.concat(beforeFooter);
} else {
lines.push(beforeFooter);
}
}
if (footer) {
if (helpers.isArray(footer)) {
lines = lines.concat(footer);
} else {
lines.push(footer);
}
}
if (afterFooter) {
if (helpers.isArray(afterFooter)) {
lines = lines.concat(afterFooter);
} else {
lines.push(afterFooter);
}
}
return lines; return lines;
}, },