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.
 
 
 

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