| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 part of observatory; |
| 6 |
| 7 abstract class TableTreeRow { |
| 8 final TableTreeRow parent; |
| 9 final int depth; |
| 10 final List<TableTreeRow> children = new List<TableTreeRow>(); |
| 11 final List<String> columns = []; |
| 12 |
| 13 TableTreeRow(TableTreeRow parent) : |
| 14 parent = parent, |
| 15 depth = parent != null ? parent.depth+1 : 0; |
| 16 |
| 17 bool _expanded = false; |
| 18 bool get expanded => _expanded; |
| 19 set expanded(bool expanded) { |
| 20 var changed = _expanded != expanded; |
| 21 _expanded = expanded; |
| 22 if (changed) { |
| 23 // If the state has changed, fire callbacks. |
| 24 if (_expanded) { |
| 25 onShow(); |
| 26 } else { |
| 27 onHide(); |
| 28 } |
| 29 } |
| 30 } |
| 31 |
| 32 bool toggle() { |
| 33 expanded = !expanded; |
| 34 return expanded; |
| 35 } |
| 36 |
| 37 /// Fired when the tree row is expanded. Add children rows here. |
| 38 void onShow(); |
| 39 |
| 40 /// Fired when the tree row is collapsed. |
| 41 void onHide(); |
| 42 } |
| 43 |
| 44 class TableTree { |
| 45 final List<String> columnHeaders = []; |
| 46 final List<TableTreeRow> rows = toObservable([]); |
| 47 |
| 48 /// Create a table tree with column [headers]. |
| 49 TableTree(List<String> headers) { |
| 50 columnHeaders.addAll(headers); |
| 51 } |
| 52 |
| 53 /// Initialize the table tree with the list of root children. |
| 54 void initialize(List<TableTreeRow> children) { |
| 55 rows.clear(); |
| 56 rows.addAll(children); |
| 57 } |
| 58 |
| 59 /// Toggle expansion of row at [rowIndex]. |
| 60 void toggle(int rowIndex) { |
| 61 assert(rowIndex >= 0); |
| 62 assert(rowIndex < rows.length); |
| 63 var row = rows[rowIndex]; |
| 64 if (row.toggle()) { |
| 65 _expand(row); |
| 66 } else { |
| 67 _collapse(row); |
| 68 } |
| 69 } |
| 70 |
| 71 int _index(TableTreeRow row) => rows.indexOf(row); |
| 72 |
| 73 void _insertRow(int index, TableTreeRow row) { |
| 74 if (index == -1) { |
| 75 rows.add(row); |
| 76 } else { |
| 77 rows.insert(index, row); |
| 78 } |
| 79 } |
| 80 |
| 81 void _expand(TableTreeRow row) { |
| 82 int index = _index(row); |
| 83 assert(index != -1); |
| 84 for (var i = 0; i < row.children.length; i++) { |
| 85 _insertRow(index + i + 1, row.children[i]); |
| 86 } |
| 87 } |
| 88 |
| 89 void _collapse(TableTreeRow row) { |
| 90 var childCount = row.children.length; |
| 91 if (childCount == 0) { |
| 92 return; |
| 93 } |
| 94 for (var i = 0; i < childCount; i++) { |
| 95 // Close all inner rows. |
| 96 if (row.children[i].expanded) { |
| 97 _collapse(row.children[i]); |
| 98 } |
| 99 } |
| 100 // Collapse this row. |
| 101 row.expanded = false; |
| 102 // Remove all children. |
| 103 int index = _index(row); |
| 104 for (var i = 0; i < childCount; i++) { |
| 105 rows.removeAt(index + 1); |
| 106 } |
| 107 } |
| 108 } |
| OLD | NEW |