You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

247 lines
5.2 KiB

  1. define([
  2. 'js/system/events',
  3. 'html!ui/templates/messages/template',
  4. 'html!ui/templates/messages/tplTab',
  5. 'css!ui/templates/messages/styles',
  6. 'js/input',
  7. 'js/system/client'
  8. ], function (
  9. events,
  10. template,
  11. tplTab,
  12. styles,
  13. input,
  14. client
  15. ) {
  16. return {
  17. tpl: template,
  18. currentFilter: 'info',
  19. messages: [],
  20. maxTtl: 500,
  21. maxChatLength: 255,
  22. shiftDown: false,
  23. hoverItem: null,
  24. hoverFilter: false,
  25. postRender: function () {
  26. this.onEvent('onGetMessages', this.onGetMessages.bind(this));
  27. this.onEvent('onDoWhisper', this.onDoWhisper.bind(this));
  28. this.onEvent('onJoinChannel', this.onJoinChannel.bind(this));
  29. this.onEvent('onLeaveChannel', this.onLeaveChannel.bind(this));
  30. this.onEvent('onGetCustomChatChannels', this.onGetCustomChatChannels.bind(this));
  31. this.find('input')
  32. .on('keydown', this.sendChat.bind(this))
  33. .on('input', this.checkChatLength.bind(this))
  34. .on('blur', this.toggle.bind(this, false, true));
  35. this
  36. .find('.filter:not(.channel)')
  37. .on('mouseover', this.onFilterHover.bind(this, true))
  38. .on('mouseleave', this.onFilterHover.bind(this, false))
  39. .on('click', this.onClickFilter.bind(this));
  40. this.onEvent('onKeyDown', this.onKeyDown.bind(this));
  41. },
  42. checkChatLength: function () {
  43. let textbox = this.find('input');
  44. let val = textbox.val();
  45. if (val.length <= this.maxChatLength)
  46. return;
  47. val = val.substr(0, this.maxChatLength);
  48. textbox.val(val);
  49. },
  50. onGetCustomChatChannels: function (channels) {
  51. channels.forEach(function (c) {
  52. this.onJoinChannel(c);
  53. }, this);
  54. },
  55. onJoinChannel: function (channel) {
  56. this.find('[filter="' + channel.trim() + '"]').remove();
  57. let container = this.find('.filters');
  58. let newFilter = $(tplTab)
  59. .appendTo(container)
  60. .addClass('channel')
  61. .attr('filter', channel.trim())
  62. .html(channel.trim())
  63. .on('mouseover', this.onFilterHover.bind(this, true))
  64. .on('mouseleave', this.onFilterHover.bind(this, false))
  65. .on('click', this.onClickFilter.bind(this));
  66. },
  67. onLeaveChannel: function (channel) {
  68. this.find('.filters [filter="' + channel + '"]').remove();
  69. },
  70. onFilterHover: function (hover) {
  71. this.hoverFilter = hover;
  72. },
  73. onClickFilter: function (e) {
  74. let el = $(e.currentTarget);
  75. el.toggleClass('active');
  76. let filter = el.attr('filter');
  77. let method = (el.hasClass('active') ? 'show' : 'hide');
  78. if (method == 'show')
  79. this.find('.list').addClass(filter);
  80. else
  81. this.find('.list').removeClass(filter);
  82. if (el.hasClass('channel'))
  83. this.find('.list .' + filter)[method]();
  84. },
  85. onKeyDown: function (key, state) {
  86. if (key == 'enter')
  87. this.toggle(true);
  88. },
  89. onDoWhisper: function (charName) {
  90. this.toggle(true);
  91. let toName = charName;
  92. if (charName.indexOf(' ') > -1)
  93. toName = "'" + toName + "'";
  94. this.find('input').val('@' + toName + ' ');
  95. },
  96. onGetMessages: function (e) {
  97. let messages = e.messages;
  98. if (!messages.length)
  99. messages = [messages];
  100. let container = this.find('.list');
  101. messages.forEach(function (m) {
  102. let message = m.message;
  103. if (m.item) {
  104. let source = message.split(':')[0] + ': ';
  105. message = source + '<span class="q' + (m.item.quality || 0) + '">' + message.replace(source, '') + '</span>';
  106. }
  107. let el = $('<div class="list-message ' + m.class + '">' + message + '</div>')
  108. .appendTo(container);
  109. if (m.type != null)
  110. el.addClass(m.type);
  111. else
  112. el.addClass('info');
  113. if (m.item) {
  114. el.find('span')
  115. .on('mousemove', this.showItemTooltip.bind(this, el, m.item))
  116. .on('mouseleave', this.hideItemTooltip.bind(this));
  117. }
  118. if (m.type) {
  119. let isChannel = (['info', 'chat', 'loot', 'rep'].indexOf(m.type) == -1);
  120. if (isChannel) {
  121. if (this.find('.filter[filter="' + m.type + '"]').hasClass('active'))
  122. el.show();
  123. }
  124. }
  125. this.messages.push({
  126. ttl: this.maxTtl,
  127. el: el
  128. });
  129. }, this);
  130. container.scrollTop(9999999);
  131. },
  132. hideItemTooltip: function () {
  133. if (this.dragEl) {
  134. this.hoverCell = null;
  135. return;
  136. }
  137. events.emit('onHideItemTooltip', this.hoverItem);
  138. this.hoverItem = null;
  139. },
  140. showItemTooltip: function (el, item, e) {
  141. if (item)
  142. this.hoverItem = item;
  143. else
  144. item = this.hoverItem;
  145. if (!item)
  146. return;
  147. let ttPos = null;
  148. if (el) {
  149. ttPos = {
  150. x: ~~(e.clientX + 32),
  151. y: ~~(e.clientY)
  152. };
  153. }
  154. events.emit('onShowItemTooltip', item, ttPos, null, true);
  155. },
  156. toggle: function (show, isFake) {
  157. if ((isFake) && (this.hoverFilter))
  158. return;
  159. input.resetKeys();
  160. this.el.removeClass('typing');
  161. let textbox = this.find('input');
  162. if (show) {
  163. this.el.addClass('typing');
  164. textbox.focus();
  165. this.find('.list').scrollTop(9999999);
  166. } else
  167. textbox.val('');
  168. },
  169. sendChat: function (e) {
  170. if (e.which == 27)
  171. this.toggle(false);
  172. if (e.which != 13)
  173. return;
  174. if (!this.el.hasClass('typing')) {
  175. this.toggle(true);
  176. return;
  177. }
  178. let textbox = this.find('input');
  179. let val = textbox.val()
  180. .split('<')
  181. .join('&lt;')
  182. .split('>')
  183. .join('&gt;');
  184. textbox.blur();
  185. if (val.trim() == '')
  186. return;
  187. client.request({
  188. cpn: 'social',
  189. method: 'chat',
  190. data: {
  191. message: val
  192. }
  193. });
  194. }
  195. };
  196. });