Port core features from index.js (Open link, Unread count, Font)

This commit is contained in:
Yuya Ochiai
2015-12-23 15:53:34 +09:00
parent 7f8ba9fd56
commit c016c03741
2 changed files with 80 additions and 6 deletions

View File

@@ -9,6 +9,10 @@ const Tab = ReactBootstrap.Tab;
const electron = require('electron');
const remote = electron.remote;
const osLocale = require('os-locale');
const fs = require('fs');
const url = require('url');
const settings = require('../common/settings');
var MainPage = React.createClass({
@@ -57,13 +61,77 @@ var MainPage = React.createClass({
});
var MattermostView = React.createClass({
getInitialState: function() {
return {
unreadCount: 0
};
},
handleUnreadCountChange: function(count) {
this.setState({
unreadCount: count
});
if (this.props.onUnreadCountChange) {
this.props.onUnreadCountChange(count);
}
},
componentDidMount: function() {
var thisObj = this;
var webview = ReactDOM.findDOMNode(this.refs.webview);
// Open link in browserWindow. for exmaple, attached files.
webview.addEventListener('new-window', function(e) {
var currentURL = url.parse(webview.getURL());
var destURL = url.parse(e.url);
if (currentURL.host === destURL.host) {
window.open(e.url, 'electron-mattermost');
} else {
// if the link is external, use default browser.
require('shell').openExternal(e.url);
}
});
webview.addEventListener("dom-ready", function() {
// webview.openDevTools();
// Use 'Meiryo UI' and 'MS Gothic' to prevent CJK fonts on Windows(JP).
if (process.platform === 'win32') {
var applyCssFile = function(cssFile) {
fs.readFile(cssFile, 'utf8', function(err, data) {
if (err) {
console.log(err);
return;
}
webview.insertCSS(data);
});
};
osLocale(function(err, locale) {
if (err) {
console.log(err);
return;
}
if (locale === 'ja_JP') {
applyCssFile(__dirname + '/css/jp_fonts.css');
}
});
}
});
webview.addEventListener('ipc-message', function(event) {
switch (event.channel) {
case 'onUnreadCountChange':
var unreadCount = event.args[0];
thisObj.handleUnreadCountChange(unreadCount);
break;
}
});
},
render: function() {
// 'disablewebsecurity' is necessary to display external images.
// However, it allows also CSS/JavaScript.
// So webview should use 'allowDisplayingInsecureContent' as same as BrowserWindow.
return (
<webview style={ this.props.style } preload="webview/mattermost.js" src={ this.props.src } allowpopups></webview>
);
return (<webview style={ this.props.style } preload="webview/mattermost.js" src={ this.props.src } ref="webview"></webview>);
}
});

View File

@@ -4,10 +4,16 @@ const electron = require('electron');
const ipc = electron.ipcRenderer;
const NativeNotification = Notification;
ipc.on('retrieveUnreadCount', function() {
var unreadCountTimer = setInterval(function() {
if (!this.count) {
this.count = 0;
}
var unreadCount = document.getElementsByClassName('unread-title').length;
ipc.sendToHost('retrieveUnreadCount', unreadCount);
});
if (this.count != unreadCount) {
ipc.sendToHost('onUnreadCountChange', unreadCount);
}
this.count = unreadCount;
}, 1000);
// On Windows 8.1 and Windows 8, a shortcut with a Application User Model ID must be installed to the Start screen.
// In current version, use tray balloon for notification