novo-elements
Version:
Bullhorn's NOVO Element Repository for Angular 2
249 lines (212 loc) • 11.1 kB
text/typescript
import {
Directive, ElementRef, Input, Renderer2, HostBinding, Component, ChangeDetectionStrategy,
ChangeDetectorRef, Optional, OnInit, OnDestroy, HostListener, ViewChild
} from '@angular/core';
import { CdkCell, CdkCellDef, CdkColumnDef, CdkHeaderCell, CdkHeaderCellDef, DataSource } from '@angular/cdk/table';
import { Subscription } from 'rxjs/Subscription';
import { NovoSelection } from './sort';
import { SimpleTableColumn, SimpleTableActionColumn, SimpleTableActionColumnOption } from './interfaces';
import { Helpers } from '../../utils/Helpers';
import { NovoLabelService } from '../../services/novo-label-service';
/** Workaround for https://github.com/angular/angular/issues/17849 */
export const _NovoCellDef = CdkCellDef;
export const _NovoHeaderCellDef = CdkHeaderCellDef;
export const _NovoColumnDef = CdkColumnDef;
export const _NovoHeaderCell = CdkHeaderCell;
export const _NovoCell = CdkCell;
export class NovoSimpleCellDef extends _NovoCellDef { }
export class NovoSimpleHeaderCellDef extends _NovoHeaderCellDef { }
export class NovoSimpleColumnDef extends _NovoColumnDef {
name: string;
}
export class NovoSimpleHeaderCell<T> extends _NovoHeaderCell implements OnInit {
public role = 'columnheader';
public column: SimpleTableColumn<T>;
constructor(columnDef: CdkColumnDef, private elementRef: ElementRef, private renderer: Renderer2) {
super(columnDef, elementRef, renderer);
renderer.setAttribute(elementRef.nativeElement, 'data-automation-id', `novo-column-header-${columnDef.cssClassFriendlyName}`);
renderer.addClass(elementRef.nativeElement, `novo-column-${columnDef.cssClassFriendlyName}`);
renderer.addClass(elementRef.nativeElement, 'novo-simple-header-cell');
}
public ngOnInit(): void {
if (this.column.width) {
this.renderer.setStyle(this.elementRef.nativeElement, 'min-width', `${this.column.width}px`);
this.renderer.setStyle(this.elementRef.nativeElement, 'max-width', `${this.column.width}px`);
this.renderer.setStyle(this.elementRef.nativeElement, 'width', `${this.column.width}px`);
}
}
}
export class NovoSimpleEmptyHeaderCell extends _NovoHeaderCell {
public role = 'columnheader';
constructor(columnDef: CdkColumnDef, elementRef: ElementRef, renderer: Renderer2) {
super(columnDef, elementRef, renderer);
renderer.setAttribute(elementRef.nativeElement, 'data-automation-id', `novo-column-header-${columnDef.cssClassFriendlyName}`);
renderer.addClass(elementRef.nativeElement, `novo-column-${columnDef.cssClassFriendlyName}`);
renderer.addClass(elementRef.nativeElement, 'novo-simple-empty-header-cell');
}
}
export class NovoSimpleCheckboxHeaderCell extends _NovoHeaderCell implements OnDestroy {
public role = 'columnheader';
public selectAll: boolean = false;
private selectAllSubscription: Subscription;
constructor(columnDef: CdkColumnDef, elementRef: ElementRef, renderer: Renderer2, private _selection: NovoSelection) {
super(columnDef, elementRef, renderer);
renderer.setAttribute(elementRef.nativeElement, 'data-automation-id', `novo-checkbox-column-header-${columnDef.cssClassFriendlyName}`);
renderer.addClass(elementRef.nativeElement, `novo-checkbox-column-${columnDef.cssClassFriendlyName}`);
renderer.addClass(elementRef.nativeElement, 'novo-simple-checkbox-header-cell');
this.selectAllSubscription = _selection.novoSelectAllToggle.subscribe((value: boolean) => {
this.selectAll = value;
});
}
public ngOnDestroy(): void {
this.selectAllSubscription.unsubscribe();
}
public toggle(value: boolean): void {
this._selection.selectAll(value);
}
}
export class NovoSimpleCell<T> extends _NovoCell implements OnInit {
public role = 'gridcell';
public row: any;
public column: SimpleTableColumn<T>;
private spanElement: ElementRef;
constructor(columnDef: CdkColumnDef, private elementRef: ElementRef, private renderer: Renderer2) {
super(columnDef, elementRef, renderer);
renderer.setAttribute(elementRef.nativeElement, 'data-automation-id', `novo-column-${columnDef.cssClassFriendlyName}`);
renderer.addClass(elementRef.nativeElement, `novo-column-${columnDef.cssClassFriendlyName}`);
renderer.addClass(elementRef.nativeElement, 'novo-simple-cell');
}
public ngOnInit(): void {
if (this.column.customClass) {
this.renderer.addClass(this.elementRef.nativeElement, this.column.customClass(this.row));
}
if (this.column.width) {
this.renderer.setStyle(this.elementRef.nativeElement, 'min-width', `${this.column.width}px`);
this.renderer.setStyle(this.elementRef.nativeElement, 'max-width', `${this.column.width}px`);
this.renderer.setStyle(this.elementRef.nativeElement, 'width', `${this.column.width}px`);
// TODO - this inhibits resizing the page after the initial load -- but do we care?!?!
// this.renderer.setStyle(this.spanElement.nativeElement, 'min-width', `${this.column.width - 20}px`);
// this.renderer.setStyle(this.spanElement.nativeElement, 'max-width', `${this.column.width - 20}px`);
// this.renderer.setStyle(this.spanElement.nativeElement, 'width', `${this.column.width - 20}px`);
}
// else {
// // TODO - this inhibits resizing the page after the initial load -- but do we care?!?!
// this.renderer.setStyle(this.spanElement.nativeElement, 'min-width', `${this.elementRef.nativeElement.offsetWidth - 20}px`);
// this.renderer.setStyle(this.spanElement.nativeElement, 'max-width', `${this.elementRef.nativeElement.offsetWidth - 20}px`);
// this.renderer.setStyle(this.spanElement.nativeElement, 'width', `${this.elementRef.nativeElement.offsetWidth - 20}px`);
// }
}
public onClick(event: MouseEvent): void {
Helpers.swallowEvent(event);
if (this.column.onClick) {
this.column.onClick(this.row);
}
return;
}
}
export class NovoSimpleCheckboxCell extends _NovoCell implements OnDestroy, OnInit {
public role = 'gridcell';
public row: any;
public index: any;
public selected: boolean = false;
private selectAllSubscription: Subscription;
constructor(public columnDef: CdkColumnDef, elementRef: ElementRef, renderer: Renderer2, public _selection: NovoSelection) {
super(columnDef, elementRef, renderer);
renderer.setAttribute(elementRef.nativeElement, 'data-automation-id', `novo-checkbox-column-${columnDef.cssClassFriendlyName}`);
renderer.addClass(elementRef.nativeElement, `novo-checkbox-column-${columnDef.cssClassFriendlyName}`);
renderer.addClass(elementRef.nativeElement, 'novo-simple-checkbox-cell');
this.selectAllSubscription = _selection.novoSelectAllToggle.subscribe((value: boolean) => {
this.selected = value;
});
}
public ngOnInit(): void {
this._selection.register(this.row.id || this.index, this.row);
this.selected = this._selection.state.selectedRows.has(this.row.id || this.index);
}
public ngOnDestroy(): void {
this._selection.deregister(this.row.id || this.index);
this.selectAllSubscription.unsubscribe();
}
public toggle(value: boolean): void {
this._selection.toggle(this.row.id || this.index, value, this.row);
}
}
export class NovoSimpleActionCell<T> extends _NovoCell implements OnInit {
public role = 'gridcell';
public row: T;
public column: SimpleTableActionColumn<T>;
constructor(columnDef: CdkColumnDef, private elementRef: ElementRef, private renderer: Renderer2, private labels: NovoLabelService) {
super(columnDef, elementRef, renderer);
renderer.setAttribute(elementRef.nativeElement, 'data-automation-id', `novo-action-column-${columnDef.cssClassFriendlyName}`);
}
public ngOnInit(): void {
if (this.column.options) {
this.renderer.addClass(this.elementRef.nativeElement, 'novo-simple-dropdown-cell');
} else {
this.renderer.addClass(this.elementRef.nativeElement, 'novo-simple-button-cell');
}
}
public isDisabled(check: SimpleTableActionColumn<T> | SimpleTableActionColumnOption<T>, row: T): boolean {
if (check.disabled === true) {
return true;
}
if (check.disabledCheck) {
return check.disabledCheck(row);
}
return false;
}
}