All files / src/utils timelineStyleUtils.ts

87.32% Statements 62/71
61.85% Branches 60/97
84.61% Functions 22/26
87.32% Lines 62/71

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                          4x         395x 395x     395x     368x     481x                   514x 514x 514x 2275x 10x   2265x 1700x 565x 159x     406x 46x   46x 11x   46x 360x 12x   507x   514x 514x 11x   503x       503x   503x   36x 467x   151x 151x         316x               85x                                       85x                             85x 85x                                   5x 5x                                   5x 5x                                     5x 5x                                     5x 5x                                   5x 5x                                     40x                         44x                         4x                                                                                                                           42x 42x                   42x 42x                                             10x     19x 19x                       19x       8x 8x                   8x      
import { TimelineOptions } from '../settings/timelineOptions';
import { TimelineRowStyle } from '../settings/styles/timelineRowStyle';
import { TimelineKeyframeShape } from '../enums/timelineKeyframeShape';
import { TimelineUtils } from './timelineUtils';
import { TimelineGroupStyle } from '../settings/styles/timelineGroupStyle';
import { TimelineKeyframe } from '../models/timelineKeyframe';
 
import { TimelineGroup } from '../models/timelineGroup';
import { TimelineRow } from '../models/timelineRow';
import { defaultGroupStyle } from '../settings/defaults/defaultGroupStyle';
import { defaultTimelineKeyframeStyle } from '../settings/defaults/defaultTimelineKeyframeStyle';
import { defaultTimelineRowStyle } from '../settings/defaults/defaultTimelineRowStyle';
 
const undefinedConst = typeof undefined;
 
// TODO: create merged style for each element instead of getting per property.
export class TimelineStyleUtils {
  static getGroup(groupModel: TimelineGroup | string | null | undefined): TimelineGroup | null {
    const style = groupModel;
    Iif (style && typeof style === 'string') {
      return null;
    }
    return (style || null) as TimelineGroup;
  }
  static getGroupStyle(groupModel: TimelineGroup | string | null | undefined): TimelineGroupStyle | null {
    return TimelineStyleUtils.getGroup(groupModel)?.style || null;
  }
  static getFirstSet<T>(defaultValue: T, ...params: Array<T | undefined | null>): T {
    return TimelineStyleUtils.getValue(defaultValue, false, ...params);
  }
  /**
   * Get first value set or default.
   * @param defaultValue default value in a case when no value is set.
   * @param returnFalseIfAnyFalse - find first negative bool and return false.
   * @param params collection of values to check.
   * @returns value.
   */
  static getValue<T>(defaultValue: T, returnFalseIfAnyFalse = false, ...params: Array<T | undefined | null>): T {
    const valuesFound: T[] = [];
    let found = false;
    params.forEach((value: T | undefined | null) => {
      if (found) {
        return;
      }
      if (typeof value === undefinedConst) {
        return;
      } else if (typeof value === 'number') {
        Iif (!TimelineUtils.isNumber(value)) {
          return;
        }
      } else if (typeof value === 'boolean') {
        valuesFound.push(value as T);
        // No need to search for other values. First false is turning off current bool functionality.
        if (returnFalseIfAnyFalse && value === false) {
          found = true;
        }
        return;
      } else if (!value) {
        return;
      }
      valuesFound.push(value as T);
    });
    const toReturn = valuesFound && valuesFound.length > 0 ? valuesFound[0] : defaultValue;
    if (found) {
      return false as T;
    }
    return TimelineStyleUtils.getValueOrDefault(toReturn, defaultValue) as T;
  }
 
  static getValueOrDefault<T>(value: T, defaultValue: T): T | undefined {
    Iif (typeof value === undefinedConst) {
      return defaultValue;
    } else if (typeof value == 'boolean') {
      // variable is a boolean
      return value as T;
    } else if (typeof value == 'number') {
      // variable is a boolean
      if (value || value === 0) {
        return value as T;
      } else E{
        return defaultValue;
      }
    }
    return value || defaultValue;
  }
  static keyframeWidth(
    keyframe: TimelineKeyframe | null | undefined,
    group: TimelineGroup | string | null | undefined,
    rowStyle: TimelineRowStyle | null | undefined,
    options: TimelineOptions | null | undefined,
  ): number | string {
    return TimelineStyleUtils.getFirstSet(
      // default value
      defaultTimelineKeyframeStyle.width || '',
      keyframe?.style?.width,
      // exact style
      TimelineStyleUtils.getGroupStyle(group)?.keyframesStyle?.width,
      rowStyle?.keyframesStyle?.width,
      rowStyle?.groupsStyle?.keyframesStyle?.width,
      // global styles
      options?.rowsStyle?.groupsStyle?.keyframesStyle?.width,
      // default keyframe style
      options?.rowsStyle?.keyframesStyle?.width,
    );
  }
  static keyframeHeight(
    keyframe: TimelineKeyframe | null | undefined,
    group: TimelineGroup | string | null | undefined,
    rowStyle: TimelineRowStyle | null | undefined,
    options: TimelineOptions | null | undefined,
  ): number | string {
    return TimelineStyleUtils.getFirstSet(
      // default value
      defaultTimelineKeyframeStyle.height || '',
      keyframe?.style?.height,
      // exact style
      TimelineStyleUtils.getGroupStyle(group)?.keyframesStyle?.height,
      rowStyle?.keyframesStyle?.height,
      rowStyle?.groupsStyle?.keyframesStyle?.height,
      // global styles
      options?.rowsStyle?.groupsStyle?.keyframesStyle?.height,
      // default keyframe style
      options?.rowsStyle?.keyframesStyle?.height,
    );
  }
  static keyframeShape(keyframe: TimelineKeyframe | null, group: TimelineGroup | string | null | undefined, rowStyle: TimelineRowStyle | null, options: TimelineOptions | null): TimelineKeyframeShape {
    const defaultValue = defaultTimelineKeyframeStyle.shape || TimelineKeyframeShape.Rhomb;
    return TimelineStyleUtils.getFirstSet(
      // default value
      defaultValue,
      // exact style
      keyframe?.style?.shape,
      // style set by keyframe group
      TimelineStyleUtils.getGroupStyle(group)?.keyframesStyle?.shape,
      // style from the keyframe group
      rowStyle?.groupsStyle?.keyframesStyle?.shape,
      // style set by keyframe style
      rowStyle?.keyframesStyle?.shape,
      // style set by keyframe group style, applied when group is set
      group ? options?.rowsStyle?.groupsStyle?.keyframesStyle?.shape : undefined,
      // Style set by global options
      options?.rowsStyle?.keyframesStyle?.shape,
    );
  }
  static keyframeFillColor(keyframe: TimelineKeyframe | null, group: TimelineGroup | string | null | undefined, rowStyle: TimelineRowStyle | null, options: TimelineOptions | null): string {
    const defaultValue = defaultTimelineKeyframeStyle.fillColor || '';
    return TimelineStyleUtils.getFirstSet(
      // default value
      defaultValue,
      // exact style
      keyframe?.style?.fillColor,
      // style set by keyframe group
      TimelineStyleUtils.getGroupStyle(group)?.keyframesStyle?.fillColor,
      // style from the keyframe group
      rowStyle?.groupsStyle?.keyframesStyle?.fillColor,
      // style set by keyframe style
      rowStyle?.keyframesStyle?.fillColor,
      // style set by keyframe group style, applied when group is set
      group ? options?.rowsStyle?.groupsStyle?.keyframesStyle?.fillColor : undefined,
      // Style set by global options
      options?.rowsStyle?.keyframesStyle?.fillColor,
    );
  }
  static keyframeSelectedFillColor(keyframe: TimelineKeyframe | null, group: TimelineGroup | string | null | undefined, rowStyle: TimelineRowStyle | null, options: TimelineOptions | null): string {
    const defaultValue = defaultTimelineKeyframeStyle.selectedFillColor || '';
    return TimelineStyleUtils.getFirstSet(
      // default value
      defaultValue,
      // exact style
      keyframe?.style?.selectedFillColor,
      // style set by keyframe group
      TimelineStyleUtils.getGroupStyle(group)?.keyframesStyle?.selectedFillColor,
      // style from the keyframe group
      rowStyle?.groupsStyle?.keyframesStyle?.selectedFillColor,
      // style set by keyframe style
      rowStyle?.keyframesStyle?.selectedFillColor,
      // style set by keyframe group style, applied when group is set
      group ? options?.rowsStyle?.groupsStyle?.keyframesStyle?.selectedFillColor : undefined,
      // Style set by global options
      options?.rowsStyle?.keyframesStyle?.selectedFillColor,
    );
  }
 
  static keyframeStrokeThickness(keyframe: TimelineKeyframe | null, group: TimelineGroup | string | null | undefined, rowStyle: TimelineRowStyle | null, options: TimelineOptions | null): number {
    const defaultValue = defaultTimelineKeyframeStyle.strokeThickness || 0;
    return TimelineStyleUtils.getFirstSet(
      // default value
      defaultValue,
      // exact style
      keyframe?.style?.strokeThickness,
      // style set by keyframe group
      TimelineStyleUtils.getGroupStyle(group)?.keyframesStyle?.strokeThickness,
      // style from the keyframe group
      rowStyle?.groupsStyle?.keyframesStyle?.strokeThickness,
      // style set by keyframe style
      rowStyle?.keyframesStyle?.strokeThickness,
      // style set by keyframe group style, applied when group is set
      group ? options?.rowsStyle?.groupsStyle?.keyframesStyle?.strokeThickness : undefined,
      // Style set by global options
      options?.rowsStyle?.keyframesStyle?.strokeThickness,
    );
  }
 
  static keyframeStrokeColor(keyframe: TimelineKeyframe | null, group: TimelineGroup | string | null | undefined, rowStyle: TimelineRowStyle | null, options: TimelineOptions | null): string {
    const defaultValue = defaultTimelineKeyframeStyle.strokeColor || '';
    return TimelineStyleUtils.getFirstSet(
      // default value
      defaultValue,
      // exact style
      keyframe?.style?.strokeColor,
      // style set by keyframe group
      TimelineStyleUtils.getGroupStyle(group)?.keyframesStyle?.strokeColor,
      // style from the keyframe group
      rowStyle?.groupsStyle?.keyframesStyle?.strokeColor,
      // style set by keyframe style
      rowStyle?.keyframesStyle?.strokeColor,
      // style set by keyframe group style, applied when group is set
      group ? options?.rowsStyle?.groupsStyle?.keyframesStyle?.strokeColor : undefined,
      // Style set by global options
      options?.rowsStyle?.keyframesStyle?.strokeColor,
    );
  }
  static keyframeSelectedStrokeColor(keyframe: TimelineKeyframe | null, group: TimelineGroup | string | null, rowStyle: TimelineRowStyle | null, options: TimelineOptions | null): string {
    const defaultValue = defaultTimelineKeyframeStyle.selectedStrokeColor || '';
    return TimelineStyleUtils.getFirstSet(
      // default value
      defaultValue,
      // exact style
      keyframe?.style?.selectedStrokeColor,
      // style set by keyframe group
      TimelineStyleUtils.getGroupStyle(group)?.keyframesStyle?.selectedStrokeColor,
      // style from the keyframe group
      rowStyle?.groupsStyle?.keyframesStyle?.selectedStrokeColor,
      // style set by keyframe style
      rowStyle?.keyframesStyle?.selectedStrokeColor,
      // style set by keyframe group style, applied when group is set
      group ? options?.rowsStyle?.groupsStyle?.keyframesStyle?.selectedStrokeColor : undefined,
      // Style set by global options
      options?.rowsStyle?.keyframesStyle?.selectedStrokeColor,
    );
  }
 
  static groupHeight(options: TimelineOptions | null | undefined, group: TimelineGroup | string | null | undefined, rowStyle: TimelineRowStyle | null | undefined): number | string {
    return TimelineStyleUtils.getFirstSet(
      // default value
      defaultGroupStyle.height || 'auto',
      // exact group style
      TimelineStyleUtils.getGroupStyle(group)?.height,
      // Row row style
      rowStyle?.groupsStyle?.height,
      // global styles
      options?.rowsStyle?.groupsStyle?.height,
    );
  }
 
  static groupMarginTop(options: TimelineOptions | null | undefined, group: TimelineGroup | string | null | undefined, rowStyle: TimelineRowStyle | null | undefined): number | string {
    return TimelineStyleUtils.getFirstSet(
      // default value
      defaultGroupStyle.marginTop || 'auto',
      // exact style
      TimelineStyleUtils.getGroupStyle(group)?.marginTop,
      // Row row style
      rowStyle?.groupsStyle?.marginTop,
      // global styles
      options?.rowsStyle?.groupsStyle?.marginTop,
    );
  }
 
  static groupFillColor(options: TimelineOptions | null | undefined, group: TimelineGroup | string | null | undefined, rowStyle: TimelineRowStyle | null | undefined): string {
    return TimelineStyleUtils.getFirstSet(
      // default value
      defaultGroupStyle.fillColor || '',
      // exact style
      TimelineStyleUtils.getGroupStyle(group)?.fillColor,
      // Row row style
      rowStyle?.groupsStyle?.fillColor,
      // global styles
      options?.rowsStyle?.groupsStyle?.fillColor,
    );
  }
  static groupStrokeColor(options: TimelineOptions | null | undefined, group: TimelineGroup | string | null | undefined, rowStyle: TimelineRowStyle | null | undefined): string {
    return TimelineStyleUtils.getFirstSet(
      // default value
      defaultGroupStyle.strokeColor || '',
      // exact style
      TimelineStyleUtils.getGroupStyle(group)?.strokeColor,
      // Row row style
      rowStyle?.groupsStyle?.strokeColor,
      // global styles
      options?.rowsStyle?.groupsStyle?.strokeColor,
    );
  }
 
  static groupStrokeThickness(options: TimelineOptions | null | undefined, group: TimelineGroup | string | null | undefined, rowStyle: TimelineRowStyle | null | undefined): number {
    return (
      TimelineStyleUtils.getFirstSet(
        // default value
        defaultGroupStyle.strokeThickness || '',
        // exact style
        TimelineStyleUtils.getGroupStyle(group)?.strokeThickness,
        // Row row style
        rowStyle?.groupsStyle?.strokeThickness,
        // global styles
        options?.rowsStyle?.groupsStyle?.strokeThickness,
      ) || 0
    );
  }
 
  static groupsRadii(
    options: TimelineOptions | null | undefined,
    group: TimelineGroup | string | null | undefined,
    rowStyle: TimelineRowStyle | null | undefined,
  ): number | DOMPointInit | Iterable<number | DOMPointInit> {
    return (
      TimelineStyleUtils.getFirstSet(
        // default value
        defaultGroupStyle.radii || '',
        // exact style
        TimelineStyleUtils.getGroupStyle(group)?.radii,
        // Row row style
        rowStyle?.groupsStyle?.radii,
        // global styles
        options?.rowsStyle?.groupsStyle?.radii,
      ) || 0
    );
  }
 
  /**
   * Get current row height from styles
   */
  static getRowHeight(rowStyle: TimelineRowStyle | null, options: TimelineOptions | null): number {
    const defaultValue = defaultTimelineRowStyle.height || 0;
    return TimelineStyleUtils.getFirstSet(
      // default value
      defaultValue,
      // exact style
      rowStyle?.height,
      // Style set by global options
      options?.rowsStyle?.height,
    );
  }
  static getRowMarginBottom(rowStyle: TimelineRowStyle | null, options: TimelineOptions | null): number {
    const defaultValue = defaultTimelineRowStyle.marginBottom || 0;
    return TimelineStyleUtils.getFirstSet(
      // default value
      defaultValue,
      // exact style
      rowStyle?.marginBottom,
      // Style set by global options
      options?.rowsStyle?.marginBottom,
    );
  }
 
  static getRowFillColor(rowStyle: TimelineRowStyle | null, options: TimelineOptions | null): string {
    const defaultValue = defaultTimelineRowStyle.fillColor || '';
    return TimelineStyleUtils.getFirstSet(
      // default value
      defaultValue,
      // exact style
      rowStyle?.fillColor,
      // Style set by global options
      options?.rowsStyle?.fillColor,
    );
  }
 
  static headerHeight(options: TimelineOptions | null, defaultRowHeight = 30): number {
    return options?.headerHeight || defaultRowHeight;
  }
  static keyframeDraggable(keyframe: TimelineKeyframe | null, group: TimelineGroup | string | null, row: TimelineRow | null, options: TimelineOptions | null, defaultValue = true): boolean {
    const findFirstNegativeBool = true;
    const boolResult = TimelineStyleUtils.getValue<boolean>(
      defaultValue,
      findFirstNegativeBool,
      // Keyframe settings
      keyframe?.draggable,
      // Group settings
      TimelineStyleUtils.getGroup(group)?.keyframesDraggable,
      // Row settings
      row?.keyframesDraggable,
      // Start from global settings first.
      options?.keyframesDraggable,
    );
    return boolResult;
  }
 
  static groupDraggable(group: TimelineGroup | string | null | undefined, row: TimelineRow | null, options: TimelineOptions): boolean {
    const findFirstNegativeBool = true;
    const boolResult = TimelineStyleUtils.getValue<boolean>(
      true,
      findFirstNegativeBool,
      // Group settings
      TimelineStyleUtils.getGroup(group)?.draggable,
      // Row settings
      row?.groupsDraggable,
      // Start from global settings first.
      options?.groupsDraggable,
    );
    return boolResult;
  }
}