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.
 
 
 

211 lines
4.7 KiB

  1. define([
  2. 'html!./template',
  3. 'css!./styles',
  4. 'js/generator',
  5. 'js/client',
  6. 'js/renderer',
  7. 'js/input',
  8. 'ui/factory',
  9. 'js/events'
  10. ], function (
  11. template,
  12. styles,
  13. generator,
  14. client,
  15. renderer,
  16. input,
  17. uiFactory,
  18. events
  19. ) {
  20. return {
  21. tpl: template,
  22. postRender: function () {
  23. this.onEvent('onTreeLoaded', this.events.onTreeLoaded.bind(this));
  24. this.onEvent('onNew', this.events.onNew.bind(this));
  25. this.onEvent('onDeleteNode', this.events.onDeleteNode.bind(this));
  26. this.onEvent('onSelectNode', this.events.onSelectNode.bind(this));
  27. this.on('.btnAdd', 'click', this.actions.add.bind(this));
  28. this.on('.btnRename', 'click', this.events.onClickRename.bind(this));
  29. this.on('.btnRemove', 'click', this.events.onClickRemove.bind(this));
  30. },
  31. addGroup: function (groupName) {
  32. var el = $('<div class="item">' + groupName + '</div>')
  33. .appendTo(this.find('.list'))
  34. .attr('group', groupName);
  35. el.on('click', this.events.onClickGroup.bind(this));
  36. },
  37. actions: {
  38. add: function () {
  39. var selected = generator.nodes.filter(n => n.selected);
  40. var groupName = 'group-' + this.find('.list .item').length;
  41. selected.forEach(function (s) {
  42. if (!s.group)
  43. s.group = [];
  44. s.group.push(groupName)
  45. });
  46. this.addGroup(groupName);
  47. },
  48. rename: function (newName) {
  49. var el = this.find('.list .active').eq(0);
  50. if (!el)
  51. return;
  52. var oldName = el.attr('group');
  53. el
  54. .html(newName)
  55. .attr('group', newName);
  56. generator.nodes
  57. .filter(n => ((n.group) && (n.group.indexOf(oldName) > -1)))
  58. .forEach(function (n) {
  59. var group = n.group;
  60. group.spliceWhere(g => (g == oldName));
  61. group.push(newName);
  62. });
  63. },
  64. remove: function (group) {
  65. generator.nodes.forEach(function (g) {
  66. if ((g.group) && (g.group.indexOf(group) > -1)) {
  67. g.group.spliceWhere(g => (g == group));
  68. if (g.group.length == 0)
  69. delete g.group;
  70. }
  71. });
  72. this.find('.item[group="' + group + '"]').remove();
  73. }
  74. },
  75. events: {
  76. onNew: function () {
  77. this.find('.list').empty();
  78. },
  79. onDeleteNode: function (node) {
  80. if ((!node.group) || (node.group.length == 0))
  81. return;
  82. node.group.forEach(function (g) {
  83. var hasSiblings = generator.nodes.some(n => ((n.group) && (n.group.indexOf(g) > -1)))
  84. if (!hasSiblings)
  85. this.find('.item[group="' + g + '"]').remove();
  86. }, this);
  87. },
  88. onClickRename: function (e) {
  89. uiFactory.build('renameGroup', {
  90. onDone: this.events.onRenameGroupBuilt.bind(this)
  91. });
  92. },
  93. onRenameGroupBuilt: function () {
  94. var oldName = null;
  95. var el = this.find('.list .active').eq(0);
  96. if (el)
  97. oldName = el.attr('group');
  98. events.emit('onRenameGroup', oldName, this.actions.rename.bind(this));
  99. },
  100. onClickRemove: function (e) {
  101. var el = this.find('.list .active').eq(0);
  102. if (!el)
  103. return;
  104. el.remove();
  105. var groupName = el.attr('group');
  106. generator.nodes
  107. .filter(n => ((n.group) && (n.group.indexOf(groupName) > -1)))
  108. .forEach(function (n) {
  109. n.group.spliceWhere(g => (g == groupName));
  110. });
  111. },
  112. onTreeLoaded: function (tree) {
  113. var container = this.find('.list').empty();
  114. var groups = [];
  115. tree.nodes
  116. .filter(n => !!n.group)
  117. .sort(function (a, b) {
  118. if (a.group < b.group)
  119. return -1;
  120. else if (b.group < a.group)
  121. return 1;
  122. })
  123. .forEach(function (n) {
  124. n.group.forEach(function (g) {
  125. if (groups.indexOf(g) > -1)
  126. return;
  127. this.addGroup(g);
  128. groups.push(g);
  129. }, this);
  130. }, this);
  131. },
  132. onClickGroup: function (e) {
  133. var group = $(e.currentTarget).attr('group');
  134. var pos = {
  135. x: 0,
  136. y: 0
  137. };
  138. var nodes = generator.nodes
  139. .filter(function (n) {
  140. if ((!n.group) || (n.group.indexOf(group) == -1))
  141. return false;
  142. pos.x += n.pos.x;
  143. pos.y += n.pos.y;
  144. return true;
  145. });
  146. pos.x /= nodes.length;
  147. pos.y /= nodes.length;
  148. generator.callAction('selectNode', nodes);
  149. if (input.isKeyDown('shift')) {
  150. renderer.center({
  151. pos: pos
  152. });
  153. }
  154. renderer.makeDirty();
  155. },
  156. onSelectNode: function (nodes) {
  157. this.find('.list .active').removeClass('active');
  158. var selectedGroup = null;
  159. nodes.forEach(function (n) {
  160. (n.group || []).forEach(function (g) {
  161. var list = generator.nodes.filter(a => ((a.group) && (a.group.indexOf(g) > -1)));
  162. var check = nodes.filter(a => ((a.group) && (a.group.indexOf(g) > -1)));
  163. if (list.length == check.length)
  164. selectedGroup = g;
  165. });
  166. });
  167. if (selectedGroup)
  168. this.find('.list .item[group="' + selectedGroup + '"]').addClass('active');
  169. }
  170. }
  171. }
  172. });