annotate browser_side/notification.py @ 366:39ae5bf92786

browser_side: allow one blogger to moderate the comments made on his own blog messages.
author souliane <souliane@mailoo.org>
date Fri, 21 Feb 2014 18:44:47 +0100
parents 835a8ae799e7
children f9130176bc8d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
328
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 from __pyjamas__ import JS
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 class Notification(object):
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 """
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 If the browser supports it, the user allowed it to and the tab is in the
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 background, send desktop notifications on messages.
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 Requires both Web Notifications and Page Visibility API.
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 """
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 def __init__(self):
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 JS("""
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 this.enabled = false;
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 if (!('hidden' in document))
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 document.hidden = false;
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 if (!('Notification' in window))
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 return;
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 if (Notification.permission === 'granted')
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 this.enabled = true;
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 else if (Notification.permission === 'default') {
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 Notification.requestPermission(function(permission){
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 if (permission !== 'granted')
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 return;
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 this.enabled = true;
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 });
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 }
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 """)
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 def notify(self, title, body, icon='/media/icons/apps/48/sat.png'):
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 JS("""
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 if (this.enabled && document.hidden) {
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 notification = Notification(title, {body: body, icon: icon});
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 // Probably won’t work, but it doesn’t hurt to try.
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 notification.addEventListener('click', function() {
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 window.focus();
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 });
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 }
835a8ae799e7 Add notifications support, fixes bug 7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 """)