All files / src options.ts

100% Statements 23/23
100% Branches 18/18
100% Functions 4/4
100% Lines 23/23

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      4x 116x 492x 8x   484x                                                                                                                                                                                                               4x                                                                   4x       23x 23x   23x 167x 167x   167x 6x 161x 112x   112x 60x 60x 28x         112x 84x         23x    
import { assignDeep, each, isObject, keys, isArray, hasOwnProperty, isFunction } from 'support';
import { PartialOptions } from 'typings';
 
const stringify = (value: any) =>
  JSON.stringify(value, (_, val) => {
    if (isFunction(val)) {
      throw new Error();
    }
    return val;
  });
 
export type ResizeBehavior = 'none' | 'both' | 'horizontal' | 'vertical';
 
export type OverflowBehavior = 'hidden' | 'scroll' | 'visible' | 'visible-hidden';
 
export type VisibilityBehavior = 'visible' | 'hidden' | 'auto';
 
export type AutoHideBehavior = 'never' | 'scroll' | 'leave' | 'move';
 
export type ScrollBehavior = 'always' | 'ifneeded' | 'never';
 
export type BasicEventCallback = (this: any) => void;
 
export type ScrollEventCallback = (this: any, args?: UIEvent) => void;
 
export type OverflowChangedCallback = (this: any, args?: OverflowChangedArgs) => void;
 
export type OverflowAmountChangedCallback = (this: any, args?: OverflowAmountChangedArgs) => void;
 
export type DirectionChangedCallback = (this: any, args?: DirectionChangedArgs) => void;
 
export type SizeChangedCallback = (this: any, args?: SizeChangedArgs) => void;
 
export type UpdatedCallback = (this: any, args?: UpdatedArgs) => void;
 
export interface OSOptions {
  resize: ResizeBehavior;
  paddingAbsolute: boolean;
  updating: {
    elementEvents: Array<[string, string]> | null;
    attributes: string[] | null;
    debounce: number | [number, number] | null;
  };
  overflow: {
    x: OverflowBehavior;
    y: OverflowBehavior;
  };
  scrollbars: {
    visibility: VisibilityBehavior;
    autoHide: AutoHideBehavior;
    autoHideDelay: number;
    dragScroll: boolean;
    clickScroll: boolean;
    touch: boolean;
  };
  textarea: {
    dynWidth: boolean;
    dynHeight: boolean;
    inheritedAttrs: string | Array<string> | null;
  };
  nativeScrollbarsOverlaid: {
    show: boolean;
    initialize: boolean;
  };
  callbacks: {
    onUpdated: (() => any) | null;
  };
  /*
  callbacks?: {
    onInitialized?: BasicEventCallback | null;
    onInitializationWithdrawn?: BasicEventCallback | null;
    onDestroyed?: BasicEventCallback | null;
    onScrollStart?: ScrollEventCallback | null;
    onScroll?: ScrollEventCallback | null;
    onScrollStop?: ScrollEventCallback | null;
    onOverflowChanged?: OverflowChangedCallback | null;
    onOverflowAmountChanged?: OverflowAmountChangedCallback | null;
    onDirectionChanged?: DirectionChangedCallback | null;
    onContentSizeChanged?: SizeChangedCallback | null;
    onHostSizeChanged?: SizeChangedCallback | null;
    onUpdated?: UpdatedCallback | null;
  };
  */
}
 
export interface OverflowChangedArgs {
  x: boolean;
  y: boolean;
  xScrollable: boolean;
  yScrollable: boolean;
  clipped: boolean;
}
 
export interface OverflowAmountChangedArgs {
  x: number;
  y: number;
}
 
export interface DirectionChangedArgs {
  isRTL: number;
  dir: string;
}
 
export interface SizeChangedArgs {
  width: number;
  height: number;
}
 
export interface UpdatedArgs {
  forced: boolean;
}
 
export const defaultOptions: OSOptions = {
  resize: 'none', // none || both  || horizontal || vertical || n || b || h || v
  paddingAbsolute: false, // true || false
  updating: {
    elementEvents: [['img', 'load']], // array of tuples || null
    attributes: null,
    debounce: [0, 33], // number || number array || null
  },
  overflow: {
    x: 'scroll', // visible-hidden  || visible-scroll || hidden || scroll || v-h || v-s || h || s
    y: 'scroll', // visible-hidden  || visible-scroll || hidden || scroll || v-h || v-s || h || s
  },
  scrollbars: {
    visibility: 'auto', // visible || hidden || auto || v || h || a
    autoHide: 'never', // never || scroll || leave || move || n || s || l || m
    autoHideDelay: 800, // number
    dragScroll: true, // true || false
    clickScroll: false, // true || false
    touch: true, // true || false
  },
  textarea: {
    dynWidth: false, // true || false
    dynHeight: false, // true || false
    inheritedAttrs: ['style', 'class'], // string || array || null
  },
  nativeScrollbarsOverlaid: {
    show: false, // true || false
    initialize: false, // true || false
  },
  callbacks: {
    onUpdated: null,
  },
};
 
export const getOptionsDiff = <T>(
  currOptions: T,
  newOptions: PartialOptions<T>
): PartialOptions<T> => {
  const diff: PartialOptions<T> = {};
  const optionsKeys = keys(newOptions).concat(keys(currOptions));
 
  each(optionsKeys, (optionKey) => {
    const currOptionValue = currOptions[optionKey];
    const newOptionValue = newOptions[optionKey];
 
    if (isObject(currOptionValue) && isObject(newOptionValue)) {
      assignDeep((diff[optionKey] = {}), getOptionsDiff(currOptionValue, newOptionValue));
    } else if (hasOwnProperty(newOptions, optionKey) && newOptionValue !== currOptionValue) {
      let isDiff = true;
 
      if (isArray(currOptionValue) || isArray(newOptionValue)) {
        try {
          if (stringify(currOptionValue) === stringify(newOptionValue)) {
            isDiff = false;
          }
        } catch {}
      }
 
      if (isDiff) {
        diff[optionKey] = newOptionValue;
      }
    }
  });
 
  return diff;
};