Merge pull request #919 from txanatan/tidyup-cordova

Move Cordova specific fixes into main branch
This commit is contained in:
Lorenz Hübschle-Schneider 2017-04-28 14:00:39 +02:00 committed by GitHub
commit eaffb17c5a
9 changed files with 115 additions and 30 deletions

View File

@ -14,7 +14,16 @@
"angular-mocks": "1.5.x",
"html5-boilerplate": "~4.3.0",
"underscore": "~1.8",
"bootstrap": "~3.1",
"bootstrap": "~3.3",
"emojione": "~2.2"
}
},
"keywords": [
"weechat",
"irc"
],
"ignore": [
"**/.*",
"node_modules",
"bower_components"
]
}

View File

@ -135,6 +135,17 @@ weechat.filter('DOMfilter', ['$filter', '$sce', function($filter, $sce) {
};
}]);
// This is used by the cordova app to change link targets to "window.open(<url>, '_system')"
// so that they're opened in a browser window and don't navigate away from Glowing Bear
weechat.filter('linksForCordova', ['$sce', function($sce) {
return function(text) {
// XXX TODO this needs to be improved
text = text.replace(/<a (rel="[a-z ]+"\s+)?(?:target="_[a-z]+"\s+)?href="([^"]+)"/gi,
"<a $1 onClick=\"window.open('$2', '_system')\"");
return $sce.trustAsHtml(text);
};
}]);
weechat.filter('getBufferQuickKeys', function () {
return function (obj, $scope) {
if (!$scope) { return obj; }

View File

@ -1,6 +1,13 @@
(function() {
'use strict';
// cordova splash screen
document.addEventListener("deviceready", function () {
if (navigator.splashscreen !== undefined) {
navigator.splashscreen.hide();
}
}, false);
var weechat = angular.module('weechat', ['ngRoute', 'localStorage', 'weechatModels', 'bufferResume', 'plugins', 'IrcUtils', 'ngSanitize', 'ngWebsockets', 'ngTouch'], ['$compileProvider', function($compileProvider) {
// hacky way to be able to find out if we're in debug mode
weechat.compileProvider = $compileProvider;
@ -37,12 +44,12 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
'onlyUnread': false,
'hotlistsync': true,
'orderbyserver': true,
'useFavico': true,
'useFavico': !utils.isCordova(),
'soundnotification': true,
'fontsize': '14px',
'fontfamily': (utils.isMobileUi() ? 'sans-serif' : 'Inconsolata, Consolas, Monaco, Ubuntu Mono, monospace'),
'readlineBindings': false,
'enableJSEmoji': (utils.isMobileUi() ? false : true),
'enableJSEmoji': !utils.isMobileUi(),
'enableMathjax': false,
'enableQuickKeys': true,
'customCSS': '',
@ -89,10 +96,10 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
})();
// Show a TLS warning if GB was loaded over an unencrypted connection,
// except for local instances (testing or electron)
// except for local instances (testing, cordova, or electron)
$scope.show_tls_warning = (window.location.protocol !== "https:") &&
(["localhost", "127.0.0.1", "::1"].indexOf(window.location.hostname) === -1) &&
!window.is_electron && !window.cordova;
!window.is_electron && !utils.isCordova();
if (window.is_electron) {
// Use packaged emojione sprite in the electron app
@ -225,7 +232,9 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
}
});
if (!utils.isCordova()) {
$rootScope.favico = new Favico({animation: 'none'});
}
$scope.notifications = notifications.unreadCount('notification');
$scope.unread = notifications.unreadCount('unread');
@ -234,7 +243,7 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
$scope.notifications = notifications.unreadCount('notification');
$scope.unread = notifications.unreadCount('unread');
if (settings.useFavico && $rootScope.favico) {
if (!utils.isCordova() && settings.useFavico && $rootScope.favico) {
notifications.updateFavico();
}
});
@ -243,7 +252,12 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
// Reset title
$rootScope.pageTitle = '';
$rootScope.notificationStatus = '';
// cancel outstanding notifications (incl cordova)
notifications.cancelAll();
if (window.plugin !== undefined && window.plugin.notification !== undefined && window.plugin.notification.local !== undefined) {
window.plugin.notification.local.cancelAll();
}
models.reinitialize();
$rootScope.$emit('notificationChanged');
@ -369,6 +383,11 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
if (!$rootScope.connected) {
return;
}
if (utils.isCordova()) {
return; // cordova doesn't have a favicon
}
if (useFavico) {
notifications.updateFavico();
} else {
@ -382,7 +401,8 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
// This also fires when the page is loaded if enabled.
// Note that this says MathJax but we switched to KaTeX
settings.addCallback('enableMathjax', function(enabled) {
if (enabled && !$rootScope.mathjax_init) {
// no latex math support for cordova right now
if (!utils.isCordova() && enabled && !$rootScope.mathjax_init) {
// Load MathJax only once
$rootScope.mathjax_init = true;
@ -803,8 +823,11 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
if ($rootScope.connected) {
$scope.disconnect();
}
if (!utils.isCordova()) {
$scope.favico.reset();
}
}
};
$scope.init = function() {

View File

@ -1,6 +1,6 @@
var weechat = angular.module('weechat');
weechat.factory('notifications', ['$rootScope', '$log', 'models', 'settings', function($rootScope, $log, models, settings) {
weechat.factory('notifications', ['$rootScope', '$log', 'models', 'settings', 'utils', function($rootScope, $log, models, settings, utils) {
var serviceworker = false;
var notifications = [];
// Ask for permission to display desktop notifications
@ -34,6 +34,21 @@ weechat.factory('notifications', ['$rootScope', '$log', 'models', 'settings', fu
$log.info('Service Worker err:', err);
});
}
document.addEventListener('deviceready', function() {
// Add cordova local notification click handler
if (utils.isCordova() && window.cordova.plugins !== undefined && window.cordova.plugins.notification !== undefined &&
window.cordova.plugins.notification.local !== undefined) {
window.cordova.plugins.notification.local.on("click", function (notification) {
// go to buffer
var data = JSON.parse(notification.data);
models.setActiveBuffer(data.buffer);
window.focus();
// clear this notification
window.cordova.plugins.notification.local.clear(notification.id);
});
}
});
};
var showNotification = function(buffer, title, body) {
@ -66,7 +81,7 @@ weechat.factory('notifications', ['$rootScope', '$log', 'models', 'settings', fu
toastNotifier.show(toast);
} else {
} else if (typeof Notification !== 'undefined') {
var notification = new Notification(title, {
body: body,
@ -97,6 +112,22 @@ weechat.factory('notifications', ['$rootScope', '$log', 'models', 'settings', fu
delete notifications[this.id];
};
} else if (utils.isCordova() && window.cordova.plugins !== undefined && window.cordova.plugins.notification !== undefined && window.cordova.plugins.notification.local !== undefined) {
// Cordova local notification
// Calculate notification id from buffer ID
// Needs to be unique number, but we'll only ever have one per buffer
var id = parseInt(buffer.id, 16);
// Cancel previous notification for buffer (if there was one)
window.cordova.plugins.notification.local.clear(id);
// Send new notification
window.cordova.plugins.notification.local.schedule({
id: id,
text: body,
title: title,
data: { buffer: buffer.id } // remember buffer id for when the notification is clicked
});
}
};
@ -135,6 +166,10 @@ weechat.factory('notifications', ['$rootScope', '$log', 'models', 'settings', fu
};
var updateFavico = function() {
if (utils.isCordova()) {
return; // cordova doesn't have a favicon
}
var notifications = unreadCount('notification');
if (notifications > 0) {
$rootScope.favico.badge(notifications, {
@ -200,8 +235,7 @@ weechat.factory('notifications', ['$rootScope', '$log', 'models', 'settings', fu
showNotification(buffer, title, body);
if (settings.soundnotification) {
// TODO fill in a sound file
if (!utils.isCordova() && settings.soundnotification) {
var audioFile = "assets/audio/sonar";
var soundHTML = '<audio autoplay="autoplay"><source src="' + audioFile + '.ogg" type="audio/ogg" /><source src="' + audioFile + '.mp3" type="audio/mpeg" /></audio>';
document.getElementById("soundNotification").innerHTML = soundHTML;

View File

@ -21,6 +21,10 @@ weechat.factory('utils', function() {
return (document.body.clientWidth < mobile_cutoff);
};
var isCordova = function() {
return window.cordova !== undefined;
};
// Inject a javascript (used by KaTeX)
var inject_script = function(script_url) {
@ -46,6 +50,7 @@ weechat.factory('utils', function() {
changeClassStyle: changeClassStyle,
getClassStyle: getClassStyle,
isMobileUi: isMobileUi,
isCordova: isCordova,
inject_script: inject_script,
inject_css: inject_css,
};

View File

@ -9,11 +9,11 @@
},
"app": {
"urls": [
"http://glowing-bear.github.io/glowing-bear/"
"https://www.glowing-bear.org/"
],
"launch": {
"container": "panel",
"web_url": "http://glowing-bear.github.io/glowing-bear/"
"web_url": "https://www.glowing-bear.org/"
}
},
"permissions": [

View File

@ -1,12 +1,12 @@
{
"name": "Glowing Bear",
"description": "WeeChat Web frontend",
"launch_path": "/glowing-bear/index.html",
"launch_path": "/index.html",
"icons": {
"128": "/glowing-bear/assets/img/glowing_bear_128x128.png",
"60": "/glowing-bear/assets/img/glowing_bear_60x60.png",
"90": "/glowing-bear/assets/img/glowing_bear_90x90.png",
"32": "/glowing-bear/assets/img/favicon.png"
"128": "/assets/img/glowing_bear_128x128.png",
"60": "/assets/img/glowing_bear_60x60.png",
"90": "/assets/img/glowing_bear_90x90.png",
"32": "/assets/img/favicon.png"
},
"installs_allowed_from": [
"*"

View File

@ -17,17 +17,20 @@
"src": "assets/img/glowing_bear_128x128.png",
"sizes": "128x128"
}],
"scope": "/glowing-bear/",
"start_url": "/glowing-bear/index.html",
"scope": "/",
"start_url": "/index.html",
"display": "standalone",
"orientation": "portrait-primary",
"theme_color": "#181818",
"background_color": "#333",
"prefer_related_applications": false,
"chrome_related_applications": [{
"chrome_related_applications": [
{
"platform": "web"
}, {
},
{
"platform": "android",
"location": "https://play.google.com/store/apps/details?id=com.glowing_bear"
}]
}
]
}