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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 8x 8x 6x 5x 6x 1x 1x 1x 6x 8x 11x 5x 4x 4x 4x 2x 2x 5x 1x 1x 1x 5x 11x 11x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 1x 1x 11x 11x 12x 1x 1x 12x 1x 1x 10x 9x 9x 9x 11x 11x 11x 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 32x 32x 32x 32x 32x 32x 32x 2x 2x 32x 2x 2x 32x 2x 2x 32x 4x 4x 32x 22x 17x 17x 4x 17x 13x 13x 22x 5x 5x 22x 32x 32x 32x 32x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 1x 9x 9x 9x 9x 9x 9x 3x 3x 3x 3x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 4x 1x 1x 1x 1x 1x 4x 1x 1x 1x 1x 1x 1x 1x 1x 4x 7x 7x 4x 4x | // Helper methods dedicated to Sunorhc.Timeline library. // Methods: // getBaseRowCoordinate, getBaseX, getPreciseX, getNodeHeight, optimizeEventNode, findEvents, truncateLowerRulerItems, // saveToStorage, loadFromStorage, removeToStorage, import LZString from 'lz-string' import { Scale, PointerSize, StorageType, EventNode, TimelineOptions, Measures, StageRange, DateTimeObject } from '@/types/definitions' import { isEmptyObject, inRange, hash, deepCloneObject } from './common' import { getWeekNumber, parseDateTime, toValidScale } from './datetime' //import { getRect } from './dom' //import { serialize, deserialize } from './serialization' /** * Returns optimized value that calculating the vertical position of events. * * @param {Partial<EventNode>} obj * @param {StageRange} range * @param {TimelineOptions} options * @returns {number} */ export const getBaseRowCoordinate = (obj: Partial<EventNode>, range: StageRange, options: TimelineOptions): number => { // Calculating and optimizing the vertical position of events: // - row: An index number of row (start from 1) in sidebar items. If row is 0, it will belong the first // item in the sidebar. If specified, the event can be forced to place to the row of the specified // index number. // - group: Sidebar group name to which the event belongs. If there is a group enabled on the sidebar side, // the event will be placed in that sidebar group's row and grouping will occur. let baseRowCoordinates: number = -1// base point coordinates of row if (obj.hasOwnProperty('row')) { // If row exists, this value takes precedence. if (typeof obj.row === 'number' && !Number.isNaN(obj.row)) { if (obj.row >= 0/* && obj.row <= range.maxRows*/) { baseRowCoordinates = range.rowHeight * (obj.row > 0 ? obj.row - 1 : obj.row) } else { // Event is out of sidebar range. baseRowCoordinates = range.rowHeight * -1 } } } if (baseRowCoordinates === -1) { if (obj.hasOwnProperty('group')) { // When group only is set. const matchedGroupIndex = options.sidebar.items.findIndex((elm: any) => elm.hasOwnProperty('group') && elm.group === obj.group) if (matchedGroupIndex !== -1) { baseRowCoordinates = range.rowHeight * matchedGroupIndex } } else { // When row and group are not set. baseRowCoordinates = 0 } } return baseRowCoordinates } /** * Calculate the relative position within the timestamp range of the start and end of the specified timestamp and * return converted pixels value as based X coordinate. * * @param {number} eventTimestamp - Timestamp value of the position you want to obtain (unit: seconds) * @param {number} startRangeTimestamp - Container range start timestamp value (unit: seconds) * @param {number} endRangeTimestamp - Container range end timestamp value (unit: seconds) * @param {number} rangeWidth - Width of container range (unit: pixels) * @returns {number} - Horizontal position of the specified timestamp in the container range (unit: pixels) * @deprecated This method will be removed in the next major release. Use `getPreciseX` instead. */ export const getBaseX = ( eventTimestamp: number, startRangeTimestamp: number, endRangeTimestamp: number, rangeWidth: number, minScaleWidth: number, ): number => { if (startRangeTimestamp === endRangeTimestamp) { throw new Error('start and end of range timestamp cannot be the same.') } let relativeX: number if (eventTimestamp < startRangeTimestamp) { // Event timestamp is before the start range relativeX = 0 } else if (eventTimestamp > endRangeTimestamp) { // Event timestamp is after the end range relativeX = rangeWidth } else { // Event timestamp is within the range relativeX = (eventTimestamp - startRangeTimestamp) / (endRangeTimestamp - startRangeTimestamp) * rangeWidth } //console.log('getBX!!!', relativeX, relativeX / minScaleWidth, new Date(eventTimestamp * 1000).toISOString(), new Date(startRangeTimestamp * 1000).toISOString(), new Date(endRangeTimestamp * 1000).toISOString()) return Math.floor(relativeX / minScaleWidth) * minScaleWidth } /** * Calculates the relative exact position of a datetime below a given scale, returning it as a number of pixels * from a base X coordinate. * * @param {DateTimeObject} dateObj * @param {Scale} scale * @param {StageRange} range * @returns {number} */ export const getPreciseX = (dateObj: DateTimeObject, scale: Scale, range: StageRange): number => { if (isEmptyObject(range) || range.width <= 0 || ( range.hasOwnProperty('endDate') && (range.startDate.ts * 1000 + range.startDate.milliseconds) === (range.endDate.ts * 1000 + range.endDate.milliseconds) )) { throw new Error('Could not get the coordinate due to illegal range.') } const scaleWidth = range.minScaleWidth let relativeX: number = 0 let lowerTimes: number let scalePerMilliseconds: number = 0 let preciseX: number = -1 if ((dateObj.ts * 1000 + dateObj.milliseconds) < (range.startDate.ts * 1000 + range.startDate.milliseconds)) { // Event timestamp is before the start range timestamp. preciseX = 0 } else if ((dateObj.ts * 1000 + dateObj.milliseconds) > (range.endDate.ts * 1000 + range.endDate.milliseconds)) { // Event timestamp is after the end range timestamp. preciseX = range.width } switch(scale) { case 'year': // Calculate the relative base position within a range based on a global scale. for (let i = 0; i < Math.ceil(range.width / scaleWidth); i++) { const nowYear = range.startDate[scale] + i const baseX = scaleWidth * i const baseDate = new Date(Date.UTC(nowYear, 0, 1)) if (dateObj.year === baseDate.getUTCFullYear()) { //console.log(`getPX!!!?::${dateObj.ISO}:`, nowYear, baseX, baseDate.getUTCFullYear()) relativeX = baseX break } } if (preciseX === -1) { const startOfYear = new Date(Date.UTC(dateObj.year, 0, 1)) const eventDate = new Date(Date.UTC(dateObj.year, dateObj.month - 1, dateObj.day)) const diffInMilliseconds = eventDate.getTime() - startOfYear.getTime() const daysInYear = (new Date(Date.UTC(dateObj.year + 1, 0, 0)).getTime() - startOfYear.getTime()) / (24 * 60 * 60 * 1000) + 1 //console.log(`getPX!!!?::${dateObj.ISO}:`, startOfYear.toISOString(), diffInMilliseconds, daysInYear) lowerTimes = diffInMilliseconds + (dateObj.hours * 60 * 60 * 1000) + (dateObj.minutes * 60 * 1000) + (dateObj.seconds * 1000) + dateObj.milliseconds scalePerMilliseconds = daysInYear * 24 * 60 * 60 * 1000 preciseX = lowerTimes / scalePerMilliseconds * scaleWidth + relativeX } break case 'month': // Calculate the relative base position within a range based on a global scale. for (let i = 0; i < Math.ceil(range.width / scaleWidth); i++) { const nowMonth = range.startDate[scale] + i - 1 const baseX = scaleWidth * i const baseDate = new Date(Date.UTC(range.startDate.year, nowMonth, 1)) if (dateObj.year === baseDate.getUTCFullYear() && dateObj.month === baseDate.getUTCMonth() + 1) { //console.log(`getPX!!!?::${dateObj.ISO}:`, nowMonth, baseX, baseDate.getUTCFullYear(), baseDate.getUTCMonth() + 1) relativeX = baseX break } } if (preciseX === -1) { const daysInMonth = new Date(dateObj.year, dateObj.month, 0).getDate() const PixelPerDays = (dateObj.day - 1) / daysInMonth * scaleWidth // relative days as based X amount pixels // YYYY-01-01 -> 0 / 31 * 100 = 0; YYYY-01-31 -> 30 / 31 * 100 = 96.774 lowerTimes = (dateObj.hours * 60 * 60 * 1000) + (dateObj.minutes * 60 * 1000) + (dateObj.seconds * 1000) + dateObj.milliseconds // relative time of milliseconds scalePerMilliseconds = lowerTimes / (daysInMonth * 24 * 60 * 60 * 1000) // 00:00:00.000 -> (0 + 0 + 0 + 0) / 31 * 86400000 = 0; 23:59:59.999 -> (82800000 + 3540000 + 59000 + 999 = 86399999) / (31 * 86400000 = 2678400000) = 0.032 preciseX = scalePerMilliseconds * scaleWidth + PixelPerDays + relativeX // YYYY-01-01T00:00:00.000 -> 0 * 100 + 0 = 0; YYYY-01-31T23:59:59.999 -> 0.032 * 100 + 96.774 = 99.999 //console.log(`getPX!!!::${dateObj.ISO}:`, daysInMonth, dateObj.day, dateObj.day / daysInMonth, PixelPerDays, lowerTimes, scalePerMilliseconds, preciseX, relativeX) } break case 'week': // Calculate the relative base position within a range based on a global scale. for (let i = 0; i < Math.ceil(range.width / scaleWidth); i++) { const nowDay = range.startDate.day + i * 7 const baseX = scaleWidth * i const baseDate = new Date(Date.UTC(range.startDate.year, range.startDate.month - 1, nowDay)) const nowWeekNumber = getWeekNumber(baseDate) if (dateObj.year === baseDate.getUTCFullYear() && dateObj.month === baseDate.getUTCMonth() + 1 && inRange(dateObj.weeks, nowWeekNumber - 1, nowWeekNumber + 1)) { let startDayInWeek = baseDate.getUTCDate() //console.log(`getPX!!!::${dateObj.ISO}:`, baseDate.getUTCFullYear(), baseDate.getUTCMonth() + 1, startDayInWeek, nowDay, baseX, dateObj.day - startDayInWeek) relativeX = baseX + ((dateObj.day - startDayInWeek) / 7 * scaleWidth) break } } if (preciseX === -1) { lowerTimes = (dateObj.hours * 60 * 60 * 1000) + (dateObj.minutes * 60 * 1000) + (dateObj.seconds * 1000) + dateObj.milliseconds scalePerMilliseconds = 7 * 24 * 60 * 60 * 1000 preciseX = lowerTimes / scalePerMilliseconds * scaleWidth + relativeX } break case 'weekday': case 'day': // Calculate the relative base position within a range based on a global scale. for (let i = 0; i < Math.ceil(range.width / scaleWidth); i++) { const nowDay = range.startDate.day + i const baseX = scaleWidth * i const baseDate = new Date(Date.UTC(range.startDate.year, range.startDate.month - 1, nowDay)) if (dateObj.year === baseDate.getUTCFullYear() && dateObj.month === baseDate.getUTCMonth() + 1 && dateObj.day === baseDate.getUTCDate()) { //console.log(`getPX!!!?::${dateObj.ISO}:`, baseDate.getUTCFullYear(), baseDate.getUTCMonth() + 1, nowDay, baseX) relativeX = baseX break } } if (preciseX === -1) { lowerTimes = (dateObj.hours * 60 * 60 * 1000) + (dateObj.minutes * 60 * 1000) + (dateObj.seconds * 1000) + dateObj.milliseconds scalePerMilliseconds = 24 * 60 * 60 * 1000 preciseX = lowerTimes / scalePerMilliseconds * scaleWidth + relativeX } break case 'hour': // Calculate the relative base position within a range based on a global scale. for (let i = 0; i < Math.ceil(range.width / scaleWidth); i++) { const nowHour = range.startDate.hours + i const baseX = scaleWidth * i const baseDate = new Date(Date.UTC(range.startDate.year, range.startDate.month - 1, range.startDate.day, nowHour)) if (dateObj.year === baseDate.getUTCFullYear() && dateObj.month === baseDate.getUTCMonth() + 1 && dateObj.day === baseDate.getUTCDate() && dateObj.hours === baseDate.getUTCHours()) { //console.log(`getPX!!!?::${dateObj.ISO}:`, baseDate.getUTCFullYear(), baseDate.getUTCMonth() + 1, baseDate.getUTCDate(), baseDate.getUTCHours(), nowHour, baseX) relativeX = baseX break } } if (preciseX === -1) { lowerTimes = (dateObj.minutes * 60 * 1000) + (dateObj.seconds * 1000) + dateObj.milliseconds scalePerMilliseconds = 60 * 60 * 1000 preciseX = lowerTimes / scalePerMilliseconds * scaleWidth + relativeX } break case 'minute': // Calculate the relative base position within a range based on a global scale. for (let i = 0; i < Math.ceil(range.width / scaleWidth); i++) { const nowMinute = range.startDate.minutes + i const baseX = scaleWidth * i const baseDate = new Date(Date.UTC(range.startDate.year, range.startDate.month - 1, range.startDate.day, range.startDate.hours, nowMinute)) if (dateObj.year === baseDate.getUTCFullYear() && dateObj.month === baseDate.getUTCMonth() + 1 && dateObj.day === baseDate.getUTCDate() && dateObj.hours === baseDate.getUTCHours() && dateObj.minutes === baseDate.getUTCMinutes()) { //console.log(`getPX!!!?::${dateObj.ISO}:`, baseDate.getUTCFullYear(), baseDate.getUTCMonth() + 1, baseDate.getUTCDate(), baseDate.getUTCHours(), nowHour, baseX) relativeX = baseX break } } if (preciseX === -1) { lowerTimes = (dateObj.seconds * 1000) + dateObj.milliseconds scalePerMilliseconds = 60 * 1000 preciseX = lowerTimes / scalePerMilliseconds * scaleWidth + relativeX } break case 'second': // Calculate the relative base position within a range based on a global scale. for (let i = 0; i < Math.ceil(range.width / scaleWidth); i++) { const nowSecond = range.startDate.seconds + i const baseX = scaleWidth * i const baseDate = new Date(Date.UTC(range.startDate.year, range.startDate.month - 1, range.startDate.day, range.startDate.hours, range.startDate.minutes, nowSecond)) if (dateObj.year === baseDate.getUTCFullYear() && dateObj.month === baseDate.getUTCMonth() + 1 && dateObj.day === baseDate.getUTCDate() && dateObj.hours === baseDate.getUTCHours() && dateObj.minutes === baseDate.getUTCMinutes() && dateObj.seconds === baseDate.getUTCSeconds()) { //console.log(`getPX!!!?::${dateObj.ISO}:`, baseDate.getUTCFullYear(), baseDate.getUTCMonth() + 1, baseDate.getUTCDate(), baseDate.getUTCHours(), nowHour, baseX) relativeX = baseX break } } if (preciseX === -1) { lowerTimes = dateObj.milliseconds scalePerMilliseconds = 1000 preciseX = lowerTimes / scalePerMilliseconds * scaleWidth + relativeX } break case 'millisecond': // Calculate the relative base position within a range based on a global scale. for (let i = 0; i < Math.ceil(range.width / scaleWidth); i++) { const nowMillisecond = range.startDate.milliseconds + i const baseX = scaleWidth * i const baseDate = new Date(Date.UTC(range.startDate.year, range.startDate.month - 1, range.startDate.day, range.startDate.hours, range.startDate.minutes, range.startDate.seconds, nowMillisecond)) if (dateObj.year === baseDate.getUTCFullYear() && dateObj.month === baseDate.getUTCMonth() + 1 && dateObj.day === baseDate.getUTCDate() && dateObj.hours === baseDate.getUTCHours() && dateObj.minutes === baseDate.getUTCMinutes() && dateObj.seconds === baseDate.getUTCSeconds() && dateObj.milliseconds === baseDate.getUTCMilliseconds()) { console.log(`getPX!!!?::${dateObj.ISO}:`, nowMillisecond, baseX, baseDate.toISOString(), preciseX) relativeX = baseX break } } if (preciseX === -1) { preciseX = relativeX } break } return Math.round(preciseX * 1000) / 1000 } /** * Calculates the optimal height of the event node according to the stage on which it is placed. * * @param {PointerSize | undefined} size - Number of pixels or pointer size strings. * @param {StageRange} range - An object that has been defined the stage range sizes. * @returns {number} - Height of an event node (unit: pixels). */ export const getNodeHeight = (size: PointerSize | undefined = 'md', range: StageRange): number => { const minHeight = Math.min(range.minScaleWidth, range.rowHeight) / 10 const sizeStr = String(size) const maxHeight = range.rowHeight - 2 let nodeHeight: number switch (sizeStr.toLowerCase()) { case 'xs': nodeHeight = Math.min(8, Math.floor(minHeight * 1.3335)) break case 'sm': nodeHeight = Math.min(12, Math.floor(minHeight * 1.8335)) break case 'lg': nodeHeight = Math.max(24, Math.floor(minHeight * 3.1628)) break case 'xl': nodeHeight = Math.max(36, Math.floor(minHeight * 4.8989)) break default: if (/^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$/.test(sizeStr)) { const sizeNumber = Number(sizeStr) if (sizeNumber <= 0) { nodeHeight = Math.min(16, Math.floor(minHeight * 2.3428)) } else { nodeHeight = sizeNumber < 1 ? Math.floor(sizeNumber * maxHeight) : Math.min(sizeNumber, maxHeight) } } else { nodeHeight = Math.min(16, Math.floor(minHeight * 2.3428)) } break } //console.log(`size!!!::${sizeStr}:`, minHeight, maxHeight, nodeHeight) return Math.round(nodeHeight * 1000) / 1000 } /** * Optimize loaded event node data for rendering on the DOM. * * @param {Partial<EventNode>} baseObj * @param {TimelineOptions} options * @param {Record<string, any>} measurements * @returns {Partial<EventNode>} */ export const optimizeEventNode = ( baseObj: Partial<EventNode>, options: TimelineOptions, measurements: Measures ): Partial<EventNode> => { const globalScale = options.scale as Scale const newObj = deepCloneObject(baseObj)! as Partial<EventNode> // uid: You cannot set externally for the system internal value. // This value must be strictly unique in the entire event list. newObj.uid = hash(String(window.performance.now())) const range: StageRange = { width: measurements.rulerActualWidth, height: measurements.sidebarHeight, minScaleWidth: Math.floor(measurements.rulerActualWidth / measurements.rulerMaxCols), maxRows: measurements.sidebarItems, rowHeight: measurements.sidebarItemHeight, startDate: measurements.startDate, sts: measurements.startDate?.ts || 0, scept: measurements.startDate?.cept || 0, endDate: measurements.endDate, ets: measurements.endDate?.ts || 0, ecept: measurements.endDate?.cept || 0, } if (!newObj.hasOwnProperty('s') || typeof newObj.s !== 'object') { newObj.x = newObj.y = -1 newObj.w = newObj.h = 0 return newObj as Partial<EventNode> } const startBaseY = getBaseRowCoordinate(newObj, range, options) //const startBaseX = getBaseX(newObj.s!.ts, range.sts!, range.ets!, range.width, range.minScaleWidth) //newObj.extends = {} //newObj.extends.startBaseX = startBaseX //newObj.x = startBaseX + getPreciseX(newObj.s!, globalScale, range/*.minScaleWidth*/) newObj.x = getPreciseX(newObj.s!, globalScale, range) //console.log(`startX!!!::${newObj.eventId}:`, newObj) newObj.h = Math.floor((getNodeHeight(newObj.size || 'md', range)) * 1000) / 1000 if (!newObj.hasOwnProperty('end')) { newObj.h = Math.floor(newObj.h * 1000) / 1000 } newObj.y = Math.floor((startBaseY + Math.floor((range.rowHeight - newObj.h) / 2)) * 1000) / 1000 if (newObj.hasOwnProperty('end')) { const monthNames = options.ruler.filters?.monthNames ?? undefined const dayNames = options.ruler.filters?.dayNames ?? undefined const firstDayOfWeek = options.ruler.firstDayOfWeek || 0 const parsedEndDate = parseDateTime(newObj.end!, options.timezone, monthNames, dayNames, firstDayOfWeek) if (!!parsedEndDate) { // The bar-typed event node with start and end datetime. newObj.e = parsedEndDate //const endBaseX = getBaseX(newObj.e!.ts, range.sts!, range.ets!, range.width, range.minScaleWidth) //newObj.extends.endBaseX = endBaseX //const endX = (endBaseX - newObj.x >= range.minScaleWidth ? endBaseX - range.minScaleWidth : endBaseX) + getPreciseX(newObj.e!, globalScale, range.minScaleWidth) const endX = getPreciseX(newObj.e!, globalScale, range/*.minScaleWidth*/) const startDateOfMilliseconds = newObj.s.ts * 1000 + newObj.s.milliseconds const endDateOfMilliseconds = newObj.e.ts * 1000 + newObj.e.milliseconds //console.log(`!!!::${newObj.eventId}:`, startBaseX, newObj.x, endX, 'width:', endX - newObj.x, endDateOfMilliseconds, startDateOfMilliseconds) if (endDateOfMilliseconds > startDateOfMilliseconds) { if (endX >= range.width) { newObj.w = Math.round((range.width - newObj.x) * 1000) / 1000 //} else if (endX > 0) { // newObj.w = Math.round((startBaseX + endX - newObj.x) * 1000) / 1000 } else { //console.log(`endX::${newObj.eventId}:`, startBaseX, endBaseX, endX, newObj.x) //newObj.w = Math.round((endBaseX + endX - newObj.x) * 1000) / 1000 newObj.w = Math.round((endX - newObj.x) * 1000) / 1000 } } //console.log(`endX::${newObj.eventId}:`, startBaseX, '+', endX, '=', startBaseX + endX, endBaseX, endBaseX - startBaseX) /* if (endX > newObj.x) { //newObj.w = Math.round((endX > range.width ? range.width - newObj.x : endX - newObj.x) * 1000) / 1000 newObj.w = Math.round((endX > range.width ? range.width - newObj.x : endX - newObj.x) * 1000) / 1000 }*/ } else { // Handle as same the pointer-typed event node positioning when invalid end datetime. newObj.w = newObj.h if (newObj.x < range.width - newObj.w / 2) { newObj.x = Math.round((newObj.x - (newObj.w / 2)) * 1000) / 1000 } } } else { // The pointer-typed event node with start datetime only. newObj.w = newObj.h if (newObj.x < range.width - newObj.w / 2) { newObj.x = Math.round((newObj.x - (newObj.w / 2)) * 1000) / 1000 } } //console.log(`optimizeEventNode::${newObj.eventId}:`, newObj, range, newObj.x, newObj.y, newObj.w, newObj.h) return newObj as Partial<EventNode> } export const findEvents = (targetElementId: string, conditions: { [key: string]: any }/*, fromCache: boolean | undefined = false*/): any => { const targetElement = document.getElementById(targetElementId)! const eventNodeCollection = targetElement.querySelectorAll('.sunorhc-timeline-event-node')! if (eventNodeCollection.length == 0) { return [] } let eventNodes: Record<string, any>[] = [] Array.from(eventNodeCollection).forEach((elm: Element): void => { const eventNode = elm as HTMLElement eventNodes.push({ id: eventNode.dataset!.eventId, type: eventNode.classList.contains('bar-type') ? 'bar' : 'pointer', x: eventNode.offsetLeft, y: eventNode.offsetTop, w: eventNode.offsetWidth, h: eventNode.offsetHeight, }) }) let matches: any = null for (const key in conditions) { //console.log('findEvents:!!!:', eventNodes, key, conditions[key]) switch(key.toLowerCase()) { case 'latest': if (conditions[key]) { matches = { id: null, a: 0 } eventNodes.forEach((item) => { // The event with the largest end is the latest. if (matches.a <= item.x + item.w) { matches = { id: item.id, a: item.x + item.w } } }) } break case 'id': if (conditions[key]) { const pattern = new RegExp(`^(EventNode:)?${conditions[key]}$`, 'i') matches = eventNodes.filter((item) => pattern.test(item.id)) } matches = Array.isArray(matches) && matches.length > 0 ? matches[0] : null break default: break } } //console.log('findEvents:!!!:', matches, conditions, fromCache) if (!!matches && !isEmptyObject(matches)) { return targetElement.querySelector(`.sunorhc-timeline-event-node[data-event-id="${matches.id}"]`) } else { return matches } } /** * reduce an array of scale collections with scales truncation lewer than specific scale. * * @param {Scale} scale - The scale of the base point that separates the upper and lower. * @param {string[]} scaleCollection - An array containing multiple scale strings that assumes the rows property * of the RulerConfig interface. * @returns {string[]} - An array of scale collections with reduced lower scales. */ export const truncateLowerRulerItems = (scale: Scale, scaleCollection: string[]): string[] => { const scaleIndex = [ 'year', 'month', 'week', 'weekday', 'day', 'hour', 'minute', 'second', 'millisecond' ] const scaleIndexBounds = scaleIndex.findIndex(v => v === scale) const remainScales = scaleCollection.filter((scaleStr: string) => { return scaleIndexBounds >= scaleIndex.findIndex(v => v === toValidScale(scaleStr)) }) console.log('truncateLowerRulerItems!!!::', scale, scaleCollection, scaleIndexBounds, remainScales) return remainScales } /** * Save data to specified web storage. * * @param {StorageType} storage - 'sessionStorage' or 'localStorage' * @param {string} key * @param {any} data * @param {boolean} hasExpiration - Whether the data to be stored has an expiration period; Default to false. */ export const saveToStorage = (storage: StorageType, key: string, data: any, hasExpiration: boolean = false): void => { if (typeof data === 'object') { data = JSON.stringify(data) } else if (typeof data !== 'string') { data = String(data) } const compressedString = LZString.compressToUTF16(data) const base64String = btoa(encodeURIComponent(compressedString)); //console.log(base64String, base64String.length); (window[storage] as Storage).setItem(key, base64String) if (hasExpiration) { const expirationKey = `${key}:expiration` const expirationData = JSON.stringify({ hash: hash(base64String), cached: new Date().getTime() }); (window[storage] as Storage).setItem(expirationKey, expirationData) } } /** * Load data from specified web storage. * If the valid storage period expiredTime is specified, the expiration date of the saved data will be checked, * the expired data will be discarded, and null will be returned. * * @param {StorageType} storage - 'sessionStorage' or 'localStorage' * @param {string} key * @param {number} expiredTime - Time until expiration from stored (unit: milliseconds); Default to 0 is never expired. * @returns {any | null} */ export const loadFromStorage = (storage: StorageType, key: string, expiredTime: number = 0): any | null => { const rawData = (window[storage] as Storage).getItem(key) if (!rawData) { return null } if (expiredTime > 0) { // An expiration time is specified. const checkExpirationKey = `${key}:expiration` const rawExpirationData = (window[storage] as Storage).getItem(checkExpirationKey) if (rawExpirationData !== null) { const expirationData = JSON.parse(rawExpirationData) const nowTime = new Date().getTime() //console.log('getExpirationData:', expirationData, hash(rawData), nowTime, expirationData.cached + expiredTime >= nowTime, expirationData.hash !== hash(rawData)) if (expirationData.cached + expiredTime < nowTime || expirationData.hash !== hash(rawData)) { // Abort cache data because expired or unmatched hash of cache. removeToStorage(storage, checkExpirationKey, key) return null } } } const compressedString = decodeURIComponent(atob(rawData)) const decompressString = LZString.decompressFromUTF16(compressedString) if (!decompressString) { return null } try { return JSON.parse(decompressString) } catch (e) { return decompressString } } /** * Remove data from specified web storage. It can be multiple removing data at once too. * * @param {StorageType} storage - 'sessionStorage' or 'localStorage' * @param {string[]} keys - You can specify multiple keys to delete data at once. */ export const removeToStorage = (storage: StorageType, ...keys: string[]): void => { keys.forEach((key: string) => { console.log('removeToStorage:', key, storage); (window[storage] as Storage).removeItem(key) }) } |