diff --git a/bbtimeline.js b/bbtimeline.js index 6160e18..4cb6935 100644 --- a/bbtimeline.js +++ b/bbtimeline.js @@ -42,12 +42,16 @@ class BBTimeline { Colour: "#A6A6A6", Width: 1, }; + a.Events = []; a.StartDate = new Date(); a.ClientRectangle = a.getClientRectangle(); + a.Enabled = false; a.ctx = a.Container.getContext("2d"); a.ctx.canvas.width = a.Size.Width; a.ctx.canvas.height = a.Size.Height; + + a.initialiseComponents(); } Load(startDate) { @@ -55,8 +59,9 @@ class BBTimeline { a.StartDate = ((typeof(startDate) == "undefined") ? new Date() : startDate); - a.invalidate(); - a.initialiseComponents(); + a.invalidate(true); + + a.Enabled = true; } Clear(all) { @@ -64,46 +69,117 @@ class BBTimeline { if (all) { a.ctx.clearRect(0, 0, a.ctx.canvas.width, a.ctx.canvas.height); + + a.Events = []; + a.Enabled = false; } else { - const rect = a.getChartCoords(); + const rect = a.getClientCoords(); a.ctx.clearRect((rect.X1 + a.Axis.LineWidth), rect.Y1, (rect.X2 - rect.X1 - a.Axis.LineWidth), (rect.Y2 - rect.Y1 - a.Axis.LineWidth)); } } - initialiseComponents() { + FindDate(date) { const a = this; - const coords = a.getChartCoords(); - // Vertical highlight line - a.ctx.canvas.addEventListener('mousemove', function (e) { - a.Clear(false); - - if ((e.offsetX > (coords.X1 + a.Axis.LineWidth)) && (e.offsetX < coords.X2) && (e.offsetY >= coords.Y1) && (e.offsetY < (coords.Y2 - a.Axis.LineWidth))){ - a.drawVerticalLine(e.offsetX); + const points = a.getXAxis(); + for (let i=0; i= e.X1) && (x <= e.X2) && (y >= e.Y1) && (y <= e.Y2)){ + return a.Events[i]; + } + } + + return null; + } + + AddEvent(date, title, description, link) { + const a = this; + + a.Events.push({ + Date: date, + Title: title, + Description: description, + Link: link }); + a.invalidate(false); + } + + + initialiseComponents() { + const a = this; + const coords = a.getClientCoords(); + + // Vertical highlight line + // a.ctx.canvas.addEventListener('mousemove', function (e) { + // if (!a.Enabled) { + // return; + // } + + // a.invalidate(false); + + // if ((e.offsetX > (coords.X1 + a.Axis.LineWidth)) && (e.offsetX < coords.X2) && (e.offsetY >= coords.Y1) && (e.offsetY < (coords.Y2 - a.Axis.LineWidth))){ + // a.drawVerticalLine(e.offsetX); + // } + // }); + a.ctx.canvas.addEventListener('mousedown', function (e) { - console.log('mousedown'); - console.log(e); + if (!a.Enabled) { + return; + } + + var event = a.FindEventByCoords(e.offsetX, e.offsetY); + + console.log(event); + + // console.log(e); }); } - invalidate() { + invalidate(all) { const a = this; - a.Clear(true); + a.Clear(all); - a.drawAxis(); - a.drawXAxis(); - a.drawXAxisLabels(); + if (all) { + a.drawAxis(); + a.drawXAxis(); + a.drawXAxisLabels(); + } else { + const coords = a.getClientCoords(); + coords.Y1 += a.Marker.Width; + + for (let i=0; i + +

@@ -37,7 +39,7 @@ canvas { border-width: 1px; border-color: #000000; width: 100%; - height: 400px; + height: 300px; padding: 0; margin: 0; } @@ -49,15 +51,30 @@ canvas { var timeline1 = new BBTimeline("myCanvas"); timeline1.Load(); +timeline1.AddEvent("2023-10-05", "title", "description", "link"); +timeline1.AddEvent("2023-10-06", "title", "description", "link"); +timeline1.AddEvent("2023-10-16", "title", "description", "link"); function Clear() { - timeline1.Clear(); + timeline1.Clear(true); } +function LoadToday() +{ + timeline1.Load(new Date()); +} + +function LoadNext() +{ + let date = timeline1.StartDate; + date.setMonth(date.getMonth() + 1); + + timeline1.Load(date); +}