Browse Source

Merge branch 'notification' into dev

Marien Fressinaud 11 years ago
parent
commit
ad1ad913f3
4 changed files with 53 additions and 1 deletions
  1. 2 0
      app/i18n/en.php
  2. 2 0
      app/i18n/fr.php
  3. 2 0
      app/views/helpers/javascript_vars.phtml
  4. 47 1
      p/scripts/main.js

+ 2 - 0
app/i18n/en.php

@@ -343,6 +343,8 @@ return array (
 	'login_required'		=> 'Login required:',
 
 	'confirm_action'		=> 'Are you sure you want to perform this action? It cannot be cancelled!',
+	'notif_title_new_articles'	=> 'FreshRSS: new articles!',
+	'notif_body_new_articles'	=> 'There are \d new articles to read on FreshRSS.',
 
 	// DATE
 	'january'			=> 'January',

+ 2 - 0
app/i18n/fr.php

@@ -343,6 +343,8 @@ return array (
 	'login_required'		=> 'Accès protégé par mot de passe :',
 
 	'confirm_action'		=> 'Êtes-vous sûr(e) de vouloir continuer ? Cette action ne peut être annulée !',
+	'notif_title_new_articles'	=> 'FreshRSS : nouveaux articles !',
+	'notif_body_new_articles'	=> 'Il y a \d nouveaux articles à lire sur FreshRSS.',
 
 	// DATE
 	'january'			=> 'janvier',

+ 2 - 0
app/views/helpers/javascript_vars.phtml

@@ -49,6 +49,8 @@ echo 'authType="', $authType, '",',
 	'url_logout="', _url ('index', 'logout'), '",';
 
 echo 'str_confirmation="', Minz_Translate::t('confirm_action'), '"', ",\n";
+echo 'str_notif_title_articles="', Minz_Translate::t('notif_title_new_articles'), '"', ",\n";
+echo 'str_notif_body_articles="', Minz_Translate::t('notif_body_new_articles'), '"', ",\n";
 
 $autoActualise = Minz_Session::param('actualize_feeds', false);
 echo 'auto_actualize_feeds=', $autoActualise ? 'true' : 'false', ";\n";

+ 47 - 1
p/scripts/main.js

@@ -768,6 +768,46 @@ function init_notifications() {
 }
 // </notification>
 
+// <notifs html5>
+var notifs_html5_permission = 'denied',
+    notifs_html5_shown = false;
+
+function notifs_html5_is_supported() {
+	return window.Notification !== undefined;
+}
+
+function notifs_html5_ask_permission() {
+	window.Notification.requestPermission(function () {
+		notifs_html5_permission = window.Notification.permission;
+	});
+}
+
+function notifs_html5_show(nb) {
+	if (notifs_html5_permission !== "granted" || notifs_html5_shown) {
+		return
+	}
+
+	var notification = new window.Notification(str_notif_title_articles, {
+		icon: "../themes/icons/favicon-256.png",
+		body: str_notif_body_articles.replace("\d", nb)
+	});
+
+	notification.onclick = function() {
+		window.location.reload();
+	}
+
+	notifs_html5_shown = true;
+}
+
+function init_notifs_html5() {
+	if (!notifs_html5_is_supported()) {
+		return;
+	}
+
+	notifs_html5_permission = notifs_html5_ask_permission();
+}
+// </notifs html5>
+
 function refreshUnreads() {
 	$.getJSON('./?c=javascript&a=nbUnreadsPerFeed').done(function (data) {
 		var isAll = $('.category.all > .active').length > 0;
@@ -780,7 +820,12 @@ function refreshUnreads() {
 				$('#new-article').show();
 			};
 		});
-		faviconNbUnread();
+
+		var nb_unreads = str2int($('.category.all>a').attr('data-unread'));
+		if (nb_unreads > 0) {
+			faviconNbUnread(nb_unreads);
+			notifs_html5_show(nb_unreads);
+		}
 	});
 }
 
@@ -1123,6 +1168,7 @@ function init_all() {
 		init_shortcuts();
 		faviconNbUnread();
 		init_print_action();
+		init_notifs_html5();
 		window.setInterval(refreshUnreads, 120000);
 	} else {
 		init_share_observers();