uiMessage.js 14 KB

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