armisa-models
Version:
models of armisa!
108 lines (91 loc) • 3.76 kB
text/typescript
import { Tabbing } from ".";
import { ToolStripItemFactory } from "../../../ComponentFactory/ToolStrip/ToolStripItemFactory";
export class ToolboxTabbing {
public isEmpty = (): boolean => {
if (!this.focusAbleToolbox) {
return true;
} else if (this.focusAbleToolbox.length <= 0) {
return true;
}
return false;
}
public focus = (tryElement: boolean = true) => {
let result = false;
result = this.focusToActiveElementTabIndex();
if (result) {
return;
}
result = this.focusFirstElement();
if (result) {
return;
}
if (tryElement) {
this.tabbing.elementTabbing.focusToZeroTabIndexOrFirstElementOrToolbox();
}
}
public mainFocusAbleToolbox: ToolStripItemFactory[];
public get focusAbleToolbox() {
return this.mainFocusAbleToolbox.filter(i => !i.disabled && i.visible && !i.hidden && typeof i.tabIndex === 'number');
}
///-1 index equal to home button
public activeToolboxAtiveIndex: number = 0;
constructor(
public tabbing: Tabbing
) {
this.mainFocusAbleToolbox = [];
}
public isThereAnyToolboxForFocus = (): ToolStripItemFactory | undefined => {
if (this.focusAbleToolbox && this.focusAbleToolbox.length) {
return this.focusAbleToolbox[0];
}
return undefined;
}
public focusToActiveElementTabIndex = (): boolean => {
const find = this.focusAbleToolbox.find(i => i.tabIndex === this.activeToolboxAtiveIndex);
if (find && find.refOfElemetn && find.refOfElemetn.current) {
find.refOfElemetn.current.focus();
}
if (find) {
return true;
}
return false;
}
public focusFirstElement = (): boolean => {
if (this.focusAbleToolbox && this.focusAbleToolbox.length > 0 && this.focusAbleToolbox[0] && typeof this.focusAbleToolbox[0].tabIndex === 'number') {
this.activeToolboxAtiveIndex = this.focusAbleToolbox[0].tabIndex;
this.tabbing.activeElementIndex = -1;
this.tabbing.focus();
return true;
}
return false;
}
public focuseToThisToolStripItem = (toolStripItem: ToolStripItemFactory) => {
if (toolStripItem.refOfElemetn && toolStripItem.refOfElemetn.current) {
this.tabbing.activeElementIndex = -1;
this.activeToolboxAtiveIndex = toolStripItem.tabIndex;
toolStripItem.refOfElemetn.current.focus();
}
}
public focusNextItem = () => {
const currentIndex = this.focusAbleToolbox.findIndex(i => i.tabIndex === this.activeToolboxAtiveIndex);
if (currentIndex === this.focusAbleToolbox.length - 1) {
this.focuseToThisToolStripItem(this.focusAbleToolbox[0]);
} else {
this.focuseToThisToolStripItem(this.focusAbleToolbox[currentIndex + 1]);
}
}
public focusPreviousItem = () => {
const currentIndex = this.focusAbleToolbox.findIndex(i => i.tabIndex === this.activeToolboxAtiveIndex);
if (currentIndex === 0) {
this.focuseToThisToolStripItem(this.focusAbleToolbox[this.focusAbleToolbox.length - 1]);
} else {
this.focuseToThisToolStripItem(this.focusAbleToolbox[currentIndex - 1]);
}
}
focusLastElementOfForm = () => {
this.tabbing.elementTabbing.focusToMaxTabIndexOrLastElement();
}
focusFirstElementOfForm = () => {
this.tabbing.elementTabbing.focusToZeroTabIndexOrFirstElementOrToolbox();
}
}