uiMessage.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. style,
  72. classes,
  73. isInternal = false;
  74. if (sep === -1) {
  75. link = str;
  76. } else {
  77. link = str.substr(0, sep);
  78. text = str.substr(sep +1);
  79. }
  80. var newLink;
  81. if (link[0] === '@') {
  82. newLink = msgContext.context.getId() +'|' +link.substr(1);
  83. var user = DATA.context.getUser(newLink);
  84. if (user) {
  85. isInternal = true;
  86. link = '#' +user.privateRoom.id;
  87. text = '@' +user.getName();
  88. style = "background-color:" +makeUserColor(user.getName());
  89. classes = [ "userLink" ]; // FIXME R.
  90. } else {
  91. return null;
  92. }
  93. } else if (link[0] === '#') {
  94. newLink = msgContext.context.getId() +'|' +link.substr(1);
  95. var chan = DATA.context.getChannel(newLink);
  96. if (chan) {
  97. isInternal = true;
  98. link = '#' +newLink;
  99. text = '#' +chan.name;
  100. classes = [ "chanLink" ]; // FIXME R.
  101. } else {
  102. return null;
  103. }
  104. } else {
  105. if (!link.match(/^(https?|mailto):\/\//i))
  106. return null;
  107. isInternal = false;
  108. }
  109. return {
  110. link: link,
  111. text: text || link,
  112. style: style,
  113. classes: classes,
  114. isInternal: isInternal
  115. };
  116. },
  117. _formatText = function(_this, text) {
  118. return formatText(text, {
  119. highlights: _this.context.self.prefs.highlights,
  120. emojiFormatFunction: function(emoji) {
  121. if (emoji[0] === ':' && emoji[emoji.length -1] === ':')
  122. emoji = emoji.substr(1, emoji.length -2);
  123. var emojiDom = makeEmojiDom(emoji);
  124. if (emojiDom) {
  125. var domParent = document.createElement("span");
  126. domParent.className = R.klass.emoji.small;
  127. domParent.appendChild(emojiDom);
  128. return domParent.outerHTML;
  129. }
  130. return null;
  131. },
  132. linkFilter: function(link) {
  133. return _linkFilter(_this, link);
  134. }
  135. });
  136. },
  137. updateAttachments = function(_this, channelId) {
  138. var attachmentFrag = document.createDocumentFragment();
  139. for (var i =0, nbAttachments = _this.attachments.length; i < nbAttachments; i++) {
  140. var attachment = _this.attachments[i];
  141. if (attachment) {
  142. var domAttachment = createAttachmentDom(channelId, _this, attachment, i);
  143. if (domAttachment)
  144. attachmentFrag.appendChild(domAttachment);
  145. }
  146. }
  147. _this.dom.attachments.textContent = '';
  148. _this.dom.attachments.appendChild(attachmentFrag);
  149. },
  150. updateHover = function(_this, channelId) {
  151. if (_this.dom.hover.hoverStar)
  152. _this.dom.hover.hoverStar.style.backgroundImage = _this.starred ? 'url("star_full.png")' : 'url("star_empty.png")';
  153. },
  154. updateCommon = function(_this, sender) {
  155. _this.dom.ts.innerHTML = locale.formatDate(_this.ts);
  156. _this.dom.textDom.innerHTML = _formatText(_this, _this.text);
  157. _this.dom.authorName.textContent = sender ? sender.getName() : (_this.username || "?");
  158. };
  159. return {
  160. /** @type {Element|null} *
  161. dom: null,
  162. /** @type {boolean} *
  163. uiNeedRefresh: true,
  164. */
  165. /** @param {IUiMessage} _this */
  166. invalidate: function(_this) {
  167. _this.uiNeedRefresh = true;
  168. return _this;
  169. },
  170. /** @param {UiMessage|UiMeMessage|UiNoticeMessage} _this */
  171. removeDom: function(_this) {
  172. if (_this.dom && _this.dom.parentElement) {
  173. _this.dom.remove();
  174. delete(_this.dom);
  175. }
  176. return _this;
  177. },
  178. /** @param {UiMessage|UiMeMessage|UiNoticeMessage} _this */
  179. getDom: function(_this) {
  180. if (!_this.dom) {
  181. _this.createDom().updateDom();
  182. } else if (_this.uiNeedRefresh) {
  183. _this.uiNeedRefresh = false;
  184. _this.updateDom();
  185. }
  186. return _this.dom;
  187. },
  188. /** @param {UiMessage|UiMeMessage|UiNoticeMessage} _this */
  189. updateDom: function(_this) {
  190. var sender = DATA.context.getUser(_this.userId);
  191. updateCommon(_this, sender);
  192. updateAttachments(_this, _this.channelId);
  193. updateReactions(_this, _this.channelId);
  194. updateHover(_this, _this.channelId);
  195. if (_this.edited) {
  196. _this.dom.edited.innerHTML = locale.edited(_this.edited);
  197. _this.dom.classList.add(R.klass.msg.editedStatus);
  198. }
  199. return _this;
  200. },
  201. duplicateDom: function(_this) {
  202. return _this.getDom().cloneNode(true);
  203. },
  204. formatText: function(_this, text) {
  205. return _formatText(_this, text);
  206. }
  207. };
  208. })();
  209. /**
  210. * @constructor
  211. * @implements {IUiMessage}
  212. * @extends {MeMessage}
  213. * @param {*} ev
  214. * @param {number} ts
  215. **/
  216. function UiMeMessage(channelId, ev, ts) {
  217. // Extends AbstractUiMessage and MeMessage
  218. MeMessage.call(this, ev, ts);
  219. /** @const @type {ChatContext} */
  220. this.context = DATA.context.getChannelContext(channelId).getChatContext();
  221. /** @type {string} @const */
  222. this.channelId = channelId;
  223. this.dom = AbstractUiMessage.dom;
  224. this.uiNeedRefresh = AbstractUiMessage.uiNeedRefresh;
  225. }
  226. UiMeMessage.prototype = Object.create(MeMessage.prototype);
  227. UiMeMessage.prototype.constructor = UiMeMessage;
  228. UiMeMessage.prototype.invalidate = function() {
  229. return AbstractUiMessage.invalidate(this);
  230. };
  231. UiMeMessage.prototype.formatText = function(text) {
  232. return AbstractUiMessage.formatText(this, text);
  233. };
  234. UiMeMessage.prototype.removeDom = function() {
  235. return AbstractUiMessage.removeDom(this);
  236. };
  237. UiMeMessage.prototype.getDom = function() {
  238. return AbstractUiMessage.getDom(this);
  239. };
  240. UiMeMessage.prototype.createDom = function() {
  241. this.dom = doCreateMessageDom(this);
  242. this.dom.classList.add(R.klass.msg.meMessage);
  243. return this;
  244. };
  245. UiMeMessage.prototype.duplicateDom = function() {
  246. return AbstractUiMessage.duplicateDom(this);
  247. };
  248. UiMeMessage.prototype.updateDom = function() {
  249. AbstractUiMessage.updateDom(this);
  250. return this;
  251. };
  252. UiMeMessage.prototype.update = function(ev, ts) {
  253. MeMessage.prototype.update.call(this, ev, ts);
  254. this.invalidate();
  255. };
  256. /**
  257. * @constructor
  258. * @implements {IUiMessage}
  259. * @extends {Message}
  260. * @param {*} ev
  261. * @param {number} ts
  262. **/
  263. function UiMessage(channelId, ev, ts) {
  264. // Extends AbstractUiMessage and Message
  265. Message.call(this, ev, ts);
  266. /** @const @type {ChatContext} */
  267. this.context = DATA.context.getChannelContext(channelId).getChatContext();
  268. /** @type {string} @const */
  269. this.channelId = channelId;
  270. /** @type {Element} */
  271. this.dom = AbstractUiMessage.dom;
  272. /** @type {boolean} */
  273. this.uiNeedRefresh = AbstractUiMessage.uiNeedRefresh;
  274. }
  275. UiMessage.prototype = Object.create(Message.prototype);
  276. UiMessage.prototype.constructor = UiMessage;
  277. UiMessage.prototype.invalidate = function() {
  278. return AbstractUiMessage.invalidate(this);
  279. };
  280. UiMessage.prototype.formatText = function(text) {
  281. return AbstractUiMessage.formatText(this, text);
  282. };
  283. UiMessage.prototype.removeDom = function() {
  284. return AbstractUiMessage.removeDom(this);
  285. };
  286. UiMessage.prototype.getDom = function() {
  287. return AbstractUiMessage.getDom(this);
  288. };
  289. UiMessage.prototype.createDom = function() {
  290. this.dom = doCreateMessageDom(this);
  291. return this;
  292. };
  293. UiMessage.prototype.duplicateDom = function() {
  294. return AbstractUiMessage.duplicateDom(this);
  295. };
  296. UiMessage.prototype.updateDom = function() {
  297. AbstractUiMessage.updateDom(this);
  298. return this;
  299. };
  300. UiMessage.prototype.update = function(ev, ts) {
  301. Message.prototype.update.call(this, ev, ts);
  302. this.invalidate();
  303. var match = this.text.match(/^<?https:\/\/www\.openstreetmap\.org\/\?mlat=(-?[0-9\.]+)(&amp;|&)mlon=(-?[0-9\.]+)(&amp;|&)macc=([0-9\.]+)[^\s]*/);
  304. if (match) {
  305. var lat = match[1],
  306. lon = match[3],
  307. acc = match[5];
  308. this.text = this.text.substr(0, match.index) +this.text.substr(match.index +match[0].length).trim();
  309. this.attachments.unshift({
  310. "color": "#008000",
  311. "text": match[0],
  312. "footer": "Open Street Map",
  313. "footer_icon": "https://www.openstreetmap.org/assets/favicon-32x32-36d06d8a01933075bc7093c9631cffd02d49b03b659f767340f256bb6839d990.png",
  314. "geo": {
  315. "latitude": match[1],
  316. "longitude": match[3],
  317. "accuracy": match[5]
  318. }
  319. });
  320. }
  321. };
  322. /**
  323. * @constructor
  324. * @implements {IUiMessage}
  325. * @extends {NoticeMessage}
  326. * @param {*} ev
  327. * @param {number} ts
  328. **/
  329. function UiNoticeMessage(channelId, ev, ts) {
  330. // Extends AbstractUiMessage and NoticeMessage
  331. NoticeMessage.call(this, ev, ts);
  332. /** @const @type {ChatContext} */
  333. this.context = DATA.context.getChannelContext(channelId).getChatContext();
  334. /** @type {string} @const */
  335. this.channelId = channelId;
  336. /** @type {Element} */
  337. this.dom; // jshint ignore:line
  338. /** @type {Element} */
  339. this.domWrapper = null;
  340. /** @type {boolean} */
  341. this.uiNeedRefresh = true;
  342. }
  343. UiNoticeMessage.prototype = Object.create(NoticeMessage.prototype);
  344. UiNoticeMessage.prototype.constructor = UiNoticeMessage;
  345. UiNoticeMessage.prototype.invalidate = function() {
  346. return AbstractUiMessage.invalidate(this);
  347. };
  348. UiNoticeMessage.prototype.formatText = function(text) {
  349. return AbstractUiMessage.formatText(this, text);
  350. };
  351. UiNoticeMessage.prototype.removeDom = function() {
  352. if (this.domWrapper && this.domWrapper.parentElement) {
  353. this.domWrapper.remove();
  354. delete(this.domWrapper);
  355. }
  356. if (this.dom)
  357. delete(this.dom);
  358. return this;
  359. };
  360. UiNoticeMessage.prototype.getDom = function() {
  361. AbstractUiMessage.getDom(this);
  362. return this.domWrapper;
  363. };
  364. UiNoticeMessage.prototype.duplicateDom = function() {
  365. return this.domWrapper.cloneNode(true);
  366. };
  367. UiNoticeMessage.prototype.createDom = function() {
  368. this.dom = doCreateMessageDom(this);
  369. this.domWrapper = document.createElement("span");
  370. this.dom.classList.add(R.klass.msg.notice);
  371. this.domWrapper.className = R.klass.msg.notice;
  372. this.domWrapper.textContent = locale.onlyVisible;
  373. this.domWrapper.appendChild(this.dom);
  374. return this;
  375. };
  376. UiNoticeMessage.prototype.updateDom = function() {
  377. AbstractUiMessage.updateDom(this);
  378. return this;
  379. };
  380. UiNoticeMessage.prototype.update = function(ev, ts) {
  381. NoticeMessage.prototype.update.call(this, ev, ts);
  382. this.invalidate();
  383. };