Przeglądaj źródła

add browsertab plugin

Dele Olajide 4 lat temu
rodzic
commit
9ef7ec8352

+ 4 - 1
index.html

@@ -16,6 +16,9 @@
     <link type="text/css" rel="stylesheet" media="screen" href="packages/stickers/stickers.css" />
     <script src="packages/stickers/stickers.js"></script>
 
+    <script src="packages/browsertab/favico.js"></script>
+    <script src="packages/browsertab/browsertab.js"></script>
+
 </head>
 <body class="reset">
     <div class="converse-content" style="display:none">
@@ -65,7 +68,7 @@
         discover_connection_methods: false,
         bosh_service_url: 'https://conversejs.org/http-bind/',
         view_mode: 'fullscreen',
-        whitelisted_plugins: ["download-dialog", "stickers", "toolbar-utilities"]
+        whitelisted_plugins: ["download-dialog", "stickers", "toolbar-utilities", "browsertab"]
     };
 
     converse.initialize(config);

+ 117 - 0
packages/browsertab/browsertab.js

@@ -0,0 +1,117 @@
+(function (root, factory) {
+    if (typeof define === 'function' && define.amd) {
+        define(["converse"], factory);
+    } else {
+        factory(converse);
+    }
+}(this, function (converse) {
+    let Strophe, $iq, $msg, $pres, $build, b64_sha1, _ , __, dayjs, html, _converse;
+    let tabTitle = document.title, favicon;
+
+    converse.plugins.add("browsertab", {
+
+        dependencies: [],
+
+        initialize: function () {
+            _converse = this._converse;
+
+            Strophe = converse.env.Strophe;
+            $iq = converse.env.$iq;
+            $msg = converse.env.$msg;
+            $pres = converse.env.$pres;
+            $build = converse.env.$build;
+            b64_sha1 = converse.env.b64_sha1;
+            _ = converse.env._;
+            dayjs = converse.env.dayjs;
+
+            _converse.api.settings.extend({
+                browsertab_use_title: true,
+                browsertab_use_favicon: true,
+                browsertab_settings: {type : 'circle', position: 'up', animation: 'none'}
+            });
+
+            _converse.on('message', function (data)
+            {
+                console.debug('message', data);
+
+                var chatbox = data.chatbox;
+                var message = data.stanza;
+                var history = message.querySelector('forwarded');
+                var body = message.querySelector('body');
+
+                if (!history && body && chatbox)
+                {
+                    setActiveConversationsUread(body.innerHTML);
+                }
+            });
+
+            _converse.api.listen.on('connected', function()
+            {
+                tabTitle = document.title;
+                favicon = new Favico(_converse.api.settings.get("browsertab_settings"));
+            });
+
+            _converse.api.listen.on('chatBoxViewInitialized', function(view)
+            {
+                console.debug("chatBoxInitialized", view.model.get("jid"));
+
+                view.el.querySelector('.chat-textarea').addEventListener("focus", function(evt)
+                {
+                    console.debug("chatBox focus", evt);
+                    setUnreadCount(0);
+                });
+
+                view.el.querySelector('.chat-textarea').addEventListener("blur", function(evt)
+                {
+                    console.debug("chatBox blur", evt);
+                    setUnreadCount(0);
+                });
+            });
+
+            console.log("browsertab plugin is ready");
+        }
+    });
+
+
+    function setActiveConversationsUread(newMessage)
+    {
+        let count = 0;
+
+        _converse.chatboxes.each(function (chatbox)
+        {
+            if (chatbox.get("type") == "chatbox")
+            {
+                count = count + chatbox.get("num_unread");
+            }
+            else
+
+            if (chatbox.get("type") == "chatroom")
+            {
+                count = count + chatbox.get("num_unread_general");
+            }
+        });
+
+        setUnreadCount(count);
+    }
+
+    function setUnreadCount(count)
+    {
+        console.debug("setUnreadCount", count, _converse.api.settings.get("browsertab_use_favicon"), _converse.api.settings.get("browsertab_use_title"));
+
+        if (_converse.api.settings.get("browsertab_use_title"))
+        {
+            if (count == 0 || count == "0")
+            {
+                document.title = tabTitle;
+            }
+            else {
+                document.title = '(' + count + ') ' + tabTitle;
+            }
+        }
+
+        if (_converse.api.settings.get("browsertab_use_favicon"))
+        {
+            if (favicon) favicon.badge(count);
+        }
+    }
+}));

BIN
packages/browsertab/browsertab.png


Plik diff jest za duży
+ 6 - 0
packages/browsertab/favico.js


+ 11 - 0
packages/browsertab/readme.md

@@ -0,0 +1,11 @@
+# Browser tab plugin for converse.js
+
+<img src="https://github.com/conversejs/community-plugins/blob/master/packages/browsertab/browsertab.png?raw=true" />
+
+## Overview
+This plugin puts a counter in browser tab in converse.js  for both direct and groupchat unread messages
+
+## Install
+see https://m.conversejs.org/docs/html/plugin_development.html on how to install this plugin
+
+

Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików