Skip to content

add-server: Make adding server dynamic [WIP]. #496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 47 additions & 2 deletions app/renderer/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ class ServerManagerView {
this.toggleSidebar(showSidebar);
}

initTabs() {
const servers = DomainUtil.getDomains();
initTabs(servers = DomainUtil.getDomains()) {
if (servers.length > 0) {
for (let i = 0; i < servers.length; i++) {
this.initServer(servers[i], i);
Expand All @@ -137,6 +136,45 @@ class ServerManagerView {
}
}

/*
This function does a bit of updating our internal props
to make sure everything works as expected, the thing that are
done here is if there are any FunctionlTabs we need to move them
after the tabs so the tabs have correct id's.
*/
addServer(server) {
const functionalTabsData = [];
this.tabs.forEach((tab, index) => {
if (tab instanceof FunctionalTab) {
functionalTabsData.push({
index,
tab
});

this.tabs.splice(index, 1);
}
});

const index = this.tabs.length;
this.initServer(server, index);
this.tabs[index].webview.init();
this.activateTab(index);

// restore back function tabs
functionalTabsData.forEach(data => {
this.tabs.push(data.tab);
for (const tabName in this.functionalTabs) {
const value = this.functionalTabs[tabName];
if (value === data.index) {
const newIndex = this.tabs.length - 1;
const webview = document.querySelector(`webview[data-tab-id="${data.index}"]`);
webview.setAttribute('data-tab-id', newIndex);
this.functionalTabs[tabName] = newIndex;
}
}
});
}

initServer(server, index) {
const tabIndex = this.getTabIndex();
this.tabs.push(new ServerTab({
Expand Down Expand Up @@ -563,6 +601,13 @@ window.onload = () => {
const serverManagerView = new ServerManagerView();
const reconnectUtil = new ReconnectUtil(serverManagerView);
serverManagerView.init();

window.serverManagerView = serverManagerView;
window.FunctionalTab = FunctionalTab;
ipcRenderer.on('add-server', (event, server) => {
serverManagerView.addServer(server);
});

window.addEventListener('online', () => {
reconnectUtil.pollInternetAndReload();
});
Expand Down
10 changes: 5 additions & 5 deletions app/renderer/js/pages/preference/new-server-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const BaseComponent = require(__dirname + '/../../components/base.js');
const DomainUtil = require(__dirname + '/../../utils/domain-util.js');
const { ipcRenderer } = require('electron');
const shell = require('electron').shell;

class NewServerForm extends BaseComponent {
Expand Down Expand Up @@ -50,12 +51,11 @@ class NewServerForm extends BaseComponent {
this.$newServerUrl = this.$newServerForm.querySelectorAll('input.setting-input-value')[0];
}

submitFormHandler() {
async submitFormHandler() {
this.$saveServerButton.children[0].innerHTML = 'Connecting...';
DomainUtil.checkDomain(this.$newServerUrl.value).then(serverConf => {
DomainUtil.addDomain(serverConf).then(() => {
this.props.onChange(this.props.index);
});
DomainUtil.checkDomain(this.$newServerUrl.value).then(async serverConf => {
const server = await DomainUtil.addDomain(serverConf);
ipcRenderer.send('forward-message', 'add-server', server);
}, errorMessage => {
this.$saveServerButton.children[0].innerHTML = 'Connect';
alert(errorMessage);
Expand Down
3 changes: 1 addition & 2 deletions app/renderer/js/pages/preference/servers-section.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class ServersSection extends BaseSection {

initNewServerForm() {
new NewServerForm({
$root: this.$newServerContainer,
onChange: this.reloadApp
$root: this.$newServerContainer
}).init();
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/renderer/js/utils/domain-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ class DomainUtil {
server.icon = localIconUrl;
this.db.push('/domains[]', server, true);
this.reloadDB();
resolve();
resolve(server);
});
} else {
server.icon = defaultIconUrl;
this.db.push('/domains[]', server, true);
this.reloadDB();
resolve();
resolve(server);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.