5 lines
12 KiB
JavaScript
5 lines
12 KiB
JavaScript
|
/**
|
||
|
* BBTimeline
|
||
|
* @version v0.1.0.076 (2023/09/30 1432)
|
||
|
*/
|
||
|
class BBTimeline{constructor(el){const a=this;a.Container=document.getElementById(el),a.DateParsePattern="yyyy-MM-dd",a.Debug=!1,a.Padding={Left:20,Top:20,Right:20,Bottom:0},a.Size={W:a.Container.innerWidth||a.Container.clientWidth,H:a.Container.innerHeight||a.Container.clientHeight},a.ctx=a.Container.getContext("2d"),a.ctx.canvas.width=a.Size.W,a.ctx.canvas.height=a.Size.H,a.Axis={LineColour1:"#CFCFCF",LineWidth:1,Font:"8pt Arial",LabelColour:"#000000",LabelSpacing:6,X:{NoPartPerDay:4,HourLineSpace:6,HourLineHeight:10,HourLineColour:"#A6A6A6",DayLineHeight:20,DayLineColour:"#282828"}},a.Marker={BorderColour:"#3A5D9C",BorderWidth:2,BackColour:"#D4DEEF",Width:10,ForeColour:"#3A5D9C",Font:"9pt Arial",CollisionMargin:8},a.HighlightLine={Colour:"#A6A6A6",Width:1},a.Events=[],a.StartDate=a.DateToString(new Date,a.DateParsePattern),a.ShowDate=a.StartDate,a.GraphRectangle=a.calcGraphRectangle(),a.Enabled=!1,a.initialiseComponents()}AddEvent(date,label,options){const a=this,_options=Object.assign(a.GenerateEventItem(),options);let event=a.FindEvent(date);null==event&&(a.Events.push(a.GenerateEvent(date)),event=a.FindEvent(date)),null!=label&&(event.Label=label),event.Events.push(_options)}CalcEndDate(){const a=this,calcdays=Math.floor(a.GraphRectangle.W/(a.Axis.X.NoPartPerDay*a.Axis.X.HourLineSpace));let date=a.ConvertToDate(a.ShowDate);return date.setDate(date.getDate()+calcdays),date.setDate(date.getDate()-1),a.DateToString(date,a.DateParsePattern)}Clear(){const a=this;a.ctx.clearRect(0,0,a.ctx.canvas.width,a.ctx.canvas.height),a.StartDate=a.DateToString(new Date,a.DateParsePattern),a.ShowDate=a.StartDate,a.Enabled=!1,a.Events=[]}DeleteMarker(date){const a=this;for(let i=0;i<a.Events.length;i++)a.Events[i].Date==date&&a.Events.splice(i,1)}FindDatePosition(date){const a=undefined,points=this.getXAxis();for(let i=0;i<points.length;i++)if(points[i].Date==date)return points[i];return null}FindVisibleEvents(){const a=this;let result=[];const availableX=undefined;return a.getXAxis().forEach((function(e){const event=a.FindEvent(e.Date);null!=event&&(event.Position.X=e.X,result.push(event))})),result}FindEvent(date){const a=this;for(let i=0;i<a.Events.length;i++)if(a.Events[i].Date==date)return a.Events[i];return null}FindEventsByCoords(x,y){const a=this;for(let i=0;i<a.Events.length;i++){const e=a.Events[i].HitBox;if(null==a.Events[i].HitBox)continue;const x2=e.X+e.W,y2=e.Y+e.H;if(x>=e.X&&x<=x2&&y>=e.Y&&y<=y2)return a.Events[i]}return null}Load(startDate){const a=this;a.StartDate=startDate,a.Show(startDate)}Show(date){const a=this;a.ConvertToDate(date)<a.ConvertToDate(a.StartDate)&&(date=a.StartDate),a.ShowDate=date,a.Enabled=!0,a.Invalidate(!0,!0)}ShowNext(){const a=this;let date=a.ConvertToDate(a.ShowDate);date.setMonth(date.getMonth()+1),a.Show(a.DateToString(date,a.DateParsePattern))}ShowPrevious(){const a=this;let date=a.ConvertToDate(a.ShowDate);date.setMonth(date.getMonth()-1),a.Show(a.DateToString(date,a.DateParsePattern))}UpdateLabel(date,label){const a=this;let event=a.FindEvent(date);null!=event&&(event.Label=label,a.Invalidate(!1,!0))}UpdateMarker(date,borderColour,backColour){const a=this;let event=a.FindEvent(date);null!=event&&(event.BorderColour=borderColour,event.BackColour=backColour,a.Invalidate(!1,!0))}initialiseComponents(){const a=this;a.ctx.canvas.addEventListener("mousedown",(function(e){if(a.Enabled){var event=a.FindEventsByCoords(e.offsetX,e.offsetY);null!=event&&(a.Debug&&console.log(event),a.OnMouseDown(this,e,event))}})),a.ctx.canvas.addEventListener("click",(function(e){if(a.Enabled){var event=a.FindEventsByCoords(e.offsetX,e.offsetY);null!=event&&(a.Debug&&console.log(event),a.OnClick(this,e,event))}}))}Invalidate(redrawAxis,redrawMarkers){const a=this;if(redrawAxis&&(a.ctx.clearRect(0,0,a.ctx.canvas.width,a.ctx.canvas.height),a.drawAxis(),a.drawXAxis(),a.drawXAxisLabels()),redrawMarkers){a.clearChart();const startPosY=a.GraphRectangle.Y+a.Marker.Width,visibleEvents=a.FindVisibleEvents();a.Debug&&console.log(visibleEvents),visibleEvents.forEach((function(e,i){let posY=a.calcMarkerPosition(e.Position.X,
|