cinny/src/client/state/navigation.js

104 lines
3.6 KiB
JavaScript
Raw Normal View History

2021-07-28 16:15:52 +03:00
import EventEmitter from 'events';
import appDispatcher from '../dispatcher';
import cons from './cons';
class Navigation extends EventEmitter {
constructor() {
super();
2021-09-05 16:26:34 +03:00
this.selectedTab = cons.tabs.HOME;
2021-09-03 15:28:01 +03:00
this.selectedSpaceId = null;
2021-09-05 16:26:34 +03:00
this.selectedSpacePath = [cons.tabs.HOME];
2021-09-03 15:28:01 +03:00
this.selectedRoomId = null;
2021-07-28 16:15:52 +03:00
this.isPeopleDrawerVisible = true;
}
2021-09-03 15:28:01 +03:00
_setSpacePath(roomId) {
2021-09-05 16:26:34 +03:00
if (roomId === null || roomId === cons.tabs.HOME) {
this.selectedSpacePath = [cons.tabs.HOME];
2021-09-03 15:28:01 +03:00
return;
}
if (this.selectedSpacePath.includes(roomId)) {
const spIndex = this.selectedSpacePath.indexOf(roomId);
this.selectedSpacePath = this.selectedSpacePath.slice(0, spIndex + 1);
return;
}
this.selectedSpacePath.push(roomId);
2021-07-28 16:15:52 +03:00
}
navigate(action) {
const actions = {
2021-09-05 16:26:34 +03:00
[cons.actions.navigation.SELECT_TAB]: () => {
2021-09-03 15:28:01 +03:00
this.selectedTab = action.tabId;
2021-09-05 16:26:34 +03:00
if (this.selectedTab !== cons.tabs.DIRECTS) {
if (this.selectedTab === cons.tabs.HOME) {
this.selectedSpacePath = [cons.tabs.HOME];
this.selectedSpaceId = null;
} else {
this.selectedSpacePath = [this.selectedTab];
this.selectedSpaceId = this.selectedTab;
}
this.emit(cons.events.navigation.SPACE_SELECTED, this.selectedSpaceId);
} else this.selectedSpaceId = null;
this.emit(cons.events.navigation.TAB_SELECTED, this.selectedTab);
2021-09-03 15:28:01 +03:00
},
[cons.actions.navigation.SELECT_SPACE]: () => {
this._setSpacePath(action.roomId);
this.selectedSpaceId = action.roomId;
2021-09-05 16:26:34 +03:00
this.emit(cons.events.navigation.SPACE_SELECTED, this.selectedSpaceId);
2021-07-28 16:15:52 +03:00
},
[cons.actions.navigation.SELECT_ROOM]: () => {
2021-09-03 15:28:01 +03:00
const prevSelectedRoomId = this.selectedRoomId;
this.selectedRoomId = action.roomId;
this.emit(cons.events.navigation.ROOM_SELECTED, this.selectedRoomId, prevSelectedRoomId);
2021-07-28 16:15:52 +03:00
},
[cons.actions.navigation.TOGGLE_PEOPLE_DRAWER]: () => {
this.isPeopleDrawerVisible = !this.isPeopleDrawerVisible;
this.emit(cons.events.navigation.PEOPLE_DRAWER_TOGGLED, this.isPeopleDrawerVisible);
},
[cons.actions.navigation.OPEN_INVITE_LIST]: () => {
this.emit(cons.events.navigation.INVITE_LIST_OPENED);
},
2021-08-31 16:13:31 +03:00
[cons.actions.navigation.OPEN_PUBLIC_ROOMS]: () => {
this.emit(cons.events.navigation.PUBLIC_ROOMS_OPENED, action.searchTerm);
2021-07-28 16:15:52 +03:00
},
2021-08-31 16:13:31 +03:00
[cons.actions.navigation.OPEN_CREATE_ROOM]: () => {
this.emit(cons.events.navigation.CREATE_ROOM_OPENED);
2021-07-28 16:15:52 +03:00
},
[cons.actions.navigation.OPEN_INVITE_USER]: () => {
this.emit(cons.events.navigation.INVITE_USER_OPENED, action.roomId, action.searchTerm);
2021-07-28 16:15:52 +03:00
},
[cons.actions.navigation.OPEN_SETTINGS]: () => {
this.emit(cons.events.navigation.SETTINGS_OPENED);
},
2021-08-14 07:49:29 +03:00
[cons.actions.navigation.OPEN_EMOJIBOARD]: () => {
this.emit(
cons.events.navigation.EMOJIBOARD_OPENED,
action.cords, action.requestEmojiCallback,
);
},
2021-08-16 15:07:29 +03:00
[cons.actions.navigation.OPEN_READRECEIPTS]: () => {
this.emit(
cons.events.navigation.READRECEIPTS_OPENED,
action.roomId,
action.eventId,
);
},
2021-09-09 15:19:57 +03:00
[cons.actions.navigation.OPEN_ROOMOPTIONS]: () => {
this.emit(
cons.events.navigation.ROOMOPTIONS_OPENED,
action.cords,
action.roomId,
);
},
2021-07-28 16:15:52 +03:00
};
actions[action.type]?.();
}
}
const navigation = new Navigation();
appDispatcher.register(navigation.navigate.bind(navigation));
export default navigation;