All files / src/setups structureSetup.ts

100% Statements 79/79
100% Branches 58/58
100% Functions 11/11
100% Lines 79/79

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                                                                                                                          2x   2x 416x 416x     2x   373x           373x   373x 249x 249x     373x     2x             559x 559x     2x             746x 746x           746x 146x     600x     2x     373x           373x 373x 373x 373x     373x 373x 373x 373x 373x 373x                                                               373x                 373x 2238x 2238x   373x 1708x 373x 373x 373x 373x       442x     373x     373x 48x   48x 48x 48x       373x 373x 373x 373x   373x 373x 373x 373x   373x 372x 12x 12x 12x   360x 100x   360x 168x   360x 148x   360x 360x 360x 360x       373x 62x   373x 249x 249x     373x       372x        
import {
  isHTMLElement,
  appendChildren,
  is,
  createDiv,
  contents,
  insertAfter,
  addClass,
  parent,
  indexOf,
  removeElements,
  removeClass,
  push,
  runEach,
  insertBefore,
  attr,
  isBoolean,
  isFunction,
  keys,
} from 'support';
import {
  classNameHost,
  classNamePadding,
  classNameViewport,
  classNameViewportArrange,
  classNameContent,
  classNameViewportScrollbarStyling,
} from 'classnames';
import {
  getEnvironment,
  StructureInitializationStaticElement,
  StructureInitializationDynamicElement,
  StructureInitializationStrategy,
} from 'environment';
import { OSTarget, OSTargetElement, StructureInitialization } from 'typings';
 
export interface OSTargetContext {
  _isTextarea: boolean;
  _isBody: boolean;
  _htmlElm: HTMLHtmlElement;
  _bodyElm: HTMLBodyElement;
  _windowElm: Window;
  _documentElm: HTMLDocument;
  _targetIsElm: boolean;
}
 
export interface PreparedOSTargetObject {
  _target: OSTargetElement;
  _host: HTMLElement;
  _viewport: HTMLElement;
  _padding: HTMLElement | false;
  _content: HTMLElement | false;
  _viewportArrange: HTMLStyleElement | false;
}
 
export interface StructureSetup {
  _targetObj: PreparedOSTargetObject;
  _targetCtx: OSTargetContext;
  _destroy: () => void;
}
 
let contentArrangeCounter = 0;
 
const unwrap = (elm: HTMLElement | false | null | undefined) => {
  appendChildren(parent(elm), contents(elm));
  removeElements(elm);
};
 
const createUniqueViewportArrangeElement = (): HTMLStyleElement | false => {
  const { _nativeScrollbarStyling, _nativeScrollbarIsOverlaid, _cssCustomProperties } =
    getEnvironment();
  /* istanbul ignore next */
  const create =
    !_cssCustomProperties &&
    !_nativeScrollbarStyling &&
    (_nativeScrollbarIsOverlaid.x || _nativeScrollbarIsOverlaid.y);
  const result = create ? document.createElement('style') : false;
 
  if (result) {
    attr(result, 'id', `${classNameViewportArrange}-${contentArrangeCounter}`);
    contentArrangeCounter++;
  }
 
  return result;
};
 
const staticCreationFromStrategy = (
  target: OSTargetElement,
  initializationValue: HTMLElement | undefined,
  strategy: StructureInitializationStaticElement,
  elementClass: string
): HTMLElement => {
  const result =
    initializationValue || (isFunction(strategy) ? strategy(target) : (strategy as null));
  return result || createDiv(elementClass);
};
 
const dynamicCreationFromStrategy = (
  target: OSTargetElement,
  initializationValue: HTMLElement | boolean | undefined,
  strategy: StructureInitializationDynamicElement,
  elementClass: string,
  defaultValue: boolean
): HTMLElement | false => {
  const takeInitializationValue = isBoolean(initializationValue) || initializationValue;
  const result = takeInitializationValue
    ? (initializationValue as boolean | HTMLElement)
    : isFunction(strategy)
    ? strategy(target)
    : strategy;
 
  if (result === null) {
    return defaultValue ? createDiv(elementClass) : false;
  }
 
  return result === true ? createDiv(elementClass) : result;
};
 
export const createStructureSetup = (
  target: OSTarget | StructureInitialization
): StructureSetup => {
  const { _getInitializationStrategy, _nativeScrollbarStyling } = getEnvironment();
  const {
    _host: hostInitializationStrategy,
    _viewport: viewportInitializationStrategy,
    _padding: paddingInitializationStrategy,
    _content: contentInitializationStrategy,
  } = _getInitializationStrategy() as StructureInitializationStrategy;
  const targetIsElm = isHTMLElement(target);
  const targetStructureInitialization = target as StructureInitialization;
  const targetElement = targetIsElm
    ? (target as OSTargetElement)
    : targetStructureInitialization.target;
  const isTextarea = is(targetElement, 'textarea');
  const isBody = !isTextarea && is(targetElement, 'body');
  const ownerDocument: HTMLDocument = targetElement!.ownerDocument;
  const bodyElm = ownerDocument.body as HTMLBodyElement;
  const wnd = ownerDocument.defaultView as Window;
  const evaluatedTargetObj: PreparedOSTargetObject = {
    _target: targetElement,
    _host: isTextarea
      ? staticCreationFromStrategy(
          targetElement,
          targetStructureInitialization.host,
          hostInitializationStrategy,
          classNameHost
        )
      : (targetElement as HTMLElement),
    _viewport: staticCreationFromStrategy(
      targetElement,
      targetStructureInitialization.viewport,
      viewportInitializationStrategy,
      classNameViewport
    ),
    _padding: dynamicCreationFromStrategy(
      targetElement,
      targetStructureInitialization.padding,
      paddingInitializationStrategy,
      classNamePadding,
      !_nativeScrollbarStyling // default value for padding
    ),
    _content: dynamicCreationFromStrategy(
      targetElement,
      targetStructureInitialization.content,
      contentInitializationStrategy,
      classNameContent,
      false // default value for content
    ),
    _viewportArrange: createUniqueViewportArrangeElement(),
  };
  const ctx: OSTargetContext = {
    _windowElm: wnd,
    _documentElm: ownerDocument,
    _htmlElm: parent(bodyElm) as HTMLHtmlElement,
    _bodyElm: bodyElm,
    _isTextarea: isTextarea,
    _isBody: isBody,
    _targetIsElm: targetIsElm,
  };
  const generatedElements = keys(evaluatedTargetObj).reduce((arr, key: string) => {
    const value = evaluatedTargetObj[key];
    return push(arr, value && !parent(value) ? value : false);
  }, [] as HTMLElement[]);
  const elementIsGenerated = (elm: HTMLElement | false) =>
    elm ? indexOf(generatedElements, elm) > -1 : null;
  const { _target, _host, _padding, _viewport, _content, _viewportArrange } = evaluatedTargetObj;
  const destroyFns: (() => any)[] = [];
  const isTextareaHostGenerated = isTextarea && elementIsGenerated(_host);
  const targetContents = isTextarea
    ? _target
    : contents(
        [_content, _viewport, _padding, _host, _target].find(
          (elm) => elementIsGenerated(elm) === false
        )
      );
  const contentSlot = _content || _viewport;
 
  // only insert host for textarea after target if it was generated
  if (isTextareaHostGenerated) {
    insertAfter(_target, _host);
 
    push(destroyFns, () => {
      insertAfter(_host, _target);
      removeElements(_host);
    });
  }
 
  appendChildren(contentSlot, targetContents);
  appendChildren(_host, _padding);
  appendChildren(_padding || _host, _viewport);
  appendChildren(_viewport, _content);
 
  addClass(_host, classNameHost);
  addClass(_padding, classNamePadding);
  addClass(_viewport, classNameViewport);
  addClass(_content, classNameContent);
 
  push(destroyFns, () => {
    if (targetIsElm) {
      appendChildren(_host, contents(contentSlot));
      removeElements(_padding || _viewport);
      removeClass(_host, classNameHost);
    } else {
      if (elementIsGenerated(_content)) {
        unwrap(_content);
      }
      if (elementIsGenerated(_viewport)) {
        unwrap(_viewport);
      }
      if (elementIsGenerated(_padding)) {
        unwrap(_padding);
      }
      removeClass(_host, classNameHost);
      removeClass(_padding, classNamePadding);
      removeClass(_viewport, classNameViewport);
      removeClass(_content, classNameContent);
    }
  });
 
  if (_nativeScrollbarStyling) {
    push(destroyFns, removeClass.bind(0, _viewport, classNameViewportScrollbarStyling));
  }
  if (_viewportArrange) {
    insertBefore(_viewport, _viewportArrange);
    push(destroyFns, removeElements.bind(0, _viewportArrange));
  }
 
  return {
    _targetObj: evaluatedTargetObj,
    _targetCtx: ctx,
    _destroy: () => {
      runEach(destroyFns);
    },
  };
};