All files / src/lib/wvr-button wvr-button.component.ts

80.43% Statements 37/46
43.75% Branches 7/16
90% Functions 18/20
80% Lines 36/45

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                          1x   76x     76x           76x         76x       50x         50x         50x         50x         50x         50x         50x         50x         50x         50x         50x                                                               8x         4x         1x 1x 1x                       76x         76x 76x         76x                                                             9x 9x           9x   9x 1x   1x     8x 2x   2x   2x     6x                  
import { AfterViewInit, ChangeDetectionStrategy, Component, HostBinding, Injector, Input } from '@angular/core';
import * as JSON5 from 'json5';
import { ActionRegistryService } from '../core/action-registry.service';
import { ThemeVariantName } from '../shared/theme';
import { projectContent } from '../shared/utility/projection.utility';
import { WvrBaseComponent } from '../shared/wvr-base.component';
 
@Component({
  selector: 'wvr-button-component',
  templateUrl: './wvr-button.component.html',
  styleUrls: ['./wvr-button.component.scss'],
  changeDetection: ChangeDetectionStrategy.Default
})
export class WvrButtonComponent extends WvrBaseComponent implements AfterViewInit {
 
  htmlId = `wvr-button-${this.id}`;
 
  /** Used to define the class type for button component.  */
  @Input() themeVariant: ThemeVariantName = 'primary';
 
  /** Used to define the size for button component.  */
  @Input() btnSize: 'large' | 'small' | 'block';
 
  /** Used to define the type of a button.  */
  @Input() btnType: 'button' | 'checkbox' | 'radio' | 'reset' | 'submit' | 'link' = 'button';
 
  /** Allows for the button component to be an anchor tag component if href property present. */
  @Input() href: string;
 
  @Input() disabled = false;
 
  /** Allows for the override of background */
  @Input() set background(value: string) {
    this.applyThemeOverride(`--${this.themeVariant}-button-bg`, value);
  }
 
  /** Allows for the override of active background */
  @Input() set backgroundActive(value: string) {
    this.applyThemeOverride(`--${this.themeVariant}-button-active-bg`, value);
  }
 
  /** Allows for the override of hover background */
  @Input() set backgroundHover(value: string) {
    this.applyThemeOverride(`--${this.themeVariant}-button-hover-bg`, value);
  }
 
  /** Allows for the override of border */
  @Input() set borderColor(value: string) {
    this.applyThemeOverride(`--${this.themeVariant}-button-border`, value);
  }
 
  /** Allows for the override of active border */
  @Input() set borderActive(value: string) {
    this.applyThemeOverride(`--${this.themeVariant}-button-active-border`, value);
  }
 
  /** Allows for the override of hover border */
  @Input() set borderHover(value: string) {
    this.applyThemeOverride(`--${this.themeVariant}-button-hover-border`, value);
  }
 
  /** Allows for the override of button border in focus state */
  @Input() set borderFocus(value: string) {
    this.applyThemeOverride(`--${this.themeVariant}-button-focus-border`, value);
  }
 
  /** Allows for override of box-shadow propery when the button is in focus state */
  @Input() set boxShadowFocus(value: string) {
    this.applyThemeOverride(`--${this.themeVariant}-button-focus-box-shadow`, value);
  }
 
  /** Allows for the override of color */
  @Input() set color(value: string) {
    this.applyThemeOverride(`--${this.themeVariant}-button-color`, value);
  }
 
  /** Allows for the override of active color */
  @Input() set colorActive(value: string) {
    this.applyThemeOverride(`--${this.themeVariant}-button-active-color`, value);
  }
 
  /** Allows for the override of hover color */
  @Input() set colorHover(value: string) {
    this.applyThemeOverride(`--${this.themeVariant}-button-hover-color`, value);
  }
 
  /** Allows for the override of button border radius */
  @HostBinding('style.--wvr-btn-border-radius') @Input() borderRadius;
 
  /** Allows for the override of button cursor property */
  @HostBinding('style.--wvr-btn-cursor') @Input() cursor;
 
  /** Allows for the override of button font family property */
  @HostBinding('style.--wvr-btn-font-family-sans-serif') @Input() fontFamily;
 
  /** Allows for the override of button font size property */
  @HostBinding('style.--wvr-btn-font-size') @Input() fontSize;
 
  /** Allows for the override of button font weight property */
  @HostBinding('style.--wvr-btn-font-weight') @Input() fontWeight;
 
  /** Allows for the override of button hover line height property */
  @HostBinding('style.--wvr-btn-line-height') @Input() lineHeight;
 
  /** Allows for the override of button padding property */
  @HostBinding('style.--wvr-btn-padding') @Input() padding;
 
  /** Allows for the override of button text align property */
  @HostBinding('style.--wvr-btn-text-align') @Input() textAlign;
 
  /** Allows for the override of button vertical align property */
  @HostBinding('style.--wvr-btn-vertical-align') @Input() verticalAlign;
 
  private _action: any;
  @Input() set dispatchAction(value: string) {
    this._action = this.parseActionNameAndType(value);
  }
 
  private _actionProps: any;
  @Input() set dispatchActionProps(value: string) {
    this._actionProps = JSON5.parse(value);
  }
 
  private _dispatchActions: Array<ActionAndProps>;
  @Input() set dispatchActions(value: string) {
    const actionInputs = JSON5.parse(value);
    this._dispatchActions = actionInputs.map(ai =>
      ({
        action: this.parseActionNameAndType(ai.action),
        props: ai.props
      }));
  }
 
  @Input() emitEvent: string;
 
  @Input() btnTxt: string;
 
  @Input() testInput: boolean;
 
  variantTypes = ['button'];
 
  actionRegistry?: ActionRegistryService;
 
  constructor(injector: Injector) {
    super(injector);
    this.actionRegistry = injector.get(ActionRegistryService);
  }
 
  /** Called after the view has been intialized. Handles the rendering of the projected content. */
  ngAfterViewInit(): void {
    projectContent(this.eRef, 'template[button-content]', 'span[button-content]');
  }
 
  onClick(): void {
    if (this._dispatchActions) {
      this._dispatchActions.forEach(actionAndProp => {
        this.store.dispatch(actionAndProp.action(
          actionAndProp.props
        ));
      });
    } else if (this._action) {
      this._actionProps ?
      this.store.dispatch(this._action(
        this._actionProps
      )) :
      this.store.dispatch(this._action());
    }
 
    if (this.emitEvent) {
      this.eRef.nativeElement.dispatchEvent(new CustomEvent(this.emitEvent, {
        bubbles: true,
        detail: {
          data: (this.eRef.nativeElement as HTMLElement).dataset,
          button: this
        }
      }));
    }
  }
 
  // tslint:disable-next-line:prefer-function-over-method
  private parseActionNameAndType(nameAndType: string): any {
    const parts = nameAndType.split('.');
    Iif (parts.length !== 2) {
      console.warn(`'${nameAndType}' is not a valid value for 'dispatch-action'. Must in form '[ActionType].[ActionName]'`);
 
      return;
    }
 
    const registeredActions = this.actionRegistry.getActions(parts[0]);
 
    if (!registeredActions) {
      console.warn(`No registered actions were found for '${nameAndType}'.`);
 
      return;
    }
 
    if (!parts[1] || !registeredActions[parts[1]]) {
      const actions = Object.keys(registeredActions)
        .join(',');
      console.warn(`'${parts[1]}' is not a known action of ${parts[0]} (${actions}).`);
 
      return;
    }
 
    return registeredActions[parts[1]];
  }
 
}
 
export interface ActionAndProps {
  action: any;
  props: Object;
}