aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 1 | <html> |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 2 | <!-- Empty head might be needed for setSenderImage --> |
| 3 | <head> |
Frederic Guimont | d8343e6 | 2016-11-01 23:31:25 -0400 | [diff] [blame] | 4 | <meta name="viewport" content="width=device-width, initial-scale=1" /> |
Frédéric Guimont | 13778a6 | 2016-11-02 21:21:21 -0400 | [diff] [blame] | 5 | <meta charset=“utf-8”> |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 6 | </head> |
| 7 | |
| 8 | <body> |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 9 | <div class="navbar-wrapper"> |
| 10 | <div id="navbar"> |
| 11 | <div id="backButton" class="nav-button non-action-button nav-left" onmouseover="addBackButtonHoverProperty()" onclick="backToWelcomeView()" title="Hide chat view"> |
| 12 | <svg class="svgicon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24"> |
| 13 | <path d="M11.67 3.87L9.9 2.1 0 12l9.9 9.9 1.77-1.77L3.54 12z"/> |
| 14 | <path fill="none" d="M0 0h24v24H0z"/> |
| 15 | </svg> |
| 16 | </div> |
| 17 | <div id="nav-contactid" class="nav-left"> |
| 18 | <div id="nav-contactid-alias"></div> |
| 19 | <div id="nav-contactid-bestId"></div> |
| 20 | </div> |
| 21 | <div style="display:none" class="deactivated nav-button action-button nav-right" onclick="moreOptions()" title="Options"> |
| 22 | <svg class="svgicon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24"> |
| 23 | <path d="M0 0h24v24H0z" fill="none"/> |
| 24 | <path d="M12 8c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2zm0 2c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm0 6c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2z"/> |
| 25 | </svg> |
| 26 | </div> |
| 27 | <div id="callButtons"> <!-- callButtons block allows more efficient hiding of placeCallButton and placeAudioCallButton --> |
| 28 | <div id="placeCallButton" class="nav-button action-button nav-right" onclick="placeCall()" title="Place video call"> |
| 29 | <svg class="svgicon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"> |
| 30 | <path d="M0 0h24v24H0z" fill="none"/> |
| 31 | <path d="M17 10.5V7c0-.55-.45-1-1-1H4c-.55 0-1 .45-1 1v10c0 .55.45 1 1 1h12c.55 0 1-.45 1-1v-3.5l4 4v-11l-4 4z"/> |
| 32 | </svg> |
| 33 | </div> |
| 34 | <div id="placeAudioCallButton" class="nav-button action-button nav-right" onclick="placeAudioCall()" title="Place audio call"> |
| 35 | <svg class="svgicon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"> |
| 36 | <path fill="none" d="M0 0h24v24H0z"/> |
| 37 | <path d="M20.01 15.38c-1.23 0-2.42-.2-3.53-.56-.35-.12-.74-.03-1.01.24l-1.57 1.97c-2.83-1.35-5.48-3.9-6.89-6.83l1.95-1.66c.27-.28.35-.67.24-1.02-.37-1.11-.56-2.3-.56-3.53 0-.54-.45-.99-.99-.99H4.19C3.65 3 3 3.24 3 3.99 3 13.28 10.73 21 20.01 21c.71 0 .99-.63.99-1.18v-3.45c0-.54-.45-.99-.99-.99z"/> |
| 38 | </svg> |
| 39 | </div> |
| 40 | </div> |
| 41 | <div id="addToConversationsButton" class="nav-button action-button nav-right" onclick="addToConversations()" title="Add to conversations"> |
| 42 | <svg class="svgicon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"> |
| 43 | <path d="M0 0h24v24H0z" fill="none"/> |
| 44 | <path d="M15 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm-9-2V7H4v3H1v2h3v3h2v-3h3v-2H6zm9 4c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/> |
| 45 | </svg> |
| 46 | </div> |
| 47 | <div id="addBannedContactButton" class="nav-button action-critical-button nav-right" onclick="addBannedContact()" title="Unban banned contact"> |
| 48 | <svg class="svgicon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"> |
| 49 | <path fill="none" d="M0 0h24v24H0V0z"/> |
| 50 | <circle cx="15" cy="8" r="4"/> |
| 51 | <path d="M23 20v-2c0-2.3-4.1-3.7-6.9-3.9l6 5.9h.9zm-11.6-5.5C9.2 15.1 7 16.3 7 18v2h9.9l4 4 1.3-1.3-21-20.9L0 3.1l4 4V10H1v2h3v3h2v-3h2.9l2.5 2.5zM6 10v-.9l.9.9H6z"/> |
| 52 | </svg> |
| 53 | </div> |
| 54 | </div> |
| 55 | <div id="invitation"> |
| 56 | <div id="text"> |
| 57 | </div> |
| 58 | <div id="actions"> |
| 59 | <div id="accept-btn" class="invitation-button button-green" onclick="acceptInvitation()" >Accept</div> |
| 60 | <div id="refuse-btn" class="invitation-button button-red" onclick="refuseInvitation()" >Refuse</div> |
| 61 | <div id="block-btn" class="invitation-button button-red" onclick="blockConversation()" >Block</div> |
| 62 | </div> |
| 63 | </div> |
| 64 | </div> |
Adrien Beraud | 8e25afb | 2017-04-19 01:38:57 +0200 | [diff] [blame] | 65 | <div id="container"> |
| 66 | <div id="messages"></div> |
AmarOk | b425324 | 2017-07-13 11:21:39 -0400 | [diff] [blame] | 67 | <div id="sendMessage"> |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 68 | <div class="nav-button action-button" onclick="sendFile()" title="Send File"> |
Hugo Lefeuvre | 8127fe1 | 2018-05-23 10:53:12 -0400 | [diff] [blame] | 69 | <svg class="svgicon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"> |
| 70 | <path d="M16.5 6v11.5c0 2.21-1.79 4-4 4s-4-1.79-4-4V5c0-1.38 1.12-2.5 2.5-2.5s2.5 1.12 2.5 2.5v10.5c0 .55-.45 1-1 1s-1-.45-1-1V6H10v9.5c0 1.38 1.12 2.5 2.5 2.5s2.5-1.12 2.5-2.5V5c0-2.21-1.79-4-4-4S7 2.79 7 5v12.5c0 3.04 2.46 5.5 5.5 5.5s5.5-2.46 5.5-5.5V6h-1.5z"/> |
| 71 | <path d="M0 0h24v24H0z" fill="none"/> |
| 72 | </svg> |
AmarOk | b425324 | 2017-07-13 11:21:39 -0400 | [diff] [blame] | 73 | </div> |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 74 | <textarea id="message" autofocus placeholder="Type a message" onkeyup="grow_text_area()" onkeydown="process_messagebar_keydown()" rows="1" disabled="false"></textarea> |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 75 | <div class="nav-button action-button" onclick="sendMessage()" title="Send"> |
Hugo Lefeuvre | 8127fe1 | 2018-05-23 10:53:12 -0400 | [diff] [blame] | 76 | <svg class="svgicon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"> |
| 77 | <path d="M2.01 21L23 12 2.01 3 2 10l15 2-15 2z"/> |
| 78 | <path d="M0 0h24v24H0z" fill="none"/> |
| 79 | </svg> |
Guillaume Roguez | 5b137be | 2018-02-21 10:44:58 -0500 | [diff] [blame] | 80 | </div> |
AmarOk | b425324 | 2017-07-13 11:21:39 -0400 | [diff] [blame] | 81 | </div> |
Adrien Beraud | 8e25afb | 2017-04-19 01:38:57 +0200 | [diff] [blame] | 82 | </div> |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 83 | </body> |
| 84 | |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 85 | <script> |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 86 | "use strict" |
AmarOk | 6286ad4 | 2017-07-14 12:11:08 -0400 | [diff] [blame] | 87 | |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 88 | /* Constants used at several places*/ |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 89 | const messageBarPlaceHolder = "Type a message" |
| 90 | const avatar_size = 35 |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 91 | // scrollDetectionThresh represents the number of pixels a user can scroll |
| 92 | // without disabling the automatic go-back-to-bottom when a new message is |
| 93 | // received |
| 94 | const scrollDetectionThresh = 200 |
| 95 | |
| 96 | /* Buffers */ |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 97 | var historyBufferIndex = 0 // When showing a large amount of interactions, this counter store the interaction's index to show |
| 98 | var historyBuffer = [] // Before showing a large amount of interactions, this array is used as a buffer. |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 99 | |
| 100 | /* We retrieve refs to the most used navbar and message bar elements for efficiency purposes */ |
| 101 | /* NOTE: always use getElementById when possible, way more efficient */ |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 102 | const backButton = document.getElementById("backButton") |
| 103 | const aliasField = document.getElementById("nav-contactid-alias") |
| 104 | const bestIdField = document.getElementById("nav-contactid-bestId") |
| 105 | const idField = document.getElementById("nav-contactid") |
| 106 | const messageBar = document.getElementById("sendMessage") |
| 107 | const messageBarInput = document.getElementById("message") |
| 108 | const messages = document.getElementById("messages") |
| 109 | const addToConvButton = document.getElementById("addToConversationsButton") |
| 110 | const invitation = document.getElementById("invitation") |
| 111 | const navbar = document.getElementById("navbar") |
| 112 | const invitationText = document.getElementById("text") |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 113 | |
| 114 | /* States: allows us to avoid re-doing something if it isn't meaningful */ |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 115 | var displayLinksEnabled = false |
| 116 | var hoverBackButtonAllowed = true |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 117 | var hasInvitation = false |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 118 | var isTemporary = false |
| 119 | var isBanned = false |
| 120 | |
| 121 | /** |
| 122 | * Generic wrapper. Execute passed function keeping scroll position identical. |
| 123 | * |
| 124 | * @param func function to execute |
| 125 | * @param args parameters as array |
| 126 | */ |
| 127 | function exec_keeping_scroll_position(func, args) { |
| 128 | var atEnd = messages.scrollTop >= messages.scrollHeight - messages.clientHeight - scrollDetectionThresh |
| 129 | func(...args) |
| 130 | if (atEnd) { |
| 131 | messages.scrollTop = messages.scrollHeight |
| 132 | } |
| 133 | } |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 134 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 135 | /** |
| 136 | * Update common frame between conversations. |
| 137 | * |
| 138 | * Whenever the current conversation is switched, information from the navbar |
| 139 | * and message bar have to be updated to match new contact. This function |
| 140 | * handles most of the required work (except the showing/hiding the invitation, |
| 141 | * which is handled by showInvitation()). |
| 142 | * |
| 143 | * @param banned whether contact is banned or not |
| 144 | * @param temporary whether contact is temporary or not |
| 145 | * @param alias |
| 146 | * @param bestId |
| 147 | */ |
| 148 | /* exported update_chatview_frame */ |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 149 | function update_chatview_frame(banned, temporary, alias, bestid) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 150 | navbar.style.display = "none" |
AmarOk | 6286ad4 | 2017-07-14 12:11:08 -0400 | [diff] [blame] | 151 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 152 | hoverBackButtonAllowed = true |
AmarOk | 6286ad4 | 2017-07-14 12:11:08 -0400 | [diff] [blame] | 153 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 154 | aliasField.innerHTML = (alias ? alias : bestid) |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 155 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 156 | if(alias) { |
| 157 | bestIdField.innerHTML = bestid |
| 158 | idField.classList.remove("oneEntry") |
| 159 | } else { |
| 160 | idField.classList.add("oneEntry") |
| 161 | } |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 162 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 163 | if (isBanned !== banned) { |
| 164 | isBanned = banned |
| 165 | hideMessageBar(banned) |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 166 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 167 | if(banned) { |
| 168 | // contact is banned. update navbar and states |
| 169 | navbar.classList.add("onBannedState") |
| 170 | } else { |
| 171 | navbar.classList.remove("onBannedState") |
| 172 | } |
| 173 | } else if (isTemporary !== temporary) { |
| 174 | isTemporary = temporary |
| 175 | if (temporary) { |
| 176 | addToConvButton.style.display = "flex" |
| 177 | messageBarInput.placeholder = "Note: an interaction will create a new contact." |
| 178 | } else { |
| 179 | addToConvButton.style.display = "" |
| 180 | messageBarInput.placeholder = messageBarPlaceHolder |
| 181 | } |
| 182 | } |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 183 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 184 | navbar.style.display = "" |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 188 | * Hide or show invitation. |
| 189 | * |
| 190 | * Invitation is hidden if no contactAlias/invalid alias is passed. |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 191 | * Otherwise, invitation div is updated. |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 192 | * |
| 193 | * @param contactAlias |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 194 | */ |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 195 | /* exported showInvitation */ |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 196 | function showInvitation(contactAlias) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 197 | if (!contactAlias) { |
| 198 | if (hasInvitation) { |
| 199 | hasInvitation = false |
| 200 | invitation.style.visibility = "" |
| 201 | } |
| 202 | } else { |
| 203 | hasInvitation = true |
| 204 | invitationText.innerHTML = "<h1>" + contactAlias + " sends you an invitation</h1>" |
| 205 | + "Do you want to add them to the conversations list?<br>" |
| 206 | + "Note: you can automatically accept this invitation by sending a message." |
| 207 | invitation.style.visibility = "visible" |
| 208 | } |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Hide or show navbar, and update body top padding accordingly. |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 213 | * |
| 214 | * @param isVisible whether navbar should be displayed or not |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 215 | */ |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 216 | /* exported displayNavbar */ |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 217 | function displayNavbar(isVisible) |
| 218 | { |
| 219 | if (isVisible) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 220 | navbar.classList.remove("hiddenState") |
| 221 | document.documentElement.style.setProperty("--navbar-size", undefined) |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 222 | } else { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 223 | navbar.classList.add("hiddenState") |
| 224 | document.documentElement.style.setProperty("--navbar-size", "0") |
AmarOk | 6286ad4 | 2017-07-14 12:11:08 -0400 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 228 | /** |
| 229 | * Hide or show message bar, and update body bottom padding accordingly. |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 230 | * |
| 231 | * @param isHidden whether message bar should be displayed or not |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 232 | */ |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 233 | /* exported hideMessageBar */ |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 234 | function hideMessageBar(isHidden) { |
| 235 | if (isHidden) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 236 | messageBar.classList.add("hiddenState") |
| 237 | document.body.style.setProperty("--messagebar-size", "0") |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 238 | } else { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 239 | messageBar.classList.remove("hiddenState") |
| 240 | document.body.style.removeProperty("--messagebar-size") |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 244 | /* exported setDisplayLinks */ |
| 245 | function setDisplayLinks(display) { |
| 246 | displayLinksEnabled = display |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * This event handler dynamically resizes the message bar depending on the amount of |
| 251 | * text entered, while adjusting the body paddings so that that the message bar doesn't |
| 252 | * overlap messages when it grows. |
| 253 | */ |
| 254 | /* exported grow_text_area */ |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 255 | function grow_text_area() { |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 256 | exec_keeping_scroll_position(function(){ |
| 257 | var old_height = window.getComputedStyle(messageBar).height |
| 258 | messageBarInput.style.height = "auto" /* <-- necessary, no clue why */ |
| 259 | messageBarInput.style.height = messageBarInput.scrollHeight + "px" |
| 260 | var new_height = window.getComputedStyle(messageBar).height |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 261 | |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 262 | var msgbar_size = window.getComputedStyle(document.body).getPropertyValue("--messagebar-size") |
| 263 | var total_size = parseInt(msgbar_size) + parseInt(new_height) - parseInt(old_height) |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 264 | |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 265 | document.body.style.setProperty("--messagebar-size", total_size.toString() + "px") |
| 266 | }, []) |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /** |
| 270 | * This event handler processes keydown events in the navbar. When pressed key is |
| 271 | * the enter key, send the message unless shift or control was pressed too. |
| 272 | * |
| 273 | * @param key the pressed key |
| 274 | */ |
| 275 | /* exported process_messagebar_keydown */ |
| 276 | function process_messagebar_keydown(key) { |
| 277 | key = key || event |
| 278 | var map = {} |
| 279 | map[key.keyCode] = key.type == "keydown" |
| 280 | if (key.ctrlKey || key.shiftKey) { |
| 281 | return true |
| 282 | } |
| 283 | if (map[13]) { |
| 284 | sendMessage() |
| 285 | key.preventDefault() |
| 286 | } |
| 287 | return true |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Disable or enable textarea. |
| 292 | * |
| 293 | * @param isDisabled whether message bar should be enabled or disabled |
| 294 | */ |
| 295 | /* exported disableSendMessage */ |
| 296 | function disableSendMessage(isDisabled) |
| 297 | { |
| 298 | messageBarInput.disabled = isDisabled |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * This event handler adds the hover property back to the "back to welcome view" |
| 303 | * button. |
| 304 | * |
| 305 | * This is a hack. It needs some explanations. |
| 306 | * |
| 307 | * Problem: Whenever the "back to welcome view" button is clicked, the webview |
| 308 | * freezes and the GTK ring welcome view is displayed. While the freeze |
| 309 | * itself is perfectly fine (probably necessary for good performances), this |
| 310 | * is a big problem for us when the user opens a chatview again: Since the |
| 311 | * chatview was freezed, the back button has «remembered» the hover state and |
| 312 | * still displays the blue background for a small instant. This is a very bad |
| 313 | * looking artefact. |
| 314 | * |
| 315 | * In order to counter this problem, we introduced the following evil mechanism: |
| 316 | * Whenever a user clicks on the "back to welcome view" button, the hover |
| 317 | * property is disabled. The hover property stays disabled until the user calls |
| 318 | * this event handler by hover-ing the button. |
| 319 | */ |
| 320 | /* exported addBackButtonHoverProperty */ |
| 321 | function addBackButtonHoverProperty() |
| 322 | { |
| 323 | if(hoverBackButtonAllowed) { |
| 324 | backButton.classList.add("non-action-button") |
| 325 | } |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Sébastien Blin | c6f3a26 | 2017-07-28 16:12:24 -0400 | [diff] [blame] | 328 | /* |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 329 | * Update timestamps messages. |
Sébastien Blin | c6f3a26 | 2017-07-28 16:12:24 -0400 | [diff] [blame] | 330 | */ |
| 331 | function updateView() { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 332 | updateTimestamps() |
Sébastien Blin | c6f3a26 | 2017-07-28 16:12:24 -0400 | [diff] [blame] | 333 | } |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 334 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 335 | setInterval(updateView, 60000) |
Sébastien Blin | c6f3a26 | 2017-07-28 16:12:24 -0400 | [diff] [blame] | 336 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 337 | window.onresize = function() { |
| 338 | updateTimestamps() |
| 339 | } |
Sébastien Blin | 82d0d2d | 2018-03-14 17:21:42 -0400 | [diff] [blame] | 340 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 341 | /* exported addBannedContact */ |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 342 | function addBannedContact() |
| 343 | { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 344 | window.prompt("UNBLOCK") |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 345 | } |
| 346 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 347 | /* exported addToConversations */ |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 348 | function addToConversations() |
| 349 | { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 350 | window.prompt("ADD_TO_CONVERSATIONS") |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 351 | } |
| 352 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 353 | /* exported placeCall */ |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 354 | function placeCall() |
| 355 | { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 356 | window.prompt("PLACE_CALL") |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 357 | } |
| 358 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 359 | /* exported placeAudioCall */ |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 360 | function placeAudioCall() |
| 361 | { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 362 | window.prompt("PLACE_AUDIO_CALL") |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 363 | } |
| 364 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 365 | /* exported backToWelcomeView */ |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 366 | function backToWelcomeView() |
| 367 | { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 368 | backButton.classList.remove("non-action-button") |
| 369 | hoverBackButtonAllowed = false |
| 370 | window.prompt("CLOSE_CHATVIEW") |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 371 | } |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 372 | |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 373 | /** |
| 374 | * Transform a date to a string group like "1 hour ago". |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 375 | * |
| 376 | * @param date |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 377 | */ |
| 378 | function formatDate(date) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 379 | const seconds = Math.floor((new Date() - date) / 1000) |
| 380 | var interval = Math.floor(seconds / (3600 * 24)) |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 381 | if (interval > 5) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 382 | return date.toLocaleDateString() |
Sébastien Blin | 70dc0b7 | 2017-07-31 16:24:41 -0400 | [diff] [blame] | 383 | } |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 384 | if (interval > 1) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 385 | return interval + " days ago" |
Sébastien Blin | c6f3a26 | 2017-07-28 16:12:24 -0400 | [diff] [blame] | 386 | } |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 387 | if (interval === 1) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 388 | return interval + " day ago" |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 389 | } |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 390 | interval = Math.floor(seconds / 3600) |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 391 | if (interval > 1) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 392 | return interval + " hours ago" |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 393 | } |
| 394 | if (interval === 1) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 395 | return interval + " hour ago" |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 396 | } |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 397 | interval = Math.floor(seconds / 60) |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 398 | if (interval > 1) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 399 | return interval + " minutes ago" |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 400 | } |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 401 | return "just now" |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 402 | } |
Sébastien Blin | c6f3a26 | 2017-07-28 16:12:24 -0400 | [diff] [blame] | 403 | |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 404 | /** |
| 405 | * Send #sendMessage #message value |
| 406 | */ |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 407 | function sendMessage() |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 408 | { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 409 | var message = messageBarInput.value |
| 410 | if (message.length > 0) { |
| 411 | messageBarInput.value = "" |
| 412 | window.prompt("SEND:" + message) |
| 413 | } |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 414 | } |
Sébastien Blin | f4f9028 | 2017-10-03 14:07:16 -0400 | [diff] [blame] | 415 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 416 | /* exported acceptInvitation */ |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 417 | function acceptInvitation() |
| 418 | { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 419 | window.prompt("ACCEPT") |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 420 | } |
Sébastien Blin | f4f9028 | 2017-10-03 14:07:16 -0400 | [diff] [blame] | 421 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 422 | /* exported refuseInvitation */ |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 423 | function refuseInvitation() |
| 424 | { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 425 | window.prompt("REFUSE") |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 426 | } |
Sébastien Blin | f4f9028 | 2017-10-03 14:07:16 -0400 | [diff] [blame] | 427 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 428 | /* exported blockConversation */ |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 429 | function blockConversation() |
| 430 | { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 431 | window.prompt("BLOCK") |
| 432 | } |
| 433 | |
| 434 | /* exported sendFile */ |
| 435 | function sendFile() |
| 436 | { |
| 437 | window.prompt("SEND_FILE") |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 438 | } |
Sébastien Blin | f4f9028 | 2017-10-03 14:07:16 -0400 | [diff] [blame] | 439 | |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 440 | /** |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 441 | * Clear all messages. |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 442 | */ |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 443 | /* exported clearMessages */ |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 444 | function clearMessages() |
| 445 | { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 446 | messages.innerHTML = "" |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /** |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 450 | * Convert text to HTML. |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 451 | */ |
| 452 | function escapeHtml(html) |
| 453 | { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 454 | var text = document.createTextNode(html) |
| 455 | var div = document.createElement("div") |
| 456 | div.appendChild(text) |
| 457 | return div.innerHTML |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | |
| 461 | /** |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 462 | * Get the youtube video id from a URL. |
| 463 | * @param url |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 464 | */ |
| 465 | function youtube_id(url) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 466 | const regExp = /^.*(youtu\.be\/|v\/|\/u\/w|embed\/|watch\?v=|&v=)([^#&?]*).*/ |
| 467 | const match = url.match(regExp) |
| 468 | return (match && match[2].length == 11) ? match[2] : null |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /** |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 472 | * Returns HTML message from the message text, cleaned and linkified. |
| 473 | * @param message_text |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 474 | */ |
| 475 | function getMessageHtml(message_text) |
| 476 | { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 477 | const escaped_message = escapeHtml(message_text) |
| 478 | var linkified_message = linkifyHtml(escaped_message, {}) // eslint-disable-line no-undef |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 479 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 480 | const textPart = document.createElement("pre") |
| 481 | textPart.innerHTML = linkified_message |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 482 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 483 | return textPart.outerHTML |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Returns the message status, formatted for display |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 488 | * @param message_delivery_status |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 489 | */ |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 490 | /* exported getMessageDeliveryStatusText */ |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 491 | function getMessageDeliveryStatusText(message_delivery_status) |
| 492 | { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 493 | var formatted_delivery_status = message_delivery_status |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 494 | |
| 495 | switch(message_delivery_status) |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 496 | { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 497 | case "sending": |
| 498 | case "ongoing": |
| 499 | formatted_delivery_status = "Sending<svg overflow='visible' viewBox='0 -2 16 14' height='16px' width='16px'><circle class='status_circle anim-first' cx='4' cy='12' r='1'/><circle class='status_circle anim-second' cx='8' cy='12' r='1'/><circle class='status_circle anim-third' cx='12' cy='12' r='1'/></svg>" |
| 500 | break |
| 501 | case "failure": |
| 502 | formatted_delivery_status = "Failure <svg overflow='visible' viewBox='0 -2 16 14' height='16px' width='16px'><path class='status-x x-first' stroke='#AA0000' stroke-linecap='round' stroke-linejoin='round' stroke-width='3' fill='none' d='M4,4 L12,12'/><path class='status-x x-second' stroke='#AA0000' stroke-linecap='round' stroke-linejoin='round' stroke-width='3' fill='none' d='M12,4 L4,12'/></svg>" |
| 503 | break |
| 504 | case "sent": |
| 505 | case "finished": |
| 506 | case "unknown": |
| 507 | case "read": |
| 508 | formatted_delivery_status = "" |
| 509 | break |
| 510 | default: |
| 511 | break |
Sébastien Blin | 82d0d2d | 2018-03-14 17:21:42 -0400 | [diff] [blame] | 512 | } |
| 513 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 514 | return formatted_delivery_status |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 515 | } |
Sébastien Blin | 82d0d2d | 2018-03-14 17:21:42 -0400 | [diff] [blame] | 516 | |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 517 | /** |
| 518 | * Returns the message date, formatted for display |
| 519 | */ |
| 520 | function getMessageTimestampText(message_timestamp, custom_format) |
| 521 | { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 522 | const date = new Date(1000 * message_timestamp) |
| 523 | if(custom_format) { |
| 524 | return formatDate(date) |
| 525 | } else { |
| 526 | return date.toLocaleString() |
| 527 | } |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 528 | } |
Sébastien Blin | 82d0d2d | 2018-03-14 17:21:42 -0400 | [diff] [blame] | 529 | |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 530 | /** |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 531 | * Merge timestamps if they are from the same group. For instance, several "1 hour ago" |
| 532 | * timestamps will be merged into a single one. |
| 533 | * |
| 534 | * @param baseNode |
| 535 | * @param endIndex |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 536 | */ |
| 537 | function cleanPreviousTimestamps (baseNode, endIndex) { |
| 538 | // Remove previous elements with the same formatted timestamp |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 539 | exec_keeping_scroll_position(function() { |
| 540 | for (var c = endIndex - 1 ; c >= 0 ; --c) { |
| 541 | const child = messages.children[c] |
| 542 | if (child.className.indexOf("timestamp") !== -1) { |
| 543 | if (child.className === baseNode.className && |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 544 | child.innerHTML === baseNode.innerHTML) { |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 545 | messages.removeChild(child) |
| 546 | } else { |
| 547 | break // A different timestamp is met, we can stop here. |
| 548 | } |
Sébastien Blin | 1e2dd35 | 2018-04-13 11:34:53 -0400 | [diff] [blame] | 549 | } |
Sébastien Blin | 82d0d2d | 2018-03-14 17:21:42 -0400 | [diff] [blame] | 550 | } |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 551 | }, []) |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 552 | } |
Sébastien Blin | 82d0d2d | 2018-03-14 17:21:42 -0400 | [diff] [blame] | 553 | |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 554 | /** |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 555 | * Update timestamps. |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 556 | */ |
| 557 | function updateTimestamps() { |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 558 | exec_keeping_scroll_position(function() { |
| 559 | const timestamps = messages.getElementsByClassName(".timestamp") |
| 560 | const is_image_out = messages.querySelector(".message_out .sender_image") |
| 561 | const is_image_in = messages.querySelector(".message_in .sender_image") |
| 562 | if (timestamps) { |
| 563 | for ( var c = 0 ; c < messages.children.length ; ++c) { |
| 564 | const child = messages.children[c] |
| 565 | if (child.className.indexOf("timestamp") !== -1) { |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 566 | // Update timestamp |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 567 | child.innerHTML = getMessageTimestampText(child.getAttribute("message_timestamp"), true) |
Sébastien Blin | 82d0d2d | 2018-03-14 17:21:42 -0400 | [diff] [blame] | 568 | |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 569 | var desktop_margin = "25%" |
| 570 | const height = document.body.clientHeight |
| 571 | const width = document.body.clientWidth |
| 572 | if (width <= 1920 || height <= 1080) { |
| 573 | desktop_margin = "15%" |
| 574 | } |
| 575 | if (width <= 1000 || height <= 480) { |
| 576 | desktop_margin = "0px" |
| 577 | } |
| 578 | if (child.className.indexOf("timestamp_out") !== -1) { |
| 579 | const avatar_px = is_image_out ? (is_image_out.offsetHeight === avatar_size ? "60px" : "20px") : "20px" |
| 580 | child.style.paddingRight = `calc(${desktop_margin} + ${avatar_px})` |
| 581 | } else if (child.className.indexOf("timestamp_in") !== -1) { |
| 582 | const avatar_px = is_image_in ? (is_image_in.offsetHeight === avatar_size ? "60px" : "20px") : "20px" |
| 583 | child.style.paddingLeft = `calc(${desktop_margin} + ${avatar_px})` |
| 584 | } |
| 585 | // Remove previous elements with the same formatted timestamp |
| 586 | cleanPreviousTimestamps(child, c) |
Adrien Beraud | 8e25afb | 2017-04-19 01:38:57 +0200 | [diff] [blame] | 587 | } |
| 588 | } |
Frédéric Guimont | 3e5f1b6 | 2016-11-22 11:41:38 -0500 | [diff] [blame] | 589 | } |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 590 | }, []) |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 591 | } |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 592 | |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 593 | /** |
| 594 | * Convert a value in filesize |
| 595 | */ |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 596 | function humanFileSize(bytes) { |
| 597 | var thresh = 1024 |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 598 | if(Math.abs(bytes) < thresh) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 599 | return bytes + " B" |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 600 | } |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 601 | var units = ["kB","MB","GB","TB","PB","EB","ZB","YB"] |
| 602 | var u = -1 |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 603 | do { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 604 | bytes /= thresh |
| 605 | ++u |
| 606 | } while(Math.abs(bytes) >= thresh && u < units.length - 1) |
| 607 | return bytes.toFixed(1)+" "+units[u] |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /** |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 611 | * Change the value of the progress bar. |
| 612 | * |
| 613 | * @param progress_bar |
| 614 | * @param message_object |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 615 | */ |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 616 | function updateProgressBar(progress_bar, message_object) { |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 617 | exec_keeping_scroll_position(function() { |
| 618 | var delivery_status = message_object["delivery_status"] |
| 619 | if ("progress" in message_object && !isErrorStatus(delivery_status) && message_object["progress"] !== 100) { |
| 620 | var progress_percent = (100 * message_object["progress"] / message_object["totalSize"]) |
| 621 | if (progress_percent !== 100) |
| 622 | progress_bar.childNodes[0].setAttribute("style", "width: " + progress_percent + "%") |
| 623 | else |
| 624 | progress_bar.setAttribute("style", "display: none") |
| 625 | } else |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 626 | progress_bar.setAttribute("style", "display: none") |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 627 | }, []) |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | /** |
| 631 | * Check if a status is an error status |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 632 | * @param |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 633 | */ |
| 634 | function isErrorStatus(status) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 635 | return (status === "failure" |
| 636 | || status === "awaiting peer timeout" |
| 637 | || status === "canceled" |
| 638 | || status === "unjoinable peer") |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | /** |
| 642 | * Build a new file interaction |
| 643 | * @param message_id |
| 644 | */ |
| 645 | function fileInteraction(message_id) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 646 | var message_wrapper = document.createElement("div") |
| 647 | message_wrapper.setAttribute("class", "message_wrapper") |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 648 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 649 | /* A file interaction contains buttons at the left for status information |
| 650 | or accept/refuse actions. The text is bold and clickable. */ |
| 651 | var left_buttons = document.createElement("div") |
| 652 | left_buttons.setAttribute("class", "left_buttons") |
| 653 | message_wrapper.appendChild(left_buttons) |
| 654 | var text_div = document.createElement("div") |
| 655 | text_div.setAttribute("class", "text") |
| 656 | |
| 657 | text_div.addEventListener("click", function () { |
| 658 | // ask ring to open the file |
| 659 | const filename = document.querySelector("#message_" + message_id + " .full").innerText |
| 660 | window.prompt("OPEN_FILE:" + filename) |
| 661 | }) |
| 662 | |
| 663 | var full_div = document.createElement("div") |
| 664 | full_div.setAttribute("class", "full") |
| 665 | full_div.style = "visibility: hidden; display: none;" |
| 666 | var message_text = document.createElement("div") |
| 667 | message_text.setAttribute("class", "filename") |
| 668 | |
| 669 | // And some informations like size or error message. |
| 670 | var informations_div = document.createElement("div") |
| 671 | informations_div.setAttribute("class", "informations") |
| 672 | text_div.appendChild(message_text) |
| 673 | text_div.appendChild(full_div) |
| 674 | text_div.appendChild(informations_div) |
| 675 | message_wrapper.appendChild(text_div) |
| 676 | |
| 677 | // And finally, a progress bar |
| 678 | var message_transfer_progress_bar = document.createElement("span") |
| 679 | message_transfer_progress_bar.setAttribute("class", "message_progress_bar") |
| 680 | var message_transfer_progress_completion = document.createElement("span") |
| 681 | message_transfer_progress_bar.appendChild(message_transfer_progress_completion) |
| 682 | message_wrapper.appendChild(message_transfer_progress_bar) |
| 683 | return message_wrapper |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | /** |
| 687 | * Update a file interaction (icons + filename + status + progress bar) |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 688 | * |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 689 | * @param message_div the message to update |
| 690 | * @param message_object new informations |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 691 | * @param forceTypeToFile |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 692 | */ |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 693 | function updateFileInteraction(message_div, message_object, forceTypeToFile = false) { |
| 694 | if (!message_div.querySelector(".informations")) return // media |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 695 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 696 | var acceptSvg = "<svg height=\"24\" viewBox=\"0 0 24 24\" width=\"24\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M0 0h24v24H0z\" fill=\"none\"/><path d=\"M9 16.2L4.8 12l-1.4 1.4L9 19 21 7l-1.4-1.4L9 16.2z\"/></svg>", |
| 697 | refuseSvg = "<svg height=\"24\" viewBox=\"0 0 24 24\" width=\"24\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z\"/><path d=\"M0 0h24v24H0z\" fill=\"none\"/></svg>", |
| 698 | fileSvg = "<svg fill=\"#000000\" height=\"24\" viewBox=\"0 0 24 24\" width=\"24\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M16.5 6v11.5c0 2.21-1.79 4-4 4s-4-1.79-4-4V5c0-1.38 1.12-2.5 2.5-2.5s2.5 1.12 2.5 2.5v10.5c0 .55-.45 1-1 1s-1-.45-1-1V6H10v9.5c0 1.38 1.12 2.5 2.5 2.5s2.5-1.12 2.5-2.5V5c0-2.21-1.79-4-4-4S7 2.79 7 5v12.5c0 3.04 2.46 5.5 5.5 5.5s5.5-2.46 5.5-5.5V6h-1.5z\"/><path d=\"M0 0h24v24H0z\" fill=\"none\"/></svg>", |
| 699 | warningSvg = "<svg fill=\"#000000\" height=\"24\" viewBox=\"0 0 24 24\" width=\"24\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M0 0h24v24H0z\" fill=\"none\"/><path d=\"M1 21h22L12 2 1 21zm12-3h-2v-2h2v2zm0-4h-2v-4h2v4z\"/></svg>" |
| 700 | var message_delivery_status = message_object["delivery_status"] |
| 701 | var message_direction = message_object["direction"] |
| 702 | var message_id = message_object["id"] |
| 703 | var message_text = message_object["text"] |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 704 | |
| 705 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 706 | if (isImage(message_text) && message_delivery_status === "finished" && displayLinksEnabled && !forceTypeToFile) { |
| 707 | // Replace the old wrapper by the downloaded image |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 708 | exec_keeping_scroll_position(function() { |
| 709 | if (message_div.querySelector(".message_wrapper")) { |
| 710 | var wrapper = message_div.querySelector(".message_wrapper") |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 711 | wrapper.parentNode.removeChild(wrapper) |
| 712 | } |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 713 | message_div.append(mediaInteraction(message_id, message_text)) |
| 714 | message_div.querySelector("img").id = message_id |
| 715 | message_div.querySelector("img").msg_obj = message_object |
| 716 | message_div.querySelector("img").onerror = function() { |
| 717 | message_div = document.getElementById("#message_" + this.id) |
| 718 | var wrapper = message_div.querySelector(".message_wrapper") |
| 719 | if (wrapper) { |
| 720 | wrapper.parentNode.removeChild(wrapper) |
| 721 | } |
| 722 | message_div.append(fileInteraction(message_id)) |
| 723 | updateFileInteraction(message_div, this.msg_obj, true) |
| 724 | } |
| 725 | }, []) |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 726 | return |
| 727 | } |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 728 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 729 | // Set informations text |
| 730 | var informations_div = message_div.querySelector(".informations") |
| 731 | var informations_txt = getMessageTimestampText(message_object["timestamp"], true) |
| 732 | if (message_object["totalSize"] && message_object["progress"]) { |
| 733 | if (message_delivery_status === "finished") { |
| 734 | informations_txt += " - " + humanFileSize(message_object["totalSize"]) |
| 735 | } else { |
| 736 | informations_txt += " - " + humanFileSize(message_object["progress"]) |
| 737 | + " / " + humanFileSize(message_object["totalSize"]) |
| 738 | } |
| 739 | } |
| 740 | informations_txt += " - " + message_delivery_status |
| 741 | informations_div.innerText = informations_txt |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 742 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 743 | // Update flat buttons |
| 744 | var left_buttons = message_div.querySelector(".left_buttons") |
| 745 | left_buttons.innerHTML = "" |
| 746 | if (message_delivery_status === "awaiting peer" || message_delivery_status === "awaiting host" || message_delivery_status.indexOf("ongoing") === 0) { |
| 747 | if (message_direction === "in" && message_delivery_status.indexOf("ongoing") !== 0) { |
| 748 | // add buttons to accept or refuse a call. |
| 749 | var accept_button = document.createElement("div") |
| 750 | accept_button.innerHTML = acceptSvg |
| 751 | accept_button.setAttribute("title", "Accept") |
| 752 | accept_button.setAttribute("class", "flat-button accept") |
| 753 | accept_button.onclick = function() { |
| 754 | window.prompt("ACCEPT_FILE:" + message_id) |
| 755 | } |
| 756 | left_buttons.appendChild(accept_button) |
| 757 | } |
| 758 | var refuse_button = document.createElement("div") |
| 759 | refuse_button.innerHTML = refuseSvg |
| 760 | refuse_button.setAttribute("title", "Refuse") |
| 761 | refuse_button.setAttribute("class", "flat-button refuse") |
| 762 | refuse_button.onclick = function() { |
| 763 | window.prompt("REFUSE_FILE:" + message_id) |
| 764 | } |
| 765 | left_buttons.appendChild(refuse_button) |
| 766 | } else { |
| 767 | var status_button = document.createElement("div") |
| 768 | var statusFile = fileSvg |
| 769 | if (isErrorStatus(message_delivery_status)) |
| 770 | statusFile = warningSvg |
| 771 | status_button.innerHTML = statusFile |
| 772 | status_button.setAttribute("class", "flat-button") |
| 773 | left_buttons.appendChild(status_button) |
| 774 | } |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 775 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 776 | message_div.querySelector(".full").innerText = message_text |
| 777 | message_div.querySelector(".filename").innerText = message_text.split("/").pop() |
| 778 | message_div.querySelector(".filename").innerText = message_text.split("/").pop() |
| 779 | updateProgressBar(message_div.querySelector(".message_progress_bar"), message_object) |
| 780 | updateProgressBar(message_div.querySelector(".message_progress_bar"), message_object) |
| 781 | } |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 782 | |
| 783 | /** |
| 784 | * Return if a file is an image |
| 785 | * @param file |
| 786 | */ |
| 787 | function isImage(file) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 788 | return file.toLowerCase().match(/\.(jpeg|jpg|gif|png)$/) !== null |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | /** |
| 792 | * Return if a file is a youtube video |
| 793 | * @param file |
| 794 | */ |
| 795 | function isVideo(file) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 796 | const availableProtocols = ["http:", "https:"] |
| 797 | const videoHostname = ["youtube.com", "www.youtube.com", "youtu.be"] |
| 798 | const urlParser = document.createElement("a") |
| 799 | urlParser.href = file |
| 800 | return (availableProtocols.includes(urlParser.protocol) && videoHostname.includes(urlParser.hostname)) |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | /** |
| 804 | * Try to show an image or a video link (youtube for now) |
| 805 | * @param message_id |
| 806 | * @param link to show |
| 807 | * @param ytid if it's a youtube video |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 808 | * @param noerror |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 809 | */ |
| 810 | function mediaInteraction(message_id, link, ytid, noerror) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 811 | /* TODO promise? |
| 812 | Try to display images. */ |
| 813 | const media_wrapper = document.createElement("div") |
| 814 | media_wrapper.setAttribute("class", "media_wrapper") |
| 815 | const linkElt = document.createElement("a") |
| 816 | linkElt.href = link |
| 817 | linkElt.style = "text-decoration: none; border:none;" |
| 818 | const imageElt = document.createElement("img") |
| 819 | /* Note, here, we don't check the size of the image. |
| 820 | in the future, we can check the content-type and content-length with a request |
| 821 | and maybe disable svg */ |
| 822 | if (noerror) |
| 823 | imageElt.setAttribute("onerror", "this.style.display='none'") |
| 824 | if (ytid) { |
| 825 | imageElt.src = `http://img.youtube.com/vi/${ytid}/0.jpg` |
| 826 | } else { |
| 827 | imageElt.src = link |
| 828 | } |
| 829 | linkElt.appendChild(imageElt) |
| 830 | if (ytid) { |
| 831 | const containerElt = document.createElement("div") |
| 832 | containerElt.setAttribute("class", "containerVideo") |
| 833 | const playDiv = document.createElement("div") |
| 834 | playDiv.setAttribute("class", "playVideo") |
| 835 | playDiv.innerHTML = "<svg fill=\"#ffffff\" viewBox=\"0 0 24 24\" xmlns=\"http://www.w3.org/2000/svg\">\ |
| 836 | <path d=\"M8 5v14l11-7z\"/>\ |
| 837 | <path d=\"M0 0h24v24H0z\" fill=\"none\"/>\ |
| 838 | </svg>" |
| 839 | linkElt.appendChild(playDiv) |
| 840 | containerElt.appendChild(linkElt) |
| 841 | media_wrapper.appendChild(containerElt) |
| 842 | } else { |
| 843 | media_wrapper.appendChild(linkElt) |
| 844 | } |
| 845 | return media_wrapper |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | /** |
| 849 | * Build a new text interaction |
| 850 | * @param message_id |
| 851 | * @param htmlText the DOM to show |
| 852 | */ |
| 853 | function textInteraction(message_id, htmlText) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 854 | const message_wrapper = document.createElement("div") |
| 855 | message_wrapper.setAttribute("class", "message_wrapper") |
| 856 | var message_text = document.createElement("span") |
| 857 | message_text.setAttribute("class", "message_text") |
| 858 | message_text.innerHTML = htmlText |
| 859 | message_wrapper.appendChild(message_text) |
| 860 | // TODO STATUS |
| 861 | return message_wrapper |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | /** |
| 865 | * Update a text interaction (text) |
| 866 | * @param message_div the message to update |
| 867 | * @param delivery_status the status of the message |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 868 | */ |
| 869 | function updateTextInteraction(message_div, delivery_status) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 870 | if (!message_div.querySelector(".message_text")) return // media |
| 871 | var sending = message_div.querySelector(".sending") |
| 872 | switch(delivery_status) |
| 873 | { |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 874 | case "ongoing": |
| 875 | case "sending": |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 876 | if (!sending) { |
| 877 | sending = document.createElement("div") |
| 878 | sending.setAttribute("class", "sending") |
| 879 | sending.innerHTML = "<svg overflow=\"hidden\" viewBox=\"0 -2 16 14\" height=\"16px\" width=\"16px\"><circle class=\"status_circle anim-first\" cx=\"4\" cy=\"12\" r=\"1\"/><circle class=\"status_circle anim-second\" cx=\"8\" cy=\"12\" r=\"1\"/><circle class=\"status_circle anim-third\" cx=\"12\" cy=\"12\" r=\"1\"/></svg>" |
| 880 | // add sending animation to message; |
Sébastien Blin | d2ea6dd | 2018-04-18 11:35:33 -0400 | [diff] [blame] | 881 | message_div.insertBefore(sending, message_div.querySelector(".menu_interaction")) |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 882 | } |
| 883 | message_div.querySelector(".message_text").style = "color: #888" |
| 884 | break |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 885 | case "failure": |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 886 | // change text color to red |
| 887 | message_div.querySelector(".message_wrapper").style = "background-color: #f3a6a6" |
| 888 | message_div.querySelector(".message_text").style = "color: #000" |
| 889 | var failure_div = message_div.querySelector(".failure") |
| 890 | if (!failure_div) { |
| 891 | failure_div = document.createElement("div") |
| 892 | failure_div.setAttribute("class", "failure") |
| 893 | failure_div.innerHTML = "<svg overflow=\"visible\" viewBox=\"0 -2 16 14\" height=\"16px\" width=\"16px\"><path class=\"status-x x-first\" stroke=\"#AA0000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"3\" fill=\"none\" d=\"M4,4 L12,12\"/><path class=\"status-x x-second\" stroke=\"#AA0000\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"3\" fill=\"none\" d=\"M12,4 L4,12\"/></svg>" |
| 894 | // add failure animation to message |
Sébastien Blin | d2ea6dd | 2018-04-18 11:35:33 -0400 | [diff] [blame] | 895 | message_div.insertBefore(failure_div, message_div.querySelector(".menu_interaction")) |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 896 | } |
| 897 | if (sending) sending.style.display = "none" |
| 898 | break |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 899 | case "sent": |
| 900 | case "finished": |
| 901 | case "unknown": |
| 902 | case "read": |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 903 | // change text color to black |
| 904 | message_div.querySelector(".message_text").style = "color: #000" |
| 905 | if (sending) sending.style.display = "none" |
| 906 | break |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 907 | default: |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 908 | break |
| 909 | } |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | /** |
| 913 | * Build a new interaction (call or contact) |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 914 | */ |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 915 | function actionInteraction() { |
| 916 | var message_wrapper = document.createElement("div") |
| 917 | message_wrapper.setAttribute("class", "message_wrapper") |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 918 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 919 | // An file interaction contains buttons at the left of the interaction |
| 920 | // for the status or accept/refuse |
| 921 | var left_buttons = document.createElement("div") |
| 922 | left_buttons.setAttribute("class", "left_buttons") |
| 923 | message_wrapper.appendChild(left_buttons) |
| 924 | // Also contains a bold clickable text |
| 925 | var text_div = document.createElement("div") |
| 926 | text_div.setAttribute("class", "text") |
| 927 | message_wrapper.appendChild(text_div) |
| 928 | return message_wrapper |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | /** |
| 932 | * Update a call interaction (icon + text) |
| 933 | * @param message_div the message to update |
| 934 | * @param message_object new informations |
| 935 | */ |
| 936 | function updateCallInteraction(message_div, message_object) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 937 | const outgoingCall = "<svg fill=\"#219d55\" height=\"24\" viewBox=\"0 0 24 24\" width=\"24\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M0 0h24v24H0z\" fill=\"none\"/><path d=\"M9 5v2h6.59L4 18.59 5.41 20 17 8.41V15h2V5z\"/></svg>" |
| 938 | const callMissed = "<svg fill=\"#dc2719\" height=\"24\" viewBox=\"0 0 24 24\" width=\"24\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M0 0h24v24H0z\" fill=\"none\"/><path d=\"M19.59 7L12 14.59 6.41 9H11V7H3v8h2v-4.59l7 7 9-9z\"/></svg>" |
| 939 | const outgoingMissed = "<svg fill=\"#dc2719\" height=\"24\" viewBox=\"0 0 24 24\" width=\"24\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><path d=\"M24 24H0V0h24v24z\" id=\"a\"/></defs><clipPath id=\"b\"><use overflow=\"visible\" xlink:href=\"#a\"/></clipPath><path clip-path=\"url(#b)\" d=\"M3 8.41l9 9 7-7V15h2V7h-8v2h4.59L12 14.59 4.41 7 3 8.41z\"/></svg>" |
| 940 | const callReceived = "<svg fill=\"#219d55\" height=\"24\" viewBox=\"0 0 24 24\" width=\"24\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M0 0h24v24H0z\" fill=\"none\"/><path d=\"M20 5.41L18.59 4 7 15.59V9H5v10h10v-2H8.41z\"/></svg>" |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 941 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 942 | const message_text = message_object["text"] |
| 943 | const message_direction = (message_text.toLowerCase().indexOf("incoming") !== -1) ? "in" : "out" |
| 944 | const missed = message_text.indexOf("Missed") !== -1 |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 945 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 946 | message_div.querySelector(".text").innerText = message_text.substring(2) |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 947 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 948 | var left_buttons = message_div.querySelector(".left_buttons") |
| 949 | left_buttons.innerHTML = "" |
| 950 | var status_button = document.createElement("div") |
| 951 | var statusFile = "" |
| 952 | if (missed) |
| 953 | statusFile = (message_direction === "in") ? callMissed : outgoingMissed |
| 954 | else |
| 955 | statusFile = (message_direction === "in") ? callReceived : outgoingCall |
| 956 | status_button.innerHTML = statusFile |
| 957 | status_button.setAttribute("class", "flat-button") |
| 958 | left_buttons.appendChild(status_button) |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | /** |
| 962 | * Update a contact interaction (icon + text) |
| 963 | * @param message_div the message to update |
| 964 | * @param message_object new informations |
| 965 | */ |
| 966 | function updateContactInteraction(message_div, message_object) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 967 | const message_text = message_object["text"] |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 968 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 969 | message_div.querySelector(".text").innerText = message_text |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 970 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 971 | var left_buttons = message_div.querySelector(".left_buttons") |
| 972 | left_buttons.innerHTML = "" |
| 973 | var status_button = document.createElement("div") |
| 974 | status_button.innerHTML = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\">\ |
| 975 | <path d=\"M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z\"/>\ |
| 976 | <path d=\"M0 0h24v24H0z\" fill=\"none\"/></svg>" |
| 977 | status_button.setAttribute("class", "flat-button") |
| 978 | left_buttons.appendChild(status_button) |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | /** |
Sébastien Blin | d2ea6dd | 2018-04-18 11:35:33 -0400 | [diff] [blame] | 982 | * Remove an interaction from the conversation |
| 983 | * @param interaction_id |
| 984 | */ |
| 985 | /* exported removeInteraction */ |
| 986 | function removeInteraction(interaction_id) { |
| 987 | var interaction = document.querySelector(`#message_${interaction_id}`) |
| 988 | var child = interaction |
| 989 | var messages = document.querySelector("#messages") |
| 990 | var i = 0 |
| 991 | while( (child = child.previousSibling) != null ) |
| 992 | i++ |
| 993 | if (interaction) interaction.parentNode.removeChild(interaction) |
| 994 | if (i < messages.children.length) { |
| 995 | var timestampAfter = messages.children[i].classList.contains("timestamp") |
| 996 | var timestampBefore = messages.children[i].classList.contains("timestamp") |
| 997 | if (timestampAfter && timestampBefore) { |
| 998 | messages.children[i].parentNode.removeChild(messages.children[i]) |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | /** |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1004 | * Add a message to the conversation. |
| 1005 | * @param message_object to treat |
| 1006 | * @param new_message if it's a new message or if we need to update |
| 1007 | * @param insert_after if we want the message at the end or the top of the conversation |
| 1008 | */ |
| 1009 | function addOrUpdateMessage(message_object, new_message, insert_after = true) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1010 | const message_id = message_object["id"] |
| 1011 | const message_type = message_object["type"] |
| 1012 | const message_text = message_object["text"] |
| 1013 | const message_direction = message_object["direction"] |
| 1014 | const message_timestamp = message_object["timestamp"] |
| 1015 | const delivery_status = message_object["delivery_status"] |
| 1016 | const message_sender_contact_method = message_object["sender_contact_method"] |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1017 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1018 | var message_div = document.getElementById("message_" + message_id) |
| 1019 | var type = "" |
| 1020 | if (new_message) { |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1021 | // Build new message |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1022 | var classes = [ |
| 1023 | "message", |
| 1024 | `message_${message_direction}`, |
| 1025 | `message_type_${message_type}` |
| 1026 | ] |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1027 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1028 | message_div = document.createElement("div") |
| 1029 | message_div.setAttribute("id", `message_${message_id}`) |
| 1030 | message_div.setAttribute("class", classes.join(" ")) |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1031 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1032 | // Build message for each types. |
| 1033 | // Add sender images if necessary (like if the interaction doesn't take the whole width) |
| 1034 | const need_sender = (message_type === "data_transfer" || message_type === "text") |
| 1035 | if (need_sender) { |
| 1036 | var message_sender_image = document.createElement("span") |
| 1037 | message_sender_image.setAttribute("class", `sender_image sender_image_${message_sender_contact_method}`) |
| 1038 | message_div.appendChild(message_sender_image) |
Frédéric Guimont | 3e5f1b6 | 2016-11-22 11:41:38 -0500 | [diff] [blame] | 1039 | } |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1040 | |
| 1041 | // Build main content |
| 1042 | if (message_type === "data_transfer") { |
| 1043 | if (isImage(message_text) && delivery_status === "finished" && displayLinksEnabled) { |
| 1044 | message_div.append(mediaInteraction(message_id, message_text, null, false)) |
| 1045 | message_div.querySelector("img").id = message_id |
| 1046 | message_div.querySelector("img").msg_obj = message_object |
| 1047 | message_div.querySelector("img").onerror = function() { |
| 1048 | message_div = document.getElementById("#message_" + this.id) |
| 1049 | /* FIXME in many cases, message_div is null here and this code doesn't do anything */ |
| 1050 | if (message_div.querySelector(".message_wrapper")) { |
| 1051 | var wrapper = message_div.querySelector(".message_wrapper") |
| 1052 | wrapper.parentNode.removeChild(wrapper) |
| 1053 | } |
| 1054 | message_div.append(fileInteraction(message_id)) |
| 1055 | updateFileInteraction(message_div, this.msg_obj, true) |
| 1056 | } |
| 1057 | } else { |
| 1058 | message_div.append(fileInteraction(message_id)) |
| 1059 | } |
| 1060 | } else if (message_type === "text") { |
| 1061 | // TODO add the possibility to update messages (remove and rebuild) |
| 1062 | const htmlText = getMessageHtml(message_text) |
| 1063 | if (displayLinksEnabled) { |
| 1064 | const parser = new DOMParser() |
| 1065 | const DOMMsg = parser.parseFromString(htmlText, "text/xml") |
| 1066 | const links = DOMMsg.querySelectorAll("a") |
| 1067 | if (DOMMsg.childNodes.length && links.length) { |
| 1068 | var isTextToShow = (DOMMsg.childNodes[0].childNodes.length > 1) |
| 1069 | const ytid = (isVideo(message_text))? youtube_id(message_text) : "" |
| 1070 | if (!isTextToShow && (ytid || isImage(message_text))) { |
| 1071 | type = "media" |
| 1072 | message_div.append(mediaInteraction(message_id, message_text, ytid)) |
| 1073 | } |
| 1074 | } |
| 1075 | } |
| 1076 | if (type !== "media") { |
| 1077 | type = "text" |
| 1078 | message_div.append(textInteraction(message_id, htmlText)) |
| 1079 | } |
| 1080 | } else if (message_type === "call" || message_type === "contact") { |
| 1081 | message_div.append(actionInteraction()) |
| 1082 | } else { |
| 1083 | const temp = document.createElement("div") |
| 1084 | temp.innerText = message_type |
| 1085 | message_div.appendChild(temp) |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1086 | } |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1087 | |
Sébastien Blin | d2ea6dd | 2018-04-18 11:35:33 -0400 | [diff] [blame] | 1088 | const menu_element = document.createElement("div") |
| 1089 | menu_element.setAttribute("class", "menu_interaction") |
| 1090 | menu_element.innerHTML = "<label for=\"showmenu\">\ |
| 1091 | <svg fill=\"#888888\" height=\"12\" viewBox=\"0 0 24 24\" width=\"12\" xmlns=\"http://www.w3.org/2000/svg\">\ |
| 1092 | <path d=\"M0 0h24v24H0z\" fill=\"none\"/>\ |
| 1093 | <path d=\"M6 10c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm12 0c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm-6 0c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2z\"/>\ |
| 1094 | </svg>\ |
| 1095 | </label>\ |
| 1096 | <input type=\"checkbox\" id=\"showmenu\" hidden />" |
| 1097 | menu_element.onclick = function() { |
| 1098 | const button = this.querySelector("#showmenu") |
| 1099 | button.checked = !button.checked |
| 1100 | } |
| 1101 | menu_element.onmouseleave = function() { |
| 1102 | const button = this.querySelector("#showmenu") |
| 1103 | button.checked = false |
| 1104 | } |
| 1105 | const dropdown = document.createElement("div") |
| 1106 | const dropdown_classes = [ |
| 1107 | "dropdown", |
| 1108 | `dropdown_${message_id}` |
| 1109 | ] |
| 1110 | dropdown.setAttribute("class", dropdown_classes.join(" ")) |
Sébastien Blin | 04ac84b | 2018-05-03 11:18:00 -0400 | [diff] [blame^] | 1111 | |
Sébastien Blin | d2ea6dd | 2018-04-18 11:35:33 -0400 | [diff] [blame] | 1112 | const remove = document.createElement("div") |
Sébastien Blin | 04ac84b | 2018-05-03 11:18:00 -0400 | [diff] [blame^] | 1113 | remove.setAttribute("class", "delete") |
Sébastien Blin | d2ea6dd | 2018-04-18 11:35:33 -0400 | [diff] [blame] | 1114 | remove.innerHTML = "Delete" |
| 1115 | remove.msg_id = message_id |
| 1116 | remove.onclick = function() { |
| 1117 | window.prompt(`DELETE_INTERACTION:${this.msg_id}`) |
| 1118 | } |
| 1119 | dropdown.appendChild(remove) |
Sébastien Blin | 04ac84b | 2018-05-03 11:18:00 -0400 | [diff] [blame^] | 1120 | |
Sébastien Blin | d2ea6dd | 2018-04-18 11:35:33 -0400 | [diff] [blame] | 1121 | menu_element.appendChild(dropdown) |
| 1122 | if (message_type !== "call") { |
| 1123 | message_div.appendChild(menu_element) |
| 1124 | } else { |
| 1125 | var wrapper = message_div.querySelector(".message_wrapper") |
| 1126 | wrapper.insertBefore(menu_element, wrapper.firstChild) |
| 1127 | } |
| 1128 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1129 | // Get timestamp to add |
| 1130 | const formattedTimestamp = getMessageTimestampText(message_timestamp, true) |
| 1131 | // Create the timestamp object |
| 1132 | const date_elt = document.createElement("div") |
| 1133 | date_elt.innerText = formattedTimestamp |
| 1134 | var typeIsCallOrContact = (message_type === "call" || message_type === "contact") |
| 1135 | var timestamp_div_classes = ["timestamp", typeIsCallOrContact ? "timestamp_action" : `timestamp_${message_direction}`] |
| 1136 | date_elt.setAttribute("class", timestamp_div_classes.join(" ")) |
| 1137 | date_elt.setAttribute("message_timestamp", message_timestamp) |
| 1138 | |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 1139 | // Remove last timestamp if it's the same |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1140 | if (messages.getElementsByClassName(".timestamp")) |
| 1141 | cleanPreviousTimestamps(date_elt, messages.children.length) |
| 1142 | |
| 1143 | // Add message and the new timestamp |
| 1144 | if (insert_after) { |
| 1145 | if (message_type === "call" || message_type === "contact") { |
| 1146 | message_div.querySelector(".message_wrapper").appendChild(date_elt) |
| 1147 | messages.appendChild(message_div) |
| 1148 | } else { |
| 1149 | messages.appendChild(message_div) |
| 1150 | messages.appendChild(date_elt) |
| 1151 | } |
| 1152 | } else { |
| 1153 | if (message_type === "call" || message_type === "contact") { |
| 1154 | message_div.querySelector(".message_wrapper").appendChild(date_elt) |
| 1155 | messages.insertBefore(message_div, messages.firstChild) |
| 1156 | } else { |
| 1157 | messages.insertBefore(date_elt, messages.firstChild) |
| 1158 | messages.insertBefore(message_div, messages.firstChild) |
| 1159 | } |
| 1160 | } |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1161 | } |
| 1162 | |
Sébastien Blin | 04ac84b | 2018-05-03 11:18:00 -0400 | [diff] [blame^] | 1163 | if (isErrorStatus(delivery_status) && message_direction === "out") { |
| 1164 | const dropdown = document.querySelector(`.dropdown_${message_id}`) |
| 1165 | const retry = document.createElement("div") |
| 1166 | retry.innerHTML = "Retry" |
| 1167 | retry.msg_id = message_id |
| 1168 | retry.onclick = function() { |
| 1169 | window.prompt(`RETRY_INTERACTION:${this.msg_id}`) |
| 1170 | } |
| 1171 | dropdown.insertBefore(retry, message_div.querySelector(".delete")) |
| 1172 | } |
| 1173 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1174 | // Update informations if needed |
| 1175 | if (message_type === "data_transfer") |
| 1176 | updateFileInteraction(message_div, message_object) |
| 1177 | if (message_type === "text" && message_direction === "out") |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1178 | // Modify sent status if necessary |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1179 | updateTextInteraction(message_div, delivery_status) |
| 1180 | if (message_type === "call") |
| 1181 | updateCallInteraction(message_div, message_object) |
| 1182 | if (message_type === "contact") |
| 1183 | updateContactInteraction(message_div, message_object) |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1184 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1185 | // Clean timestamps |
| 1186 | updateTimestamps() |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1187 | } |
| 1188 | |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1189 | /** |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 1190 | * Wrapper for addOrUpdateMessage. |
| 1191 | * |
| 1192 | * This function adds or updates a message and makes sure the scrollbar position |
| 1193 | * is refreshed correctly. |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1194 | * |
| 1195 | * @param message_object message to be added |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1196 | */ |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1197 | /* exported addMessage */ |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1198 | function addMessage(message_object) |
| 1199 | { |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 1200 | exec_keeping_scroll_position(addOrUpdateMessage, [message_object, true]) |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | /** |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 1204 | * Update a message that was previously added with addMessage |
| 1205 | * @param message_object message to be updated |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1206 | */ |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 1207 | /* exported updateMessage */ |
| 1208 | function updateMessage(message_object) |
| 1209 | { |
| 1210 | exec_keeping_scroll_position(addOrUpdateMessage, [message_object, false]) |
| 1211 | } |
| 1212 | |
| 1213 | /** |
| 1214 | * This function displays the history in reverse order and makes sure the |
| 1215 | * scrollbar position is refreshed correctly. |
| 1216 | */ |
| 1217 | function printHistoryPart() |
| 1218 | { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1219 | if(historyBuffer.length == 0 || historyBufferIndex === historyBuffer.length) { |
| 1220 | // nothing to print |
| 1221 | return |
| 1222 | } |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1223 | |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 1224 | exec_keeping_scroll_position(function() { |
| 1225 | // Show 10 messages |
| 1226 | for (var i = 0; i < 10; ++i) { |
| 1227 | addOrUpdateMessage(historyBuffer[historyBuffer.length - 1 - historyBufferIndex], true, false) |
| 1228 | historyBufferIndex ++ |
Hugo Lefeuvre | edad883 | 2018-05-14 16:36:06 -0400 | [diff] [blame] | 1229 | |
Hugo Lefeuvre | 2722ab9 | 2018-05-31 17:43:06 -0400 | [diff] [blame] | 1230 | if (historyBufferIndex === historyBuffer.length) |
| 1231 | break |
| 1232 | } |
| 1233 | }, []) |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1234 | |
| 1235 | // Make a short pause to minimize ressources consumption |
| 1236 | // show quickly the first hundred messages |
| 1237 | if (historyBufferIndex !== historyBuffer.length) |
| 1238 | setTimeout(printHistoryPart, (historyBufferIndex > 100) ? 100 : 1) |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | /** |
| 1242 | * Show the whole history in the chatview. |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1243 | * @param messages_array array containing history to be printed |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1244 | */ |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1245 | /* exported printHistory */ |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1246 | function printHistory(messages_array) |
| 1247 | { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1248 | historyBuffer = messages_array |
| 1249 | historyBufferIndex = 0 |
| 1250 | setTimeout(printHistoryPart, 0) |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | /** |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1254 | * Sets the image for a given sender |
| 1255 | * set_sender_image object should contain the following keys: |
| 1256 | * - sender: the name of the sender |
| 1257 | * - sender_image: base64 png encoding of the sender image |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1258 | * |
| 1259 | * @param set_sender_image_object sender image object as previously described |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1260 | */ |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1261 | /* exported setSenderImage */ |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1262 | function setSenderImage(set_sender_image_object) |
| 1263 | { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1264 | var sender_contact_method = set_sender_image_object["sender_contact_method"], |
| 1265 | sender_image = set_sender_image_object["sender_image"], |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1266 | sender_image_id = "sender_image_" + sender_contact_method, |
| 1267 | currentSenderImage = document.getElementById("#" + sender_image_id), // Remove the currently set sender image |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1268 | style |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1269 | |
| 1270 | if (currentSenderImage) { |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1271 | currentSenderImage.parentNode.removeChild(currentSenderImage) |
Guillaume Roguez | 5b137be | 2018-02-21 10:44:58 -0500 | [diff] [blame] | 1272 | } |
| 1273 | |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1274 | // Create a new style element |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1275 | style = document.createElement("style") |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1276 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1277 | style.type = "text/css" |
| 1278 | style.id = sender_image_id |
| 1279 | style.innerHTML = "." + sender_image_id + " {content: url(data:image/png;base64," + sender_image + ");height: 35px;width: 35px;}" |
| 1280 | document.head.appendChild(style) |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1281 | } |
| 1282 | |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1283 | /* exported clearSenderImages */ |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1284 | function clearSenderImages() |
| 1285 | { |
| 1286 | var styles = document.head.querySelectorAll("style"), |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1287 | i = styles.length |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1288 | |
| 1289 | while (i--){ |
Hugo Lefeuvre | 368c47c | 2018-05-29 15:50:07 -0400 | [diff] [blame] | 1290 | document.head.removeChild(styles[i]) |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 1291 | } |
Hugo Lefeuvre | 65a41d6 | 2018-05-23 10:12:24 -0400 | [diff] [blame] | 1292 | } |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 1293 | |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 1294 | </script> |
aviau | 039001d | 2016-09-29 16:39:05 -0400 | [diff] [blame] | 1295 | </html> |