File

projects/k-components/src/lib/components/tabcontrol/tabcontrol.component.ts

Metadata

changeDetection ChangeDetectionStrategy.OnPush
selector wm-tabcontrol
templateUrl ./tabcontrol.component.html

Index

Properties
Methods
Inputs

Constructor

constructor(injector: Injector)
Parameters :
Name Type Optional
injector Injector No

Inputs

model
Type : TabControlModel

Methods

changeSelectedTab
changeSelectedTab(index: number)
Parameters :
Name Type Optional
index number No
Returns : void
ngAfterViewInit
ngAfterViewInit()
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void
onTabSelected
onTabSelected($event)
Parameters :
Name Optional
$event No
Returns : void
Private synchWithModel
synchWithModel()
Returns : void

Properties

Private collectionChangeHandlerToRemove
Type : function
isInitialized
Default value : false
items
Type : any[]
Default value : []
model
Type : TabControlModel
Decorators :
@Input()
Private modelChangeHandlerToRemove
Type : function
tabComponent
Type : TabStripComponent
Decorators :
@ViewChild(TabStripComponent)
viewChildren
Type : QueryList<TabItem>
Decorators :
@ContentChildren(TabItem)
import {
  Component,
  Input,
  ContentChild,
  TemplateRef,
  ContentChildren,
  QueryList,
  Injector,
  ViewChild,
} from '@angular/core';
import { TabStripComponent } from '@progress/kendo-angular-layout';
import {
  TabItemModel,
  TabControlModel,
  ComponentId,
  AngularComponentId,
} from '@mobilize/wms-framework';

@Component({
  selector: 'wm-tabitem',
  template: `<div></div>`,
})
export class TabItem {
  @Input()
  model: TabItemModel;

  @Input()
  title = '';

  @ContentChild('itemContentTemplate', { static: true })
  itemContentTemplate: TemplateRef<any>;

  ngOnInit(): void {
    this.model = this.model ?? new TabItemModel();
    this.model.header = this.title !== '' ? this.title : this.model.header;
    this.model.templateRef = this.itemContentTemplate;
  }
}

@Component({
  selector: 'wm-tabcontrol',
  templateUrl: './tabcontrol.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
@ComponentId([AngularComponentId.tabControl])
export class TabControl {
  isInitialized = false;

  @Input()
  model: TabControlModel;
  @ContentChildren(TabItem) viewChildren: QueryList<TabItem>;
  @ViewChild(TabStripComponent) tabComponent: TabStripComponent;

  items: any[] = [];
  private collectionChangeHandlerToRemove: () => void;
  private modelChangeHandlerToRemove: (p1: string) => void;

  constructor(private injector: Injector) {}

  ngOnInit(): void {
    this.model = this.model ?? new TabControlModel();

    this.collectionChangeHandlerToRemove =
      this.model.items.collectionChangeEvent.addHandler(() =>
        this.synchWithModel()
      );
    this.modelChangeHandlerToRemove = this.model.change.addHandler(
      (property) => {
        if (property === 'SelectedIndex') {
          this.changeSelectedTab(this.model.selectedIndex);
        }
      }
    );
    this.synchWithModel();
  }

  ngAfterViewInit(): void {
    if (!this.isInitialized) {
      const tmpItems = [];
      this.viewChildren.forEach((item) => {
        tmpItems.push(item.model);
      });
      this.isInitialized = true;
      // the following timeout avoid runtime error
      setTimeout(() => {
        this.model.syncWithDesignItems(tmpItems);
      });
    }
  }

  ngOnDestroy(): void {
    this.model.items.collectionChangeEvent.removeHandler(
      this.collectionChangeHandlerToRemove
    );
    this.model.change.removeHandler(this.modelChangeHandlerToRemove);
  }

  onTabSelected($event): void {
    if (typeof $event.index === 'number') {
      this.model.selectedTabIndex = $event.index;
    }
  }

  changeSelectedTab(index: number): void {
    this.tabComponent?.selectTab(index);
  }

  private synchWithModel(): void {
    this.items = this.model.items.internalArray;
  }
}
<kendo-tabstrip (tabSelect)="onTabSelected($event)" style="height: 100%">
  <kendo-tabstrip-tab
    *ngFor="let item of items; let i = index"
    [title]="item.header"
    [selected]="item.isSelected"
  >
    <ng-template kendoTabContent="">
      <ng-container *ngIf="item.templateRef; else elseBlock">
        <ng-container *ngTemplateOutlet="item.templateRef"></ng-container>
      </ng-container>
      <ng-template #elseBlock>
        <wm-content [model]="item"></wm-content>
      </ng-template>
    </ng-template>
  </kendo-tabstrip-tab>
</kendo-tabstrip>
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""