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-12-22 16:48:32 +02:00
|
|
|
this.isRoomSettings = false;
|
2021-12-11 06:25:38 +02:00
|
|
|
this.recentRooms = [];
|
2021-12-11 07:20:34 +02:00
|
|
|
|
2022-05-14 05:54:21 +03:00
|
|
|
this.rawModelStack = [];
|
2021-07-28 16:15:52 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-11 06:25:38 +02:00
|
|
|
removeRecentRoom(roomId) {
|
|
|
|
if (typeof roomId !== 'string') return;
|
|
|
|
const roomIdIndex = this.recentRooms.indexOf(roomId);
|
|
|
|
if (roomIdIndex >= 0) {
|
|
|
|
this.recentRooms.splice(roomIdIndex, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addRecentRoom(roomId) {
|
|
|
|
if (typeof roomId !== 'string') return;
|
|
|
|
|
|
|
|
this.recentRooms.push(roomId);
|
|
|
|
if (this.recentRooms.length > 10) {
|
|
|
|
this.recentRooms.splice(0, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 05:54:21 +03:00
|
|
|
get isRawModalVisible() {
|
|
|
|
return this.rawModelStack.length > 0;
|
|
|
|
}
|
|
|
|
|
2021-12-11 07:20:34 +02:00
|
|
|
setIsRawModalVisible(visible) {
|
2022-05-14 05:54:21 +03:00
|
|
|
if (visible) this.rawModelStack.push(true);
|
|
|
|
else this.rawModelStack.pop();
|
2021-12-11 07:20:34 +02:00
|
|
|
}
|
|
|
|
|
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);
|
2022-02-23 16:18:24 +02:00
|
|
|
this.selectedSpaceId = action.roomId === cons.tabs.HOME ? null : 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;
|
2021-12-11 07:20:34 +02:00
|
|
|
this.removeRecentRoom(prevSelectedRoomId);
|
2021-12-11 06:25:38 +02:00
|
|
|
this.addRecentRoom(prevSelectedRoomId);
|
|
|
|
this.removeRecentRoom(this.selectedRoomId);
|
2021-12-26 12:04:20 +02:00
|
|
|
if (this.isRoomSettings && typeof this.selectedRoomId === 'string') {
|
2021-12-22 16:48:32 +02:00
|
|
|
this.isRoomSettings = !this.isRoomSettings;
|
|
|
|
this.emit(cons.events.navigation.ROOM_SETTINGS_TOGGLED, this.isRoomSettings);
|
|
|
|
}
|
2021-12-03 15:02:10 +02:00
|
|
|
this.emit(
|
|
|
|
cons.events.navigation.ROOM_SELECTED,
|
|
|
|
this.selectedRoomId,
|
|
|
|
prevSelectedRoomId,
|
|
|
|
action.eventId,
|
|
|
|
);
|
2021-07-28 16:15:52 +03:00
|
|
|
},
|
2022-01-29 10:18:06 +02:00
|
|
|
[cons.actions.navigation.OPEN_SPACE_SETTINGS]: () => {
|
2022-02-16 16:55:36 +02:00
|
|
|
this.emit(cons.events.navigation.SPACE_SETTINGS_OPENED, action.roomId, action.tabText);
|
|
|
|
},
|
|
|
|
[cons.actions.navigation.OPEN_SPACE_MANAGE]: () => {
|
|
|
|
this.emit(cons.events.navigation.SPACE_MANAGE_OPENED, action.roomId);
|
2022-01-29 10:18:06 +02:00
|
|
|
},
|
2022-02-20 16:47:13 +02:00
|
|
|
[cons.actions.navigation.OPEN_SPACE_ADDEXISTING]: () => {
|
|
|
|
this.emit(cons.events.navigation.SPACE_ADDEXISTING_OPENED, action.roomId);
|
|
|
|
},
|
2021-12-22 16:48:32 +02:00
|
|
|
[cons.actions.navigation.TOGGLE_ROOM_SETTINGS]: () => {
|
|
|
|
this.isRoomSettings = !this.isRoomSettings;
|
2022-01-16 14:48:33 +02:00
|
|
|
this.emit(
|
|
|
|
cons.events.navigation.ROOM_SETTINGS_TOGGLED,
|
|
|
|
this.isRoomSettings,
|
|
|
|
action.tabText,
|
|
|
|
);
|
2021-12-22 16:48:32 +02:00
|
|
|
},
|
2022-03-06 14:22:04 +02:00
|
|
|
[cons.actions.navigation.OPEN_SHORTCUT_SPACES]: () => {
|
|
|
|
this.emit(cons.events.navigation.SHORTCUT_SPACES_OPENED);
|
|
|
|
},
|
2021-07-28 16:15:52 +03:00
|
|
|
[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]: () => {
|
2022-02-26 17:30:52 +02:00
|
|
|
this.emit(
|
|
|
|
cons.events.navigation.CREATE_ROOM_OPENED,
|
|
|
|
action.isSpace,
|
|
|
|
action.parentId,
|
|
|
|
);
|
2021-07-28 16:15:52 +03:00
|
|
|
},
|
2022-05-03 13:31:50 +03:00
|
|
|
[cons.actions.navigation.OPEN_JOIN_ALIAS]: () => {
|
|
|
|
this.emit(
|
|
|
|
cons.events.navigation.JOIN_ALIAS_OPENED,
|
|
|
|
action.term,
|
|
|
|
);
|
|
|
|
},
|
2021-07-28 16:15:52 +03:00
|
|
|
[cons.actions.navigation.OPEN_INVITE_USER]: () => {
|
2021-08-08 07:53:26 +03:00
|
|
|
this.emit(cons.events.navigation.INVITE_USER_OPENED, action.roomId, action.searchTerm);
|
2021-07-28 16:15:52 +03:00
|
|
|
},
|
2021-10-18 18:25:52 +03:00
|
|
|
[cons.actions.navigation.OPEN_PROFILE_VIEWER]: () => {
|
|
|
|
this.emit(cons.events.navigation.PROFILE_VIEWER_OPENED, action.userId, action.roomId);
|
|
|
|
},
|
2021-07-28 16:15:52 +03:00
|
|
|
[cons.actions.navigation.OPEN_SETTINGS]: () => {
|
2022-03-21 06:16:11 +02:00
|
|
|
this.emit(cons.events.navigation.SETTINGS_OPENED, action.tabText);
|
2021-07-28 16:15:52 +03:00
|
|
|
},
|
2022-04-24 13:23:10 +03:00
|
|
|
[cons.actions.navigation.OPEN_NAVIGATION]: () => {
|
|
|
|
this.emit(cons.events.navigation.NAVIGATION_OPENED);
|
|
|
|
},
|
2021-08-14 07:49:29 +03:00
|
|
|
[cons.actions.navigation.OPEN_EMOJIBOARD]: () => {
|
|
|
|
this.emit(
|
|
|
|
cons.events.navigation.EMOJIBOARD_OPENED,
|
2022-03-21 06:16:11 +02:00
|
|
|
action.cords,
|
|
|
|
action.requestEmojiCallback,
|
2021-08-14 07:49:29 +03:00
|
|
|
);
|
|
|
|
},
|
2021-08-16 15:07:29 +03:00
|
|
|
[cons.actions.navigation.OPEN_READRECEIPTS]: () => {
|
|
|
|
this.emit(
|
|
|
|
cons.events.navigation.READRECEIPTS_OPENED,
|
|
|
|
action.roomId,
|
2021-12-03 15:02:10 +02:00
|
|
|
action.userIds,
|
2021-08-16 15:07:29 +03:00
|
|
|
);
|
|
|
|
},
|
2022-02-22 15:31:04 +02:00
|
|
|
[cons.actions.navigation.OPEN_VIEWSOURCE]: () => {
|
|
|
|
this.emit(
|
|
|
|
cons.events.navigation.VIEWSOURCE_OPENED,
|
|
|
|
action.event,
|
|
|
|
);
|
|
|
|
},
|
2021-11-20 09:59:32 +02:00
|
|
|
[cons.actions.navigation.CLICK_REPLY_TO]: () => {
|
|
|
|
this.emit(
|
|
|
|
cons.events.navigation.REPLY_TO_CLICKED,
|
|
|
|
action.userId,
|
|
|
|
action.eventId,
|
|
|
|
action.body,
|
|
|
|
);
|
|
|
|
},
|
2021-12-10 13:52:53 +02:00
|
|
|
[cons.actions.navigation.OPEN_SEARCH]: () => {
|
|
|
|
this.emit(
|
|
|
|
cons.events.navigation.SEARCH_OPENED,
|
|
|
|
action.term,
|
|
|
|
);
|
|
|
|
},
|
2022-01-11 17:16:41 +02:00
|
|
|
[cons.actions.navigation.OPEN_REUSABLE_CONTEXT_MENU]: () => {
|
|
|
|
this.emit(
|
|
|
|
cons.events.navigation.REUSABLE_CONTEXT_MENU_OPENED,
|
|
|
|
action.placement,
|
|
|
|
action.cords,
|
|
|
|
action.render,
|
2022-01-13 15:13:22 +02:00
|
|
|
action.afterClose,
|
2022-01-11 17:16:41 +02:00
|
|
|
);
|
|
|
|
},
|
2022-03-31 18:09:05 +03:00
|
|
|
[cons.actions.navigation.OPEN_REUSABLE_DIALOG]: () => {
|
|
|
|
this.emit(
|
|
|
|
cons.events.navigation.REUSABLE_DIALOG_OPENED,
|
|
|
|
action.title,
|
|
|
|
action.render,
|
|
|
|
action.afterClose,
|
|
|
|
);
|
|
|
|
},
|
2022-05-01 10:52:55 +03:00
|
|
|
[cons.actions.navigation.OPEN_EMOJI_VERIFICATION]: () => {
|
|
|
|
this.emit(
|
|
|
|
cons.events.navigation.EMOJI_VERIFICATION_OPENED,
|
|
|
|
action.request,
|
2022-05-03 10:22:26 +03:00
|
|
|
action.targetDevice,
|
2022-05-01 10:52:55 +03:00
|
|
|
);
|
|
|
|
},
|
2021-07-28 16:15:52 +03:00
|
|
|
};
|
|
|
|
actions[action.type]?.();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const navigation = new Navigation();
|
|
|
|
appDispatcher.register(navigation.navigate.bind(navigation));
|
|
|
|
|
|
|
|
export default navigation;
|