From d6d6c2aa4c0f439687d80931333998ddcbd0e5a2 Mon Sep 17 00:00:00 2001 From: Pierre Date: Thu, 3 May 2018 22:44:22 +0200 Subject: [PATCH] Added users tag support --- assets/css/common/global.css | 14 ++++ .../css/components/conversations/windows.css | 4 ++ assets/js/components/textParser.js | 66 ++++++++++++++++++- 3 files changed, 82 insertions(+), 2 deletions(-) diff --git a/assets/css/common/global.css b/assets/css/common/global.css index e39b6884..0bc5b947 100644 --- a/assets/css/common/global.css +++ b/assets/css/common/global.css @@ -9,4 +9,18 @@ */ a { cursor: pointer; +} + +/** + * like elements + */ +.a { + cursor: pointer; + outline: none; + text-decoration: none; + color: #3c8dbc; +} + +.a:focus, .a:hover { + color: #72afd2; } \ No newline at end of file diff --git a/assets/css/components/conversations/windows.css b/assets/css/components/conversations/windows.css index e21ddf59..29396e8e 100644 --- a/assets/css/components/conversations/windows.css +++ b/assets/css/components/conversations/windows.css @@ -100,6 +100,10 @@ height: 1em; } +#conversationsElem .direct-chat-text a, #conversationsElem .direct-chat-text .a { + color: inherit; +} + #conversationsElem .direct-chat-text.not-first-message::before, #conversationsElem .direct-chat-text.not-first-message::after { display: none; diff --git a/assets/js/components/textParser.js b/assets/js/components/textParser.js index bb1b2e6e..1bac9e53 100644 --- a/assets/js/components/textParser.js +++ b/assets/js/components/textParser.js @@ -13,13 +13,75 @@ ComunicWeb.components.textParser = { */ parse: function(info){ - + //Add space at the begining and the end of the content to ensure + //parsing will not encounter errors + info.element.innerHTML = " " + info.element.innerHTML + " "; + + //Prepare users tag parsing + this._prepare_user_tag_parsing(info.element); + //Parse emojies ComunicWeb.components.emoji.parser.parse({ element: info.element }); - } + //Parse users tags + this._parse_users_tag(info.element); + }, + + /** + * Prepare users tag parsing + * + * @param {HTMLElement} target The target element to prepare + */ + _prepare_user_tag_parsing: function(target){ + + //Find all occurences of users tag + while(target.innerHTML.match(/@[a-zA-Z0-9.]+/i)){ + + //Get user tag + var userTag = target.innerHTML.match(/@[a-zA-Z0-9.]+/i)[0]; + var userID = userTag.replace("@", ""); + + target.innerHTML = target.innerHTML.replace(userTag, ""+userID+""); + + } + + }, + + /** + * Parse users tag + * + * @param {HTMLElement} target The target element where user tags has + * to be parsed + */ + _parse_users_tag: function(target){ + + //Get the list of user tags of the target + var nodes = target.getElementsByTagName("userTag"); + + for (const num in nodes) { + if (nodes.hasOwnProperty(num)) { + const node = nodes[num]; + + //Get target user ID + const userID = node.innerHTML; + + //Adapt node content + node.innerHTML = "@" + userID; + node.className = "a"; + + //Set event listener + node.addEventListener("click", function(ev){ + + //Open user page + openUserPage(userID); + + }); + } + } + + }, } \ No newline at end of file