All files / src/lib/shared wvr-base.component.ts

57.14% Statements 44/77
13.79% Branches 4/29
54.55% Functions 12/22
57.33% Lines 43/75

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                                          1x                                       240x         240x                     240x       240x     240x       6x       240x       3x                                                   760x       240x     240x                             240x 240x 240x   240x 240x 240x 240x   240x 240x   240x   240x 240x 240x         234x 234x 234x 234x   234x   234x 234x             234x 234x         232x 232x   232x 252x         234x                                                                                                                           564x 564x                                       234x                                                                                                                           240x          
import { AfterContentInit, ChangeDetectorRef, Directive, ElementRef, EventEmitter, HostBinding, Injector, Input, OnDestroy, OnInit, Output, ViewChild } from '@angular/core';
import { select, Store } from '@ngrx/store';
import * as JSON5 from 'json5';
import { BehaviorSubject, Observable, Subscription } from 'rxjs';
import { filter } from 'rxjs/operators';
import { AnimationService } from '../core/animation.service';
import { ComponentRegistryService } from '../core/component-registry.service';
import { WvrDataSelect } from '../core/data-select';
import * as ManifestActions from '../core/manifest/manifest.actions';
import { NgBindingsService, RefBindingSubject } from '../core/ng-bindings.service';
import { RootState, selectIsMobileLayout, selectManifestEntryResponse } from '../core/store';
import { ThemeService } from '../core/theme/theme.service';
import { AppConfig, APP_CONFIG } from './config';
import { ThemeVariantName } from './theme';
import { wvrParseProjectedContent } from './utility';
import { WvrAnimationComponent } from './wvr-animation.component';
import { WvrDataComponent } from './wvr-data.component';
import { WvrThemeableComponent } from './wvr-themeable.component';
 
@Directive()
// tslint:disable-next-line:directive-class-suffix
export abstract class WvrBaseComponent implements AfterContentInit, OnInit, OnDestroy, WvrAnimationComponent, WvrDataComponent, WvrThemeableComponent {
 
  /** A reference to the ComponentRegistryService */
  readonly componentRegistry: ComponentRegistryService<WvrBaseComponent>;
 
  /** A generated unique identifier for this comonent. */
  readonly id: number;
 
  /** A reference to the ElementRef */
  readonly eRef: ElementRef;
 
  /** A reference to the ChangeDetectorRef */
  readonly cdRef: ChangeDetectorRef;
 
  /** A reference to the AppConfig */
  readonly appConfig: AppConfig;
 
  /** A reference to the Store */
  readonly store: Store<RootState>;
 
  data: { [as: string]: Observable<any> } = {};
 
  // tslint:disable-next-line: prefer-readonly
  @Input() private wvrData: string;
 
  themeOverrides = {};
 
  /** Allows for the override of theme for the particular component.  */
  @Input() set wvrTheme(themeName: string) {
    this.themeService.applyThemeByName(themeName, this);
  }
 
  /** Used to define the class type of an alert component.  */
  @Input() themeVariant: ThemeVariantName;
 
  /** A host binding used to ensure the presense of the `wvr-bootstrap` class. */
  @HostBinding('class.wvr-bootstrap') wvrBootstrap = true;
 
  @HostBinding('style') style;
 
  variantTypes = [];
 
  /** An object representation of the animation instructions for this component. */
  private _animationSettings: any = {};
 
  /** A setter which parses a json string describing animation instructions and stores the derived object in `_animationSettings`. */
  @Input() set animate(value: string) {
    this._animationSettings = JSON5.parse(value);
  }
 
  /** An object representation of the settings specifying the specific behavior of the animation for this component. */
  private _animationConfig: any = {};
 
  /** A setter which parses a json string describing animation setting and stores the derived object in `_animationConfig`. */
  @Input() set animateConfig(value: string) {
    this._animationConfig = JSON5.parse(value);
  }
 
  /** An identifier used to access the animation state for this component. */
  private animationStateId: number;
 
  /** An attribute input allowing for the designation of an animation identifier for the purpose of animation targeting. */
  @Input() animateId: string;
 
  /** An attribute input allowing for the designation of an animation target for animation events. */
  @Input() animateTarget: string;
 
  /** A view child of the template element containing the #animationRoot identifier. */
  @ViewChild('animationRoot') animationRootElem: ElementRef;
 
  /** A reference to the AnimationService */
  private readonly _animationService: AnimationService<WvrBaseComponent>;
 
  /** A reference to the ThemeService */
  private readonly themeService: ThemeService;
 
  /** A reference to the NgBindingsService */
  private readonly ngBindingsService: NgBindingsService;
 
  /** A host bound accessor which applies the wvr-hidden class if both isMobileLayout and hiddenInMobile evaluate to true.  */
  @HostBinding('class.wvr-hidden') private get _hiddenInMobile(): boolean {
    return this.isMobileLayout && this.hiddenInMobile;
  }
 
  /** An attribute input specifying if this component should be hidden in the mobile layout. */
  @Input() hiddenInMobile = false;
 
  /** An Output biding used for triggering animations. */
  @Output() protected readonly animationEventTrigger = new EventEmitter<Event>();
 
  isMobileLayout: boolean;
 
  private _ngBindings: { [key: string]: string };
 
  @Input() set ngBindings(value: string) {
    this._ngBindings = JSON5.parse(value);
  }
 
  isMobile: Observable<boolean>;
 
  protected subscriptions: Array<Subscription>;
 
  constructor(injector: Injector) {
    this.subscriptions = [];
    this.componentRegistry = injector.get(ComponentRegistryService);
    this.id = this.componentRegistry.register(this);
 
    this.eRef = injector.get(ElementRef);
    this.cdRef = injector.get(ChangeDetectorRef);
    this.appConfig = injector.get(APP_CONFIG);
    this.store = injector.get<Store<RootState>>(Store);
 
    this._animationService = injector.get(AnimationService);
    this.themeService = injector.get(ThemeService);
 
    this.ngBindingsService = injector.get(NgBindingsService);
 
    const element = (this.eRef.nativeElement as HTMLElement);
    const htmlIDAttrName = element.hasAttribute('id') ? 'wvr-id' : 'id';
    element.setAttribute(htmlIDAttrName, `${ComponentRegistryService.HTML_ID_BASE}-${this.id}`);
  }
 
  /** Used to setup this component for animating. */
  ngOnInit(): void {
    this.processData();
    this.initializeAnimationRegistration();
    this.themeService.registerComponent(this.id, this);
    wvrParseProjectedContent(this, this.eRef.nativeElement, this.subscriptions);
 
    this.isMobile = this.store.pipe(select(selectIsMobileLayout));
 
    this.subscriptions.push(this.isMobile.subscribe((isMobile: boolean) => {
      this.isMobileLayout = isMobile;
    }));
  }
 
  // TODO: fix this
  /** Used for post content initialization animation setup. */
  ngAfterContentInit(): void {
    this.initializeAnimationElement();
    this.bootstrapNgBindings();
  }
 
  /** Handles the the unregistering of this component with the component registry. */
  ngOnDestroy(): void {
    this.componentRegistry.unRegisterComponent(this.id);
    this.themeService.unRegisterComponent(this.id);
 
    this.subscriptions.forEach((subscription: Subscription) => {
      subscription.unsubscribe();
    });
  }
 
  bootstrapNgBindings(): void {
    Iif (!!this._ngBindings) {
      const win = window as any;
      let elem = this.eRef.nativeElement;
 
      for (const [k, v] of Object.entries(this._ngBindings)) {
        let ngScope;
 
        while (!ngScope && elem.tagName !== 'BODY') {
          const ngElem = win.angular.element(elem);
          if (ngElem.scope() && ngElem.scope()
            .hasOwnProperty(k)) {
            ngScope = ngElem.scope();
            break;
          }
          elem = elem.parentElement;
          if (elem.tagName === 'BODY') {
            console.warn(`${k} not found on ng scope`);
          }
        }
 
        if (!!ngScope) {
          const subject = new BehaviorSubject<any>(ngScope[k]);
 
          const references = this.ngBindingsService.putSubject(k, {
            subject,
            cdRef: this.cdRef,
            eRef: this.eRef
          });
 
          const attribute = this.kebabize(k);
 
          if (references.length === 1) {
            Object.defineProperty(ngScope, k, {
              get: () => subject.getValue(),
              set: (value: any) => {
                references.forEach((sub: RefBindingSubject) => {
                  const subElem = sub.eRef.nativeElement as HTMLElement;
                  if (!value || value === 'undefined' || value === 'null') {
                    subElem.removeAttribute(attribute);
                  } else {
                    subElem.setAttribute(attribute, value);
                  }
                });
              }
            });
          }
 
          Object.defineProperty(this, v, {
            get: () => subject.getValue(),
            set: (value: any): void => {
              if (value !== subject.getValue()) {
                subject.next(value);
                ngScope.$apply();
              }
            }
          });
        }
      }
    }
  }
 
  applyThemeOverride(customProperty: string, value: string): void {
    this.themeOverrides[customProperty] = value;
    this.eRef.nativeElement.style.setProperty(customProperty, value);
  }
 
  /** Plays the animation specified by the incoming animation trigger.  */
  /* istanbul ignore next */
  triggerAnimations(animationTriggerType: string): void {
    const animations: Array<string> = Array.isArray(this._animationSettings[animationTriggerType])
      ? this._animationSettings[animationTriggerType]
      : [this._animationSettings[animationTriggerType]];
    animations.forEach(an => {
      if (an === 'animationTrigger') {
        this._animationService.triggerAnimationTarget(this.animateTarget);
      } else {
        this._animationService
          .playAnimation(this.animationStateId, an, this._animationConfig, this.animationRootElem.nativeElement);
      }
    });
  }
 
  hasWvrData(): boolean {
    return !!this.wvrData;
  }
 
  getWvrData(): string {
    return this.wvrData;
  }
 
  /* istanbul ignore next */
  initializeAnimationElement(): void {
    this._animationService
      .initializeAnimationElement(this.animationStateId, this._animationConfig, this.animationRootElem);
  }
 
  /* istanbul ignore next */
  initializeAnimationRegistration(): void {
    const animationEvents = Object.keys(this._animationSettings);
    if (animationEvents.length) {
      if (this.animateId) {
        this._animationService.registerAnimationTargets(this.animateId, this);
      }
      this.animationStateId = this._animationService.registerAnimationStates();
      animationEvents.forEach(eventName => {
        if (eventName !== 'animationTrigger') {
          (this.eRef.nativeElement as HTMLElement).addEventListener(eventName, this.onAnimationEvent.bind(this));
        }
      });
    }
  }
 
  /** Trigger's the animation specified by the incoming event. */
  /* istanbul ignore next */
  onAnimationEvent($event: Event): void {
    this.triggerAnimations($event.type);
  }
 
  /* istanbul ignore next */
  private processData(): void {
 
    if (!this.wvrData) {
      return;
    }
 
    const valueParsed = JSON5.parse(this.wvrData);
    // tslint:disable-next-line:max-line-length
    const wvrDataSelects: Array<any> = Array.isArray(valueParsed) ? valueParsed : [valueParsed];
 
    wvrDataSelects
      .filter((s: WvrDataSelect) => !!s.manifest && !!s.entry && !!s.as)
      .forEach(s => {
        this.data[s.as] = this.store.pipe(
          select(selectManifestEntryResponse(s.manifest, s.entry)),
          filter(r => !!r)
        );
        this.store.dispatch(ManifestActions.submitRequest({
          request: {
            manifestName: s.manifest,
            entryName: s.entry
          }
        }));
      });
  }
 
  private readonly kebabize = (attribute: string) => attribute.split('')
    .map((letter, idx) => letter.toUpperCase() === letter ? `${idx !== 0 ? '-' : ''}${letter.toLowerCase()}` : letter)
      .join('');
 
}