All files / components/crud crud-delete-button.ts

15.38% Statements 4/26
0% Branches 0/23
0% Functions 0/6
16.66% Lines 4/24

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 801x 1x   1x         1x                                                                                                                                              
import { I18n } from '@zeedhi/core';
import { Grid } from '@zeedhi/common';
import { ICrudDeleteButton } from './interfaces';
import { CrudButton } from './crud-button';
 
/**
 * Button to be used on Crud Forms
 */
export class CrudDeleteButton extends CrudButton implements ICrudDeleteButton {
  /**
   * Button tooltip
   */
  private defaultTooltip: string = 'DELETE';
 
  /**
   * Button flat
   */
  private defaultFlat: boolean = true;
 
  /**
   * Button icon
   */
  private defaultIcon: boolean = true;
 
  /**
   * Button icon name
   */
  private defaultIconName: string = 'trashCanOutline';
 
  /**
   * Button icon color
   */
  private defaultColor: string = '#666';
 
  /**
   * Create new Crud Delete Button
   * @param props component properties
   */
  public constructor(props: ICrudDeleteButton) {
    super({ ...props, clickShortcutKey: props.clickShortcutKey || 'ctrl+d' });
    this.flat = props.flat !== undefined ? this.flat : this.defaultFlat;
    this.icon = props.icon !== undefined ? this.icon : this.defaultIcon;
    this.iconName = props.iconName !== undefined ? this.iconName : this.defaultIconName;
    this.color = props.color !== undefined ? this.color : this.defaultColor;
    const label = `${I18n.translate(this.defaultTooltip)} (${props.clickShortcutKey || 'ctrl+d'})`;
    this.tooltip.label = this.tooltip.label || label;
  }
 
  /**
   * Triggered when the component is clicked.
   * @param event DOM event
   * @param element Element clicked
   */
  public click(event: Event, element: HTMLElement) {
    super.click(event, element);
  }
 
  get parentHasCurrentRow() {
    Iif (!(this.parent instanceof Grid)) return false;
    const grid = this.parent as Grid;
 
    return !grid.selectable && grid.datasource.currentRow[grid.datasource.uniqueKey];
  }
 
  get parentHasSelectedRows() {
    Iif (!(this.parent instanceof Grid)) return false;
    const grid = this.parent as Grid;
 
    return grid.selectable && grid.selectedRows.length !== 0;
  }
 
  get disabled() {
    return this.parentIsEditing || (!this.parentHasCurrentRow && !this.parentHasSelectedRows);
  }
 
  set disabled(_value: boolean) {
    // do nothing
  }
}