]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Add a helper function to reduce code size
authoretimberg <evert.timberg@gmail.com>
Sun, 18 Oct 2015 20:31:18 +0000 (16:31 -0400)
committeretimberg <evert.timberg@gmail.com>
Sun, 18 Oct 2015 20:31:18 +0000 (16:31 -0400)
src/core/core.tooltip.js

index 9b227e1866480ad48c7f0c4a2ac9fe4fd56698e4..a45889d61224da2edf9d1c8a4dca59b18f49d23d 100644 (file)
                },
        };
 
+       // 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;
                        });
                },
 
+               // 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;
                },
 
                        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;
                },