Built files from Bizgaze WebServer
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

data_tree.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  2. /* Tabulator v4.6.3 (c) Oliver Folkerd */
  3. var DataTree = function DataTree(table) {
  4. this.table = table;
  5. this.indent = 10;
  6. this.field = "";
  7. this.collapseEl = null;
  8. this.expandEl = null;
  9. this.branchEl = null;
  10. this.elementField = false;
  11. this.startOpen = function () {};
  12. this.displayIndex = 0;
  13. };
  14. DataTree.prototype.initialize = function () {
  15. var dummyEl = null,
  16. firstCol = this.table.columnManager.getFirstVisibileColumn(),
  17. options = this.table.options;
  18. this.field = options.dataTreeChildField;
  19. this.indent = options.dataTreeChildIndent;
  20. this.elementField = options.dataTreeElementColumn || (firstCol ? firstCol.field : false);
  21. if (options.dataTreeBranchElement) {
  22. if (options.dataTreeBranchElement === true) {
  23. this.branchEl = document.createElement("div");
  24. this.branchEl.classList.add("tabulator-data-tree-branch");
  25. } else {
  26. if (typeof options.dataTreeBranchElement === "string") {
  27. dummyEl = document.createElement("div");
  28. dummyEl.innerHTML = options.dataTreeBranchElement;
  29. this.branchEl = dummyEl.firstChild;
  30. } else {
  31. this.branchEl = options.dataTreeBranchElement;
  32. }
  33. }
  34. }
  35. if (options.dataTreeCollapseElement) {
  36. if (typeof options.dataTreeCollapseElement === "string") {
  37. dummyEl = document.createElement("div");
  38. dummyEl.innerHTML = options.dataTreeCollapseElement;
  39. this.collapseEl = dummyEl.firstChild;
  40. } else {
  41. this.collapseEl = options.dataTreeCollapseElement;
  42. }
  43. } else {
  44. this.collapseEl = document.createElement("div");
  45. this.collapseEl.classList.add("tabulator-data-tree-control");
  46. this.collapseEl.tabIndex = 0;
  47. this.collapseEl.innerHTML = "<div class='tabulator-data-tree-control-collapse'></div>";
  48. }
  49. if (options.dataTreeExpandElement) {
  50. if (typeof options.dataTreeExpandElement === "string") {
  51. dummyEl = document.createElement("div");
  52. dummyEl.innerHTML = options.dataTreeExpandElement;
  53. this.expandEl = dummyEl.firstChild;
  54. } else {
  55. this.expandEl = options.dataTreeExpandElement;
  56. }
  57. } else {
  58. this.expandEl = document.createElement("div");
  59. this.expandEl.classList.add("tabulator-data-tree-control");
  60. this.expandEl.tabIndex = 0;
  61. this.expandEl.innerHTML = "<div class='tabulator-data-tree-control-expand'></div>";
  62. }
  63. switch (_typeof(options.dataTreeStartExpanded)) {
  64. case "boolean":
  65. this.startOpen = function (row, index) {
  66. return options.dataTreeStartExpanded;
  67. };
  68. break;
  69. case "function":
  70. this.startOpen = options.dataTreeStartExpanded;
  71. break;
  72. default:
  73. this.startOpen = function (row, index) {
  74. return options.dataTreeStartExpanded[index];
  75. };
  76. break;
  77. }
  78. };
  79. DataTree.prototype.initializeRow = function (row) {
  80. var childArray = row.getData()[this.field];
  81. var isArray = Array.isArray(childArray);
  82. var children = isArray || !isArray && (typeof childArray === "undefined" ? "undefined" : _typeof(childArray)) === "object" && childArray !== null;
  83. if (!children && row.modules.dataTree && row.modules.dataTree.branchEl) {
  84. row.modules.dataTree.branchEl.parentNode.removeChild(row.modules.dataTree.branchEl);
  85. }
  86. if (!children && row.modules.dataTree && row.modules.dataTree.controlEl) {
  87. row.modules.dataTree.controlEl.parentNode.removeChild(row.modules.dataTree.controlEl);
  88. }
  89. row.modules.dataTree = {
  90. index: row.modules.dataTree ? row.modules.dataTree.index : 0,
  91. open: children ? row.modules.dataTree ? row.modules.dataTree.open : this.startOpen(row.getComponent(), 0) : false,
  92. controlEl: row.modules.dataTree && children ? row.modules.dataTree.controlEl : false,
  93. branchEl: row.modules.dataTree && children ? row.modules.dataTree.branchEl : false,
  94. parent: row.modules.dataTree ? row.modules.dataTree.parent : false,
  95. children: children
  96. };
  97. };
  98. DataTree.prototype.layoutRow = function (row) {
  99. var cell = this.elementField ? row.getCell(this.elementField) : row.getCells()[0],
  100. el = cell.getElement(),
  101. config = row.modules.dataTree;
  102. if (config.branchEl) {
  103. if (config.branchEl.parentNode) {
  104. config.branchEl.parentNode.removeChild(config.branchEl);
  105. }
  106. config.branchEl = false;
  107. }
  108. if (config.controlEl) {
  109. if (config.controlEl.parentNode) {
  110. config.controlEl.parentNode.removeChild(config.controlEl);
  111. }
  112. config.controlEl = false;
  113. }
  114. this.generateControlElement(row, el);
  115. row.element.classList.add("tabulator-tree-level-" + config.index);
  116. if (config.index) {
  117. if (this.branchEl) {
  118. config.branchEl = this.branchEl.cloneNode(true);
  119. el.insertBefore(config.branchEl, el.firstChild);
  120. config.branchEl.style.marginLeft = (config.branchEl.offsetWidth + config.branchEl.style.marginRight) * (config.index - 1) + config.index * this.indent + "px";
  121. } else {
  122. el.style.paddingLeft = parseInt(window.getComputedStyle(el, null).getPropertyValue('padding-left')) + config.index * this.indent + "px";
  123. }
  124. }
  125. };
  126. DataTree.prototype.generateControlElement = function (row, el) {
  127. var _this = this;
  128. var config = row.modules.dataTree,
  129. el = el || row.getCells()[0].getElement(),
  130. oldControl = config.controlEl;
  131. if (config.children !== false) {
  132. if (config.open) {
  133. config.controlEl = this.collapseEl.cloneNode(true);
  134. config.controlEl.addEventListener("click", function (e) {
  135. e.stopPropagation();
  136. _this.collapseRow(row);
  137. });
  138. } else {
  139. config.controlEl = this.expandEl.cloneNode(true);
  140. config.controlEl.addEventListener("click", function (e) {
  141. e.stopPropagation();
  142. _this.expandRow(row);
  143. });
  144. }
  145. config.controlEl.addEventListener("mousedown", function (e) {
  146. e.stopPropagation();
  147. });
  148. if (oldControl && oldControl.parentNode === el) {
  149. oldControl.parentNode.replaceChild(config.controlEl, oldControl);
  150. } else {
  151. el.insertBefore(config.controlEl, el.firstChild);
  152. }
  153. }
  154. };
  155. DataTree.prototype.setDisplayIndex = function (index) {
  156. this.displayIndex = index;
  157. };
  158. DataTree.prototype.getDisplayIndex = function () {
  159. return this.displayIndex;
  160. };
  161. DataTree.prototype.getRows = function (rows) {
  162. var _this2 = this;
  163. var output = [];
  164. rows.forEach(function (row, i) {
  165. var config, children;
  166. output.push(row);
  167. if (row instanceof Row) {
  168. config = row.modules.dataTree.children;
  169. if (!config.index && config.children !== false) {
  170. children = _this2.getChildren(row);
  171. children.forEach(function (child) {
  172. output.push(child);
  173. });
  174. }
  175. }
  176. });
  177. return output;
  178. };
  179. DataTree.prototype.getChildren = function (row) {
  180. var _this3 = this;
  181. var config = row.modules.dataTree,
  182. children = [],
  183. output = [];
  184. if (config.children !== false && config.open) {
  185. if (!Array.isArray(config.children)) {
  186. config.children = this.generateChildren(row);
  187. }
  188. if (this.table.modExists("filter")) {
  189. children = this.table.modules.filter.filter(config.children);
  190. } else {
  191. children = config.children;
  192. }
  193. if (this.table.modExists("sort")) {
  194. this.table.modules.sort.sort(children);
  195. }
  196. children.forEach(function (child) {
  197. output.push(child);
  198. var subChildren = _this3.getChildren(child);
  199. subChildren.forEach(function (sub) {
  200. output.push(sub);
  201. });
  202. });
  203. }
  204. return output;
  205. };
  206. DataTree.prototype.generateChildren = function (row) {
  207. var _this4 = this;
  208. var children = [];
  209. var childArray = row.getData()[this.field];
  210. if (!Array.isArray(childArray)) {
  211. childArray = [childArray];
  212. }
  213. childArray.forEach(function (childData) {
  214. var childRow = new Row(childData || {}, _this4.table.rowManager);
  215. childRow.modules.dataTree.index = row.modules.dataTree.index + 1;
  216. childRow.modules.dataTree.parent = row;
  217. if (childRow.modules.dataTree.children) {
  218. childRow.modules.dataTree.open = _this4.startOpen(childRow.getComponent(), childRow.modules.dataTree.index);
  219. }
  220. children.push(childRow);
  221. });
  222. return children;
  223. };
  224. DataTree.prototype.expandRow = function (row, silent) {
  225. var config = row.modules.dataTree;
  226. if (config.children !== false) {
  227. config.open = true;
  228. row.reinitialize();
  229. this.table.rowManager.refreshActiveData("tree", false, true);
  230. this.table.options.dataTreeRowExpanded(row.getComponent(), row.modules.dataTree.index);
  231. }
  232. };
  233. DataTree.prototype.collapseRow = function (row) {
  234. var config = row.modules.dataTree;
  235. if (config.children !== false) {
  236. config.open = false;
  237. row.reinitialize();
  238. this.table.rowManager.refreshActiveData("tree", false, true);
  239. this.table.options.dataTreeRowCollapsed(row.getComponent(), row.modules.dataTree.index);
  240. }
  241. };
  242. DataTree.prototype.toggleRow = function (row) {
  243. var config = row.modules.dataTree;
  244. if (config.children !== false) {
  245. if (config.open) {
  246. this.collapseRow(row);
  247. } else {
  248. this.expandRow(row);
  249. }
  250. }
  251. };
  252. DataTree.prototype.getTreeParent = function (row) {
  253. return row.modules.dataTree.parent ? row.modules.dataTree.parent.getComponent() : false;
  254. };
  255. DataTree.prototype.getFilteredTreeChildren = function (row) {
  256. var config = row.modules.dataTree,
  257. output = [],
  258. children;
  259. if (config.children) {
  260. if (!Array.isArray(config.children)) {
  261. config.children = this.generateChildren(row);
  262. }
  263. if (this.table.modExists("filter")) {
  264. children = this.table.modules.filter.filter(config.children);
  265. } else {
  266. children = config.children;
  267. }
  268. children.forEach(function (childRow) {
  269. if (childRow instanceof Row) {
  270. output.push(childRow);
  271. }
  272. });
  273. }
  274. return output;
  275. };
  276. DataTree.prototype.getTreeChildren = function (row) {
  277. var config = row.modules.dataTree,
  278. output = [];
  279. if (config.children) {
  280. if (!Array.isArray(config.children)) {
  281. config.children = this.generateChildren(row);
  282. }
  283. config.children.forEach(function (childRow) {
  284. if (childRow instanceof Row) {
  285. output.push(childRow.getComponent());
  286. }
  287. });
  288. }
  289. return output;
  290. };
  291. DataTree.prototype.checkForRestyle = function (cell) {
  292. if (!cell.row.cells.indexOf(cell)) {
  293. cell.row.reinitialize();
  294. }
  295. };
  296. DataTree.prototype.getChildField = function () {
  297. return this.field;
  298. };
  299. DataTree.prototype.redrawNeeded = function (data) {
  300. return (this.field ? typeof data[this.field] !== "undefined" : false) || (this.elementField ? typeof data[this.elementField] !== "undefined" : false);
  301. };
  302. Tabulator.prototype.registerModule("dataTree", DataTree);