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.
 
 
 

178 lines
3.5 KiB

  1. define([
  2. 'html!ui/templates/nodeInfo/template',
  3. 'css!ui/templates/nodeInfo/styles',
  4. 'js/constants',
  5. 'js/input'
  6. ], function (
  7. template,
  8. styles,
  9. constants,
  10. input
  11. ) {
  12. return {
  13. tpl: template,
  14. nodes: null,
  15. statEl: null,
  16. postRender: function () {
  17. this.onEvent('onSelectNode', this.events.onSelectNode.bind(this));
  18. this.onEvent('onFocusNode', this.events.onFocusNode.bind(this));
  19. this.buildLookup();
  20. },
  21. buildLookup: function () {
  22. var container = this.find('.lookup');
  23. for (var p in constants.stats) {
  24. var statName = constants.stats[p];
  25. $('<div class="item">' + statName + '</div>')
  26. .appendTo(container)
  27. .on('click', this.events.onClickStat.bind(this, p));
  28. }
  29. },
  30. updateLabels: function () {
  31. var nodes = this.nodes;
  32. if (nodes.length == 1)
  33. nodes = nodes[0];
  34. var isArray = !!nodes.push;
  35. this.find('.lblId').html(isArray ? '' : nodes.id);
  36. var group = nodes.group;
  37. if (isArray) {
  38. group = nodes[0].group;
  39. if (nodes.some(n => (n.group != group)))
  40. group = '';
  41. }
  42. this.find('.lblGroup').html((group || []).toString());
  43. var size = nodes.size;
  44. if (isArray) {
  45. size = nodes[0].size;
  46. if (nodes.some(n => (n.size != size)))
  47. size = '';
  48. }
  49. size = ([
  50. 'Lesser',
  51. 'Greater',
  52. 'Core'
  53. ])[size];
  54. this.find('.lblType').html(size);
  55. var pos = isArray ? '' : nodes.pos.x + ', ' + nodes.pos.y;
  56. this.find('.lblPos').html(pos);
  57. },
  58. setStats: function () {
  59. var node = this.nodes[0];
  60. this.find('.stats').empty();
  61. if (!node.stats)
  62. node.stats = {};
  63. for (var p in node.stats) {
  64. this.buildStatSelector(p, node.stats[p]);
  65. }
  66. this.buildStatSelector();
  67. },
  68. buildStatSelector: function (stat, value) {
  69. var string = stat ? stat + ': ' + value : 'Select a stat...';
  70. $('<div class="item">' + string + '</div>')
  71. .appendTo(this.find('.stats'))
  72. .data('stat', {
  73. stat: stat,
  74. value: value
  75. })
  76. .on('mousewheel', this.events.onScrollStat.bind(this))
  77. .on('click', this.events.onShowLookup.bind(this));
  78. },
  79. updateNode: function () {
  80. var stats = {};
  81. this.find('.stats .item').toArray().forEach(function (s) {
  82. var stat = $(s).data('stat');
  83. if (!stat.stat)
  84. return;
  85. stats[stat.stat] = stat.value;
  86. });
  87. this.nodes[0].stats = stats;
  88. },
  89. actions: {
  90. },
  91. events: {
  92. onFocusNode: function (node) {
  93. this.events.onSelectNode.call(this, [node]);
  94. },
  95. onSelectNode: function (nodes) {
  96. this.nodes = nodes;
  97. if (nodes.length > 0)
  98. this.updateLabels();
  99. else if (nodes.length == 0)
  100. return;
  101. this.find('.nodeCount').html(nodes.length);
  102. this.setStats();
  103. },
  104. onClickStat: function (stat) {
  105. var string = stat + ': 1';
  106. this.statEl
  107. .html(string)
  108. .data('stat', {
  109. stat: stat,
  110. value: 1
  111. });
  112. this.statEl = null;
  113. this.find('.lookup').hide();
  114. this.updateNode();
  115. this.setStats();
  116. this.el.removeClass('picking');
  117. },
  118. onShowLookup: function (e) {
  119. this.statEl = $(e.currentTarget);
  120. this.find('.lookup').show();
  121. this.el.addClass('picking');
  122. },
  123. onScrollStat: function (e) {
  124. var el = $(e.currentTarget);
  125. var stat = el.data('stat');
  126. var delta = (e.originalEvent.deltaY > 0) ? -1 : 1;
  127. if (input.isKeyDown('shift')) {
  128. var nextValue = ~~((stat.value + (delta * 10)) / 10) * 10;
  129. delta = nextValue - stat.value;
  130. }
  131. stat.value += delta;
  132. var string = stat.stat + ': ' + stat.value;
  133. el.html(string);
  134. this.updateNode();
  135. }
  136. }
  137. }
  138. });