<commit_before>'use strict';

require(__dirname + '/js/tray.js');

const DomainUtil = require(__dirname + '/js/utils/domain-util.js');
const {linkIsInternal, skipImages} = require(__dirname + '/../main/link-helper');
const {shell, ipcRenderer} = require('electron');

class ServerManagerView {
	constructor() {
		this.$tabsContainer = document.getElementById('tabs-container');

		const $actionsContainer = document.getElementById('actions-container');
		this.$addServerButton = $actionsContainer.querySelector('#add-action');
		this.$settingsButton = $actionsContainer.querySelector('#settings-action');
		this.$content = document.getElementById('content');

		this.isLoading = false;
		this.settingsTabIndex = -1;
		this.activeTabIndex = -1;
		this.zoomFactors = [];
	}

	init() {
		this.domainUtil = new DomainUtil();
		this.initTabs();
		this.initActions();
		this.registerIpcs();
	}

	initTabs() {
		this.badgeNumberList = [];
		const servers = this.domainUtil.getDomains();
		if (servers.length > 0) {
			for (const server of servers) {
				this.initTab(server);
				this.badgeNumberList.push(0);
			}

			this.activateTab(0);
		} else {
			this.openSettings();
		}
	}

	initTab(tab) {
		const {
			url,
			icon
		} = tab;
		const tabTemplate = tab.template || `
				<div class="tab" domain="${url}">
					<div class="server-tab" style="background-image: url(${icon});"></div>
				</div>`;
		const $tab = this.insertNode(tabTemplate);
		const index = this.$tabsContainer.childNodes.length;
		this.$tabsContainer.appendChild($tab);
		$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;
		$webView.addEventListener('dom-ready', this.endLoading.bind(this, index));
		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 {
			$webView.classList.remove('disabled');
		}
	}

	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));
	}

	openSettings() {
		if (this.settingsTabIndex !== -1) {
			this.activateTab(this.settingsTabIndex);
			return;
		}
		const url = 'file://' + __dirname + '/preference.html';

		const settingsTabTemplate = `
				<div class="tab" domain="${url}">
					<div class="server-tab settings-tab">
						<i class="material-icons md-48">settings</i>
					</div>
				</div>`;
		this.initTab({
			alias: 'Settings',
			url,
			template: settingsTabTemplate
		});

		this.settingsTabIndex = this.$tabsContainer.childNodes.length - 1;
		this.activateTab(this.settingsTabIndex);
	}

	activateTab(index) {
		if (this.isLoading) {
			return;
		}

		if (this.activeTabIndex !== -1) {
			if (this.activeTabIndex === index) {
				return;
			} else {
				this.getTabAt(this.activeTabIndex).classList.remove('active');
			}
		}

		const $tab = this.getTabAt(index);
		$tab.classList.add('active');

		const domain = $tab.getAttribute('domain');
		this.startLoading(domain, index);
		this.activeTabIndex = index;
	}

	insertNode(html) {
		const wrapper = document.createElement('div');
		wrapper.innerHTML = html;
		return wrapper.firstElementChild;
	}

	getTabAt(index) {
		return this.$tabsContainer.childNodes[index];
	}

	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('page-title-updated', event => {
			const {title} = event;
			if (title.indexOf('Zulip') === -1) {
				return;
			}

			let messageCount = (/\(([0-9]+)\)/).exec(title);
			messageCount = messageCount ? Number(messageCount[1]) : 0;

			this.badgeNumberList[index] = messageCount;

			const sum = this.badgeNumberList.reduce((a, b) => {
				return a + b;
			}, 0);
			ipcRenderer.send('update-badge', sum);
		});
	}

	registerIpcs() {
		ipcRenderer.on('reload', () => {
			const activeWebview = document.getElementById(`webview-${this.activeTabIndex}`);
			activeWebview.reload();
		});

		ipcRenderer.on('back', () => {
			const activeWebview = document.getElementById(`webview-${this.activeTabIndex}`);
			if (activeWebview.canGoBack()) {<commit_msg>change badge count on organization nav change<commit_after>'use strict';

require(__dirname + '/js/tray.js');

const DomainUtil = require(__dirname + '/js/utils/domain-util.js');
const {linkIsInternal, skipImages} = require(__dirname + '/../main/link-helper');
const {shell, ipcRenderer} = require('electron');


class ServerManagerView {
	constructor() {
		this.$tabsContainer = document.getElementById('tabs-container');

		const $actionsContainer = document.getElementById('actions-container');
		this.$addServerButton = $actionsContainer.querySelector('#add-action');
		this.$settingsButton = $actionsContainer.querySelector('#settings-action');
		this.$content = document.getElementById('content');

		this.isLoading = false;
		this.settingsTabIndex = -1;
		this.activeTabIndex = -1;
		this.zoomFactors = [];
	}

	init() {
		this.domainUtil = new DomainUtil();
		this.initTabs();
		this.initActions();
		this.registerIpcs();
	}

	initTabs() {
		this.badgeNumberList = [];
		const servers = this.domainUtil.getDomains();
		if (servers.length > 0) {
			for (const server of servers) {
				this.initTab(server);
				this.badgeNumberList.push(0);
			}

			this.activateTab(0);
		} else {
			this.openSettings();
		}
	}

	initTab(tab) {
		const {
			url,
			icon
		} = tab;
		const tabTemplate = tab.template || `
				<div class="tab" domain="${url}">
					<div class="server-tab" style="background-image: url(${icon});"></div>
				</div>`;
		const $tab = this.insertNode(tabTemplate);
		const index = this.$tabsContainer.childNodes.length;
		this.$tabsContainer.appendChild($tab);
		$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;
		$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);
		});
		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');
		}
	}

	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));
	}

	openSettings() {
		if (this.settingsTabIndex !== -1) {
			this.activateTab(this.settingsTabIndex);
			return;
		}
		const url = 'file://' + __dirname + '/preference.html';

		const settingsTabTemplate = `
				<div class="tab" domain="${url}">
					<div class="server-tab settings-tab">
						<i class="material-icons md-48">settings</i>
					</div>
				</div>`;
		this.initTab({
			alias: 'Settings',
			url,
			template: settingsTabTemplate
		});

		this.settingsTabIndex = this.$tabsContainer.childNodes.length - 1;
		this.activateTab(this.settingsTabIndex);
	}

	activateTab(index) {
		if (this.isLoading) {
			return;
		}

		if (this.activeTabIndex !== -1) {
			if (this.activeTabIndex === index) {
				return;
			} else {
				this.getTabAt(this.activeTabIndex).classList.remove('active');
			}
		}

		const $tab = this.getTabAt(index);
		$tab.classList.add('active');

		const domain = $tab.getAttribute('domain');
		this.startLoading(domain, index);
		this.activeTabIndex = index;
	}

	insertNode(html) {
		const wrapper = document.createElement('div');
		wrapper.innerHTML = html;
		return wrapper.firstElementChild;
	}

	getTabAt(index) {
		return this.$tabsContainer.childNodes[index];
	}

	updateBadge (index) {
		const $activeWebView = document.getElementById(`webview-${index}`);
		const title = $activeWebView.getTitle();
		let messageCount = (/\(([0-9]+)\)/).exec(title);
		messageCount = messageCount ? Number(messageCount[1]) : 0;
		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('page-title-updated', event => {
			const {title} = event;
			if (title.indexOf('Zulip') === -1) {
				return;
			}

	
		});
	}

	registerIpcs() {
		ipcRenderer.on('reload', () => {
			const activeWebview = document.getElementById(`webview-${this.activeTabIndex}`);
			activeWebview.reload();
		});

		ipcRenderer.on('back', () => {
			const activeWebview = document.getElementById(`webview-${this.activeTabIndex}`);
			if (activeWebview.canGoBack()) {
{'fraction_non_alphanumeric': 0.16675814123673618, 'fraction_numerical': 0.003384559092572265, 'mean_word_length': 4.261308950914341, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '201566', 'n_tokens_mistral': 3980, 'n_tokens_neox': 3518, 'n_words': 583}