All files / src/components/zd-iterable iterable-columns-button.ts

100% Statements 18/18
100% Branches 6/6
100% Functions 4/4
100% Lines 17/17

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 7793x   93x   93x 93x         93x                           9x             9x   9x                                           9x 7x 2x 1x   1x         2x 2x     2x       8x      
import { Loader, Metadata } from '@zeedhi/core';
import { Column } from '..';
import { Button } from '../zd-button/button';
import { IIterableColumnsButton } from './interfaces';
import { Iterable } from './iterable';
import { IterableColumnsButtonController } from './iterable-columns-button-controller';
 
/**
 * Base class for Columns Button component
 */
export class IterableColumnsButton<T extends Column = Column> extends Button implements IIterableColumnsButton {
	/**
	 * Iterable component name
	 */
	public iterableComponentName!: string;
 
	/**
	 * Iterable component name
	 */
	public iterableComponent!: Iterable<T>;
 
	/**
	 * Columns to be ignored
	 */
	public ignoreColumns: string[] = [];
 
	/**
	 * Component controller
	 */
	public controller: any;
 
	public iconName = 'mdi-table-headers-eye';
 
	public icon = true;
 
	/* istanbul ignore next */
	/**
	 * Creates a new Iterable Page component.
	 * @param props Iterable page component properties
	 */
	constructor(props: IIterableColumnsButton) {
		super(props);
		this.iterableComponentName = this.getInitValue(
			'iterableComponentName',
			props.iterableComponentName,
			this.iterableComponentName,
		);
		this.iconName = this.getInitValue('iconName', props.iconName, this.iconName);
		this.icon = this.getInitValue('icon', props.icon, this.icon);
		this.ignoreColumns = this.getInitValue('ignoreColumns', props.ignoreColumns, this.ignoreColumns);
		this.setIterableComponent();
		this.createAccessors();
	}
 
	private setIterableComponent() {
		if (this.iterableComponentName && Metadata.getInstances(this.iterableComponentName).length > 0) {
			this.iterableComponent = Metadata.getInstance(this.iterableComponentName);
		} else if (this.parent instanceof Iterable) {
			this.iterableComponent = this.parent;
		} else {
			throw new Error(`Could not find the iterable component associated with ${this.name}`);
		}
	}
 
	public onCreated() {
		super.onCreated();
		Loader.addController(`IterableColumnsButtonController_${this.name}`, IterableColumnsButtonController, [
			this.iterableComponent,
		]);
		this.controller = Loader.getInstance(`IterableColumnsButtonController_${this.name}`);
	}
 
	public filteredColumns() {
		return this.iterableComponent.columns.filter((column) => this.ignoreColumns.indexOf(column.name) === -1);
	}
}