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.
 
 
 

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