All files / src SunorhcTimeline.ts

0% Statements 0/821
0% Branches 0/1
0% Functions 0/1
0% Lines 0/821

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
import * as packageJson from '../package.json'
import './styles/main.sass'
import { Sidebars, Rulers, Alignment, EventNode, TimelineOptions, TimelineBaseClass, Measures, RulerOptions, ZoomScaleOptions } from './types/definitions'
import { isObject, isEmptyObject, getAtts, setAtts, setStyles, setContent, deepMergeObjects, deepCloneObject, cloneObject, fetchData, deserialize, parseDateTime, convertToPixels, isElement, getRect, getParticles, getStartDatetime, getEndDatetime, createLandmarkElement, createSidebar, createSidebarItems, createRuler, createRulerItems, optimizeEventNode, placeEventNodes, showPresentTimeMarker, getDuplicateValues, validatorEventNode, validateTimelineOptions, saveToStorage, loadFromStorage, watcher, dragScroll, doAlignment, wheelScroll, dblclickZoom, onHoverTooltip } from './utils'
import { LoggerService } from './utils/logger'

// Get Env
const isDev = process.env.NODE_ENV === 'development'
const defaultLogFilePath = isDev ? 'dev-log.log' : 'prod-log.log'
//console.log('isDev:', isDev, process.env.NODE_ENV, defaultLogFilePath)

// Set default values as options initialization
const defaultOptions: TimelineOptions = {
  start: 'currently',
  end: 'auto',
  timezone: 'UTC',
  scale: 'day',
  file: null,
  header: { display: false },
  footer: { display: false },
  sidebar: {
    placement: 'both',
    sticky: false,
    overlay: false,
    width: '150px',
    itemHeight: '80px',
    items: [],
  },
  ruler: {
    placement: 'both',
    truncateLowers: false,
    firstDayOfWeek: 0,
    minGrainWidth: '48px',
    top: {
      rows: [ 'day' ],
      //rowHeight: '24px',
      //fontSize: '16px',
    },
    bottom: {
      rows: [ 'day' ],
      //rowHeight: '24px',
      //fontSize: '16px',
    },
  },
  events: '',
  layout: {
    //elevation: 0,
    //outlined: 'inside',
    //outlineCorner: 'rounded',
    //outlineStyle: 'solid',
    hideScrollbar: true,
    eventsBackground: 'plaid',
    width: 'auto',
    height: 'auto',
  },
  effects: {
    presentTime: false,
    defaultAlignment: 'latest',
    cacheExpiration: 'always',
    hoverEvent: true,
    onClickEvent: 'normal',
  },
  theme: {
    name: 'default',
  },
  useStorage: 'sessionStorage',
  zoomable: false,
  debug: isDev,
}

export class Timeline implements TimelineBaseClass {
  // Application Version
  public static readonly VERSION: string = packageJson.version

  readonly elementId: string
  targetElement: HTMLDivElement
  fragmentNode: DocumentFragment
  options = defaultOptions

  protected inputOptions: Partial<TimelineOptions>
  protected loadOptions = null
  protected measurements = {}
  protected eventNodes: object[] = []

  private logger: LoggerService

  /**
   * The constructor cannot be called directly from any methods other than self own.
   * 
   * @param {string} elementId - The ID of the HTML element.
   * @param {TimelineOptions} inputOptions - Optional timeline options.
   * @private
   */
  private constructor(elementId: string, inputOptions?: Partial<TimelineOptions>) {
    this.elementId = elementId
    this.targetElement = document.querySelector<HTMLDivElement>(`#${elementId}`)!
    this.fragmentNode  = document.createDocumentFragment()
    this.inputOptions  = inputOptions as Partial<TimelineOptions>

    const loggerOptions = {
      debug: (Object.prototype.hasOwnProperty.call(this.inputOptions, 'debug') ? this.inputOptions.debug : isDev) || false,
      logFilePath: defaultLogFilePath
    }
    this.logger = new LoggerService(loggerOptions)
  }

  /**
   * Factory method for instantiating this class.
   * 
   * @param {string} elementId - The ID of the HTML element.
   * @param {TimelineOptions} inputOptions - Optional timeline options.
   * @returns {Promise<SunorhcTimeline>} A Promise that resolves to an instance of SunorhcTimeline.
   * @static
   */
  static async create(elementId: string, inputOptions?: TimelineOptions): Promise<Timeline> {
    const instance = new Timeline(elementId, inputOptions)
    try {
      instance.options = await Promise.resolve(instance.initOptions())
      instance.measurements = await Promise.resolve(instance.initMeasure())

      // Render timeline component.
      await Promise.resolve(instance.render())

      // Here is the "initialized" hook point immediately after initialization.
      instance.initialized()

      // Load event nodes, cache them after parsing, and place them on the DOM.
      await Promise.resolve(instance.serveEventNodes())

      // Registration of various event listeners.
      instance.registerEventListeners()

      // Start monitoring rendering mode.
      instance.runChangeThemeWatcher()

    } catch (error) {
      console.error('Failed to create timeline:', error)
      // Hide the dispatcher element itself for injecting the timeline.
      setStyles(instance.targetElement, { display: 'none' })
    } finally {
      // Add an instance to the global Window object.
      if (!(window as Window).hasOwnProperty('SunorhcTimelineInstances')) {
        window.SunorhcTimelineInstances = {}
      }
      window.SunorhcTimelineInstances[instance.elementId] = instance
    }
    return instance
  }

  /**
   * Initializes the timeline options.
   * 
   * @returns {Promise<Def.TimelineOptions>} A Promise that resolves to the timeline options.
   * @private
   */
  private async initOptions(): Promise<TimelineOptions> {
    // Options initialization
    let fixedOptions = deepMergeObjects({}, this.options)

    // First, extract the options from the data-options attribute.
    if (this.targetElement.dataset.options) {
      const datasetOptions = deserialize(this.targetElement.dataset.options)
      if (isObject(datasetOptions)) {
        const validOptions = validateTimelineOptions(datasetOptions)
        fixedOptions = deepMergeObjects(fixedOptions, validOptions)
      } else if (typeof datasetOptions === 'string') {
        fixedOptions = deepMergeObjects(fixedOptions, { file: datasetOptions })
      }
    }

    // Second, it overrides the options passed in the constructor arguments.
    if (this.inputOptions && isObject(this.inputOptions) && !isEmptyObject(this.inputOptions)) {
      fixedOptions = deepMergeObjects(fixedOptions, this.inputOptions)
      if (this.inputOptions.start instanceof Date) {
        fixedOptions.start = this.inputOptions.start
      }
      if (this.inputOptions.end instanceof Date) {
        fixedOptions.end = this.inputOptions.end
      }
    }

    // Finally, if an external configuration file is specified, it is fetched and overridden.
    if (fixedOptions.hasOwnProperty('file') && typeof fixedOptions.file === 'string' && fixedOptions.file !== '') {
      const result = await fetchData({ url: fixedOptions.file })
      fixedOptions = deepMergeObjects(fixedOptions, validateTimelineOptions<TimelineOptions>(result))
      this.logger.log('Fetched options file:', result, validateTimelineOptions<TimelineOptions>(result), fixedOptions)
    }

    this.logger.info(`%cSnorch.Timeline ver.${Timeline.VERSION} is booting now...`, 'color: #0284c7; font-weight: 700;')
    return fixedOptions
  }

  /**
   * Getter to retrieve the current options.
   * @returns {TimelineOptions} The current timeline options.
   */
  getOptions(toClone: boolean = false): TimelineOptions {
    return toClone ? deepCloneObject(this.options) : this.options
  }

  /**
   * Measure the rendering area and calculate.
   * @returns {Promise<Error | Measures>} A Promise that resolves options for rendering or rejects as error handling.
   * @private
   */
  private async initMeasure(): Promise<Error | Measures> {
    const timelineContainer = document.createElement('div')
    timelineContainer.classList.add('sunorhc-timeline-container', 'preparing')
    const containerAtts: Record<string, string> = {
      'data-timeline-outlined':  this.options.layout && /^(outside|both)$/.test(this.options.layout.outlined!) ? 'true' : 'false'
    }
    if (!!this.options.layout.outlineCorner) {
      containerAtts['data-timeline-linestyle'] = this.options.layout.outlineCorner
    }
    if (!!this.options.layout.elevation) {
      containerAtts['data-timeline-elevation'] = this.options.layout.elevation.toString()
    }
    if (!!this.options.layout.rtl) {
      containerAtts['dir'] = this.options.layout.rtl ? 'rtl' : 'ltr'
    }
    setAtts(timelineContainer, containerAtts)
    const containerWidth  = this.options.layout.width || 'auto'
    const containerHeight = this.options.layout.height || 'auto'
    const containerStyles: string[] = [
      'width: ' + (typeof containerWidth  === 'number' ? `${containerWidth}px` : containerWidth),
      'height: ' + (typeof containerHeight === 'number' ? `${containerHeight}px` : containerHeight),
    ]
    if (!!this.options.layout.outlineStyle) {
      containerStyles.push(`--outline-style: ${this.options.layout.outlineStyle}`)
    }
    //console.log('initMeasure:', containerAtts, containerStyles)
    setStyles(timelineContainer, containerStyles.join('; '))
    this.fragmentNode.append(timelineContainer)
    this.targetElement.append(this.fragmentNode)

    try {
      // Parse start date
      const optionStartDate = getStartDatetime(this.options.start, this.options.timezone, this.options.scale)
      if (optionStartDate instanceof Error) {
        throw optionStartDate
      }
      // Parse end date
      const optionEndDate = getEndDatetime(this.options.end, this.options.timezone, this.options.scale, optionStartDate)
      if (optionEndDate instanceof Error) {
        throw optionEndDate
      }
      const monthNames = this.options.ruler.filters?.monthNames ?? undefined
      const dayNames = this.options.ruler.filters?.dayNames ?? undefined
      const firstDayOfWeek = this.options.ruler.firstDayOfWeek || 0
      const startDate = parseDateTime(optionStartDate, this.options.timezone, monthNames, dayNames, firstDayOfWeek)
      const endDate   = parseDateTime(optionEndDate, this.options.timezone, monthNames, dayNames, firstDayOfWeek)
      // Calculate the maximum granularity number of the ruler from the start and end range based on the scale
      const maxParticles = getParticles(optionStartDate, optionEndDate)
      const scaleParticle = getParticles(optionStartDate, optionEndDate, this.options.scale) as number

      const targetRect = getRect(this.targetElement)
      const containerRect = cloneObject(getRect(timelineContainer)) as unknown as DOMRect

      const optionSidebarWidth = convertToPixels(this.options.sidebar.width)
      const optionSidebarItemHeight = convertToPixels(this.options.sidebar.itemHeight)
      const rulerTopRows = this.options.ruler.top?.rows?.length || 0
      const rulerBottomRows = this.options.ruler.bottom?.rows?.length || 0
      const rulerTopRowHeight = convertToPixels(this.options.ruler.top?.rowHeight) || 24
      const rulerBottomRowHeight = convertToPixels(this.options.ruler.bottom?.rowHeight) || 24
      let preMeasures = {
        // Datetime
        startDate: startDate,
        endDate: endDate,
        particles: maxParticles,

        injectTo: targetRect,
        container: containerRect,
        // Container layouts
        containerWidth: containerRect.width,// = targetRect.width
        containerHeight: containerRect.height,// = targetRect.height; includes header and footer height.
        containerTop: containerRect.top,// = targetRect.top
        containerLeft: containerRect.left,// = targetRect.left
        // Body layouts
        bodyHeight: undefined,// unknown at the time of execution of this process.
        // Sidebar layouts
        sidebarWidth: optionSidebarWidth,
        sidebarHeight: optionSidebarItemHeight * this.options.sidebar.items.length,// = sidebar actual height (full items height)
        sidebarVisibleHeight: undefined,// unknown at the time of execution of this process.
        sidebarOffsetTop: /^(both|top)/i.test(this.options.ruler.placement) ? (rulerTopRows * rulerTopRowHeight) : 0,
        sidebarOffsetBottom: /^(both|bottom)/i.test(this.options.ruler.placement) ? (rulerBottomRows * rulerBottomRowHeight) : 0,
        sidebarItemHeight: optionSidebarItemHeight,// one item height
        sidebarItems: this.options.sidebar.items.length,
        // Ruler layouts
        rulerVisibleWidth: containerRect.width - (/^both$/i.test(this.options.sidebar.placement)
          ? optionSidebarWidth * 2
          : (/^(left|right)$/i.test(this.options.sidebar.placement) ? optionSidebarWidth : 0)),
        rulerActualWidth: convertToPixels(this.options.ruler.minGrainWidth) * scaleParticle,
        rulerTopRows: (this.options.ruler.top?.rows?.length || 0),
        rulerBottomRows: (this.options.ruler.bottom?.rows?.length || 0),
        rulerTopHeight: (this.options.ruler.top?.rows?.length || 0) * rulerTopRowHeight,
        rulerBottomHeight: (this.options.ruler.bottom?.rows?.length || 0) * rulerBottomRowHeight,
        rulerMaxCols: scaleParticle,
        /*
        test: {
          elm: this.targetElement,
          frag: this.fragmentNode,
          date: new Date('2024-05-13T15:00:00Z'),
          reg: new RegExp(/^some$/, 'i'),
        },*/
      } as Measures
      this.logger.log('initMeasure:', this.elementId, this.options, preMeasures)
      if (preMeasures.sidebarItems == 0 || preMeasures.rulerTopRows + preMeasures.rulerBottomRows == 0) {
        throw new Error('Missing several configurations to generate timeline.')
      }
      return Promise.resolve(preMeasures)
    } catch (error) {
      //console.error('Error:', error)
      return Promise.reject(error as unknown as string)
    }
  }

  /**
   * Render the timeline component as instance of this library.
   * @returns {Promise<void>}
   * @private
   */
  private async render(): Promise<void> {
    const referMeasurements = deepCloneObject(this.measurements) as Measures
    const filterNodesGuard = (nodes: any): Node[] => nodes.filter((e: Node): e is HTMLElement => !!e)
    let appendNodes = null
    //console.log('render:', referMeasurements )

    const header: HTMLDivElement = createLandmarkElement('header', this.options.header) as HTMLDivElement

    // Generate timeline body
    const body: HTMLDivElement = document.createElement('div')
    body.classList.add('sunorhc-timeline-body')
    setAtts(body, {
      'data-timeline-outlined':  this.options.layout && /^(inside|both)$/.test(this.options.layout.outlined!) ? 'true' : 'false',
      'data-timeline-linestyle': this.options.layout ? this.options.layout.outlineCorner! : '',
    })
    if (this.options.layout && this.options.layout.outlineStyle) {
      setStyles(body, `--outline-style: ${this.options.layout.outlineStyle}`)
    }

    // Generate sidebar containers
    //const sidebars: { [key: string]?: HTMLDivElement/* | undefined*/ } = {
    const sidebars: Sidebars = {
      left:  /^(both|left)$/i.test(this.options.sidebar.placement) ? createSidebar('left', referMeasurements) as HTMLDivElement : undefined,
      right: /^(both|right)$/i.test(this.options.sidebar.placement) ? createSidebar('right', referMeasurements) as HTMLDivElement : undefined,
    }
    // Add sidebar items
    for (const key in sidebars) {
      if (sidebars.hasOwnProperty(key) && !!sidebars[key as keyof Sidebars] && isElement(sidebars[key as keyof Sidebars])) {
        /*
        Util.setAtts(sidebars[key]!, {
          //'data-sidebar-sticky':  this.options.sidebar.sticky ? 'fixed' : 'static',// no use
          //'data-sidebar-overlay': this.options.sidebar.overlay ? 'true' : 'false',// no use
          //'data-sidebar-oveflow': 'false',// Disabled at the time of this process
        })
        */
        const sidebarItems = createSidebarItems(this.options.sidebar)
        // Adjust outlines
        if (this.options.ruler.placement === 'bottom') {
          if (this.options.layout.outlined === 'inside') {
            setStyles(sidebarItems, { borderTop: 'none' })
          }
        }
        sidebars[key as keyof Sidebars]!.append(sidebarItems)
      }
    }

    // Generate timeline main canvas
    const main: HTMLDivElement = document.createElement('div')
    main.classList.add('sunorhc-timeline-main-canvas')
    if (this.options.layout.hideScrollbar!) {
      setAtts(main, { 'data-hide-scrollbar': 'true' })
    }
    const adjustMainStyles: string[] = [`--main-visible-width: ${referMeasurements.rulerVisibleWidth}px`]
    // Adjust outlines
    if (this.options.ruler.placement === 'bottom') {
      if (this.options.layout.outlined === 'both') {
        adjustMainStyles.push(`border-top: ${this.options.layout?.outlineStyle || 'solid'} 1px var(--border-color)`)// #9ca3cf
      }
    }
    setStyles(main, adjustMainStyles.join('; '))

    // Generate ruler container
    //const rulers: { [key: string]: HTMLDivElement | undefined } = {
    const rulers: Rulers = {
      top:    /^(both|top)$/i.test(this.options.ruler.placement) ? createRuler('top', referMeasurements) as HTMLDivElement : undefined,
      bottom: /^(both|bottom)$/i.test(this.options.ruler.placement) ? createRuler('bottom', referMeasurements) as HTMLDivElement : undefined,
    }
    // Add ruler items
    for (const key in rulers) {
      if (rulers.hasOwnProperty(key) && !!rulers[key as keyof Rulers] && isElement(rulers[key as keyof Rulers])) {
        //Util.setAtts(rulers[key]!, {})
        if (this.options.ruler.hasOwnProperty(key)) {
          const rulerRows = key === 'top' ? this.options.ruler.top!.rows : this.options.ruler.bottom!.rows
          rulerRows.forEach((val: string, idx: number) => {
            const rulerOptions = {
              globalScale: this.options.scale,
              timezone: this.options.timezone,
              scale: val,
              order: idx + 1,
              minGrainWidth: convertToPixels(this.options.ruler.minGrainWidth),
              placement: key,
              firstDayOfWeek: this.options.ruler.firstDayOfWeek,
              config: key === 'top' ? this.options.ruler.top : this.options.ruler.bottom,
              filters: this.options.ruler.filters,
              maxCols: referMeasurements.rulerMaxCols,
              //particles: referMeasurements.particles,
              startDate: referMeasurements.startDate,
              endDate: referMeasurements.endDate,
            } as RulerOptions
            rulers[key as keyof Rulers]!.append(createRulerItems(rulerOptions))
            if (!!this.options.ruler.filters && this.options.ruler.filters.dayBackgroundColor || false) {
              rulers[key as keyof Rulers]!.classList.add('force-day-bg-color')
            } 
          })
        }
      }
    }

    // Generate nodes container
    const nodes: HTMLDivElement = document.createElement('div')
    nodes.classList.add('sunorhc-timeline-nodes')
    setAtts(nodes, {
      'data-grid-width': convertToPixels(this.options.ruler.minGrainWidth).toString(),
      'data-grid-height': referMeasurements.sidebarItemHeight.toString(),
      'data-grid-max-cols': referMeasurements.rulerMaxCols.toString(),
      'data-grid-max-rows': referMeasurements.sidebarItems.toString(),
      'data-range-start': referMeasurements.startDate.ts.toString(),
      'data-range-end': referMeasurements.endDate.ts.toString(),
      //'data-grid-vertical-divs': 'unkown',
      //'data-overflow-width': 'auto',
      //'data-overflow-height': 'auto',
      'data-background': this.options.layout.eventsBackground || 'plaid',
    })
    //--canvas-bg-odd: rgba(156, 163, 175, 0.1);
    //--canvas-bg-even: transparent;
    //--canvas-border: rgba(209, 213, 219, 0.5);
    let nodesContainerHeight = /^(both|top)$/.test(this.options.ruler.placement) ? referMeasurements.rulerTopHeight : 0
    nodesContainerHeight += /^(both|bottom)$/.test(this.options.ruler.placement) ? referMeasurements.rulerBottomHeight : 0
    const canvasGridWidth  = Math.floor(convertToPixels(this.options.ruler.minGrainWidth))// - (0.016129 * (1 - Math.exp(-referMeasurements.rulerMaxCols / 100000)))) * 100000) / 100000
    const canvasGridHeight = Math.floor(referMeasurements.sidebarItemHeight)// + (0.026 * (1 + Math.exp(referMeasurements.sidebarItems / 100000)))) * 100000) / 100000
    const canvasGridInterval = -1//-0.5 * (1 - Math.exp(-referMeasurements.rulerMaxCols / 500))
    setStyles(nodes, `\
      --canvas-grid-width: ${canvasGridWidth}px; \
      --canvas-grid-height: ${canvasGridHeight}px; \
      --canvas-grid-cols: ${referMeasurements.rulerMaxCols}; \
      --canvas-grid-rows: ${referMeasurements.sidebarItems}; \
      --canvas-grid-rows-half: ${referMeasurements.sidebarItems / 2}; \
      --canvas-grid-interval: ${canvasGridInterval}px; \
      width: ${referMeasurements.rulerActualWidth}px; \
      height: calc(100% - ${nodesContainerHeight}px); \
    `)

    appendNodes = filterNodesGuard([ rulers.top, nodes, rulers.bottom ])
    main.append(...appendNodes)

    const footer: HTMLDivElement = createLandmarkElement('footer', this.options.footer) as HTMLDivElement

    appendNodes = filterNodesGuard([ sidebars.left, main, sidebars.right ])
    body.append(...appendNodes)

    //console.log('render:', header, body, rulers, footer)
    appendNodes = filterNodesGuard([ header, body, footer ])
    this.fragmentNode.append(...appendNodes)
    const timelineContainer: HTMLDivElement = this.targetElement.querySelector('.sunorhc-timeline-container')!
    timelineContainer.append(this.fragmentNode)

    // Final display adjustments to the layout
    const prerenderBodyHeight = getRect(body, 'height') as number
    const sidebarActualHeight = referMeasurements.sidebarHeight + (referMeasurements.sidebarOffsetTop + referMeasurements.sidebarOffsetBottom)// = fixed actual body height
    //console.log('!:', prerenderBodyHeight, sidebarActualHeight)
    const fixedBodyHeight = prerenderBodyHeight < sidebarActualHeight
      ? Math.floor(prerenderBodyHeight) // Timeline body full height is smaller than sidebar actual full height, therefore sidebar is in overflowed.
      : sidebarActualHeight // Timeline body full height is greater than the sidebar actual full height therefore will be a margin at the bottom of the body, so should adjust the body full height to the sidebar actual full height.
    referMeasurements.bodyHeight = fixedBodyHeight
    referMeasurements.sidebarVisibleHeight = fixedBodyHeight - (referMeasurements.sidebarOffsetTop + referMeasurements.sidebarOffsetBottom)
    const nodesContainer = timelineContainer.querySelector('.sunorhc-timeline-nodes') as HTMLDivElement
    const timelineRulers: HTMLDivElement[] = Array.from(timelineContainer.querySelectorAll('.sunorhc-timeline-ruler'))
    let nodesContainerStyles: string[]
    //console.log('!!!:', referMeasurements)
    if (referMeasurements.sidebarVisibleHeight < referMeasurements.sidebarHeight) {
      const updateStyles: string[] = [
        `--sidebar-max-width: ${referMeasurements.sidebarWidth}px`,
        `--sidebar-max-height: ${referMeasurements.sidebarVisibleHeight}px`,
        `margin-top: ${referMeasurements.sidebarOffsetTop}px`,
        `margin-bottom: ${referMeasurements.sidebarOffsetBottom}px`,
      ]
      setAtts(Array.from(timelineContainer.querySelectorAll('.sunorhc-timeline-sidebar')), {
        'data-sidebar-overflow': 'true',
        'style': updateStyles.join('; '),
      })
      nodesContainerStyles = (getAtts(nodesContainer, 'style')! as string).split(';').filter(e => e !== '').map(e => {
        const style = e.trim()
        return /^height\:/.test(style) ? `height: ${referMeasurements.sidebarHeight}px` : style
      })
      //console.log(nodesContainerStyles)
      timelineRulers.forEach((ruler: HTMLDivElement) => {
        const position = getAtts(ruler, 'data-ruler-position')
        //Util.setStyles(ruler, `position: sticky; ${position}: 0; z-index: 25; background-color: #ffffff;`)// dark-theme: #030712
        setAtts(ruler, { 'data-overlay-sticky': position } as { [key: string]: string })
      })
      setStyles(nodesContainer, nodesContainerStyles.join('; '))
    } else {
      nodesContainerStyles = (getAtts(nodesContainer, 'style')! as string).split(';').filter(e => e !== '').map(e => {
        const style = e.trim()
        return /^height\:/.test(style) ? `height: ${referMeasurements.sidebarHeight + 2}px` : style
      })
      //console.log(nodesContainerStyles)
      setStyles(nodesContainer, nodesContainerStyles.join('; '))
      const timelineBody: HTMLDivElement = timelineContainer.querySelector('.sunorhc-timeline-body')!
      let stackHeight: number = 0
      timelineRulers.forEach((ruler: HTMLDivElement) => {
        const thisHeight = Math.ceil(getRect(ruler, 'height') as number)
        setStyles(ruler, { height: `${thisHeight}px` })
        stackHeight += thisHeight
      })
      //console.log(stackHeight, stackHeight + referMeasurements.sidebarHeight)
      setStyles(timelineBody, { height: `${Math.floor(stackHeight + referMeasurements.sidebarHeight)}px` })
      /*
      if (referMeasurements.rulerTopRows > 6) {
        const renderedRulerTopHeight = Util.getRect(timelineRulers.filter(elm => elm.dataset.rulerPosition === 'top')![0], 'height') as number
        const timelineSidebars: HTMLDivElement[] = Array.from(timelineContainer.querySelectorAll('.sunorhc-timeline-sidebar'))
        timelineSidebars.forEach((sidebar: HTMLDivElement) => {
          const adjustAmount: number = renderedRulerTopHeight - Util.convertToPixels(sidebar.style.marginTop)
          console.log('adjusting:', renderedRulerTopHeight, adjustAmount, sidebar.style.marginTop, window.getComputedStyle(sidebar).getPropertyValue('margin-top'))
          const sidebarStyles = Util.getAtts(sidebar, 'style')! as string
          Util.setAtts(sidebar, { style: `--sidebar-adjust-top: ${adjustAmount}px; ${sidebarStyles}` })
        })
      }
      */
    }
    const renderedRulerTopHeight = getRect(timelineRulers.filter(elm => elm.dataset.rulerPosition === 'top')![0], 'height') as number
    const timelineSidebars: HTMLDivElement[] = Array.from(timelineContainer.querySelectorAll('.sunorhc-timeline-sidebar'))
    timelineSidebars.forEach((sidebar: HTMLDivElement) => {
      const adjustAmount: number = renderedRulerTopHeight - convertToPixels(sidebar.style.marginTop)
      //console.log('adjusting:', renderedRulerTopHeight, adjustAmount, sidebar.style.marginTop, window.getComputedStyle(sidebar).getPropertyValue('margin-top'))
      const sidebarStyles = getAtts(sidebar, 'style')! as string
      setAtts(sidebar, { style: `--sidebar-adjust-top: ${adjustAmount}px; ${sidebarStyles}` })
    })
    //console.log(Util.getRect(body, 'height'), referMeasurements, this.measurements)

    // All elements have been completely rendered.
    this.measurements = referMeasurements
    timelineContainer.classList.remove('preparing')
  }

  /**
   * Optimizes the event node data retrieved and serve on timeline with saving as cache to web storage.
   * If given true to argument will force clearing of any existing cache before retrieving the event node data.
   * @param {boolean} forceClearCache 
   * @returns {Promise<void>}
   * @private
   */
  private async serveEventNodes(forceClearCache: boolean = false): Promise<void> {
    const cacheKey = `${this.elementId}:cachedEvents`
    let result: any = null
    let eventIds: string[] = []
    if (forceClearCache) {
      // This process is not yet implemented.
      this.logger.log('Force clear cache!')

    }
    try {
      if (typeof this.options.events === 'string' && this.options.events !== '') {
        if (this.options.events === cacheKey) {
          // Read event nodes data from cached storage.
          let expiredTime: number = -1
          if (/^\d+$/i.test(String(this.options.effects.cacheExpiration))) {
            expiredTime = Number(this.options.effects.cacheExpiration)
          } else if (/^always$/i.test(String(this.options.effects.cacheExpiration))) {
            expiredTime = 0
          }
          result = loadFromStorage(this.options.useStorage, cacheKey, expiredTime)
          if (result !== null && Array.isArray(result) && result.length !== 0) {
            this.logger.log('Loaded cached event data:', result)
            this.options.events = result
          }
        }
        if (!isObject(this.options.events) && this.options.events !== cacheKey) {
          // Fetch data from the specified destination.
          result = await fetchData({ url: this.options.events as string })
          if (!result || !Array.isArray(result) || result.length == 0) {
            throw new Error('The fetched data does not contain any event nodes.')
          }
          this.logger.log('Fetched external event data:', result)
          this.options.events = result
        }
      } else if (Array.isArray(this.options.events) && this.options.events.length == 0) {
        throw new Error('No enabled events within the current timeline display range.')
      }

      if (!isEmptyObject(this.options.events)) {
        // First, optimize the original event data stored in `options.events`.
        this.eventNodes = [];
        (this.options.events as object[]).forEach((item: any, index: number) => {
          let checkedEventNode = validateTimelineOptions<EventNode>(item, validatorEventNode) as Partial<EventNode>
          if (checkedEventNode.hasOwnProperty('eventId')) {
            eventIds.push(checkedEventNode.eventId!)
          } else {
            const newEventId = `EventNode:${String(index + 1)}`
            checkedEventNode.eventId = newEventId
            eventIds.push(newEventId)
          }
          if (checkedEventNode.hasOwnProperty('start')) {
            // Events without a start datetime is disabled.
            // Also, event with invalid start datetime that cannot be parsed are invalid too.
            const monthNames = this.options.ruler.filters?.monthNames ?? undefined
            const dayNames = this.options.ruler.filters?.dayNames ?? undefined
            const firstDayOfWeek = this.options.ruler.firstDayOfWeek || 0
            const parsedStartDate = parseDateTime(checkedEventNode.start!, this.options.timezone, monthNames, dayNames, firstDayOfWeek)
            if (!!parsedStartDate) {
              checkedEventNode.s = parsedStartDate
              //console.log('Allowed valid event:', checkedEventNode)
              // Calculate internal reference value of event node.
              const finalEventNode = optimizeEventNode(checkedEventNode, this.options, this.measurements as Measures)
              this.eventNodes.push(finalEventNode)
            }
          }
        })

        // Check and optimize event ID uniqueness.
        const duplicateIds = getDuplicateValues(eventIds)
        if (duplicateIds.length > 0) {
          duplicateIds.forEach((duplicateId: string) => {
            let matchCount = 0
            for (let item of this.eventNodes) {
              const eventObj = item as EventNode
              if (eventObj.eventId === duplicateId) {
                matchCount++
                if (matchCount > 1) {
                  eventObj.eventId = `EventNode:${String(eventObj.uid)}`
                  //console.log('Updated event Id:', duplicateId, eventObj.eventId, matchCount)
                }
              }
            }
          })
        }

        // Then, it cache the registered all events in web storage.
        saveToStorage(this.options.useStorage, cacheKey, this.options.events, true)
        this.options.events = cacheKey
      }

    } catch (error) {
      console.warn(error)
    } finally {
      const nodesContainer: HTMLDivElement = this.targetElement.querySelector('.sunorhc-timeline-nodes')!
      placeEventNodes(nodesContainer, this.eventNodes as EventNode[])
      this.logger.log('serveEventNodes:', this.eventNodes, this.options.events, this.options.effects.presentTime)
      if (this.options.effects.presentTime) {
        showPresentTimeMarker(this.targetElement)
      }
      /* simulate reload
      if (this.test < 1) {
        this.test++
        this.serveEventNodes()
      }
      */
    }
  }

   

  /**
   * Start watching the rendering mode changing.
   * @private
   */
  private runChangeThemeWatcher(): void {
    const targetContainer = this.targetElement.querySelector('.sunorhc-timeline-container')!
    if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
      //console.log('changeTheme: Dark mode now')
      targetContainer.classList.add('dark-theme')
    } else {
      //console.log('changeTheme: Light mode now')
      targetContainer.classList.remove('dark-theme')
    }
    window?.matchMedia('(prefers-color-scheme: dark)')?.addEventListener('change', (event: MediaQueryListEvent) => {
      const isDarkMode: boolean = event.matches
      //console.log('Changed mode:', isDarkMode ? 'dark' : 'light', event)
      if (isDarkMode) {
        targetContainer.classList.add('dark-theme')
      } else {
        targetContainer.classList.remove('dark-theme')
      }
    })
    // Hooks when changing mode with body class; c.f. 'darkmode--activated' for darkmode.js
    watcher(document.body, (mutations) => {
      const mutation = mutations as unknown as MutationRecord
      if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
        const targetElm = mutation.target as HTMLBodyElement
        if (targetElm.classList.contains(this.options.theme.hookChangeModeClass!)) {
          //console.log('Toggled to darkmode with body class:', this.options.theme.hookChangeModeClass)
          targetContainer.classList.add('dark-theme')
        } else {
          //console.log('Toggled to lightmode with body class:', this.options.theme.hookChangeModeClass)
          targetContainer.classList.remove('dark-theme')
        }
      }
    }, { attributes: true, childList: false, subtree: false })
  }

  /**
   * Register various event listeners for timeline components.
   * @private
   */
  private registerEventListeners(): void {
    const referMeasurements: { [key: string]: any } = deepCloneObject(this.measurements)
    this.logger.log('registerEventListeners:', referMeasurements)

    // Horizontal scrolling is required if the main canvas area overflows.
    if (referMeasurements.rulerActualWidth > referMeasurements.rulerVisibleWidth) {
      dragScroll(this.targetElement)

      doAlignment(this.targetElement, this.options.effects.defaultAlignment)
    }

    // Vertical scrolling is required if the sidebar and main canvas area overflows.
    if (referMeasurements.sidebarHeight > referMeasurements.sidebarVisibleHeight) {
      wheelScroll(this.targetElement)
    }

    if (this.options.zoomable) {
      dblclickZoom(this.targetElement, this.options)
    }

    //Util.onStickyRulerItems(this.targetElement)

    onHoverTooltip(this.targetElement, this.eventNodes as EventNode[])

  }

  // public methods

  async initialized(callback?: (instance: Timeline) => void): Promise<void> {
    if (callback) {
      // Fires after rendering container and before placing events.
      this.logger.log('Callbackable "initialized" hook:')
      await Promise.resolve(callback(this))
    }
  }

  async reload(newOptions?: Partial<TimelineOptions>, callback?: (instance: Timeline) => void): Promise<void> {
    this.logger.log('Called "reload"', this.targetElement)
    // Keep container display size before and after reload.
    const timelineRect = getRect(this.targetElement) as DOMRect
    const cachedStyles = getAtts(this.targetElement, 'style')
    setStyles(this.targetElement, `min-width: ${timelineRect.width}px; min-height: ${timelineRect.height}px; ${cachedStyles}`)
    // Remove all children in the timeline container element.
    setContent(this.targetElement, '', false)
    try {
      if (newOptions) {
        //console.log('reload!!!:', newOptions)
        const updateOptions = deepMergeObjects(this.options, newOptions)
        if (newOptions.hasOwnProperty('start') && newOptions.start instanceof Date) {
          updateOptions.start = newOptions.start
        }
        if (newOptions.hasOwnProperty('end') && newOptions.end instanceof Date) {
          updateOptions.end = newOptions.end
        }
        this.options = updateOptions
      } else {
        this.options = await Promise.resolve(this.initOptions())
      }
      this.measurements = await Promise.resolve(this.initMeasure())

      // Render timeline component.
      await Promise.resolve(this.render())

      // Here is the "initialized" hook point immediately after initialization.
      this.initialized()

      // Load event nodes, cache them after parsing, and place them on the DOM.
      await Promise.resolve(this.serveEventNodes())

      // Registration of various event listeners.
      this.registerEventListeners()

      // Start monitoring rendering mode.
      this.runChangeThemeWatcher()

    } catch (error) {
      console.error('Failed to create timeline:', error)
      // Hide the dispatcher element itself for injecting the timeline.
      setStyles(this.targetElement, { display: 'none' })
    } finally {
      // Util.setStyles(this.targetElement, cachedStyles as string)
      if (callback) {
        // Fires after reload?
        await Promise.resolve(callback(this))
      }
    }
  }

  async align(alignment: Alignment, callback?: (instance: Timeline) => void): Promise<void> {
    doAlignment(this.targetElement, alignment)
    if (callback) {
      // Fires after alignment.
      await Promise.resolve(callback(this))
    }
  }

  async zoom(
    newScaleOptions: ZoomScaleOptions,
    callback?: (instance: Timeline) => void
  ): Promise<void> {
    if (
      typeof newScaleOptions !== 'object' ||
      typeof newScaleOptions.scale !== 'string' ||
      (typeof newScaleOptions.start !== 'string' && !(newScaleOptions.start instanceof Date))
    ) {
      return Promise.reject(new Error('Invalid zoom option.'))
    }
    console.log('zoom!!!:', this.options.scale, '->', newScaleOptions.scale, newScaleOptions)
    this.reload({ start: newScaleOptions.start, end: newScaleOptions.end, scale: newScaleOptions.scale, ruler: newScaleOptions.ruler })
    if (callback) {
      // Fires after zoom?
      await Promise.resolve(callback(this))
    }
  }

}