Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

179 lignes
3.9 KiB

  1. define([
  2. 'js/system/events',
  3. 'js/system/client',
  4. 'html!ui/templates/passives/template',
  5. 'css!ui/templates/passives/styles',
  6. 'ui/templates/passives/constants',
  7. 'ui/templates/passives/temp'
  8. ], function (
  9. events,
  10. client,
  11. tpl,
  12. styles,
  13. constants,
  14. temp
  15. ) {
  16. return {
  17. tpl: tpl,
  18. modal: true,
  19. centered: true,
  20. canvas: null,
  21. size: {},
  22. ctx: null,
  23. pos: {
  24. x: 0,
  25. y: 0
  26. },
  27. data: {
  28. nodes: null,
  29. links: null
  30. },
  31. postRender: function () {
  32. var data = JSON.parse(temp.json);
  33. this.data.nodes = data.nodes;
  34. this.data.links = data.links;
  35. //We need to be able to determine the size of elements
  36. this.el.css({
  37. visibility: 'hidden',
  38. display: 'block'
  39. });
  40. this.canvas = this.find('.canvas')[0];
  41. this.size.w = this.canvas.width = this.find('.bottom').width();
  42. this.size.h = this.canvas.height = this.find('.bottom').height();
  43. this.ctx = this.canvas.getContext('2d');
  44. //Reset styles after determining size
  45. this.el.css({
  46. visibility: 'visible',
  47. display: 'none'
  48. });
  49. this.ctx.lineWidth = constants.lineWidth;
  50. $(this.canvas)
  51. .on('contextmenu', function () {
  52. return false;
  53. });
  54. this.onEvent('onKeyDown', this.onKeyDown.bind(this));
  55. //Calculate midpoint
  56. this.data.nodes.forEach(function (n) {
  57. this.pos.x += n.pos.x;
  58. this.pos.y += n.pos.y;
  59. }, this);
  60. this.pos.x = ~~(this.pos.x / this.data.nodes.length) * constants.gridSize;
  61. this.pos.y = ~~(this.pos.y / this.data.nodes.length) * constants.gridSize;
  62. },
  63. renderNodes: function () {
  64. this.renderers.clear.call(this);
  65. var links = this.data.links;
  66. var nodes = this.data.nodes;
  67. links.forEach(function (l) {
  68. var linked = (
  69. nodes.find(n => (n.id == l.from.id)).selected &&
  70. nodes.find(n => (n.id == l.to.id)).selected
  71. );
  72. this.renderers.line.call(this, l.from, l.to, linked);
  73. }, this);
  74. nodes.forEach(function (n) {
  75. this.renderers.node.call(this, n, n.pos.x, n.pos.y);
  76. }, this);
  77. },
  78. toggle: function (show) {
  79. this.shown = !this.el.is(':visible');
  80. if (this.shown) {
  81. this.show();
  82. this.renderNodes();
  83. } else
  84. this.hide();
  85. },
  86. onKeyDown: function (key) {
  87. if (key == 'p')
  88. this.toggle();
  89. },
  90. renderers: {
  91. clear: function () {
  92. var pos = this.oldPos || this.pos;
  93. this.ctx.clearRect(0, 0, this.size.w, this.size.h);
  94. delete this.oldPos;
  95. },
  96. node: function (node) {
  97. var color = (node.color >= 0) ? (node.color + 1) : -1;
  98. if ((!node.stats) || (Object.keys(node.stats).length == 0))
  99. color = 0;
  100. this.ctx.fillStyle = ([
  101. '#69696e',
  102. '#c0c3cf',
  103. '#3fa7dd',
  104. '#4ac441',
  105. '#d43346',
  106. '#a24eff'
  107. ])[color];
  108. var size = ([
  109. constants.blockSize,
  110. constants.blockSize * 2,
  111. constants.blockSize * 3
  112. ])[node.size];
  113. var x = (node.pos.x * constants.gridSize) - ((size - constants.blockSize) / 2) - this.pos.x;
  114. var y = (node.pos.y * constants.gridSize) - ((size - constants.blockSize) / 2) - this.pos.y;
  115. this.ctx.fillRect(x, y, size, size);
  116. this.ctx.strokeStyle = ([
  117. '#69696e',
  118. '#69696e',
  119. '#42548d',
  120. '#386646',
  121. '#763b3b',
  122. '#533399'
  123. ])[color];
  124. this.ctx.strokeRect(x, y, size, size);
  125. if (node.selected) {
  126. this.ctx.strokeStyle = '#fafcfc';
  127. this.ctx.strokeRect(x, y, size, size);
  128. }
  129. },
  130. line: function (fromNode, toNode, linked) {
  131. var ctx = this.ctx;
  132. var halfSize = constants.blockSize / 2;
  133. var fromX = (fromNode.pos.x * constants.gridSize) + halfSize - this.pos.x;
  134. var fromY = (fromNode.pos.y * constants.gridSize) + halfSize - this.pos.y;
  135. var toX = (toNode.pos.x * constants.gridSize) + halfSize - this.pos.x;
  136. var toY = (toNode.pos.y * constants.gridSize) + halfSize - this.pos.y;
  137. ctx.strokeStyle = linked ? '#fafcfc' : '#69696e';
  138. ctx.beginPath();
  139. ctx.moveTo(fromX, fromY);
  140. ctx.lineTo(toX, toY);
  141. ctx.closePath();
  142. ctx.stroke();
  143. }
  144. }
  145. }
  146. });