refactor: move TabStorage to service
This commit is contained in:
@ -1,82 +0,0 @@
|
||||
import {MatChipListbox} from "@angular/material/chips";
|
||||
|
||||
export class TabSelect {
|
||||
public index: number;
|
||||
public name: string;
|
||||
|
||||
constructor(index: number, name: string) {
|
||||
this.index = index;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
export enum TabSelectType {
|
||||
group,
|
||||
professor,
|
||||
lecture,
|
||||
other
|
||||
}
|
||||
|
||||
export interface TabSelectData {
|
||||
selected: TabSelect[] | null;
|
||||
type: TabSelectType;
|
||||
}
|
||||
|
||||
export class TabStorageComponent {
|
||||
private static dataName = 'tabSelectedData';
|
||||
|
||||
public static trySelectChip(index: number, chip: MatChipListbox, tryCount: number = 0) {
|
||||
setTimeout(() => {
|
||||
if (chip?._chips !== undefined && chip._chips.length !== 0) {
|
||||
let selected = chip._chips.find(x => x.value == index.toString());
|
||||
if (selected !== undefined) {
|
||||
selected.select();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (tryCount < 10)
|
||||
this.trySelectChip(index, chip, ++tryCount);
|
||||
else
|
||||
return;
|
||||
|
||||
}, 100);
|
||||
}
|
||||
|
||||
public static select(selected: TabSelect, type: TabSelectType, index: number) {
|
||||
let selectedData = this.selected;
|
||||
|
||||
if (selectedData === null)
|
||||
selectedData = {} as TabSelectData;
|
||||
|
||||
if (selectedData.type !== type || selectedData.selected === null || selectedData.selected.length === 0) {
|
||||
if (index !== 0)
|
||||
return;
|
||||
else
|
||||
selectedData.selected = [selected];
|
||||
} else {
|
||||
if (selectedData.selected.length < index)
|
||||
return;
|
||||
|
||||
if (selectedData.selected.length - 1 === index)
|
||||
selectedData.selected[index] = selected;
|
||||
else if (selectedData.selected.length > index + 1)
|
||||
selectedData.selected = selectedData.selected.slice(0, index + 1);
|
||||
else
|
||||
selectedData.selected.push(selected);
|
||||
}
|
||||
|
||||
selectedData.type = type;
|
||||
|
||||
localStorage.setItem(this.dataName, JSON.stringify(selectedData));
|
||||
}
|
||||
|
||||
public static get selected(): TabSelectData | null {
|
||||
let data = localStorage.getItem(this.dataName);
|
||||
|
||||
if (data === null)
|
||||
return null;
|
||||
|
||||
return JSON.parse(data) as TabSelectData;
|
||||
}
|
||||
}
|
@ -9,11 +9,7 @@ import {FacultyResponse} from "@api/v1/facultyResponse";
|
||||
import {FacultyService} from "@api/v1/faculty.service";
|
||||
import {GroupService} from "@api/v1/group.service";
|
||||
import {IScheduleTab} from "@component/schedule/tabs/ischedule-tab";
|
||||
import {
|
||||
TabSelect,
|
||||
TabSelectType,
|
||||
TabStorageComponent
|
||||
} from "@component/common/tab-storage/tab-storage.component";
|
||||
import {TabSelect, TabSelectType, TabStorageService} from "@service/tab-storage.service";
|
||||
|
||||
@Component({
|
||||
selector: 'app-group',
|
||||
@ -58,7 +54,7 @@ export class GroupComponent implements IScheduleTab {
|
||||
@Output() eventResult = new EventEmitter<number>();
|
||||
|
||||
constructor(private facultyApi: FacultyService, private groupApi: GroupService) {
|
||||
let selectedData = TabStorageComponent.selected;
|
||||
let selectedData = TabStorageService.selected;
|
||||
if (selectedData !== null && selectedData.selected !== null) {
|
||||
if (selectedData.type === TabSelectType.group)
|
||||
this.selected = selectedData.selected;
|
||||
@ -81,7 +77,7 @@ export class GroupComponent implements IScheduleTab {
|
||||
selectedFaculty = data.find(x => x.name === this.selected![0].name);
|
||||
|
||||
if (selectedFaculty !== undefined) {
|
||||
TabStorageComponent.trySelectChip(selectedFaculty.id, this.facultyChip);
|
||||
TabStorageService.trySelectChip(selectedFaculty.id, this.facultyChip);
|
||||
this.onFacultySelected(selectedFaculty.id);
|
||||
}
|
||||
}
|
||||
@ -112,7 +108,7 @@ export class GroupComponent implements IScheduleTab {
|
||||
let selectedCourse = this.courseNumbers.find(x => x === this.selected![1].index);
|
||||
|
||||
if (selectedCourse !== undefined) {
|
||||
TabStorageComponent.trySelectChip(selectedCourse, this.courseChip);
|
||||
TabStorageService.trySelectChip(selectedCourse, this.courseChip);
|
||||
this.onCourseSelected(selectedCourse);
|
||||
}
|
||||
}
|
||||
@ -124,7 +120,7 @@ export class GroupComponent implements IScheduleTab {
|
||||
selectedGroup = data.find(x => x.name === this.selected![2].name);
|
||||
|
||||
if (selectedGroup !== undefined) {
|
||||
TabStorageComponent.trySelectChip(selectedGroup.id, this.groupChip);
|
||||
TabStorageService.trySelectChip(selectedGroup.id, this.groupChip);
|
||||
this.onGroupSelected(selectedGroup.id);
|
||||
}
|
||||
}
|
||||
@ -148,7 +144,7 @@ export class GroupComponent implements IScheduleTab {
|
||||
return;
|
||||
}
|
||||
|
||||
TabStorageComponent.select(new TabSelect(index, this.faculties!.find(x => x.id === index)?.name ?? ''), TabSelectType.group, 0);
|
||||
TabStorageService.select(new TabSelect(index, this.faculties!.find(x => x.id === index)?.name ?? ''), TabSelectType.group, 0);
|
||||
|
||||
this.facultyId = index;
|
||||
this.courseNumberPanel.open();
|
||||
@ -165,7 +161,7 @@ export class GroupComponent implements IScheduleTab {
|
||||
return;
|
||||
}
|
||||
|
||||
TabStorageComponent.select(new TabSelect(course, course.toString()), TabSelectType.group, 1);
|
||||
TabStorageService.select(new TabSelect(course, course.toString()), TabSelectType.group, 1);
|
||||
|
||||
this.courseNumber = course;
|
||||
this.groupPanel.open();
|
||||
@ -176,7 +172,7 @@ export class GroupComponent implements IScheduleTab {
|
||||
if (index === undefined)
|
||||
return;
|
||||
|
||||
TabStorageComponent.select(new TabSelect(index, this.groups!.find(x => x.id == index)?.name ?? ''), TabSelectType.group, 2);
|
||||
TabStorageService.select(new TabSelect(index, this.groups!.find(x => x.id == index)?.name ?? ''), TabSelectType.group, 2);
|
||||
|
||||
this.groupPanel.close();
|
||||
this.eventResult.emit(index);
|
||||
|
@ -10,7 +10,7 @@ import {LectureHallResponse} from "@api/v1/lectureHallResponse";
|
||||
import {CampusService} from "@api/v1/campus.service";
|
||||
import {LectureHallService} from "@api/v1/lectureHall.service";
|
||||
import {IScheduleTab} from "@component/schedule/tabs/ischedule-tab";
|
||||
import {TabSelect, TabSelectType, TabStorageComponent} from "@component/common/tab-storage/tab-storage.component";
|
||||
import {TabSelect, TabSelectType, TabStorageService} from "@service/tab-storage.service";
|
||||
|
||||
@Component({
|
||||
selector: 'app-lecture-hall',
|
||||
@ -48,7 +48,7 @@ export class LectureHallComponent implements IScheduleTab {
|
||||
private readonly selected: TabSelect[] | null = null;
|
||||
|
||||
constructor(private campusApi: CampusService, private lectureHallApi: LectureHallService) {
|
||||
let selectedData = TabStorageComponent.selected;
|
||||
let selectedData = TabStorageService.selected;
|
||||
if (selectedData !== null && selectedData.selected !== null) {
|
||||
if (selectedData.type === TabSelectType.lecture)
|
||||
this.selected = selectedData.selected;
|
||||
@ -71,7 +71,7 @@ export class LectureHallComponent implements IScheduleTab {
|
||||
selectedCampus = data.find(x => x.codeName === this.selected![0].name);
|
||||
|
||||
if (selectedCampus !== undefined) {
|
||||
TabStorageComponent.trySelectChip(selectedCampus.id, this.campusChip);
|
||||
TabStorageService.trySelectChip(selectedCampus.id, this.campusChip);
|
||||
this.onCampusSelected(selectedCampus.id);
|
||||
}
|
||||
}
|
||||
@ -85,7 +85,7 @@ export class LectureHallComponent implements IScheduleTab {
|
||||
protected onCampusSelected(index: number) {
|
||||
this.formLectureHalls.reset();
|
||||
|
||||
TabStorageComponent.select(new TabSelect(index, this.campuses!.find(x => x.id === index)?.codeName ?? ''), TabSelectType.lecture, 0);
|
||||
TabStorageService.select(new TabSelect(index, this.campuses!.find(x => x.id === index)?.codeName ?? ''), TabSelectType.lecture, 0);
|
||||
|
||||
if (index === undefined) {
|
||||
this.campusId = null;
|
||||
@ -118,7 +118,7 @@ export class LectureHallComponent implements IScheduleTab {
|
||||
selectedLecture = data.find(x => x.name === this.selected![1].name);
|
||||
|
||||
if (selectedLecture !== undefined) {
|
||||
TabStorageComponent.trySelectChip(selectedLecture.id, this.lectureChip);
|
||||
TabStorageService.trySelectChip(selectedLecture.id, this.lectureChip);
|
||||
this.onLectureHallSelected(selectedLecture.id);
|
||||
}
|
||||
}
|
||||
@ -129,7 +129,7 @@ export class LectureHallComponent implements IScheduleTab {
|
||||
if (index === undefined)
|
||||
return;
|
||||
|
||||
TabStorageComponent.select(new TabSelect(index, this.lectureHallsFiltered!.find(x => x.id === index)?.name ?? ''), TabSelectType.lecture, 1);
|
||||
TabStorageService.select(new TabSelect(index, this.lectureHallsFiltered!.find(x => x.id === index)?.name ?? ''), TabSelectType.lecture, 1);
|
||||
|
||||
this.lecturePanel.close();
|
||||
this.eventResult.emit(index);
|
||||
|
@ -8,7 +8,7 @@ import {LoadingIndicatorComponent} from "@component/common/loading-indicator/loa
|
||||
import {ProfessorResponse} from "@api/v1/professorResponse";
|
||||
import {ProfessorService} from "@api/v1/professor.service";
|
||||
import {IScheduleTab} from "@component/schedule/tabs/ischedule-tab";
|
||||
import {TabSelect, TabSelectType, TabStorageComponent} from "@component/common/tab-storage/tab-storage.component";
|
||||
import {TabSelect, TabSelectType, TabStorageService} from "@service/tab-storage.service";
|
||||
|
||||
@Component({
|
||||
selector: 'app-professor',
|
||||
@ -37,7 +37,7 @@ export class ProfessorComponent implements OnInit, IScheduleTab {
|
||||
@Output() eventResult = new EventEmitter<number>();
|
||||
|
||||
constructor(private api: ProfessorService) {
|
||||
let selectedData = TabStorageComponent.selected;
|
||||
let selectedData = TabStorageService.selected;
|
||||
if (selectedData !== null && selectedData.selected !== null) {
|
||||
if (selectedData.type === TabSelectType.professor)
|
||||
this.selected = selectedData.selected;
|
||||
@ -96,7 +96,7 @@ export class ProfessorComponent implements OnInit, IScheduleTab {
|
||||
this.professorControl.setValue(selectedOption.name);
|
||||
this.eventResult.emit(selectedOption.id);
|
||||
|
||||
TabStorageComponent.select(new TabSelect(selectedOption.id, selectedOption.name), TabSelectType.professor, 0);
|
||||
TabStorageService.select(new TabSelect(selectedOption.id, selectedOption.name), TabSelectType.professor, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,7 @@ import {GroupService} from "@api/v1/group.service";
|
||||
import {ProfessorService} from "@api/v1/professor.service";
|
||||
import {AuthRoles} from "@model/AuthRoles";
|
||||
import {HasRoleDirective} from "@/directives/has-role.directive";
|
||||
import {TabStorageComponent} from "@component/common/tab-storage/tab-storage.component";
|
||||
import {TabStorageService} from "@service/tab-storage.service";
|
||||
|
||||
export enum TabsSelect {
|
||||
Group,
|
||||
@ -44,7 +44,7 @@ export enum TabsSelect {
|
||||
],
|
||||
templateUrl: './tabs.component.html',
|
||||
styleUrl: './tabs.component.css',
|
||||
providers: [ScheduleService, DisciplineService, LectureHallService, GroupService, ProfessorService]
|
||||
providers: [ScheduleService, DisciplineService, LectureHallService, GroupService, ProfessorService, TabStorageService]
|
||||
})
|
||||
|
||||
export class TabsComponent implements AfterViewInit {
|
||||
@ -58,13 +58,13 @@ export class TabsComponent implements AfterViewInit {
|
||||
}
|
||||
|
||||
ngAfterViewInit(): void {
|
||||
let selected = TabStorageComponent.selected;
|
||||
let selected = TabStorageService.selected;
|
||||
|
||||
let index = 0;
|
||||
if (selected !== null)
|
||||
index = selected.type;
|
||||
|
||||
if (this.tabs.selectedIndex === null || this.tabs.selectedIndex === 0)
|
||||
if (index === null || index === 0)
|
||||
this.chooseTabs(0).then();
|
||||
else
|
||||
this.tabs.selectedIndex = index;
|
||||
|
Reference in New Issue
Block a user