From ceaa4ff03f7bc53bbc19711a023ea9313fdfde7f Mon Sep 17 00:00:00 2001 From: etimberg Date: Sun, 18 Oct 2015 16:31:18 -0400 Subject: [PATCH] Add a helper function to reduce code size --- src/core/core.tooltip.js | 70 ++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 46 deletions(-) diff --git a/src/core/core.tooltip.js b/src/core/core.tooltip.js index 9b227e186..a45889d61 100644 --- a/src/core/core.tooltip.js +++ b/src/core/core.tooltip.js @@ -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({ initialize: function() { var options = this._options; @@ -106,34 +119,17 @@ }); }, + // Get the title getTitle: function() { var beforeTitle = this._options.tooltips.callbacks.beforeTitle.apply(this, arguments), title = this._options.tooltips.callbacks.title.apply(this, arguments), afterTitle = this._options.tooltips.callbacks.afterTitle.apply(this, arguments); 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; }, @@ -189,34 +185,16 @@ return helpers.isArray(lines) ? lines : [lines]; }, + // Get the footer and beforeFooter and afterFooter lines getFooter: function() { - var beforeFooter = this._options.tooltips.callbacks.beforeFooter.apply(this, arguments), - footer = this._options.tooltips.callbacks.footer.apply(this, arguments), - afterFooter = this._options.tooltips.callbacks.afterFooter.apply(this, arguments); + var beforeFooter = this._options.tooltips.callbacks.beforeFooter.apply(this, arguments); + var footer = this._options.tooltips.callbacks.footer.apply(this, arguments); + var afterFooter = this._options.tooltips.callbacks.afterFooter.apply(this, arguments); var lines = []; - - if (beforeFooter) { - if (helpers.isArray(beforeFooter)) { - 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); - } - } + lines = pushOrConcat(lines, beforeFooter); + lines = pushOrConcat(lines, footer); + lines = pushOrConcat(lines, afterFooter); return lines; },