|
| 1 | +module.exports = function(Chart) { |
| 2 | + var chartHelpers = Chart.helpers; |
| 3 | + |
| 4 | + var helpers = require('./helpers.js')(Chart); |
| 5 | + var events = require('./events.js')(Chart); |
| 6 | + |
| 7 | + var annotationTypes = Chart.Annotation.types; |
| 8 | + |
| 9 | + function setAfterDataLimitsHook(axisOptions) { |
| 10 | + helpers.decorate(axisOptions, 'afterDataLimits', function(previous, scale) { |
| 11 | + if (previous) previous(scale); |
| 12 | + helpers.adjustScaleRange(scale); |
| 13 | + }); |
| 14 | + } |
| 15 | + |
| 16 | + return { |
| 17 | + beforeInit: function(chartInstance) { |
| 18 | + var chartOptions = chartInstance.options; |
| 19 | + |
| 20 | + // Initialize chart instance plugin namespace |
| 21 | + var ns = chartInstance.annotation = { |
| 22 | + elements: {}, |
| 23 | + options: helpers.initConfig(chartOptions.annotation || {}), |
| 24 | + onDestroy: [], |
| 25 | + firstRun: true |
| 26 | + }; |
| 27 | + |
| 28 | + ns[ns.options.drawTime] = function(easingDecimal) { |
| 29 | + helpers.elements(chartInstance).forEach(function(element) { |
| 30 | + element.transition(easingDecimal).draw(); |
| 31 | + }); |
| 32 | + }; |
| 33 | + |
| 34 | + // Add the annotation scale adjuster to each scale's afterDataLimits hook |
| 35 | + chartInstance.ensureScalesHaveIDs(); |
| 36 | + chartHelpers.each(chartOptions.scales.xAxes, setAfterDataLimitsHook); |
| 37 | + chartHelpers.each(chartOptions.scales.yAxes, setAfterDataLimitsHook); |
| 38 | + }, |
| 39 | + beforeUpdate: function(chartInstance) { |
| 40 | + var ns = chartInstance.annotation; |
| 41 | + |
| 42 | + if (!ns.firstRun) { |
| 43 | + ns.options = helpers.initConfig(chartInstance.options.annotation || {}); |
| 44 | + } else { |
| 45 | + ns.firstRun = false; |
| 46 | + } |
| 47 | + |
| 48 | + var elementIds = []; |
| 49 | + |
| 50 | + // Add new elements, or update existing ones |
| 51 | + ns.options.annotations.forEach(function(annotation) { |
| 52 | + var id = annotation.id || helpers.objectId(); |
| 53 | + |
| 54 | + // No element with that ID exists, and it's a valid annotation type |
| 55 | + if (!ns.elements[id] && annotationTypes[annotation.type]) { |
| 56 | + var cls = annotationTypes[annotation.type]; |
| 57 | + var element = new cls({ |
| 58 | + id: id, |
| 59 | + options: annotation, |
| 60 | + chartInstance: chartInstance, |
| 61 | + }); |
| 62 | + element.initialize(); |
| 63 | + ns.elements[id] = element; |
| 64 | + annotation.id = id; |
| 65 | + elementIds.push(id); |
| 66 | + } else if (ns.elements[id]) { |
| 67 | + // Nothing to do for update, since the element config references |
| 68 | + // the same object that exists in the chart annotation config |
| 69 | + elementIds.push(id); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + // Delete removed elements |
| 74 | + Object.keys(ns.elements).forEach(function(id) { |
| 75 | + if (elementIds.indexOf(id) === -1) { |
| 76 | + ns.elements[id].destroy(); |
| 77 | + delete ns.elements[id]; |
| 78 | + } |
| 79 | + }); |
| 80 | + }, |
| 81 | + afterScaleUpdate: function(chartInstance) { |
| 82 | + helpers.elements(chartInstance).forEach(function(element) { |
| 83 | + element.configure(); |
| 84 | + }); |
| 85 | + }, |
| 86 | + beforeDatasetsDraw: function(chartInstance, easingDecimal) { |
| 87 | + (chartInstance.annotation.beforeDatasetsDraw || helpers.noop)(easingDecimal); |
| 88 | + }, |
| 89 | + afterDatasetsDraw: function(chartInstance, easingDecimal) { |
| 90 | + (chartInstance.annotation.afterDatasetsDraw || helpers.noop)(easingDecimal); |
| 91 | + }, |
| 92 | + afterDraw: function(chartInstance, easingDecimal) { |
| 93 | + (chartInstance.annotation.afterDraw || helpers.noop)(easingDecimal); |
| 94 | + }, |
| 95 | + afterInit: function(chartInstance) { |
| 96 | + // Detect and intercept events that happen on an annotation element |
| 97 | + var watchFor = chartInstance.annotation.options.events; |
| 98 | + if (watchFor.length > 0) { |
| 99 | + var canvas = chartInstance.chart.canvas; |
| 100 | + var eventHandler = events.dispatcher.bind(chartInstance); |
| 101 | + events.collapseHoverEvents(watchFor).forEach(function(eventName) { |
| 102 | + chartHelpers.addEvent(canvas, eventName, eventHandler); |
| 103 | + chartInstance.annotation.onDestroy.push(function() { |
| 104 | + chartHelpers.removeEvent(canvas, eventName, eventHandler); |
| 105 | + }); |
| 106 | + }); |
| 107 | + } |
| 108 | + }, |
| 109 | + destroy: function(chartInstance) { |
| 110 | + var deregisterers = chartInstance.annotation.onDestroy; |
| 111 | + while (deregisterers.length > 0) { |
| 112 | + deregisterers.pop()(); |
| 113 | + } |
| 114 | + } |
| 115 | + }; |
| 116 | +}; |
0 commit comments