Change the way utils load.

This commit is contained in:
Zhongyi Tong
2017-06-16 23:09:07 +08:00
parent 7192dc69f6
commit f548a0ae53
6 changed files with 24 additions and 19 deletions

View File

@@ -23,9 +23,6 @@ class WebView extends BaseComponent {
this.zoomFactor = 1.0; this.zoomFactor = 1.0;
this.loading = false; this.loading = false;
this.isActive = isActive; this.isActive = isActive;
this.domainUtil = new DomainUtil();
this.systemUtil = new SystemUtil();
this.linkUtil = new LinkUtil();
this.badgeCount = 0; this.badgeCount = 0;
} }
@@ -51,9 +48,9 @@ class WebView extends BaseComponent {
registerListeners() { registerListeners() {
this.$el.addEventListener('new-window', event => { this.$el.addEventListener('new-window', event => {
const {url} = event; const {url} = event;
const domainPrefix = this.domainUtil.getDomain(this.index).url; const domainPrefix = DomainUtil.getDomain(this.index).url;
if (this.linkUtil.isInternal(domainPrefix, url)) { if (LinkUtil.isInternal(domainPrefix, url)) {
event.preventDefault(); event.preventDefault();
this.$el.loadURL(url); this.$el.loadURL(url);
} else { } else {
@@ -72,7 +69,7 @@ class WebView extends BaseComponent {
this.$el.addEventListener('did-fail-load', event => { this.$el.addEventListener('did-fail-load', event => {
const {errorDescription} = event; const {errorDescription} = event;
const hasConnectivityErr = (this.systemUtil.connectivityERR.indexOf(errorDescription) >= 0); const hasConnectivityErr = (SystemUtil.connectivityERR.indexOf(errorDescription) >= 0);
if (hasConnectivityErr) { if (hasConnectivityErr) {
console.error('error', errorDescription); console.error('error', errorDescription);
this.checkConnectivity(); this.checkConnectivity();
@@ -80,10 +77,10 @@ class WebView extends BaseComponent {
}); });
this.$el.addEventListener('did-start-loading', () => { this.$el.addEventListener('did-start-loading', () => {
let userAgent = this.systemUtil.getUserAgent(); let userAgent = SystemUtil.getUserAgent();
if (!userAgent) { if (!userAgent) {
this.systemUtil.setUserAgent(this.$el.getUserAgent()); SystemUtil.setUserAgent(this.$el.getUserAgent());
userAgent = this.systemUtil.getUserAgent(); userAgent = SystemUtil.getUserAgent();
} }
this.$el.setUserAgent(userAgent); this.$el.setUserAgent(userAgent);
}); });

View File

@@ -24,14 +24,13 @@ class ServerManagerView {
} }
init() { init() {
this.domainUtil = new DomainUtil();
this.initTabs(); this.initTabs();
this.initActions(); this.initActions();
this.registerIpcs(); this.registerIpcs();
} }
initTabs() { initTabs() {
const servers = this.domainUtil.getDomains(); const servers = DomainUtil.getDomains();
if (servers.length > 0) { if (servers.length > 0) {
for (let i = 0; i < servers.length; i++) { for (let i = 0; i < servers.length; i++) {
this.initServer(servers[i], i); this.initServer(servers[i], i);

View File

@@ -13,13 +13,12 @@ class PreferenceView {
} }
init() { init() {
this.domainUtil = new DomainUtil();
this.initServers(); this.initServers();
this.initActions(); this.initActions();
} }
initServers() { initServers() {
const servers = this.domainUtil.getDomains(); const servers = DomainUtil.getDomains();
this.$serverInfoContainer.innerHTML = servers.length ? '' : 'Add your first server to get started!'; this.$serverInfoContainer.innerHTML = servers.length ? '' : 'Add your first server to get started!';
this.initNewServerForm(); this.initNewServerForm();
@@ -64,7 +63,7 @@ class PreferenceView {
</div>`; </div>`;
this.$serverInfoContainer.appendChild(this.insertNode(serverInfoTemplate)); this.$serverInfoContainer.appendChild(this.insertNode(serverInfoTemplate));
document.getElementById(`delete-server-action-${index}`).addEventListener('click', () => { document.getElementById(`delete-server-action-${index}`).addEventListener('click', () => {
this.domainUtil.removeDomain(index); DomainUtil.removeDomain(index);
this.initServers(); this.initServers();
// alert('Success. Reload to apply changes.'); // alert('Success. Reload to apply changes.');
ipcRenderer.send('reload-main'); ipcRenderer.send('reload-main');
@@ -109,13 +108,13 @@ class PreferenceView {
this.$newServerButton.classList.add('hidden'); this.$newServerButton.classList.add('hidden');
}); });
this.$saveServerButton.addEventListener('click', () => { this.$saveServerButton.addEventListener('click', () => {
this.domainUtil.checkDomain(this.$newServerUrl.value).then(domain => { DomainUtil.checkDomain(this.$newServerUrl.value).then(domain => {
const server = { const server = {
alias: this.$newServerAlias.value, alias: this.$newServerAlias.value,
url: domain, url: domain,
icon: this.$newServerIcon.value icon: this.$newServerIcon.value
}; };
this.domainUtil.addDomain(server); DomainUtil.addDomain(server);
this.$saveServerButton.classList.add('hidden'); this.$saveServerButton.classList.add('hidden');
this.$newServerButton.classList.remove('hidden'); this.$newServerButton.classList.remove('hidden');
this.$newServerForm.classList.add('hidden'); this.$newServerForm.classList.add('hidden');

View File

@@ -4,9 +4,17 @@ const {app} = require('electron').remote;
const JsonDB = require('node-json-db'); const JsonDB = require('node-json-db');
const request = require('request'); const request = require('request');
let instance = null;
const defaultIconUrl = 'https://chat.zulip.org/static/images/logo/zulip-icon-128x128.271d0f6a0ca2.png'; const defaultIconUrl = 'https://chat.zulip.org/static/images/logo/zulip-icon-128x128.271d0f6a0ca2.png';
class DomainUtil { class DomainUtil {
constructor() { constructor() {
if (instance) {
return instance;
} else {
instance = this;
}
this.db = new JsonDB(app.getPath('userData') + '/domain.json', true, true); this.db = new JsonDB(app.getPath('userData') + '/domain.json', true, true);
// Migrate from old schema // Migrate from old schema
if (this.db.getData('/').domain) { if (this.db.getData('/').domain) {
@@ -16,6 +24,8 @@ class DomainUtil {
}); });
this.db.delete('/domain'); this.db.delete('/domain');
} }
return instance;
} }
getDomains() { getDomains() {
@@ -69,4 +79,4 @@ class DomainUtil {
} }
} }
module.exports = DomainUtil; module.exports = new DomainUtil();

View File

@@ -26,4 +26,4 @@ class LinkUtil {
} }
} }
module.exports = LinkUtil; module.exports = new LinkUtil();

View File

@@ -52,4 +52,4 @@ class SystemUtil {
} }
} }
module.exports = SystemUtil; module.exports = new SystemUtil();