Built files from Bizgaze WebServer
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. /* Tabulator v4.6.3 (c) Oliver Folkerd */
  2. //public group object
  3. var GroupComponent = function GroupComponent(group) {
  4. this._group = group;
  5. this.type = "GroupComponent";
  6. };
  7. GroupComponent.prototype.getKey = function () {
  8. return this._group.key;
  9. };
  10. GroupComponent.prototype.getField = function () {
  11. return this._group.field;
  12. };
  13. GroupComponent.prototype.getElement = function () {
  14. return this._group.element;
  15. };
  16. GroupComponent.prototype.getRows = function () {
  17. return this._group.getRows(true);
  18. };
  19. GroupComponent.prototype.getSubGroups = function () {
  20. return this._group.getSubGroups(true);
  21. };
  22. GroupComponent.prototype.getParentGroup = function () {
  23. return this._group.parent ? this._group.parent.getComponent() : false;
  24. };
  25. GroupComponent.prototype.getVisibility = function () {
  26. return this._group.visible;
  27. };
  28. GroupComponent.prototype.show = function () {
  29. this._group.show();
  30. };
  31. GroupComponent.prototype.hide = function () {
  32. this._group.hide();
  33. };
  34. GroupComponent.prototype.toggle = function () {
  35. this._group.toggleVisibility();
  36. };
  37. GroupComponent.prototype._getSelf = function () {
  38. return this._group;
  39. };
  40. GroupComponent.prototype.getTable = function () {
  41. return this._group.groupManager.table;
  42. };
  43. //////////////////////////////////////////////////
  44. //////////////// Group Functions /////////////////
  45. //////////////////////////////////////////////////
  46. var Group = function Group(groupManager, parent, level, key, field, generator, oldGroup) {
  47. this.groupManager = groupManager;
  48. this.parent = parent;
  49. this.key = key;
  50. this.level = level;
  51. this.field = field;
  52. this.hasSubGroups = level < groupManager.groupIDLookups.length - 1;
  53. this.addRow = this.hasSubGroups ? this._addRowToGroup : this._addRow;
  54. this.type = "group"; //type of element
  55. this.old = oldGroup;
  56. this.rows = [];
  57. this.groups = [];
  58. this.groupList = [];
  59. this.generator = generator;
  60. this.elementContents = false;
  61. this.height = 0;
  62. this.outerHeight = 0;
  63. this.initialized = false;
  64. this.calcs = {};
  65. this.initialized = false;
  66. this.modules = {};
  67. this.arrowElement = false;
  68. this.visible = oldGroup ? oldGroup.visible : typeof groupManager.startOpen[level] !== "undefined" ? groupManager.startOpen[level] : groupManager.startOpen[0];
  69. this.createElements();
  70. this.addBindings();
  71. this.createValueGroups();
  72. };
  73. Group.prototype.wipe = function () {
  74. if (this.groupList.length) {
  75. this.groupList.forEach(function (group) {
  76. group.wipe();
  77. });
  78. } else {
  79. this.element = false;
  80. this.arrowElement = false;
  81. this.elementContents = false;
  82. }
  83. };
  84. Group.prototype.createElements = function () {
  85. var arrow = document.createElement("div");
  86. arrow.classList.add("tabulator-arrow");
  87. this.element = document.createElement("div");
  88. this.element.classList.add("tabulator-row");
  89. this.element.classList.add("tabulator-group");
  90. this.element.classList.add("tabulator-group-level-" + this.level);
  91. this.element.setAttribute("role", "rowgroup");
  92. this.arrowElement = document.createElement("div");
  93. this.arrowElement.classList.add("tabulator-group-toggle");
  94. this.arrowElement.appendChild(arrow);
  95. //setup movable rows
  96. if (this.groupManager.table.options.movableRows !== false && this.groupManager.table.modExists("moveRow")) {
  97. this.groupManager.table.modules.moveRow.initializeGroupHeader(this);
  98. }
  99. };
  100. Group.prototype.createValueGroups = function () {
  101. var _this = this;
  102. var level = this.level + 1;
  103. if (this.groupManager.allowedValues && this.groupManager.allowedValues[level]) {
  104. this.groupManager.allowedValues[level].forEach(function (value) {
  105. _this._createGroup(value, level);
  106. });
  107. }
  108. };
  109. Group.prototype.addBindings = function () {
  110. var self = this,
  111. dblTap,
  112. tapHold,
  113. tap,
  114. toggleElement;
  115. //handle group click events
  116. if (self.groupManager.table.options.groupClick) {
  117. self.element.addEventListener("click", function (e) {
  118. self.groupManager.table.options.groupClick.call(self.groupManager.table, e, self.getComponent());
  119. });
  120. }
  121. if (self.groupManager.table.options.groupDblClick) {
  122. self.element.addEventListener("dblclick", function (e) {
  123. self.groupManager.table.options.groupDblClick.call(self.groupManager.table, e, self.getComponent());
  124. });
  125. }
  126. if (self.groupManager.table.options.groupContext) {
  127. self.element.addEventListener("contextmenu", function (e) {
  128. self.groupManager.table.options.groupContext.call(self.groupManager.table, e, self.getComponent());
  129. });
  130. }
  131. if (self.groupManager.table.options.groupTap) {
  132. tap = false;
  133. self.element.addEventListener("touchstart", function (e) {
  134. tap = true;
  135. }, { passive: true });
  136. self.element.addEventListener("touchend", function (e) {
  137. if (tap) {
  138. self.groupManager.table.options.groupTap(e, self.getComponent());
  139. }
  140. tap = false;
  141. });
  142. }
  143. if (self.groupManager.table.options.groupDblTap) {
  144. dblTap = null;
  145. self.element.addEventListener("touchend", function (e) {
  146. if (dblTap) {
  147. clearTimeout(dblTap);
  148. dblTap = null;
  149. self.groupManager.table.options.groupDblTap(e, self.getComponent());
  150. } else {
  151. dblTap = setTimeout(function () {
  152. clearTimeout(dblTap);
  153. dblTap = null;
  154. }, 300);
  155. }
  156. });
  157. }
  158. if (self.groupManager.table.options.groupTapHold) {
  159. tapHold = null;
  160. self.element.addEventListener("touchstart", function (e) {
  161. clearTimeout(tapHold);
  162. tapHold = setTimeout(function () {
  163. clearTimeout(tapHold);
  164. tapHold = null;
  165. tap = false;
  166. self.groupManager.table.options.groupTapHold(e, self.getComponent());
  167. }, 1000);
  168. }, { passive: true });
  169. self.element.addEventListener("touchend", function (e) {
  170. clearTimeout(tapHold);
  171. tapHold = null;
  172. });
  173. }
  174. if (self.groupManager.table.options.groupToggleElement) {
  175. toggleElement = self.groupManager.table.options.groupToggleElement == "arrow" ? self.arrowElement : self.element;
  176. toggleElement.addEventListener("click", function (e) {
  177. e.stopPropagation();
  178. e.stopImmediatePropagation();
  179. self.toggleVisibility();
  180. });
  181. }
  182. };
  183. Group.prototype._createGroup = function (groupID, level) {
  184. var groupKey = level + "_" + groupID;
  185. var group = new Group(this.groupManager, this, level, groupID, this.groupManager.groupIDLookups[level].field, this.groupManager.headerGenerator[level] || this.groupManager.headerGenerator[0], this.old ? this.old.groups[groupKey] : false);
  186. this.groups[groupKey] = group;
  187. this.groupList.push(group);
  188. };
  189. Group.prototype._addRowToGroup = function (row) {
  190. var level = this.level + 1;
  191. if (this.hasSubGroups) {
  192. var groupID = this.groupManager.groupIDLookups[level].func(row.getData()),
  193. groupKey = level + "_" + groupID;
  194. if (this.groupManager.allowedValues && this.groupManager.allowedValues[level]) {
  195. if (this.groups[groupKey]) {
  196. this.groups[groupKey].addRow(row);
  197. }
  198. } else {
  199. if (!this.groups[groupKey]) {
  200. this._createGroup(groupID, level);
  201. }
  202. this.groups[groupKey].addRow(row);
  203. }
  204. }
  205. };
  206. Group.prototype._addRow = function (row) {
  207. this.rows.push(row);
  208. row.modules.group = this;
  209. };
  210. Group.prototype.insertRow = function (row, to, after) {
  211. var data = this.conformRowData({});
  212. row.updateData(data);
  213. var toIndex = this.rows.indexOf(to);
  214. if (toIndex > -1) {
  215. if (after) {
  216. this.rows.splice(toIndex + 1, 0, row);
  217. } else {
  218. this.rows.splice(toIndex, 0, row);
  219. }
  220. } else {
  221. if (after) {
  222. this.rows.push(row);
  223. } else {
  224. this.rows.unshift(row);
  225. }
  226. }
  227. row.modules.group = this;
  228. this.generateGroupHeaderContents();
  229. if (this.groupManager.table.modExists("columnCalcs") && this.groupManager.table.options.columnCalcs != "table") {
  230. this.groupManager.table.modules.columnCalcs.recalcGroup(this);
  231. }
  232. this.groupManager.updateGroupRows(true);
  233. };
  234. Group.prototype.scrollHeader = function (left) {
  235. this.arrowElement.style.marginLeft = left;
  236. this.groupList.forEach(function (child) {
  237. child.scrollHeader(left);
  238. });
  239. };
  240. Group.prototype.getRowIndex = function (row) {};
  241. //update row data to match grouping contraints
  242. Group.prototype.conformRowData = function (data) {
  243. if (this.field) {
  244. data[this.field] = this.key;
  245. } else {
  246. console.warn("Data Conforming Error - Cannot conform row data to match new group as groupBy is a function");
  247. }
  248. if (this.parent) {
  249. data = this.parent.conformRowData(data);
  250. }
  251. return data;
  252. };
  253. Group.prototype.removeRow = function (row) {
  254. var index = this.rows.indexOf(row);
  255. var el = row.getElement();
  256. if (index > -1) {
  257. this.rows.splice(index, 1);
  258. }
  259. if (!this.groupManager.table.options.groupValues && !this.rows.length) {
  260. if (this.parent) {
  261. this.parent.removeGroup(this);
  262. } else {
  263. this.groupManager.removeGroup(this);
  264. }
  265. this.groupManager.updateGroupRows(true);
  266. } else {
  267. if (el.parentNode) {
  268. el.parentNode.removeChild(el);
  269. }
  270. this.generateGroupHeaderContents();
  271. if (this.groupManager.table.modExists("columnCalcs") && this.groupManager.table.options.columnCalcs != "table") {
  272. this.groupManager.table.modules.columnCalcs.recalcGroup(this);
  273. }
  274. }
  275. };
  276. Group.prototype.removeGroup = function (group) {
  277. var groupKey = group.level + "_" + group.key,
  278. index;
  279. if (this.groups[groupKey]) {
  280. delete this.groups[groupKey];
  281. index = this.groupList.indexOf(group);
  282. if (index > -1) {
  283. this.groupList.splice(index, 1);
  284. }
  285. if (!this.groupList.length) {
  286. if (this.parent) {
  287. this.parent.removeGroup(this);
  288. } else {
  289. this.groupManager.removeGroup(this);
  290. }
  291. }
  292. }
  293. };
  294. Group.prototype.getHeadersAndRows = function (noCalc) {
  295. var output = [];
  296. output.push(this);
  297. this._visSet();
  298. if (this.visible) {
  299. if (this.groupList.length) {
  300. this.groupList.forEach(function (group) {
  301. output = output.concat(group.getHeadersAndRows(noCalc));
  302. });
  303. } else {
  304. if (!noCalc && this.groupManager.table.options.columnCalcs != "table" && this.groupManager.table.modExists("columnCalcs") && this.groupManager.table.modules.columnCalcs.hasTopCalcs()) {
  305. if (this.calcs.top) {
  306. this.calcs.top.detachElement();
  307. this.calcs.top.deleteCells();
  308. }
  309. this.calcs.top = this.groupManager.table.modules.columnCalcs.generateTopRow(this.rows);
  310. output.push(this.calcs.top);
  311. }
  312. output = output.concat(this.rows);
  313. if (!noCalc && this.groupManager.table.options.columnCalcs != "table" && this.groupManager.table.modExists("columnCalcs") && this.groupManager.table.modules.columnCalcs.hasBottomCalcs()) {
  314. if (this.calcs.bottom) {
  315. this.calcs.bottom.detachElement();
  316. this.calcs.bottom.deleteCells();
  317. }
  318. this.calcs.bottom = this.groupManager.table.modules.columnCalcs.generateBottomRow(this.rows);
  319. output.push(this.calcs.bottom);
  320. }
  321. }
  322. } else {
  323. if (!this.groupList.length && this.groupManager.table.options.columnCalcs != "table") {
  324. if (this.groupManager.table.modExists("columnCalcs")) {
  325. if (!noCalc && this.groupManager.table.modules.columnCalcs.hasTopCalcs()) {
  326. if (this.calcs.top) {
  327. this.calcs.top.detachElement();
  328. this.calcs.top.deleteCells();
  329. }
  330. if (this.groupManager.table.options.groupClosedShowCalcs) {
  331. this.calcs.top = this.groupManager.table.modules.columnCalcs.generateTopRow(this.rows);
  332. output.push(this.calcs.top);
  333. }
  334. }
  335. if (!noCalc && this.groupManager.table.modules.columnCalcs.hasBottomCalcs()) {
  336. if (this.calcs.bottom) {
  337. this.calcs.bottom.detachElement();
  338. this.calcs.bottom.deleteCells();
  339. }
  340. if (this.groupManager.table.options.groupClosedShowCalcs) {
  341. this.calcs.bottom = this.groupManager.table.modules.columnCalcs.generateBottomRow(this.rows);
  342. output.push(this.calcs.bottom);
  343. }
  344. }
  345. }
  346. }
  347. }
  348. return output;
  349. };
  350. Group.prototype.getData = function (visible, transform) {
  351. var self = this,
  352. output = [];
  353. this._visSet();
  354. if (!visible || visible && this.visible) {
  355. this.rows.forEach(function (row) {
  356. output.push(row.getData(transform || "data"));
  357. });
  358. }
  359. return output;
  360. };
  361. // Group.prototype.getRows = function(){
  362. // this._visSet();
  363. // return this.visible ? this.rows : [];
  364. // };
  365. Group.prototype.getRowCount = function () {
  366. var count = 0;
  367. if (this.groupList.length) {
  368. this.groupList.forEach(function (group) {
  369. count += group.getRowCount();
  370. });
  371. } else {
  372. count = this.rows.length;
  373. }
  374. return count;
  375. };
  376. Group.prototype.toggleVisibility = function () {
  377. if (this.visible) {
  378. this.hide();
  379. } else {
  380. this.show();
  381. }
  382. };
  383. Group.prototype.hide = function () {
  384. this.visible = false;
  385. if (this.groupManager.table.rowManager.getRenderMode() == "classic" && !this.groupManager.table.options.pagination) {
  386. this.element.classList.remove("tabulator-group-visible");
  387. if (this.groupList.length) {
  388. this.groupList.forEach(function (group) {
  389. var rows = group.getHeadersAndRows();
  390. rows.forEach(function (row) {
  391. row.detachElement();
  392. });
  393. });
  394. } else {
  395. this.rows.forEach(function (row) {
  396. var rowEl = row.getElement();
  397. rowEl.parentNode.removeChild(rowEl);
  398. });
  399. }
  400. this.groupManager.table.rowManager.setDisplayRows(this.groupManager.updateGroupRows(), this.groupManager.getDisplayIndex());
  401. this.groupManager.table.rowManager.checkClassicModeGroupHeaderWidth();
  402. } else {
  403. this.groupManager.updateGroupRows(true);
  404. }
  405. this.groupManager.table.options.groupVisibilityChanged.call(this.table, this.getComponent(), false);
  406. };
  407. Group.prototype.show = function () {
  408. var self = this;
  409. self.visible = true;
  410. if (this.groupManager.table.rowManager.getRenderMode() == "classic" && !this.groupManager.table.options.pagination) {
  411. this.element.classList.add("tabulator-group-visible");
  412. var prev = self.getElement();
  413. if (this.groupList.length) {
  414. this.groupList.forEach(function (group) {
  415. var rows = group.getHeadersAndRows();
  416. rows.forEach(function (row) {
  417. var rowEl = row.getElement();
  418. prev.parentNode.insertBefore(rowEl, prev.nextSibling);
  419. row.initialize();
  420. prev = rowEl;
  421. });
  422. });
  423. } else {
  424. self.rows.forEach(function (row) {
  425. var rowEl = row.getElement();
  426. prev.parentNode.insertBefore(rowEl, prev.nextSibling);
  427. row.initialize();
  428. prev = rowEl;
  429. });
  430. }
  431. this.groupManager.table.rowManager.setDisplayRows(this.groupManager.updateGroupRows(), this.groupManager.getDisplayIndex());
  432. this.groupManager.table.rowManager.checkClassicModeGroupHeaderWidth();
  433. } else {
  434. this.groupManager.updateGroupRows(true);
  435. }
  436. this.groupManager.table.options.groupVisibilityChanged.call(this.table, this.getComponent(), true);
  437. };
  438. Group.prototype._visSet = function () {
  439. var data = [];
  440. if (typeof this.visible == "function") {
  441. this.rows.forEach(function (row) {
  442. data.push(row.getData());
  443. });
  444. this.visible = this.visible(this.key, this.getRowCount(), data, this.getComponent());
  445. }
  446. };
  447. Group.prototype.getRowGroup = function (row) {
  448. var match = false;
  449. if (this.groupList.length) {
  450. this.groupList.forEach(function (group) {
  451. var result = group.getRowGroup(row);
  452. if (result) {
  453. match = result;
  454. }
  455. });
  456. } else {
  457. if (this.rows.find(function (item) {
  458. return item === row;
  459. })) {
  460. match = this;
  461. }
  462. }
  463. return match;
  464. };
  465. Group.prototype.getSubGroups = function (component) {
  466. var output = [];
  467. this.groupList.forEach(function (child) {
  468. output.push(component ? child.getComponent() : child);
  469. });
  470. return output;
  471. };
  472. Group.prototype.getRows = function (compoment) {
  473. var output = [];
  474. this.rows.forEach(function (row) {
  475. output.push(compoment ? row.getComponent() : row);
  476. });
  477. return output;
  478. };
  479. Group.prototype.generateGroupHeaderContents = function () {
  480. var data = [];
  481. this.rows.forEach(function (row) {
  482. data.push(row.getData());
  483. });
  484. this.elementContents = this.generator(this.key, this.getRowCount(), data, this.getComponent());
  485. while (this.element.firstChild) {
  486. this.element.removeChild(this.element.firstChild);
  487. }if (typeof this.elementContents === "string") {
  488. this.element.innerHTML = this.elementContents;
  489. } else {
  490. this.element.appendChild(this.elementContents);
  491. }
  492. this.element.insertBefore(this.arrowElement, this.element.firstChild);
  493. };
  494. ////////////// Standard Row Functions //////////////
  495. Group.prototype.getElement = function () {
  496. this.addBindingsd = false;
  497. this._visSet();
  498. if (this.visible) {
  499. this.element.classList.add("tabulator-group-visible");
  500. } else {
  501. this.element.classList.remove("tabulator-group-visible");
  502. }
  503. for (var i = 0; i < this.element.childNodes.length; ++i) {
  504. this.element.childNodes[i].parentNode.removeChild(this.element.childNodes[i]);
  505. }
  506. this.generateGroupHeaderContents();
  507. // this.addBindings();
  508. return this.element;
  509. };
  510. Group.prototype.detachElement = function () {
  511. if (this.element && this.element.parentNode) {
  512. this.element.parentNode.removeChild(this.element);
  513. }
  514. };
  515. //normalize the height of elements in the row
  516. Group.prototype.normalizeHeight = function () {
  517. this.setHeight(this.element.clientHeight);
  518. };
  519. Group.prototype.initialize = function (force) {
  520. if (!this.initialized || force) {
  521. this.normalizeHeight();
  522. this.initialized = true;
  523. }
  524. };
  525. Group.prototype.reinitialize = function () {
  526. this.initialized = false;
  527. this.height = 0;
  528. if (Tabulator.prototype.helpers.elVisible(this.element)) {
  529. this.initialize(true);
  530. }
  531. };
  532. Group.prototype.setHeight = function (height) {
  533. if (this.height != height) {
  534. this.height = height;
  535. this.outerHeight = this.element.offsetHeight;
  536. }
  537. };
  538. //return rows outer height
  539. Group.prototype.getHeight = function () {
  540. return this.outerHeight;
  541. };
  542. Group.prototype.getGroup = function () {
  543. return this;
  544. };
  545. Group.prototype.reinitializeHeight = function () {};
  546. Group.prototype.calcHeight = function () {};
  547. Group.prototype.setCellHeight = function () {};
  548. Group.prototype.clearCellHeight = function () {};
  549. //////////////// Object Generation /////////////////
  550. Group.prototype.getComponent = function () {
  551. return new GroupComponent(this);
  552. };
  553. //////////////////////////////////////////////////
  554. ////////////// Group Row Extension ///////////////
  555. //////////////////////////////////////////////////
  556. var GroupRows = function GroupRows(table) {
  557. this.table = table; //hold Tabulator object
  558. this.groupIDLookups = false; //enable table grouping and set field to group by
  559. this.startOpen = [function () {
  560. return false;
  561. }]; //starting state of group
  562. this.headerGenerator = [function () {
  563. return "";
  564. }];
  565. this.groupList = []; //ordered list of groups
  566. this.allowedValues = false;
  567. this.groups = {}; //hold row groups
  568. this.displayIndex = 0; //index in display pipeline
  569. };
  570. //initialize group configuration
  571. GroupRows.prototype.initialize = function () {
  572. var self = this,
  573. groupBy = self.table.options.groupBy,
  574. startOpen = self.table.options.groupStartOpen,
  575. groupHeader = self.table.options.groupHeader;
  576. this.allowedValues = self.table.options.groupValues;
  577. if (Array.isArray(groupBy) && Array.isArray(groupHeader) && groupBy.length > groupHeader.length) {
  578. console.warn("Error creating group headers, groupHeader array is shorter than groupBy array");
  579. }
  580. self.headerGenerator = [function () {
  581. return "";
  582. }];
  583. this.startOpen = [function () {
  584. return false;
  585. }]; //starting state of group
  586. self.table.modules.localize.bind("groups|item", function (langValue, lang) {
  587. self.headerGenerator[0] = function (value, count, data) {
  588. //header layout function
  589. return (typeof value === "undefined" ? "" : value) + "<span>(" + count + " " + (count === 1 ? langValue : lang.groups.items) + ")</span>";
  590. };
  591. });
  592. this.groupIDLookups = [];
  593. if (Array.isArray(groupBy) || groupBy) {
  594. if (this.table.modExists("columnCalcs") && this.table.options.columnCalcs != "table" && this.table.options.columnCalcs != "both") {
  595. this.table.modules.columnCalcs.removeCalcs();
  596. }
  597. } else {
  598. if (this.table.modExists("columnCalcs") && this.table.options.columnCalcs != "group") {
  599. var cols = this.table.columnManager.getRealColumns();
  600. cols.forEach(function (col) {
  601. if (col.definition.topCalc) {
  602. self.table.modules.columnCalcs.initializeTopRow();
  603. }
  604. if (col.definition.bottomCalc) {
  605. self.table.modules.columnCalcs.initializeBottomRow();
  606. }
  607. });
  608. }
  609. }
  610. if (!Array.isArray(groupBy)) {
  611. groupBy = [groupBy];
  612. }
  613. groupBy.forEach(function (group, i) {
  614. var lookupFunc, column;
  615. if (typeof group == "function") {
  616. lookupFunc = group;
  617. } else {
  618. column = self.table.columnManager.getColumnByField(group);
  619. if (column) {
  620. lookupFunc = function lookupFunc(data) {
  621. return column.getFieldValue(data);
  622. };
  623. } else {
  624. lookupFunc = function lookupFunc(data) {
  625. return data[group];
  626. };
  627. }
  628. }
  629. self.groupIDLookups.push({
  630. field: typeof group === "function" ? false : group,
  631. func: lookupFunc,
  632. values: self.allowedValues ? self.allowedValues[i] : false
  633. });
  634. });
  635. if (startOpen) {
  636. if (!Array.isArray(startOpen)) {
  637. startOpen = [startOpen];
  638. }
  639. startOpen.forEach(function (level) {
  640. level = typeof level == "function" ? level : function () {
  641. return true;
  642. };
  643. });
  644. self.startOpen = startOpen;
  645. }
  646. if (groupHeader) {
  647. self.headerGenerator = Array.isArray(groupHeader) ? groupHeader : [groupHeader];
  648. }
  649. this.initialized = true;
  650. };
  651. GroupRows.prototype.setDisplayIndex = function (index) {
  652. this.displayIndex = index;
  653. };
  654. GroupRows.prototype.getDisplayIndex = function () {
  655. return this.displayIndex;
  656. };
  657. //return appropriate rows with group headers
  658. GroupRows.prototype.getRows = function (rows) {
  659. if (this.groupIDLookups.length) {
  660. this.table.options.dataGrouping.call(this.table);
  661. this.generateGroups(rows);
  662. if (this.table.options.dataGrouped) {
  663. this.table.options.dataGrouped.call(this.table, this.getGroups(true));
  664. }
  665. return this.updateGroupRows();
  666. } else {
  667. return rows.slice(0);
  668. }
  669. };
  670. GroupRows.prototype.getGroups = function (compoment) {
  671. var groupComponents = [];
  672. this.groupList.forEach(function (group) {
  673. groupComponents.push(compoment ? group.getComponent() : group);
  674. });
  675. return groupComponents;
  676. };
  677. GroupRows.prototype.getChildGroups = function (group) {
  678. var _this2 = this;
  679. var groupComponents = [];
  680. if (!group) {
  681. group = this;
  682. }
  683. group.groupList.forEach(function (child) {
  684. if (child.groupList.length) {
  685. groupComponents = groupComponents.concat(_this2.getChildGroups(child));
  686. } else {
  687. groupComponents.push(child);
  688. }
  689. });
  690. return groupComponents;
  691. };
  692. GroupRows.prototype.wipe = function () {
  693. this.groupList.forEach(function (group) {
  694. group.wipe();
  695. });
  696. };
  697. GroupRows.prototype.pullGroupListData = function (groupList) {
  698. var self = this;
  699. var groupListData = [];
  700. groupList.forEach(function (group) {
  701. var groupHeader = {};
  702. groupHeader.level = 0;
  703. groupHeader.rowCount = 0;
  704. groupHeader.headerContent = "";
  705. var childData = [];
  706. if (group.hasSubGroups) {
  707. childData = self.pullGroupListData(group.groupList);
  708. groupHeader.level = group.level;
  709. groupHeader.rowCount = childData.length - group.groupList.length; // data length minus number of sub-headers
  710. groupHeader.headerContent = group.generator(group.key, groupHeader.rowCount, group.rows, group);
  711. groupListData.push(groupHeader);
  712. groupListData = groupListData.concat(childData);
  713. } else {
  714. groupHeader.level = group.level;
  715. groupHeader.headerContent = group.generator(group.key, group.rows.length, group.rows, group);
  716. groupHeader.rowCount = group.getRows().length;
  717. groupListData.push(groupHeader);
  718. group.getRows().forEach(function (row) {
  719. groupListData.push(row.getData("data"));
  720. });
  721. }
  722. });
  723. return groupListData;
  724. };
  725. GroupRows.prototype.getGroupedData = function () {
  726. return this.pullGroupListData(this.groupList);
  727. };
  728. GroupRows.prototype.getRowGroup = function (row) {
  729. var match = false;
  730. this.groupList.forEach(function (group) {
  731. var result = group.getRowGroup(row);
  732. if (result) {
  733. match = result;
  734. }
  735. });
  736. return match;
  737. };
  738. GroupRows.prototype.countGroups = function () {
  739. return this.groupList.length;
  740. };
  741. GroupRows.prototype.generateGroups = function (rows) {
  742. var self = this,
  743. oldGroups = self.groups;
  744. self.groups = {};
  745. self.groupList = [];
  746. if (this.allowedValues && this.allowedValues[0]) {
  747. this.allowedValues[0].forEach(function (value) {
  748. self.createGroup(value, 0, oldGroups);
  749. });
  750. rows.forEach(function (row) {
  751. self.assignRowToExistingGroup(row, oldGroups);
  752. });
  753. } else {
  754. rows.forEach(function (row) {
  755. self.assignRowToGroup(row, oldGroups);
  756. });
  757. }
  758. };
  759. GroupRows.prototype.createGroup = function (groupID, level, oldGroups) {
  760. var groupKey = level + "_" + groupID,
  761. group;
  762. oldGroups = oldGroups || [];
  763. group = new Group(this, false, level, groupID, this.groupIDLookups[0].field, this.headerGenerator[0], oldGroups[groupKey]);
  764. this.groups[groupKey] = group;
  765. this.groupList.push(group);
  766. };
  767. // GroupRows.prototype.assignRowToGroup = function(row, oldGroups){
  768. // var groupID = this.groupIDLookups[0].func(row.getData()),
  769. // groupKey = "0_" + groupID;
  770. // if(!this.groups[groupKey]){
  771. // this.createGroup(groupID, 0, oldGroups);
  772. // }
  773. // this.groups[groupKey].addRow(row);
  774. // };
  775. GroupRows.prototype.assignRowToExistingGroup = function (row, oldGroups) {
  776. var groupID = this.groupIDLookups[0].func(row.getData()),
  777. groupKey = "0_" + groupID;
  778. if (this.groups[groupKey]) {
  779. this.groups[groupKey].addRow(row);
  780. }
  781. };
  782. GroupRows.prototype.assignRowToGroup = function (row, oldGroups) {
  783. var groupID = this.groupIDLookups[0].func(row.getData()),
  784. newGroupNeeded = !this.groups["0_" + groupID];
  785. if (newGroupNeeded) {
  786. this.createGroup(groupID, 0, oldGroups);
  787. }
  788. this.groups["0_" + groupID].addRow(row);
  789. return !newGroupNeeded;
  790. };
  791. GroupRows.prototype.updateGroupRows = function (force) {
  792. var self = this,
  793. output = [],
  794. oldRowCount;
  795. self.groupList.forEach(function (group) {
  796. output = output.concat(group.getHeadersAndRows());
  797. });
  798. //force update of table display
  799. if (force) {
  800. var displayIndex = self.table.rowManager.setDisplayRows(output, this.getDisplayIndex());
  801. if (displayIndex !== true) {
  802. this.setDisplayIndex(displayIndex);
  803. }
  804. self.table.rowManager.refreshActiveData("group", true, true);
  805. }
  806. return output;
  807. };
  808. GroupRows.prototype.scrollHeaders = function (left) {
  809. left = left + "px";
  810. this.groupList.forEach(function (group) {
  811. group.scrollHeader(left);
  812. });
  813. };
  814. GroupRows.prototype.removeGroup = function (group) {
  815. var groupKey = group.level + "_" + group.key,
  816. index;
  817. if (this.groups[groupKey]) {
  818. delete this.groups[groupKey];
  819. index = this.groupList.indexOf(group);
  820. if (index > -1) {
  821. this.groupList.splice(index, 1);
  822. }
  823. }
  824. };
  825. Tabulator.prototype.registerModule("groupRows", GroupRows);