浏览代码

Add new public API method `converse.insertInto`

JC Brand 5 年之前
父节点
当前提交
cf258f0b4c
共有 3 个文件被更改,包括 26 次插入1 次删除
  1. 1 0
      CHANGES.md
  2. 1 1
      Makefile
  3. 24 0
      src/converse-chatboxviews.js

+ 1 - 0
CHANGES.md

@@ -43,6 +43,7 @@ Soon we'll deprecate the latter, so prepare now.
 - New config option [muc_hats_from_vcard](https://conversejs.org/docs/html/configuration.html#muc-hats-from-vcard).
 - New config option [muc_send_probes](https://conversejs.org/docs/html/configuration.html#muc-send-probes).
 - New config option [show_message_avatar](https://conversejs.org/docs/html/configuration.html#show-message-avatar).
+- New public API [converse.insertInto](https://conversejs.org/docs/html/api/converse.html#.insertInto)
 
 ## 6.0.0 (2020-01-09)
 

+ 1 - 1
Makefile

@@ -208,7 +208,7 @@ test:
 ## Documentation
 
 ./bin/activate:
-	python3.7 -m venv .
+	python3 -m venv .
 
 .installed.cfg: requirements.txt buildout.cfg
 	./bin/pip install -r requirements.txt

+ 24 - 0
src/converse-chatboxviews.js

@@ -154,5 +154,29 @@ converse.plugins.add('converse-chatboxviews', {
         api.listen.on('chatBoxViewsInitialized', () => calculateViewportHeightUnit());
         window.addEventListener('resize', () => calculateViewportHeightUnit());
         /************************ END Event Handlers ************************/
+
+
+        Object.assign(converse, {
+            /**
+             * Public API method which will ensure that the #conversejs element
+             * is inserted into a container element.
+             *
+             * This method is useful when the #conversejs element has been
+             * detached from the DOM somehow.
+             * @async
+             * @memberOf converse
+             * @method insertInto
+             * @example
+             * converse.insertInto(document.querySelector('#converse-container'));
+             */
+            insertInto (container) {
+                const el = _converse.chatboxviews?.el;
+                if (el && !container.contains(el)) {
+                    container.insertAdjacentElement('afterBegin', el);
+                } else if (!el) {
+                    throw new Error("Cannot insert non-existing #conversejs element into the DOM");
+                }
+            }
+        });
     }
 });