treeview.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * Helper method to start a single XUL tree test.
  3. */
  4. function loadXULTreeAndDoTest(aDoTestFunc, aTreeID, aTreeView)
  5. {
  6. var doTestFunc = aDoTestFunc ? aDoTestFunc : gXULTreeLoadContext.doTestFunc;
  7. var treeID = aTreeID ? aTreeID : gXULTreeLoadContext.treeID;
  8. var treeView = aTreeView ? aTreeView : gXULTreeLoadContext.treeView;
  9. function loadXULTree(aTreeID, aTreeView)
  10. {
  11. this.treeNode = getNode(aTreeID);
  12. this.eventSeq = [
  13. new invokerChecker(EVENT_REORDER, this.treeNode)
  14. ];
  15. this.invoke = function loadXULTree_invoke()
  16. {
  17. this.treeNode.view = aTreeView;
  18. }
  19. this.getID = function loadXULTree_getID()
  20. {
  21. return "Load XUL tree " + prettyName(aTreeID);
  22. }
  23. }
  24. gXULTreeLoadContext.queue = new eventQueue();
  25. gXULTreeLoadContext.queue.push(new loadXULTree(treeID, treeView));
  26. gXULTreeLoadContext.queue.onFinish = function()
  27. {
  28. SimpleTest.executeSoon(doTestFunc);
  29. return DO_NOT_FINISH_TEST;
  30. }
  31. gXULTreeLoadContext.queue.invoke();
  32. }
  33. /**
  34. * Analogy of addA11yLoadEvent, nice helper to load XUL tree and start the test.
  35. */
  36. function addA11yXULTreeLoadEvent(aDoTestFunc, aTreeID, aTreeView)
  37. {
  38. gXULTreeLoadContext.doTestFunc = aDoTestFunc;
  39. gXULTreeLoadContext.treeID = aTreeID;
  40. gXULTreeLoadContext.treeView = aTreeView;
  41. addA11yLoadEvent(loadXULTreeAndDoTest);
  42. }
  43. function nsTableTreeView(aRowCount)
  44. {
  45. this.__proto__ = new nsTreeView();
  46. for (var idx = 0; idx < aRowCount; idx++)
  47. this.mData.push(new treeItem("row" + String(idx) + "_"));
  48. }
  49. function nsTreeTreeView()
  50. {
  51. this.__proto__ = new nsTreeView();
  52. this.mData = [
  53. new treeItem("row1"),
  54. new treeItem("row2_", true, [new treeItem("row2.1_"), new treeItem("row2.2_")]),
  55. new treeItem("row3_", false, [new treeItem("row3.1_"), new treeItem("row3.2_")]),
  56. new treeItem("row4")
  57. ];
  58. }
  59. function nsTreeView()
  60. {
  61. this.mTree = null;
  62. this.mData = [];
  63. }
  64. nsTreeView.prototype =
  65. {
  66. //////////////////////////////////////////////////////////////////////////////
  67. // nsITreeView implementation
  68. get rowCount()
  69. {
  70. return this.getRowCountIntl(this.mData);
  71. },
  72. setTree: function setTree(aTree)
  73. {
  74. this.mTree = aTree;
  75. },
  76. getCellText: function getCellText(aRow, aCol)
  77. {
  78. var data = this.getDataForIndex(aRow);
  79. if (aCol.id in data.colsText)
  80. return data.colsText[aCol.id];
  81. return data.text + aCol.id;
  82. },
  83. getCellValue: function getCellValue(aRow, aCol)
  84. {
  85. var data = this.getDataForIndex(aRow);
  86. return data.value;
  87. },
  88. getRowProperties: function getRowProperties(aIndex) { return ""; },
  89. getCellProperties: function getCellProperties(aIndex, aCol)
  90. {
  91. if (!aCol.cycler)
  92. return "";
  93. var data = this.getDataForIndex(aIndex);
  94. return this.mCyclerStates[data.cyclerState];
  95. },
  96. getColumnProperties: function getColumnProperties(aCol) { return ""; },
  97. getParentIndex: function getParentIndex(aRowIndex)
  98. {
  99. var info = this.getInfoByIndex(aRowIndex);
  100. return info.parentIndex;
  101. },
  102. hasNextSibling: function hasNextSibling(aRowIndex, aAfterIndex) { },
  103. getLevel: function getLevel(aIndex)
  104. {
  105. var info = this.getInfoByIndex(aIndex);
  106. return info.level;
  107. },
  108. getImageSrc: function getImageSrc(aRow, aCol) {},
  109. getProgressMode: function getProgressMode(aRow, aCol) {},
  110. isContainer: function isContainer(aIndex)
  111. {
  112. var data = this.getDataForIndex(aIndex);
  113. return data.open != undefined;
  114. },
  115. isContainerOpen: function isContainerOpen(aIndex)
  116. {
  117. var data = this.getDataForIndex(aIndex);
  118. return data.open;
  119. },
  120. isContainerEmpty: function isContainerEmpty(aIndex)
  121. {
  122. var data = this.getDataForIndex(aIndex);
  123. return data.open == undefined;
  124. },
  125. isSeparator: function isSeparator(aIndex) {},
  126. isSorted: function isSorted() {},
  127. toggleOpenState: function toggleOpenState(aIndex)
  128. {
  129. var data = this.getDataForIndex(aIndex);
  130. data.open = !data.open;
  131. var rowCount = this.getRowCountIntl(data.children);
  132. if (data.open)
  133. this.mTree.rowCountChanged(aIndex + 1, rowCount);
  134. else
  135. this.mTree.rowCountChanged(aIndex + 1, -rowCount);
  136. },
  137. selectionChanged: function selectionChanged() {},
  138. cycleHeader: function cycleHeader(aCol) {},
  139. cycleCell: function cycleCell(aRow, aCol)
  140. {
  141. var data = this.getDataForIndex(aRow);
  142. data.cyclerState = (data.cyclerState + 1) % 3;
  143. this.mTree.invalidateCell(aRow, aCol);
  144. },
  145. isEditable: function isEditable(aRow, aCol)
  146. {
  147. return true;
  148. },
  149. isSelectable: function isSelectable(aRow, aCol) {},
  150. setCellText: function setCellText(aRow, aCol, aValue)
  151. {
  152. var data = this.getDataForIndex(aRow);
  153. data.colsText[aCol.id] = aValue;
  154. },
  155. setCellValue: function setCellValue(aRow, aCol, aValue)
  156. {
  157. var data = this.getDataForIndex(aRow);
  158. data.value = aValue;
  159. this.mTree.invalidateCell(aRow, aCol);
  160. },
  161. performAction: function performAction(aAction) {},
  162. performActionOnRow: function performActionOnRow(aAction, aRow) {},
  163. performActionOnCell: function performActionOnCell(aAction, aRow, aCol) {},
  164. //////////////////////////////////////////////////////////////////////////////
  165. // public implementation
  166. appendItem: function appendItem(aText)
  167. {
  168. this.mData.push(new treeItem(aText));
  169. },
  170. //////////////////////////////////////////////////////////////////////////////
  171. // private implementation
  172. getDataForIndex: function getDataForIndex(aRowIndex)
  173. {
  174. return this.getInfoByIndex(aRowIndex).data;
  175. },
  176. getInfoByIndex: function getInfoByIndex(aRowIndex)
  177. {
  178. var info = {
  179. data: null,
  180. parentIndex: -1,
  181. level: 0,
  182. index: -1
  183. };
  184. this.getInfoByIndexIntl(aRowIndex, info, this.mData, 0);
  185. return info;
  186. },
  187. getRowCountIntl: function getRowCountIntl(aChildren)
  188. {
  189. var rowCount = 0;
  190. for (var childIdx = 0; childIdx < aChildren.length; childIdx++) {
  191. rowCount++;
  192. var data = aChildren[childIdx];
  193. if (data.open)
  194. rowCount += this.getRowCountIntl(data.children);
  195. }
  196. return rowCount;
  197. },
  198. getInfoByIndexIntl: function getInfoByIndexIntl(aRowIdx, aInfo,
  199. aChildren, aLevel)
  200. {
  201. var rowIdx = aRowIdx;
  202. for (var childIdx = 0; childIdx < aChildren.length; childIdx++) {
  203. var data = aChildren[childIdx];
  204. aInfo.index++;
  205. if (rowIdx == 0) {
  206. aInfo.data = data;
  207. aInfo.level = aLevel;
  208. return -1;
  209. }
  210. if (data.open) {
  211. var parentIdx = aInfo.index;
  212. rowIdx = this.getInfoByIndexIntl(rowIdx - 1, aInfo, data.children,
  213. aLevel + 1);
  214. if (rowIdx == -1) {
  215. if (aInfo.parentIndex == -1)
  216. aInfo.parentIndex = parentIdx;
  217. return 0;
  218. }
  219. } else {
  220. rowIdx--;
  221. }
  222. }
  223. return rowIdx;
  224. },
  225. mCyclerStates: [
  226. "cyclerState1",
  227. "cyclerState2",
  228. "cyclerState3"
  229. ]
  230. };
  231. function treeItem(aText, aOpen, aChildren)
  232. {
  233. this.text = aText;
  234. this.colsText = {};
  235. this.open = aOpen;
  236. this.value = "true";
  237. this.cyclerState = 0;
  238. if (aChildren)
  239. this.children = aChildren;
  240. }
  241. /**
  242. * Used in conjunction with loadXULTreeAndDoTest and addA11yXULTreeLoadEvent.
  243. */
  244. var gXULTreeLoadContext =
  245. {
  246. doTestFunc: null,
  247. treeID: null,
  248. treeView: null,
  249. queue: null
  250. };