uiMessage.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /* jshint sub: true */
  2. /**
  3. * @constructor
  4. * @extends {RoomHistory}
  5. * @param {Room|string} room or roomId
  6. * @param {number} keepMessages number of messages to keep in memory
  7. * @param {Array|undefined} evts
  8. * @param {number|undefined} now
  9. **/
  10. function UiRoomHistory(room, keepMessages, evts, now) {
  11. RoomHistory.call(this, room, keepMessages, 0, evts, now);
  12. }
  13. UiRoomHistory.prototype = Object.create(RoomHistory.prototype);
  14. UiRoomHistory.prototype.constructor = UiRoomHistory;
  15. UiRoomHistory.prototype.messageFactory = function(ev, ts) {
  16. if (ev["isMeMessage"] === true)
  17. return new UiMeMessage(this.id, ev, ts);
  18. if (ev["isNotice"] === true)
  19. return new UiNoticeMessage(this.id, ev, ts);
  20. return new UiMessage(this.id, ev, ts);
  21. };
  22. UiRoomHistory.prototype.invalidateAllMessages = function() {
  23. this.messages.forEach(function(m) {
  24. m.invalidate();
  25. });
  26. };
  27. /** @interface */
  28. function IUiMessage() {}
  29. /**
  30. * @return {Element}
  31. **/
  32. IUiMessage.prototype.getDom = function() {};
  33. /**
  34. * @return {IUiMessage}
  35. **/
  36. IUiMessage.prototype.removeDom = function() {};
  37. /**
  38. * @return {IUiMessage}
  39. **/
  40. IUiMessage.prototype.invalidate = function() {};
  41. /**
  42. * @return {IUiMessage}
  43. **/
  44. IUiMessage.prototype.createDom = function() {};
  45. /**
  46. * @return {IUiMessage}
  47. **/
  48. IUiMessage.prototype.updateDom = function() {};
  49. /**
  50. * @return {Element}
  51. **/
  52. IUiMessage.prototype.duplicateDom = function() {};
  53. /** @const */
  54. var AbstractUiMessage = (function() {
  55. var updateReactions = function(_this, channelId) {
  56. var reactionFrag = document.createDocumentFragment();
  57. if (_this.reactions) {
  58. for (var reaction in _this.reactions) {
  59. var reac = createReactionDom(channelId, _this.id, reaction, _this.reactions[reaction]);
  60. if (reac)
  61. reactionFrag.appendChild(reac);
  62. }
  63. }
  64. _this.dom.reactions.textContent = '';
  65. _this.dom.reactions.appendChild(reactionFrag);
  66. },
  67. _linkFilter = function(msgContext, str) {
  68. var sep = str.indexOf('|'),
  69. link,
  70. text,
  71. isInternal = false;
  72. if (sep === -1) {
  73. link = str;
  74. } else {
  75. link = str.substr(0, sep);
  76. text = str.substr(sep +1);
  77. }
  78. var newLink;
  79. if (link[0] === '@') {
  80. newLink = msgContext.context.getId() +'|' +link.substr(1);
  81. var user = DATA.context.getUser(newLink);
  82. if (user) {
  83. isInternal = true;
  84. link = '#' +user.privateRoom.id;
  85. text = '@' +user.getName();
  86. } else {
  87. return null;
  88. }
  89. } else if (link[0] === '#') {
  90. newLink = msgContext.context.getId() +'|' +link.substr(1);
  91. var chan = DATA.context.getChannel(newLink);
  92. if (chan) {
  93. isInternal = true;
  94. link = '#' +newLink;
  95. text = '#' +chan.name;
  96. } else {
  97. return null;
  98. }
  99. } else {
  100. if (!link.match(/^(https?|mailto):\/\//i))
  101. return null;
  102. isInternal = false;
  103. }
  104. return {
  105. link: link,
  106. text: text || link,
  107. isInternal: isInternal
  108. };
  109. },
  110. _formatText = function(_this, text) {
  111. return formatText(text, {
  112. highlights: _this.context.self.prefs.highlights,
  113. emojiFormatFunction: function(emoji) {
  114. if (emoji[0] === ':' && emoji[emoji.length -1] === ':')
  115. emoji = emoji.substr(1, emoji.length -2);
  116. var emojiDom = makeEmojiDom(emoji);
  117. if (emojiDom) {
  118. var domParent = document.createElement("span");
  119. domParent.className = R.klass.emoji.small;
  120. domParent.appendChild(emojiDom);
  121. return domParent.outerHTML;
  122. }
  123. return null;
  124. },
  125. linkFilter: function(link) {
  126. return _linkFilter(_this, link);
  127. }
  128. });
  129. },
  130. updateAttachments = function(_this, channelId) {
  131. var attachmentFrag = document.createDocumentFragment();
  132. for (var i =0, nbAttachments = _this.attachments.length; i < nbAttachments; i++) {
  133. var attachment = _this.attachments[i];
  134. if (attachment) {
  135. var domAttachment = createAttachmentDom(channelId, _this, attachment, i);
  136. if (domAttachment)
  137. attachmentFrag.appendChild(domAttachment);
  138. }
  139. }
  140. _this.dom.attachments.textContent = '';
  141. _this.dom.attachments.appendChild(attachmentFrag);
  142. },
  143. updateCommon = function(_this, sender) {
  144. _this.dom.ts.innerHTML = locale.formatDate(_this.ts);
  145. _this.dom.textDom.innerHTML = _formatText(_this, _this.text);
  146. _this.dom.authorName.textContent = sender ? sender.getName() : (_this.username || "?");
  147. };
  148. return {
  149. /** @type {Element|null} *
  150. dom: null,
  151. /** @type {boolean} *
  152. uiNeedRefresh: true,
  153. */
  154. /** @param {IUiMessage} _this */
  155. invalidate: function(_this) {
  156. _this.uiNeedRefresh = true;
  157. return _this;
  158. },
  159. /** @param {UiMessage|UiMeMessage|UiNoticeMessage} _this */
  160. removeDom: function(_this) {
  161. if (_this.dom && _this.dom.parentElement) {
  162. _this.dom.remove();
  163. delete(_this.dom);
  164. }
  165. return _this;
  166. },
  167. /** @param {UiMessage|UiMeMessage|UiNoticeMessage} _this */
  168. getDom: function(_this) {
  169. if (!_this.dom) {
  170. _this.createDom().updateDom();
  171. } else if (_this.uiNeedRefresh) {
  172. _this.uiNeedRefresh = false;
  173. _this.updateDom();
  174. }
  175. return _this.dom;
  176. },
  177. /** @param {UiMessage|UiMeMessage|UiNoticeMessage} _this */
  178. updateDom: function(_this) {
  179. var sender = DATA.context.getUser(_this.userId);
  180. updateCommon(_this, sender);
  181. updateAttachments(_this, _this.channelId);
  182. updateReactions(_this, _this.channelId);
  183. if (_this.edited) {
  184. _this.dom.edited.innerHTML = locale.edited(_this.edited);
  185. _this.dom.classList.add(R.klass.msg.editedStatus);
  186. }
  187. return _this;
  188. },
  189. duplicateDom: function(_this) {
  190. return _this.dom.cloneNode(true);
  191. },
  192. formatText: function(_this, text) {
  193. return _formatText(_this, text);
  194. }
  195. };
  196. })();
  197. /**
  198. * @constructor
  199. * @implements {IUiMessage}
  200. * @extends {MeMessage}
  201. * @param {*} ev
  202. * @param {number} ts
  203. **/
  204. function UiMeMessage(channelId, ev, ts) {
  205. // Extends AbstractUiMessage and MeMessage
  206. MeMessage.call(this, ev, ts);
  207. /** @const @type {ChatContext} */
  208. this.context = DATA.context.getChannelContext(channelId).getChatContext();
  209. /** @type {string} @const */
  210. this.channelId = channelId;
  211. this.dom = AbstractUiMessage.dom;
  212. this.uiNeedRefresh = AbstractUiMessage.uiNeedRefresh;
  213. }
  214. UiMeMessage.prototype = Object.create(MeMessage.prototype);
  215. UiMeMessage.prototype.constructor = UiMeMessage;
  216. UiMeMessage.prototype.invalidate = function() {
  217. return AbstractUiMessage.invalidate(this);
  218. };
  219. UiMeMessage.prototype.formatText = function(text) {
  220. return AbstractUiMessage.formatText(this, text);
  221. };
  222. UiMeMessage.prototype.removeDom = function() {
  223. return AbstractUiMessage.removeDom(this);
  224. };
  225. UiMeMessage.prototype.getDom = function() {
  226. return AbstractUiMessage.getDom(this);
  227. };
  228. UiMeMessage.prototype.createDom = function() {
  229. this.dom = doCreateMessageDom(this, false);
  230. this.dom.classList.add(R.klass.msg.meMessage);
  231. return this;
  232. };
  233. UiMeMessage.prototype.duplicateDom = function() {
  234. return AbstractUiMessage.duplicateDom(this);
  235. };
  236. UiMeMessage.prototype.updateDom = function() {
  237. AbstractUiMessage.updateDom(this);
  238. return this;
  239. };
  240. UiMeMessage.prototype.update = function(ev, ts) {
  241. MeMessage.prototype.update.call(this, ev, ts);
  242. this.invalidate();
  243. };
  244. /**
  245. * @constructor
  246. * @implements {IUiMessage}
  247. * @extends {Message}
  248. * @param {*} ev
  249. * @param {number} ts
  250. **/
  251. function UiMessage(channelId, ev, ts) {
  252. // Extends AbstractUiMessage and Message
  253. Message.call(this, ev, ts);
  254. /** @const @type {ChatContext} */
  255. this.context = DATA.context.getChannelContext(channelId).getChatContext();
  256. /** @type {string} @const */
  257. this.channelId = channelId;
  258. /** @type {Element} */
  259. this.dom = AbstractUiMessage.dom;
  260. /** @type {boolean} */
  261. this.uiNeedRefresh = AbstractUiMessage.uiNeedRefresh;
  262. }
  263. UiMessage.prototype = Object.create(Message.prototype);
  264. UiMessage.prototype.constructor = UiMessage;
  265. UiMessage.prototype.invalidate = function() {
  266. return AbstractUiMessage.invalidate(this);
  267. };
  268. UiMessage.prototype.formatText = function(text) {
  269. return AbstractUiMessage.formatText(this, text);
  270. };
  271. UiMessage.prototype.removeDom = function() {
  272. return AbstractUiMessage.removeDom(this);
  273. };
  274. UiMessage.prototype.getDom = function() {
  275. return AbstractUiMessage.getDom(this);
  276. };
  277. UiMessage.prototype.createDom = function() {
  278. this.dom = doCreateMessageDom(this, false);
  279. return this;
  280. };
  281. UiMessage.prototype.duplicateDom = function() {
  282. return AbstractUiMessage.duplicateDom(this);
  283. };
  284. UiMessage.prototype.updateDom = function() {
  285. AbstractUiMessage.updateDom(this);
  286. return this;
  287. };
  288. UiMessage.prototype.update = function(ev, ts) {
  289. Message.prototype.update.call(this, ev, ts);
  290. this.invalidate();
  291. var match = this.text.match(/^<?https:\/\/www\.openstreetmap\.org\/\?mlat=(-?[0-9\.]+)(&amp;|&)mlon=(-?[0-9\.]+)(&amp;|&)macc=([0-9\.]+)[^\s]*/);
  292. if (match) {
  293. var lat = match[1],
  294. lon = match[3],
  295. acc = match[5];
  296. this.text = this.text.substr(0, match.index) +this.text.substr(match.index +match[0].length).trim();
  297. this.attachments.unshift({
  298. "color": "#008000",
  299. "text": match[0],
  300. "footer": "Open Street Map",
  301. "footer_icon": "https://www.openstreetmap.org/assets/favicon-32x32-36d06d8a01933075bc7093c9631cffd02d49b03b659f767340f256bb6839d990.png",
  302. "geo": {
  303. "latitude": match[1],
  304. "longitude": match[3],
  305. "accuracy": match[5]
  306. }
  307. });
  308. }
  309. };
  310. /**
  311. * @constructor
  312. * @implements {IUiMessage}
  313. * @extends {NoticeMessage}
  314. * @param {*} ev
  315. * @param {number} ts
  316. **/
  317. function UiNoticeMessage(channelId, ev, ts) {
  318. // Extends AbstractUiMessage and NoticeMessage
  319. NoticeMessage.call(this, ev, ts);
  320. /** @const @type {ChatContext} */
  321. this.context = DATA.context.getChannelContext(channelId).getChatContext();
  322. /** @type {string} @const */
  323. this.channelId = channelId;
  324. /** @type {Element} */
  325. this.dom; // jshint ignore:line
  326. /** @type {Element} */
  327. this.domWrapper = null;
  328. /** @type {boolean} */
  329. this.uiNeedRefresh = true;
  330. }
  331. UiNoticeMessage.prototype = Object.create(NoticeMessage.prototype);
  332. UiNoticeMessage.prototype.constructor = UiNoticeMessage;
  333. UiNoticeMessage.prototype.invalidate = function() {
  334. return AbstractUiMessage.invalidate(this);
  335. };
  336. UiNoticeMessage.prototype.formatText = function(text) {
  337. return AbstractUiMessage.formatText(this, text);
  338. };
  339. UiNoticeMessage.prototype.removeDom = function() {
  340. if (this.domWrapper && this.domWrapper.parentElement) {
  341. this.domWrapper.remove();
  342. delete(this.domWrapper);
  343. }
  344. if (this.dom)
  345. delete(this.dom);
  346. return this;
  347. };
  348. UiNoticeMessage.prototype.getDom = function() {
  349. AbstractUiMessage.getDom(this);
  350. return this.domWrapper;
  351. };
  352. UiNoticeMessage.prototype.duplicateDom = function() {
  353. return this.domWrapper.cloneNode(true);
  354. };
  355. UiNoticeMessage.prototype.createDom = function() {
  356. this.dom = doCreateMessageDom(this, false);
  357. this.domWrapper = document.createElement("span");
  358. this.dom.classList.add(R.klass.msg.notice);
  359. this.domWrapper.className = R.klass.msg.notice;
  360. this.domWrapper.textContent = locale.onlyVisible;
  361. this.domWrapper.appendChild(this.dom);
  362. return this;
  363. };
  364. UiNoticeMessage.prototype.updateDom = function() {
  365. AbstractUiMessage.updateDom(this);
  366. return this;
  367. };
  368. UiNoticeMessage.prototype.update = function(ev, ts) {
  369. NoticeMessage.prototype.update.call(this, ev, ts);
  370. this.invalidate();
  371. };