All files / components/crud crud-button.ts

22.22% Statements 4/18
0% Branches 0/4
0% Functions 0/4
22.22% Lines 4/18

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 701x     1x   1x         1x                                                                                                                      
import {
  Button, ITooltip, Tooltip, GridEditable,
} from '@zeedhi/common';
import { KeyMap } from '@zeedhi/core';
import { ICrudButton } from './interfaces';
import { CrudForm } from './crud-form';
 
/**
 * Button to be used on Crud Forms
 */
export class CrudButton extends Button implements ICrudButton {
  /**
   * Click shortcut key
   */
  public clickShortcutKey?: string;
 
  /**
   * Button tooltip
   */
  public tooltip: Tooltip;
 
  private tooltipDefault: ITooltip = { name: 'crud-button-tooltip', component: 'ZdTooltip', bottom: true };
 
  /**
   * Create new Crud Button
   * @param props component properties
   */
  public constructor(props: ICrudButton) {
    super(props);
    this.clickShortcutKey = props.clickShortcutKey;
    this.tooltip = new Tooltip({ ...this.tooltipDefault, ...props.tooltip });
    Iif (this.clickShortcutKey) {
      this.keyMap = {
        ...this.keyMap,
        [this.clickShortcutKey]: {
          event: this.shortcutClick.bind(this),
          stop: true,
          active: true,
          input: true,
        },
      };
    }
  }
 
  private shortcutClick({ event, element }: any) {
    Iif (!this.disabled) {
      this.click(event, element);
    }
  }
 
  public initKeyMap(parentElement: HTMLElement) {
    KeyMap.bind(this.keyMap, this, parentElement);
  }
 
  /**
   * Return if parent (form or grid) is editing
   */
  get parentIsEditing() {
    Iif (this.parent instanceof CrudForm) {
      return (this.parent as CrudForm).editing;
    }
 
    Iif (this.parent instanceof GridEditable) {
      return (this.parent as GridEditable).editing;
    }
 
    return false;
  }
}