uiMessage.js 11 KB

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