All files / src/utils event.ts

40.67% Statements 240/590
72.5% Branches 29/40
77.77% Functions 7/9
40.67% Lines 240/590

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 5911x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 5x 5x 2x 2x 2x 2x 2x 2x 5x 5x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5x 5x 5x 3x 5x 5x 5x 1x 5x 5x 5x 1x 5x 5x 5x 2x 2x 2x 5x 5x 5x 5x     5x 5x 5x   5x 5x 5x   5x 5x 5x         5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 12x 9x 12x 3x 3x 4x 4x 4x 4x 4x 4x 4x 4x 12x 3x 3x 12x 12x 12x 1x 1x 12x 12x 12x 3x 2x 2x 6x 6x 6x 6x 2x 12x 12x 12x 12x 12x 12x 12x       12x 12x 12x                             12x 12x 12x   12x 12x 12x   12x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x           3x 1x 1x       1x 1x 1x 3x 3x           3x 3x               3x               3x               3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                         1x 1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
// DOM event helper methods
// Methods:
// dragScroll, wheelScroll, doAlignment
 
import { Scale, EventNodes, EventNode, TimelineOptions } from '@/types/definitions'
import { getRect, setContent, setStyles } from './dom'
import { findEvents, truncateLowerRulerItems } from './helper'
import { toValidScale, parseDateTime } from './datetime'
 
/**
 * Register event listeners that scroll by dragging when the main canvas of the timeline overflows.
 *  
 * @param {HTMLDivElement} timelineElement - Element containing an instance of the timeline.
 */
export const dragScroll = (timelineElement: HTMLDivElement): void => {
    const scrollableElement: HTMLDivElement = timelineElement.querySelector('.sunorhc-timeline-main-canvas')!
    // Elements to synchronize with main canvas scrolling.
    const scrollableSidebarElements: HTMLDivElement[] = Array.from(timelineElement.querySelectorAll('.sunorhc-timeline-sidebar[data-sidebar-overflow="true"]')!)
 
    // Handle scrolling the based main canvas element.
    if (scrollableElement) {
        let isDown = false
        let startX: number
        let startY: number
        let scrollLeft: number
        let scrollTop: number
 
        const startDragging = (clientX: number, clientY: number) => {
            isDown = true
            // The added class does not affect the operation, but is assigned for checking the status on the DOM.
            scrollableElement.classList.add('draggable')
            if (scrollableSidebarElements.length > 0) {
                scrollableSidebarElements.forEach(elm => elm.classList.add('draggable'))
            }
            startX = clientX - scrollableElement.offsetLeft
            startY = clientY - scrollableElement.offsetTop
            scrollLeft = scrollableElement.scrollLeft
            scrollTop = scrollableElement.scrollTop
        }
 
        const stopDragging = () => {
            isDown = false
            scrollableElement.classList.remove('draggable')
            if (scrollableSidebarElements.length > 0) {
                scrollableSidebarElements.forEach(elm => elm.classList.remove('draggable'))
            }
        }
 
        const handleDragging = (clientX: number, clientY: number) => {
            if (!isDown) return
            const x = clientX - scrollableElement.offsetLeft
            const y = clientY - scrollableElement.offsetTop
            const walkX = x - startX
            const walkY = y - startY
            scrollableElement.scrollLeft = scrollLeft - walkX
            //console.log('handleDragging:', scrollableElement, scrollableSidebarElements)
            // Scroll synchronization of sidebar elements is vertical only.
            if (scrollableSidebarElements.length > 0) {
                scrollableElement.scrollTop = scrollTop - walkY
                scrollableSidebarElements.forEach(elm => elm.scrollTop = scrollTop - walkY)
            }
        }
 
        // Mouse events
        scrollableElement.addEventListener('mousedown', (e: MouseEvent) => {
            startDragging(e.pageX, e.pageY)
        })
 
        scrollableElement.addEventListener('mouseleave', () => {
            stopDragging()
        })
 
        scrollableElement.addEventListener('mouseup', () => {
            stopDragging()
        })
 
        scrollableElement.addEventListener('mousemove', (e: MouseEvent) => {
            if (!isDown) return
            e.preventDefault()
            handleDragging(e.pageX, e.pageY)
        })
 
        // Touch events
        scrollableElement.addEventListener('touchstart', (e: TouchEvent) => {
            const touch = e.touches[0]
            startDragging(touch.clientX, touch.clientY)
        })
 
        scrollableElement.addEventListener('touchend', () => {
            stopDragging()
        })
 
        scrollableElement.addEventListener('touchcancel', () => {
            stopDragging()
        })
 
        scrollableElement.addEventListener('touchmove', (e: TouchEvent) => {
            if (!isDown) return
            e.preventDefault()
            const touch = e.touches[0]
            handleDragging(touch.clientX, touch.clientY)
        })
    }
}
 
/**
 * Register event listeners that scroll by mouse wheeling with sync main canvas when the sidebar of the timeline overflows.
 * 
 * @param {HTMLDivElement} timelineElement - Element containing an instance of the timeline.
 */
export const wheelScroll = (timelineElement: HTMLDivElement): void => {
    const wheelableBaseElement: HTMLDivElement = timelineElement.querySelector('.sunorhc-timeline-main-canvas')!
    // Elements to synchronize with main canvas wheel scrolling.
    const wheelableSidebarElements: HTMLDivElement[] = Array.from(timelineElement.querySelectorAll('.sunorhc-timeline-sidebar[data-sidebar-overflow="true"]')!)
    const wheelableElements: HTMLDivElement[] = [wheelableBaseElement, ...wheelableSidebarElements]
    //console.log('Called wheelScroll', wheelableElements)
 
    const toggleWheelableClass = (isEnable: boolean): void => {
        wheelableElements.forEach(elm => {
            if (isEnable) {
                elm.classList.add('wheelable')
            } else {
                elm.classList.remove('wheelable')
            }
        })
    }
 
    // Handle wheel scrolling the based main canvas element.
    if (wheelableBaseElement) {
        let isFocused = false
 
        wheelableElements.forEach(elm => {
            elm.addEventListener('mouseenter', () => {
                isFocused = true
                toggleWheelableClass(isFocused)
            })
 
            elm.addEventListener('mouseleave', () => {
                isFocused = false
                toggleWheelableClass(isFocused)
            })
 
            elm.addEventListener('wheel', (e: WheelEvent) => {
                if (!isFocused) return
                e.preventDefault()
                Array.from(timelineElement.querySelectorAll('.wheelable')!).forEach(wheelableElm => {
                    if (wheelableElm instanceof HTMLElement) {
                        //console.log('Do wheel scrolling!', elm.outerHTML)
                        wheelableElm.scrollBy({ top: e.deltaY, left: e.deltaX, behavior: 'smooth' })
                    }
                })
            })
 
            // Touch events
            let touchStartY: number
            let touchStartX: number
 
            elm.addEventListener('touchstart', (e: TouchEvent) => {
                const touch = e.touches[0]
                touchStartX = touch.clientX
                touchStartY = touch.clientY
            })
 
            elm.addEventListener('touchmove', (e: TouchEvent) => {
                if (e.touches.length !== 1) return
                const touch = e.touches[0]
                const touchEndX = touch.clientX
                const touchEndY = touch.clientY
                const deltaX = touchStartX - touchEndX
                const deltaY = touchStartY - touchEndY
                Array.from(timelineElement.querySelectorAll('.wheelable')!).forEach(wheelableElm => {
                    if (wheelableElm instanceof HTMLElement) {
                        wheelableElm.scrollBy({ top: deltaY, left: deltaX, behavior: 'smooth' })
                    }
                })

                touchStartX = touchEndX
                touchStartY = touchEndY
            })
 
            elm.addEventListener('touchend', () => {
                // Optionally handle touch end if needed
            })
 
            elm.addEventListener('touchcancel', () => {
                // Optionally handle touch cancel if needed
            })
        })
    }
}
 
/**
 * Event handler to move the horizontal position of the main canvas of the timeline to the specified position.
 * 
 * @param {HTMLDivElement} timelineElement - Element containing an instance of the timeline.
 * @param {Alignment} position 
 */
export const doAlignment = (timelineElement: HTMLDivElement, position: string | number): void => {
    // type Alignment = number | 'left' | 'begin' | 'center' | 'right' | 'end' | 'current' | 'currently' | 'latest';
    if (!timelineElement || !/^(\d+|left|begin|center|right|end|current(|ly)|latest)$/.test(String(position))) {
        return
    }
    // `fullWidth` must be the entire scrollable width of the target element.
    const fullWidth = (timelineElement.querySelector('.sunorhc-timeline-nodes')! as HTMLDivElement).scrollWidth
    const targetElement: HTMLDivElement = timelineElement.querySelector('.sunorhc-timeline-main-canvas')!
    // `visibleWidth` is the visible layout width of the target element.
    // This is an "offsetWidth", so this value includes the element's width, padding, border, and vertical scrollbar (if present).
    const visibleWidth = targetElement.offsetWidth
    console.log('Called doAlignment:', position, fullWidth, visibleWidth)
 
    switch(position) {
        case 'left':
        case 'begin':
            // Align to the starting point (left edge) of the ruler.
            if (targetElement.scrollLeft > 0) {
                targetElement.scrollTo({ top: 0, left: 0, behavior: 'smooth' })
            }
            break
        case 'center':
            // Align with the center of the ruler.
            if (targetElement.scrollLeft > 0) {
                // Initialize scroll start position. (for the Mozilla browser etc.)
                targetElement.scrollTo({ left: 0 })
            }
            const toLeft = Math.abs((visibleWidth / 2) - (fullWidth / 2))
            targetElement.scrollBy({ top: 0, left: toLeft, behavior: 'smooth' })
            break
        case 'right':
        case 'end':
            // Align to the ending point (right edge) of the ruler.
            if (targetElement.scrollLeft < fullWidth) {
                targetElement.scrollTo({ top: 0, left: fullWidth, behavior: 'smooth' })
            }
            break
        case 'current':
        case 'currently':
            // Centers that point of the ruler when contains the current datetime.
            const currentPositionElement = timelineElement.querySelector('[data-ruler-grain="min"] > [data-item-present-time="true"]') as HTMLElement
            if (currentPositionElement) {
                targetElement.scrollTo({ top: 0, left: currentPositionElement.offsetLeft, behavior: 'smooth' })
                currentPositionElement.dispatchEvent(new MouseEvent('mouseover', { bubbles: true, cancelable: true, view: window }))
            }
            break
        case 'latest':
            // Place focus on the most recent event node.
            const latestEvent = findEvents(timelineElement.id, { latest: true })
            if (latestEvent) {
                latestEvent.dispatchEvent(new MouseEvent('mouseover', { bubbles: true, cancelable: true, view: window }))
                targetElement.scrollTo({ top: 0, left: latestEvent.offsetLeft, behavior: 'smooth' })
            }
            break
        default:
            // Places focus on the event node with the specified event ID.
            const targetEvent = findEvents(timelineElement.id, { id: position })
            if (targetEvent) {
                targetEvent.dispatchEvent(new MouseEvent('mouseover', { bubbles: true, cancelable: true, view: window }))
                targetElement.scrollTo({ top: 0, left: targetEvent.offsetLeft, behavior: 'smooth' })
            }
            break
    }
}
 
/*
export const onStickyRulerItems = (timelineElement: HTMLDivElement): void => {
    const overflowContainer = timelineElement.querySelector('.sunorhc-timeline-main-canvas')! as HTMLDivElement
    if (overflowContainer.offsetWidth > overflowContainer.scrollWidth) {
        return
    }
    const rulerItems = overflowContainer.querySelectorAll('.sunorhc-timeline-ruler-row > li[data-item-colspan]')
    console.log('onStickyRulerItems:', rulerItems, overflowContainer.offsetWidth, overflowContainer.scrollWidth)
    Array.from(rulerItems).forEach((rulerItem) => {
        const stickyRulerItem = rulerItem as HTMLLIElement
        wrapChildNodes(stickyRulerItem, 'span')
        stickyRulerItem.classList.add('sticky-ruler-item')
        setAtts(stickyRulerItem, { 'data-initial-offset-left': stickyRulerItem.offsetLeft.toString() })
    })
 
    overflowContainer.addEventListener('scroll', () => {
        const scrollLeft = overflowContainer.scrollLeft
        const visibleWidth = overflowContainer.offsetWidth - 2
        Array.from(rulerItems).forEach((rulerItem) => {
            const stickyRulerItem = rulerItem as HTMLLIElement
            const initialOffsetLeft = Number(stickyRulerItem.dataset.initialOffsetLeft)
            const stickyLeft = Math.floor((visibleWidth - (stickyRulerItem.firstChild as HTMLSpanElement)!.offsetWidth) / 2)
            const stickyPosition = Math.max(/*initialOffsetLeft,* / stickyLeft, scrollLeft)
            //stickyRulerItem.style.position = 'relative'
            //stickyRulerItem.style.left = `${stickyPosition}px`
        })
    })
}
*/
 
/**
 * Register the event listeners to display a tooltip on mouseover on an event node.
 * 
 * @param {HTMLDivElement} timelineElement 
 * @param {EventNodes} eventNodes 
 */
export const onHoverTooltip = (timelineElement: HTMLDivElement, eventNodes: EventNodes): void => {
    let isShownTooltip = false
    const fragment: DocumentFragment = document.createDocumentFragment()
    const timelineMainCanvas: HTMLDivElement = timelineElement.querySelector('.sunorhc-timeline-main-canvas')!
    //const eventNodesCanvas: HTMLDivElement = timelineElement.querySelector('.sunorhc-timeline-nodes')!
    const eventNodeElements: HTMLDivElement[] = Array.from(timelineElement.querySelectorAll('.sunorhc-timeline-event-node')!)
    const containerRect = getRect(timelineMainCanvas) as DOMRect
    eventNodeElements.forEach((eventNode: HTMLDivElement) => {
        eventNode.addEventListener('mouseleave', (e: MouseEvent) => {
            const selfElement = e.target! as HTMLDivElement
            const tooltipElement = selfElement.querySelector('.tooltip')! as HTMLDivElement
            if (tooltipElement) {
                selfElement.removeChild(tooltipElement)
            }
            isShownTooltip = false
        })
        eventNode.addEventListener('mouseover', (e: MouseEvent) => {
            if (isShownTooltip) return
            e.preventDefault()
            const selfElement = e.target! as HTMLDivElement
            const eventId = selfElement.dataset!.eventId
            const matchedEventNodes = (eventNodes as EventNode[]).filter((item: any) => item.eventId === eventId)
            if (matchedEventNodes.length > 0) {
                const eventNode = matchedEventNodes.shift()!
                const eventNodeElementRect = getRect(selfElement) as DOMRect
                const tooltipElement = document.createElement('div')
                tooltipElement.classList.add('tooltip')
                setStyles(tooltipElement, { top: `${(eventNodeElementRect.height / -2)}px`, left: `${(eventNodeElementRect.width / -2)}px` })
                // <li>StartBaseX: ${eventNode.extends?.startBaseX ?? 'Undefined'}, EndBaseX: ${eventNode.extends?.endBaseX ?? 'Undefined'}</li>\
                setContent(tooltipElement, `<ul>\
                <li>${eventNode.start} ~ ${eventNode.end || 'Undefined'}<li>\
                <li>X: ${eventNode.x}, Width:&nbsp; ${eventNode.w || 'Undefined'}</li>\
                <li>Y: ${eventNode.y}, Height: ${eventNode.h || 'Undefined'}</li>\
                <li>EventId: <span style="color:#f87171;font-weight:600;">${eventNode.eventId}</span></li>\
                <li>${eventNode.label || 'Untitled'}</li>\
                </ul>`, false)
                fragment.append(tooltipElement)
                selfElement.append(fragment)
                const tooltipRect = getRect(tooltipElement) as DOMRect
                let tooltipX = (tooltipRect.width - eventNodeElementRect.width) / -2
                if (eventNode.x! < 0) {
                    if (eventNode.w! + eventNode.x! > timelineMainCanvas.offsetWidth) {
                        tooltipX = (e.pageX + containerRect.left + timelineMainCanvas.scrollLeft) / 2 - eventNode.x!
                    } else {
                        tooltipX = eventNodeElementRect.width - ((tooltipRect.width - e.pageX) / -2)
                    }
                }
                let tooltipY = eventNode.h || eventNodeElementRect.height
                //console.log('onHoverTooltip::X:', tooltipX, eventNode.x, eventNode.w, eventNodeElementRect.x, eventNodeElementRect.width, tooltipRect.x, mouseX, mouseY, timelineMainCanvas.offsetWidth, tooltipX)
                //console.log('onHoverTooltip::Y:', tooltipY, eventNode.y, eventNode.h, eventNodeElementRect.y, eventNodeElementRect.height, tooltipRect.y, tooltipRect.height)
                //console.log('onHoverTooltip:', eventNode, canvasRect, eventNodeElementRect, tooltipRect, tooltipX, tooltipY, e.pageX, e.pageY, eventNode.x!)
                setStyles(tooltipElement, `--tooltip-y: ${tooltipY}px; --tooltip-x: ${tooltipX}px;`)
                //await Promise.resolve(setStyles(tooltipElement, `--tooltip-y: ${tooltipY}px; --tooltip-x: ${tooltipX}px;`))
                //.then(() => {
                    tooltipElement.classList.add('shown')
                    isShownTooltip = true
                //})
            }
        })
    })
}
 
export const dblclickZoom = (timelineElement: HTMLDivElement, timelineOptions: TimelineOptions): void => {
    const zoomableBaseContainer: HTMLDivElement = timelineElement.querySelector('.sunorhc-timeline-main-canvas')!
    const zoomableElements = [
        ...Array.from(zoomableBaseContainer.querySelectorAll('.sunorhc-timeline-ruler')),
        zoomableBaseContainer.querySelector('.sunorhc-timeline-nodes'),
    ]

    const toDateFromOffsetX = (offsetX: number, scale: string): string => {
        const containerElement = zoomableBaseContainer.querySelector('.sunorhc-timeline-nodes')!
        const perX = offsetX / containerElement.clientWidth
        const parsedStartDate = parseDateTime(timelineOptions.start, timelineOptions.timezone)
        const parsedEndDate   = parseDateTime(timelineOptions.end, timelineOptions.timezone)
        const rangeTimes = parsedEndDate!.ts - parsedStartDate!.ts
        const targetTime = (perX * rangeTimes + parsedStartDate!.ts) * 1000
        const baseDate = new Date(targetTime)
        const pad = (num: number, size: number): string => String(num).padStart(size, '0')
        const year = baseDate.getUTCFullYear()
        const month = pad(baseDate.getUTCMonth() + 1, 2)
        const date = pad(baseDate.getUTCDate(), 2)
        const hour = pad(baseDate.getUTCHours(), 2)
        const minute = pad(baseDate.getUTCMinutes(), 2)
        const second = pad(baseDate.getUTCSeconds(), 2)
        const millisecond = pad(baseDate.getUTCMilliseconds(), 3)
        let dateStr: string = ''
        switch(scale) {
            case 'year':
                dateStr = year.toString()
                break
            case 'month':
                dateStr = `${year}-${month}`
                break
            case 'week':
            case 'weekday':
            case 'day':
                dateStr = `${year}-${month}-${date}`
                break
            case 'hour':
                dateStr = `${year}-${month}-${date}T${hour}`
                break
            case 'minute':
                dateStr = `${year}-${month}-${date}T${hour}:${minute}`
                break
            case 'second':
                dateStr = `${year}-${month}-${date}T${hour}:${minute}:${second}`
                break
            case 'millisecond':
                dateStr = `${year}-${month}-${date}T${hour}:${minute}:${second}.${millisecond}`
                break
        }
        return dateStr
    }

    const doZoom = (evt: MouseEvent) => {
        const targetElement = evt.target as HTMLElement
        const clientX = evt.pageX // Absolute X coordinates of the entire visible page.
        const clientY = evt.pageY // Absolute Y coordinates of the entire visible page.
        const offsetX = evt.offsetX // Relative X coordinates on the zoomableBaseContainer element.
        const offsetY = evt.offsetY // Relative Y coordinates on the zoomableBaseContainer element.
        console.log('zoomReady!!::', targetElement.nodeName, clientX, clientY, offsetX, offsetY, evt, zoomableBaseContainer.clientWidth)
        
        let newStartDate: string = ''
        let newEndDate: string = 'auto'
        let previousScale: string | boolean = ''
        let zoomToScale: string = ''
        let newMinGrainWidth = Number(timelineOptions.ruler.minGrainWidth)
        if (targetElement.nodeName === 'LI') {
            // Zoom to the scale range of the ruler item that is double-clicked.
            newStartDate = targetElement.dataset.itemDatetime || ''
            previousScale = toValidScale(targetElement.parentElement?.dataset.rulerType || '')
            if (!previousScale) {
                previousScale = timelineOptions.scale
            }
            //console.log('zoomToScale!!!::', newStartDate, newEndDate, previousScale)
        } else {
            // Zooms to the scale range of the minimum ruler grain to which the X-axis of the double-clicked node canvas belongs.
            previousScale = timelineOptions.scale
            newStartDate = toDateFromOffsetX(offsetX, previousScale)
            console.log('zoomToSetting!!!::', newStartDate, newEndDate, previousScale)
        }
        let matches = null
        let baseDate = null
        let amount = null
        switch(previousScale) {
            case 'year':
                // newStartDate: /^(?<year>\d{1,4})$/
                zoomToScale = 'month'
                newStartDate = `${newStartDate}-01-01T00:00:00.000`
                newMinGrainWidth = Math.ceil(zoomableBaseContainer.clientWidth / 12)
                break
            case 'month':
                // newStartDate: /^(?<year>\d{1,4})-(?<month>\d{2})$/
                zoomToScale = 'day'
                matches = /^(?<year>\d{1,4})-(?<month>\d{2})$/.exec(newStartDate)
                if (!!matches && matches.groups) {
                    const { year, month } = matches.groups
                    const oneDay = new Date(new Date(Date.UTC(Number(year), Number(month), 1, 0, 0, 0)).getTime() - 1)
                    const monthDays = oneDay.getUTCDate()
                    console.log('!!!:', matches, oneDay, monthDays)
                    newMinGrainWidth = Math.ceil(zoomableBaseContainer.clientWidth / monthDays)
                    newEndDate = `${newStartDate}-${monthDays}T23:59:59.999`
                } else {
                    newMinGrainWidth = Math.ceil(zoomableBaseContainer.clientWidth / 30)
                }
                newStartDate = `${newStartDate}-01T00:00:00.000`
                break
            case 'week':
                // newStartDate: /^(?<year>\d{1,4})-(?<week>\d{2})$/
                zoomToScale = 'day'
                matches = /^(?<year>\d{1,4})-(?<week>\d{2})$/.exec(newStartDate)
                if (!!matches && matches.groups) {
                    const { year, week } = matches.groups
                    let weeknumber = 1
                    for (let d = 1; d <= Number(week) * 7; d++) {
                        const oneDay = new Date(Date.UTC(Number(year), 0, d, 0, 0, 0))
                        if (oneDay.getUTCDay() === timelineOptions.ruler.firstDayOfWeek) {
                            weeknumber++
                            if (weeknumber === Number(week)) {
                                newStartDate = oneDay.toISOString().replace(/Z$/, '')
                                newEndDate = `${year}-${String(oneDay.getUTCMonth() + 1).padStart(2, '0')}-${String(oneDay.getUTCDate() + 6).padStart(2, '0')}T23:59:59.999`
                                break
                            }
                        }
                    }
                }
                newMinGrainWidth = Math.ceil(zoomableBaseContainer.clientWidth / 7)
                break
            case 'weekday':
            case 'day':
                // newStartDate: /^(?<year>\d{1,4})-(?<month>\d{2})-(?<day>\d{2}),(?<weekday>\w{3})$/
                zoomToScale = 'hour'
                baseDate = newStartDate.replace(/(,\w+)$/, '')
                newStartDate = `${baseDate}T00:00:00.000`
                matches = /^(?<year>\d{1,4})-(?<month>\d{2})-(?<day>\d{2})$/.exec(baseDate)
                if (!!matches && matches.groups) {
                    const { year, month, day } = matches.groups
                    amount = Number(day) + 1
                    newEndDate = `${year}-${month.padStart(2, '0')}-${String(amount).padStart(2, '0')}T00:00:00.000`
                }
                newMinGrainWidth = 60// Math.ceil(zoomableBaseContainer.clientWidth / 24)
                break
            case 'hour':
                // newStartDate: /^(?<year>\d{1,4})-(?<month>\d{2})-(?<day>\d{2})T(?<hours>\d{2})$/
                zoomToScale = 'minute'
                baseDate = newStartDate
                newStartDate = `${baseDate}:00:00.000`
                matches = /^(?<year>\d{1,4})-(?<month>\d{2})-(?<day>\d{2})T(?<hours>\d{2})$/.exec(baseDate)
                if (!!matches && matches.groups) {
                    const { year, month, day, hours } = matches.groups
                    amount = Number(hours) + 1
                    newEndDate = `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}T${String(amount).padStart(2, '0')}:00:00.000`
                }
                newMinGrainWidth = Math.ceil(zoomableBaseContainer.clientWidth / 60)
                break
            case 'minute':
                // newStartDate: /^(?<year>\d{1,4})-(?<month>\d{2})-(?<day>\d{2})T(?<hours>\d{2}):(?<minutes>\d{2})$/
                zoomToScale = 'second'
                baseDate = newStartDate
                newStartDate = `${baseDate}:00.000`
                matches = /^(?<year>\d{1,4})-(?<month>\d{2})-(?<day>\d{2})T(?<hours>\d{2}):(?<minutes>\d{2})$/.exec(baseDate)
                if (!!matches && matches.groups) {
                    const { year, month, day, hours, minutes } = matches.groups
                    amount = Number(minutes) + 1
                    newEndDate = `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}T${hours.padStart(2, '0')}:${String(amount).padStart(2, '0')}:00.000`
                }
                newMinGrainWidth = Math.ceil(zoomableBaseContainer.clientWidth / 60)
                break
            case 'second':
                // newStartDate: /^(?<year>\d{1,4})-(?<month>\d{2})-(?<day>\d{2})T(?<hours>\d{2}):(?<minutes>\d{2}):(?<seconds>\d{2})$/
                zoomToScale = 'millisecond'
                baseDate = newStartDate
                newStartDate = `${baseDate}.000`
                matches = /^(?<year>\d{1,4})-(?<month>\d{2})-(?<day>\d{2})T(?<hours>\d{2}):(?<minutes>\d{2}):(?<seconds>\d{2})$/.exec(baseDate)
                if (!!matches && matches.groups) {
                    const { year, month, day, hours, minutes, seconds } = matches.groups
                    amount = Number(seconds) + 1
                    newEndDate = `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}T${hours.padStart(2, '0')}:${minutes.padStart(2, '0')}:${String(amount).padStart(2, '0')}.000`
                }
                newMinGrainWidth = Math.ceil(zoomableBaseContainer.clientWidth / 1000)
                break
            case 'millisecond':
                // newStartDate: /^(?<year>\d{1,4})-(?<month>\d{2})-(?<day>\d{2})T(?<hours>\d{2}):(?<minutes>\d{2}):(?<seconds>\d{2})\.(?<milliseconds>\d{3})$/
            default:
                // Do not zoom at this scale.
                zoomToScale = timelineOptions.scale
                break
        }
        newStartDate += timelineOptions.timezone === 'UTC' ? 'Z' : ''
        newEndDate += newEndDate !== 'auto' && timelineOptions.timezone === 'UTC' ? 'Z' : ''
        const newRulerOptions = { ...timelineOptions.ruler }
        if (zoomToScale !== 'millisecond') {
            newRulerOptions.minGrainWidth = !!newMinGrainWidth && newMinGrainWidth < 48 ? 48 : newMinGrainWidth
        } else {
            newRulerOptions.minGrainWidth = !!newMinGrainWidth && newMinGrainWidth < 30 ? 30 : newMinGrainWidth
        }
        if (timelineOptions.ruler.hasOwnProperty('truncateLowers') && timelineOptions.ruler.truncateLowers) {
            if (newRulerOptions.hasOwnProperty('top') && newRulerOptions.top!.rows.length > 0) {
                newRulerOptions.top!.rows = truncateLowerRulerItems(zoomToScale as Scale, newRulerOptions.top!.rows)
            }
            if (newRulerOptions.hasOwnProperty('bottom') && newRulerOptions.bottom!.rows.length > 0) {
                newRulerOptions.bottom!.rows = truncateLowerRulerItems(zoomToScale as Scale, newRulerOptions.bottom!.rows)
            }
        }

        const zoomOptions = {
            prevScale: previousScale,
            scale: zoomToScale,
            start: newStartDate,
            end: newEndDate,
            ruler: newRulerOptions,
        }
        console.log('Before Zoom!!!::', zoomOptions, timelineOptions.ruler)
        if ((window as Window).hasOwnProperty('SunorhcTimelineInstances')) {
            window.SunorhcTimelineInstances[timelineElement.id].zoom(zoomOptions)
        }
    }

    /*
    zoomableBaseContainer.querySelector<HTMLDivElement>('.sunorhc-timeline-nodes')?.addEventListener('mousemove', (e: MouseEvent) => {
        const toDateStr = toDateFromOffsetX(e.offsetX, timelineOptions.scale)
        console.log('offsetX: %s -> date: %s', e.offsetX, toDateStr)
    })
    */

    zoomableElements.forEach(zoomableElement => {
        (zoomableElement as HTMLElement)!.addEventListener('dblclick', (e: MouseEvent) => {
            doZoom(e)
        })
    })

}