levelcontainer.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. define( function ( require, exports, module ) {
  2. var Group = require( 'graphic/group' );
  3. var Connection = require( '../graffle/bezierconnection' );
  4. var Query = require( '../pathdiagram/dataquery' );
  5. var Rect = require( 'graphic/rect' );
  6. var Text = require( 'graphic/text' );
  7. var TextSpan = require( 'graphic/textspan' );
  8. var PathNode = require( '../pathdiagram/pathnode' );
  9. var colors = require( '../pathdiagram/colors' );
  10. var rHeight = 25;
  11. var rWidth = 250;
  12. var rMargin = 10;
  13. var lMargin = 100;
  14. var LevelContainer = require( 'core/class' ).createClass( 'LevelContainer', {
  15. base: Group,
  16. constructor: function ( level ) {
  17. this.callBase();
  18. this.level = level;
  19. },
  20. getPathNodes: function() {
  21. return this.getItems();
  22. },
  23. render: function( stream, x, yMiddle ) {
  24. this.clear();
  25. if(!stream) {
  26. return;
  27. }
  28. var yStart, i, y, node, data, text, pBox;
  29. yStart = yMiddle - ( rHeight + rMargin ) * (stream.length - 1) / 2;
  30. for(i = 0, y = yStart; i < stream.length; i++, y += rHeight + rMargin) {
  31. data = stream[ i ];
  32. node = new PathNode( x, y, rWidth, rHeight, data, this.level === 0 );
  33. this.addShape( node );
  34. node.on('click', this.onNodeClick.bind(this));
  35. }
  36. },
  37. onNodeClick: function( e ) {
  38. var node = e.targetShape;
  39. while( false === (node instanceof PathNode)) {
  40. node = node.container;
  41. }
  42. if( this.selected != node ) {
  43. this.select(node);
  44. } else {
  45. this.select(null);
  46. }
  47. },
  48. select: function(node) {
  49. if(this.level === 0 && node === null) {
  50. return;
  51. }
  52. if(typeof(node) === 'number') {
  53. node = this.getItem(node);
  54. }
  55. var last = this.selected;
  56. if(this.selected) {
  57. this.selected.unselect();
  58. }
  59. this.selected = node;
  60. if(node) {
  61. node.select();
  62. this.levelWord = node.data.word;
  63. }
  64. this.trigger('selected', {
  65. level: this.level,
  66. levelContainer: this,
  67. pathNode: node,
  68. lastPathNode: last
  69. });
  70. },
  71. getSelected: function() {
  72. return this.selected;
  73. }
  74. } );
  75. LevelContainer.RECT_SIZE = {
  76. width: rWidth,
  77. height: rHeight
  78. };
  79. return LevelContainer;
  80. } );