uiMessage.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 = [ R.klass.msg.linkuser, R.klass.msg.link ];
  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 = [ R.klass.msg.linkchan, R.klass.msg.link ];
  101. } else {
  102. return null;
  103. }
  104. } else {
  105. if (!link.match(/^(https?|mailto):\/\//i))
  106. return null;
  107. classes = [ R.klass.msg.link ];
  108. isInternal = false;
  109. }
  110. return {
  111. link: link,
  112. text: text || link,
  113. style: style,
  114. classes: classes,
  115. isInternal: isInternal
  116. };
  117. },
  118. _formatText = function(_this, text) {
  119. return formatText(text, {
  120. highlights: _this.context.self.prefs.highlights,
  121. emojiFormatFunction: function(emoji) {
  122. if (emoji[0] === ':' && emoji[emoji.length -1] === ':')
  123. emoji = emoji.substr(1, emoji.length -2);
  124. var emojiDom = makeEmojiDom(emoji);
  125. if (emojiDom) {
  126. var domParent = document.createElement("span");
  127. domParent.className = R.klass.emoji.small;
  128. domParent.appendChild(emojiDom);
  129. return domParent.outerHTML;
  130. }
  131. return null;
  132. },
  133. linkFilter: function(link) {
  134. return _linkFilter(_this, link);
  135. }
  136. });
  137. },
  138. updateAttachments = function(_this, channelId) {
  139. var attachmentFrag = document.createDocumentFragment(),
  140. containImages = false,
  141. hasAttachment = false;
  142. for (var i =0, nbAttachments = _this.attachments.length; i < nbAttachments; i++) {
  143. var attachment = _this.attachments[i];
  144. if (attachment) {
  145. var domAttachment = createAttachmentDom(channelId, _this, attachment, i);
  146. if (domAttachment) {
  147. attachmentFrag.appendChild(domAttachment);
  148. containImages |= !!attachment["image_url"];
  149. hasAttachment = true;
  150. }
  151. }
  152. }
  153. _this.dom.attachments.textContent = '';
  154. _this.dom.attachments.appendChild(attachmentFrag);
  155. if (hasAttachment) {
  156. _this.dom.attachmentWrapper.classList.remove(R.klass.hidden);
  157. if (CONFIG.shouldExpandAttachment(containImages))
  158. _this.dom.attachmentWrapper["open"] = true;
  159. else
  160. _this.dom.attachmentWrapper["open"] = false;
  161. } else {
  162. _this.dom.attachmentWrapper.classList.add(R.klass.hidden);
  163. }
  164. },
  165. updateHover = function(_this, channelId) {
  166. if (_this.dom.hover.hoverStar)
  167. _this.dom.hover.hoverStar.style.backgroundImage = _this.starred ? 'url("star_full.png")' : 'url("star_empty.png")';
  168. },
  169. updateCommon = function(_this, sender) {
  170. _this.dom.ts.innerHTML = locale.formatDate(_this.ts);
  171. _this.dom.textDom.innerHTML = _formatText(_this, _this.text);
  172. _this.dom.authorName.textContent = sender ? sender.getName() : (_this.username || "?");
  173. };
  174. return {
  175. /** @type {Element|null} *
  176. dom: null,
  177. /** @type {boolean} *
  178. uiNeedRefresh: true,
  179. */
  180. /** @param {IUiMessage} _this */
  181. invalidate: function(_this) {
  182. _this.uiNeedRefresh = true;
  183. return _this;
  184. },
  185. /** @param {UiMessage|UiMeMessage|UiNoticeMessage} _this */
  186. removeDom: function(_this) {
  187. if (_this.dom && _this.dom.parentElement) {
  188. _this.dom.remove();
  189. delete(_this.dom);
  190. }
  191. return _this;
  192. },
  193. /** @param {UiMessage|UiMeMessage|UiNoticeMessage} _this */
  194. getDom: function(_this) {
  195. if (!_this.dom) {
  196. _this.createDom().updateDom();
  197. } else if (_this.uiNeedRefresh) {
  198. _this.uiNeedRefresh = false;
  199. _this.updateDom();
  200. }
  201. return _this.dom;
  202. },
  203. /** @param {UiMessage|UiMeMessage|UiNoticeMessage} _this */
  204. updateDom: function(_this) {
  205. var sender = DATA.context.getUser(_this.userId);
  206. updateCommon(_this, sender);
  207. updateAttachments(_this, _this.channelId);
  208. updateReactions(_this, _this.channelId);
  209. updateHover(_this, _this.channelId);
  210. if (_this.edited) {
  211. _this.dom.edited.innerHTML = locale.edited(_this.edited);
  212. _this.dom.classList.add(R.klass.msg.editedStatus);
  213. }
  214. return _this;
  215. },
  216. duplicateDom: function(_this) {
  217. return _this.getDom().cloneNode(true);
  218. },
  219. formatText: function(_this, text) {
  220. return _formatText(_this, text);
  221. }
  222. };
  223. })();
  224. /**
  225. * @constructor
  226. * @implements {IUiMessage}
  227. * @extends {MeMessage}
  228. * @param {*} ev
  229. * @param {number} ts
  230. **/
  231. function UiMeMessage(channelId, ev, ts) {
  232. // Extends AbstractUiMessage and MeMessage
  233. MeMessage.call(this, ev, ts);
  234. /** @const @type {ChatContext} */
  235. this.context = DATA.context.getChannelContext(channelId).getChatContext();
  236. /** @type {string} @const */
  237. this.channelId = channelId;
  238. this.dom = AbstractUiMessage.dom;
  239. this.uiNeedRefresh = AbstractUiMessage.uiNeedRefresh;
  240. }
  241. UiMeMessage.prototype = Object.create(MeMessage.prototype);
  242. UiMeMessage.prototype.constructor = UiMeMessage;
  243. UiMeMessage.prototype.invalidate = function() {
  244. return AbstractUiMessage.invalidate(this);
  245. };
  246. UiMeMessage.prototype.formatText = function(text) {
  247. return AbstractUiMessage.formatText(this, text);
  248. };
  249. UiMeMessage.prototype.removeDom = function() {
  250. return AbstractUiMessage.removeDom(this);
  251. };
  252. UiMeMessage.prototype.getDom = function() {
  253. return AbstractUiMessage.getDom(this);
  254. };
  255. UiMeMessage.prototype.createDom = function() {
  256. this.dom = doCreateMessageDom(this);
  257. this.dom.classList.add(R.klass.msg.meMessage);
  258. return this;
  259. };
  260. UiMeMessage.prototype.duplicateDom = function() {
  261. return AbstractUiMessage.duplicateDom(this);
  262. };
  263. UiMeMessage.prototype.updateDom = function() {
  264. AbstractUiMessage.updateDom(this);
  265. return this;
  266. };
  267. UiMeMessage.prototype.update = function(ev, ts) {
  268. MeMessage.prototype.update.call(this, ev, ts);
  269. this.invalidate();
  270. };
  271. /**
  272. * @constructor
  273. * @implements {IUiMessage}
  274. * @extends {Message}
  275. * @param {*} ev
  276. * @param {number} ts
  277. **/
  278. function UiMessage(channelId, ev, ts) {
  279. // Extends AbstractUiMessage and Message
  280. Message.call(this, ev, ts);
  281. /** @const @type {ChatContext} */
  282. this.context = DATA.context.getChannelContext(channelId).getChatContext();
  283. /** @type {string} @const */
  284. this.channelId = channelId;
  285. /** @type {Element} */
  286. this.dom = AbstractUiMessage.dom;
  287. /** @type {boolean} */
  288. this.uiNeedRefresh = AbstractUiMessage.uiNeedRefresh;
  289. }
  290. UiMessage.prototype = Object.create(Message.prototype);
  291. UiMessage.prototype.constructor = UiMessage;
  292. UiMessage.prototype.invalidate = function() {
  293. return AbstractUiMessage.invalidate(this);
  294. };
  295. UiMessage.prototype.formatText = function(text) {
  296. return AbstractUiMessage.formatText(this, text);
  297. };
  298. UiMessage.prototype.removeDom = function() {
  299. return AbstractUiMessage.removeDom(this);
  300. };
  301. UiMessage.prototype.getDom = function() {
  302. return AbstractUiMessage.getDom(this);
  303. };
  304. UiMessage.prototype.createDom = function() {
  305. this.dom = doCreateMessageDom(this);
  306. return this;
  307. };
  308. UiMessage.prototype.duplicateDom = function() {
  309. return AbstractUiMessage.duplicateDom(this);
  310. };
  311. UiMessage.prototype.updateDom = function() {
  312. AbstractUiMessage.updateDom(this);
  313. return this;
  314. };
  315. UiMessage.prototype.update = function(ev, ts) {
  316. Message.prototype.update.call(this, ev, ts);
  317. this.invalidate();
  318. var match = this.text.match(/^<?https:\/\/www\.openstreetmap\.org\/\?mlat=(-?[0-9\.]+)(&amp;|&)mlon=(-?[0-9\.]+)(&amp;|&)macc=([0-9\.]+)[^\s]*/);
  319. if (match) {
  320. var lat = match[1],
  321. lon = match[3],
  322. acc = match[5];
  323. this.text = this.text.substr(0, match.index) +this.text.substr(match.index +match[0].length).trim();
  324. this.attachments.unshift({
  325. "color": "#008000",
  326. "text": match[0],
  327. "footer": "Open Street Map",
  328. "footer_icon": "https://www.openstreetmap.org/assets/favicon-32x32-36d06d8a01933075bc7093c9631cffd02d49b03b659f767340f256bb6839d990.png",
  329. "geo": {
  330. "latitude": match[1],
  331. "longitude": match[3],
  332. "accuracy": match[5]
  333. }
  334. });
  335. }
  336. };
  337. /**
  338. * @constructor
  339. * @implements {IUiMessage}
  340. * @extends {NoticeMessage}
  341. * @param {*} ev
  342. * @param {number} ts
  343. **/
  344. function UiNoticeMessage(channelId, ev, ts) {
  345. // Extends AbstractUiMessage and NoticeMessage
  346. NoticeMessage.call(this, ev, ts);
  347. /** @const @type {ChatContext} */
  348. this.context = DATA.context.getChannelContext(channelId).getChatContext();
  349. /** @type {string} @const */
  350. this.channelId = channelId;
  351. /** @type {Element} */
  352. this.dom; // jshint ignore:line
  353. /** @type {Element} */
  354. this.domWrapper = null;
  355. /** @type {boolean} */
  356. this.uiNeedRefresh = true;
  357. }
  358. UiNoticeMessage.prototype = Object.create(NoticeMessage.prototype);
  359. UiNoticeMessage.prototype.constructor = UiNoticeMessage;
  360. UiNoticeMessage.prototype.invalidate = function() {
  361. return AbstractUiMessage.invalidate(this);
  362. };
  363. UiNoticeMessage.prototype.formatText = function(text) {
  364. return AbstractUiMessage.formatText(this, text);
  365. };
  366. UiNoticeMessage.prototype.removeDom = function() {
  367. if (this.domWrapper && this.domWrapper.parentElement) {
  368. this.domWrapper.remove();
  369. delete(this.domWrapper);
  370. }
  371. if (this.dom)
  372. delete(this.dom);
  373. return this;
  374. };
  375. UiNoticeMessage.prototype.getDom = function() {
  376. AbstractUiMessage.getDom(this);
  377. return this.domWrapper;
  378. };
  379. UiNoticeMessage.prototype.duplicateDom = function() {
  380. return this.domWrapper.cloneNode(true);
  381. };
  382. UiNoticeMessage.prototype.createDom = function() {
  383. this.dom = doCreateMessageDom(this);
  384. this.domWrapper = document.createElement("span");
  385. this.dom.classList.add(R.klass.msg.notice);
  386. this.domWrapper.className = R.klass.msg.notice;
  387. this.domWrapper.textContent = locale.onlyVisible;
  388. this.domWrapper.appendChild(this.dom);
  389. return this;
  390. };
  391. UiNoticeMessage.prototype.updateDom = function() {
  392. AbstractUiMessage.updateDom(this);
  393. return this;
  394. };
  395. UiNoticeMessage.prototype.update = function(ev, ts) {
  396. NoticeMessage.prototype.update.call(this, ev, ts);
  397. this.invalidate();
  398. };
  399. /**
  400. * @param {string} input
  401. * @param {boolean} isMe
  402. * @return {Element}
  403. **/
  404. function createTmpMsgDom(input, isMe) {
  405. var dom = doCreateMessageDom(null),
  406. sender = SELECTED_CONTEXT.self;
  407. dom.classList.add(R.klass.msg.pending);
  408. if (isMe)
  409. dom.classList.add(R.klass.msg.meMessage);
  410. dom.textDom.innerHTML = formatText(input, {
  411. emojiFormatFunction: function(emoji) {
  412. if (emoji[0] === ':' && emoji[emoji.length -1] === ':')
  413. emoji = emoji.substr(1, emoji.length -2);
  414. var emojiDom = makeEmojiDom(emoji);
  415. if (emojiDom) {
  416. var domParent = document.createElement("span");
  417. domParent.className = R.klass.emoji.small;
  418. domParent.appendChild(emojiDom);
  419. return domParent.outerHTML;
  420. }
  421. return null;
  422. }
  423. });
  424. dom.authorName.textContent = SELECTED_CONTEXT.self ? SELECTED_CONTEXT.self.getName() : "";
  425. var dot = document.createElement("span");
  426. dot.className = R.klass.typing.dot1;
  427. dot.textContent = '.';
  428. dom.ts.appendChild(dot);
  429. dot = document.createElement("span");
  430. dot.className = R.klass.typing.dot2;
  431. dot.textContent = '.';
  432. dom.ts.appendChild(dot);
  433. dot = document.createElement("span");
  434. dot.className = R.klass.typing.dot3;
  435. dot.textContent = '.';
  436. dom.ts.appendChild(dot);
  437. dom.ts.classList.add(R.klass.typing.container);
  438. return dom;
  439. }