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.
 
 
 

133 lines
2.5 KiB

  1. define([
  2. 'js/system/events',
  3. 'js/system/client',
  4. 'js/system/globals',
  5. 'html!ui/templates/online/template',
  6. 'css!ui/templates/online/styles',
  7. 'html!ui/templates/online/templateListItem'
  8. ], function (
  9. events,
  10. client,
  11. globals,
  12. template,
  13. styles,
  14. templateListItem
  15. ) {
  16. return {
  17. tpl: template,
  18. centered: true,
  19. onlineList: [],
  20. modal: true,
  21. postRender: function () {
  22. globals.onlineList = this.onlineList;
  23. this.onEvent('onGetConnectedPlayer', this.onGetConnectedPlayer.bind(this));
  24. this.onEvent('onGetDisconnectedPlayer', this.onGetDisconnectedPlayer.bind(this));
  25. this.onEvent('onKeyDown', this.onKeyDown.bind(this));
  26. this.onEvent('onShowOnline', this.toggle.bind(this));
  27. },
  28. onKeyDown: function (key) {
  29. if (key == 'o')
  30. this.toggle();
  31. },
  32. toggle: function () {
  33. this.shown = !this.el.is(':visible');
  34. if (this.shown) {
  35. this.show();
  36. this.build();
  37. } else
  38. this.hide();
  39. },
  40. onGetConnectedPlayer: function (list) {
  41. if (!list.length)
  42. list = [list];
  43. let onlineList = this.onlineList;
  44. list.forEach(function (l) {
  45. let exists = onlineList.find(function (o) {
  46. return (o.name == l.name);
  47. });
  48. if (exists)
  49. $.extend(true, exists, l);
  50. else
  51. onlineList.push(l);
  52. });
  53. onlineList
  54. .sort(function (a, b) {
  55. if (a.level == b.level) {
  56. if (a.name > b.name)
  57. return 1;
  58. return -1;
  59. } return b.level - a.level;
  60. });
  61. if (this.shown)
  62. this.build();
  63. },
  64. onGetDisconnectedPlayer: function (name) {
  65. let onlineList = this.onlineList;
  66. onlineList.spliceWhere(function (o) {
  67. return (o.name == name);
  68. });
  69. if (this.shown)
  70. this.build();
  71. },
  72. build: function () {
  73. let container = this.el.find('.list');
  74. container
  75. .children(':not(.heading)')
  76. .remove();
  77. this.onlineList.forEach(function (l) {
  78. let html = templateListItem
  79. .replace('$NAME$', l.name)
  80. .replace('$LEVEL$', l.level)
  81. .replace('$CLASS$', l.class);
  82. $(html)
  83. .appendTo(container)
  84. .on('contextmenu', this.showContext.bind(this, l));
  85. }, this);
  86. },
  87. showContext: function (char, e) {
  88. if (char.name != window.player.name) {
  89. events.emit('onContextMenu', [{
  90. text: 'invite to party',
  91. callback: this.invite.bind(this, char.id)
  92. }, {
  93. text: 'whisper',
  94. callback: events.emit.bind(events, 'onDoWhisper', char.name)
  95. }], e);
  96. }
  97. e.preventDefault;
  98. return false;
  99. },
  100. invite: function (charId) {
  101. this.hide();
  102. client.request({
  103. cpn: 'social',
  104. method: 'getInvite',
  105. id: charId
  106. });
  107. }
  108. };
  109. });