uiMessage.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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(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 FORCE get user (translate id into mimou-chat-id)
  75. } else if (link[0] === '#') {
  76. isInternal = true;
  77. // FIXME FORCE get channel (translate id into mimou-chat-id)
  78. } else {
  79. isInternal = false;
  80. // TODO check javascript:// injections
  81. }
  82. return {
  83. link: link,
  84. text: text || link,
  85. isInternal: isInternal
  86. };
  87. },
  88. _formatText = function(text) {
  89. return formatText(text, {
  90. highlight: [ "fromage" ],
  91. emojiFormatFunction: function(emoji) {
  92. if (emoji[0] === ':' && emoji[emoji.length -1] === ':')
  93. emoji = emoji.substr(1, emoji.length -2);
  94. var emojiDom = makeEmojiDom(emoji);
  95. if (emojiDom) {
  96. var domParent = document.createElement("span");
  97. domParent.className = R.klass.emoji.small;
  98. domParent.appendChild(emojiDom);
  99. return domParent.outerHTML;
  100. }
  101. return null;
  102. },
  103. linkFilter: linkFilter
  104. });
  105. },
  106. updateAttachments = function(_this, channelId) {
  107. var attachmentFrag = document.createDocumentFragment();
  108. for (var i =0, nbAttachments = _this.attachments.length; i < nbAttachments; i++) {
  109. var attachment = _this.attachments[i];
  110. if (attachment) {
  111. var domAttachment = createAttachmentDom(channelId, _this, attachment, i);
  112. if (domAttachment)
  113. attachmentFrag.appendChild(domAttachment);
  114. }
  115. }
  116. _this.dom.attachments.textContent = '';
  117. _this.dom.attachments.appendChild(attachmentFrag);
  118. },
  119. updateCommon = function(_this, sender) {
  120. _this.dom.ts.innerHTML = locale.formatDate(_this.ts);
  121. _this.dom.textDom.innerHTML = _formatText(_this.text);
  122. _this.dom.authorName.textContent = sender ? sender.name : (_this.username || "?");
  123. };
  124. return {
  125. /** @type {Element|null} *
  126. dom: null,
  127. /** @type {boolean} *
  128. uiNeedRefresh: true,
  129. */
  130. /** @param {IUiMessage} _this */
  131. invalidate: function(_this) {
  132. _this.uiNeedRefresh = true;
  133. return _this;
  134. },
  135. /** @param {UiMessage|UiMeMessage|UiNoticeMessage} _this */
  136. removeDom: function(_this) {
  137. if (_this.dom && _this.dom.parentElement) {
  138. _this.dom.remove();
  139. delete(_this.dom);
  140. }
  141. return _this;
  142. },
  143. /** @param {UiMessage|UiMeMessage|UiNoticeMessage} _this */
  144. getDom: function(_this) {
  145. if (!_this.dom) {
  146. _this.createDom().updateDom();
  147. } else if (_this.uiNeedRefresh) {
  148. _this.uiNeedRefresh = false;
  149. _this.updateDom();
  150. }
  151. return _this.dom;
  152. },
  153. /** @param {UiMessage|UiMeMessage|UiNoticeMessage} _this */
  154. updateDom: function(_this) {
  155. var sender = SLACK.context.users[_this.userId];
  156. updateCommon(_this, sender);
  157. updateAttachments(_this, _this.channelId);
  158. updateReactions(_this, _this.channelId);
  159. if (_this.edited)
  160. _this.dom.classList.add(R.klass.msg.editedStatus);
  161. return _this;
  162. },
  163. duplicateDom: function(_this) {
  164. return _this.dom.cloneNode(true);
  165. },
  166. formatText: function(_this, text) {
  167. return _formatText(text);
  168. }
  169. };
  170. })();
  171. /**
  172. * @constructor
  173. * @implements {IUiMessage}
  174. * @extends {MeMessage}
  175. * @param {*} ev
  176. * @param {number} ts
  177. **/
  178. function UiMeMessage(channelId, ev, ts) {
  179. // Extends AbstractUiMessage and MeMessage
  180. MeMessage.call(this, ev, ts);
  181. /** @type {string} @const */
  182. this.channelId = channelId;
  183. this.dom = AbstractUiMessage.dom;
  184. this.uiNeedRefresh = AbstractUiMessage.uiNeedRefresh;
  185. }
  186. UiMeMessage.prototype = Object.create(MeMessage.prototype);
  187. UiMeMessage.prototype.constructor = UiMeMessage;
  188. UiMeMessage.prototype.invalidate = function() {
  189. return AbstractUiMessage.invalidate(this);
  190. };
  191. UiMeMessage.prototype.formatText = function(text) {
  192. return AbstractUiMessage.formatText(this, text);
  193. };
  194. UiMeMessage.prototype.removeDom = function() {
  195. return AbstractUiMessage.removeDom(this);
  196. };
  197. UiMeMessage.prototype.getDom = function() {
  198. return AbstractUiMessage.getDom(this);
  199. };
  200. UiMeMessage.prototype.createDom = function() {
  201. this.dom = doCreateMessageDom(this, false);
  202. this.dom.classList.add(R.klass.msg.meMessage);
  203. return this;
  204. };
  205. UiMeMessage.prototype.duplicateDom = function() {
  206. return AbstractUiMessage.duplicateDom(this);
  207. };
  208. UiMeMessage.prototype.updateDom = function() {
  209. AbstractUiMessage.updateDom(this);
  210. return this;
  211. };
  212. UiMeMessage.prototype.update = function(ev, ts) {
  213. MeMessage.prototype.update.call(this, ev, ts);
  214. this.invalidate();
  215. };
  216. /**
  217. * @constructor
  218. * @implements {IUiMessage}
  219. * @extends {Message}
  220. * @param {*} ev
  221. * @param {number} ts
  222. **/
  223. function UiMessage(channelId, ev, ts) {
  224. // Extends AbstractUiMessage and Message
  225. Message.call(this, ev, ts);
  226. /** @type {string} @const */
  227. this.channelId = channelId;
  228. /** @type {Element} */
  229. this.dom = AbstractUiMessage.dom;
  230. /** @type {boolean} */
  231. this.uiNeedRefresh = AbstractUiMessage.uiNeedRefresh;
  232. }
  233. UiMessage.prototype = Object.create(Message.prototype);
  234. UiMessage.prototype.constructor = UiMessage;
  235. UiMessage.prototype.invalidate = function() {
  236. return AbstractUiMessage.invalidate(this);
  237. };
  238. UiMessage.prototype.formatText = function(text) {
  239. return AbstractUiMessage.formatText(this, text);
  240. };
  241. UiMessage.prototype.removeDom = function() {
  242. return AbstractUiMessage.removeDom(this);
  243. };
  244. UiMessage.prototype.getDom = function() {
  245. return AbstractUiMessage.getDom(this);
  246. };
  247. UiMessage.prototype.createDom = function() {
  248. this.dom = doCreateMessageDom(this, false);
  249. return this;
  250. };
  251. UiMessage.prototype.duplicateDom = function() {
  252. return AbstractUiMessage.duplicateDom(this);
  253. };
  254. UiMessage.prototype.updateDom = function() {
  255. AbstractUiMessage.updateDom(this);
  256. return this;
  257. };
  258. UiMessage.prototype.update = function(ev, ts) {
  259. Message.prototype.update.call(this, ev, ts);
  260. this.invalidate();
  261. };
  262. /**
  263. * @constructor
  264. * @implements {IUiMessage}
  265. * @extends {NoticeMessage}
  266. * @param {*} ev
  267. * @param {number} ts
  268. **/
  269. function UiNoticeMessage(channelId, ev, ts) {
  270. // Extends AbstractUiMessage and NoticeMessage
  271. NoticeMessage.call(this, ev, ts);
  272. /** @type {string} @const */
  273. this.channelId = channelId;
  274. this.dom = AbstractUiMessage.dom;
  275. /** @type {Element} */
  276. this.domWrapper = null;
  277. this.uiNeedRefresh = AbstractUiMessage.uiNeedRefresh;
  278. }
  279. UiNoticeMessage.prototype = Object.create(NoticeMessage.prototype);
  280. UiNoticeMessage.prototype.constructor = UiNoticeMessage;
  281. UiNoticeMessage.prototype.invalidate = function() {
  282. return AbstractUiMessage.invalidate(this);
  283. };
  284. UiNoticeMessage.prototype.formatText = function(text) {
  285. return AbstractUiMessage.formatText(this, text);
  286. };
  287. UiNoticeMessage.prototype.removeDom = function() {
  288. if (this.domWrapper && this.domWrapper.parentElement) {
  289. this.domWrapper.remove();
  290. delete(this.domWrapper);
  291. }
  292. if (this.dom)
  293. delete(this.dom);
  294. return this;
  295. };
  296. UiNoticeMessage.prototype.getDom = function() {
  297. AbstractUiMessage.getDom(this);
  298. return this.domWrapper;
  299. };
  300. UiNoticeMessage.prototype.duplicateDom = function() {
  301. return this.domWrapper.cloneNode(true);
  302. };
  303. UiNoticeMessage.prototype.createDom = function() {
  304. this.dom = doCreateMessageDom(this, false);
  305. this.domWrapper = document.createElement("span");
  306. this.dom.classList.add(R.klass.msg.notice);
  307. this.domWrapper.className = R.klass.msg.notice;
  308. this.domWrapper.textContent = locale.onlyVisible;
  309. return this;
  310. };
  311. UiNoticeMessage.prototype.updateDom = function() {
  312. AbstractUiMessage.updateDom(this);
  313. return this;
  314. };
  315. UiNoticeMessage.prototype.update = function(ev, ts) {
  316. NoticeMessage.prototype.update.call(this, ev, ts);
  317. this.invalidate();
  318. };