Componentize WebView.

This commit is contained in:
Zhongyi Tong
2017-06-04 03:49:58 +08:00
parent 84808313fe
commit 0708519816
4 changed files with 165 additions and 115 deletions

View File

@@ -7,6 +7,7 @@ const SystemUtil = require(__dirname + '/js/utils/system-util.js');
const {linkIsInternal, skipImages} = require(__dirname + '/../main/link-helper');
const {shell, ipcRenderer} = require('electron');
const {app, dialog} = require('electron').remote;
const WebView = require(__dirname + '/js/components/webview.js');
class ServerManagerView {
constructor() {
@@ -20,12 +21,11 @@ class ServerManagerView {
this.isLoading = false;
this.settingsTabIndex = -1;
this.activeTabIndex = -1;
this.zoomFactors = [];
this.webviews = [];
}
init() {
this.domainUtil = new DomainUtil();
this.systemUtil = new SystemUtil();
this.initTabs();
this.initActions();
this.registerIpcs();
@@ -34,8 +34,16 @@ class ServerManagerView {
initTabs() {
const servers = this.domainUtil.getDomains();
if (servers.length > 0) {
for (const server of servers) {
for (let i = 0; i < servers.length;i++) {
const server = servers[i];
this.initTab(server);
this.webviews.push(new WebView({
$root: this.$content,
index: i,
url: server.url,
name: server.alias,
nodeIntegration: false
}))
}
this.activateTab(0);
@@ -59,46 +67,6 @@ class ServerManagerView {
$tab.addEventListener('click', this.activateTab.bind(this, index));
}
initWebView(url, index, nodeIntegration = false) {
const webViewTemplate = `
<webview
id="webview-${index}"
class="loading"
src="${url}"
${nodeIntegration ? 'nodeIntegration' : ''}
disablewebsecurity
preload="js/preload.js"
webpreferences="allowRunningInsecureContent, javascript=yes">
</webview>
`;
const $webView = this.insertNode(webViewTemplate);
this.$content.appendChild($webView);
this.isLoading = true;
this.registerListeners($webView, index);
this.zoomFactors[index] = 1;
}
startLoading(url, index) {
const $activeWebView = document.getElementById(`webview-${this.activeTabIndex}`);
if ($activeWebView) {
$activeWebView.classList.add('disabled');
}
const $webView = document.getElementById(`webview-${index}`);
if ($webView === null) {
this.initWebView(url, index, this.settingsTabIndex === index);
} else {
this.updateBadge(index);
$webView.classList.remove('disabled');
$webView.focus();
}
}
endLoading(index) {
const $webView = document.getElementById(`webview-${index}`);
this.isLoading = false;
$webView.classList.remove('loading');
}
initActions() {
this.$addServerButton.addEventListener('click', this.openSettings.bind(this));
this.$settingsButton.addEventListener('click', this.openSettings.bind(this));
@@ -123,28 +91,35 @@ class ServerManagerView {
template: settingsTabTemplate
});
this.settingsTabIndex = this.$tabsContainer.childNodes.length - 1;
this.activateTab(this.settingsTabIndex);
this.settingsTabIndex = this.webviews.length;
this.webviews.push(new WebView({
$root: this.$content,
index: this.settingsTabIndex,
url: url,
name: "Settings",
nodeIntegration: true
}));
this.activateTab(this.settingsTabIndex);
}
activateTab(index) {
if (this.isLoading) {
return;
}
// if (this.webviews[index].loading) {
// return;
// }
if (this.activeTabIndex !== -1) {
if (this.activeTabIndex === index) {
return;
} else {
this.getTabAt(this.activeTabIndex).classList.remove('active');
this.webviews[this.activeTabIndex].hide();
}
}
const $tab = this.getTabAt(index);
$tab.classList.add('active');
const domain = $tab.getAttribute('domain');
this.startLoading(domain, index);
this.webviews[index].load();
this.activeTabIndex = index;
}
@@ -166,66 +141,6 @@ class ServerManagerView {
ipcRenderer.send('update-badge', messageCount);
}
registerListeners($webView, index) {
$webView.addEventListener('new-window', event => {
const {url} = event;
const domainPrefix = this.domainUtil.getDomain(this.activeTabIndex).url;
if (linkIsInternal(domainPrefix, url) && url.match(skipImages) === null) {
event.preventDefault();
return $webView.loadURL(url);
}
event.preventDefault();
shell.openExternal(url);
});
$webView.addEventListener('dom-ready', this.endLoading.bind(this, index));
$webView.addEventListener('dom-ready', () => {
// We need to wait until the page title is ready to get badge count
setTimeout(() => this.updateBadge(index), 1000);
});
$webView.addEventListener('dom-ready', () => {
$webView.focus();
});
// Set webview's user-agent
$webView.addEventListener('did-start-loading', () => {
let userAgent = this.systemUtil.getUserAgent();
if (!this.systemUtil.getUserAgent()) {
this.systemUtil.setUserAgent($webView.getUserAgent());
userAgent = this.systemUtil.getUserAgent();
}
$webView.setUserAgent(userAgent);
});
// eslint-disable-next-line arrow-parens
$webView.addEventListener('did-fail-load', (event) => {
// eslint-disable-next-line no-unused-vars
const {errorCode, errorDescription, validatedURL} = event;
const hasConnectivityErr = (this.systemUtil.connectivityERR.indexOf(errorDescription) >= 0);
if (hasConnectivityErr) {
console.error('error', errorDescription);
this.checkConnectivity();
}
});
}
checkConnectivity() {
return dialog.showMessageBox({
title: 'Internet connection problem',
message: 'No internet available! Try again?',
type: 'warning',
buttons: ['Try again', 'Close'],
defaultId: 0
}, index => {
if (index === 0) {
const activeWebview = document.getElementById(`webview-${this.activeTabIndex}`);
activeWebview.reload();
ipcRenderer.send('reload');
ipcRenderer.send('destroytray');
}
if (index === 1) {
app.quit();
}
});
}
registerIpcs() {
// ipcRenderer.on('reload', () => {
// const activeWebview = document.getElementById(`webview-${this.activeTabIndex}`);