ui.pivot_grid.area_item.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /**
  2. * DevExtreme (ui/pivot_grid/ui.pivot_grid.area_item.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var _renderer = require("../../core/renderer");
  11. var _renderer2 = _interopRequireDefault(_renderer);
  12. var _class = require("../../core/class");
  13. var _class2 = _interopRequireDefault(_class);
  14. var _dom = require("../../core/utils/dom");
  15. var _extend = require("../../core/utils/extend");
  16. var _type = require("../../core/utils/type");
  17. function _interopRequireDefault(obj) {
  18. return obj && obj.__esModule ? obj : {
  19. "default": obj
  20. }
  21. }
  22. var PIVOTGRID_EXPAND_CLASS = "dx-expand";
  23. var getRealElementWidth = function(element) {
  24. var width = 0;
  25. var offsetWidth = element.offsetWidth;
  26. if (element.getBoundingClientRect) {
  27. var clientRect = element.getBoundingClientRect();
  28. width = clientRect.width;
  29. if (!width) {
  30. width = clientRect.right - clientRect.left
  31. }
  32. if (width <= offsetWidth - 1) {
  33. width = offsetWidth
  34. }
  35. }
  36. return width > 0 ? width : offsetWidth
  37. };
  38. function getFakeTableOffset(scrollPos, elementOffset, tableSize, viewPortSize) {
  39. var offset = 0;
  40. var halfTableCount = 0;
  41. var halfTableSize = tableSize / 2;
  42. if (scrollPos + viewPortSize - (elementOffset + tableSize) > 1) {
  43. if (scrollPos >= elementOffset + tableSize + halfTableSize) {
  44. halfTableCount = parseInt((scrollPos - (elementOffset + tableSize)) / halfTableSize, 10)
  45. }
  46. offset = elementOffset + tableSize + halfTableSize * halfTableCount
  47. } else {
  48. if (scrollPos < elementOffset) {
  49. if (scrollPos <= elementOffset - halfTableSize) {
  50. halfTableCount = parseInt((scrollPos - (elementOffset - halfTableSize)) / halfTableSize, 10)
  51. }
  52. offset = elementOffset - (tableSize - halfTableSize * halfTableCount)
  53. } else {
  54. offset = elementOffset
  55. }
  56. }
  57. return offset
  58. }
  59. exports.AreaItem = _class2.default.inherit({
  60. _getRowElement: function(index) {
  61. var that = this;
  62. if (that._tableElement && that._tableElement.length > 0) {
  63. return that._tableElement[0].rows[index]
  64. }
  65. return null
  66. },
  67. _createGroupElement: function() {
  68. return (0, _renderer2.default)("<div>")
  69. },
  70. _createTableElement: function() {
  71. return (0, _renderer2.default)("<table>")
  72. },
  73. _getCellText: function(cell, encodeHtml) {
  74. var cellText = cell.isWhiteSpace ? "&nbsp" : cell.text || "&nbsp";
  75. if (encodeHtml && (cellText.indexOf("<") !== -1 || cellText.indexOf(">") !== -1)) {
  76. cellText = (0, _renderer2.default)("<div>").text(cellText).html()
  77. }
  78. return cellText
  79. },
  80. _getRowClassNames: function() {},
  81. _applyCustomStyles: function(options) {
  82. if (options.cell.width) {
  83. options.cssArray.push("min-width:" + options.cell.width + "px")
  84. }
  85. if (options.cell.sorted) {
  86. options.classArray.push("dx-pivotgrid-sorted")
  87. }
  88. },
  89. _getMainElementMarkup: function() {
  90. return "<tbody>"
  91. },
  92. _getCloseMainElementMarkup: function() {
  93. return "</tbody>"
  94. },
  95. _renderTableContent: function(tableElement, data) {
  96. var that = this;
  97. var rowsCount = data.length;
  98. var row;
  99. var cell;
  100. var i;
  101. var j;
  102. var rowElement;
  103. var cellElement;
  104. var cellText;
  105. var rtlEnabled = that.option("rtlEnabled");
  106. var markupArray = [];
  107. var encodeHtml = that.option("encodeHtml");
  108. var rowClassNames;
  109. var colspan = "colspan='";
  110. var rowspan = "rowspan='";
  111. tableElement.data("area", that._getAreaName());
  112. tableElement.data("data", data);
  113. tableElement.css("width", "");
  114. markupArray.push(that._getMainElementMarkup());
  115. for (i = 0; i < rowsCount; i++) {
  116. row = data[i];
  117. var columnMarkupArray = [];
  118. rowClassNames = [];
  119. markupArray.push("<tr ");
  120. for (j = 0; j < row.length; j++) {
  121. cell = row[j];
  122. this._getRowClassNames(i, cell, rowClassNames);
  123. columnMarkupArray.push("<td ");
  124. if (cell) {
  125. cell.rowspan && columnMarkupArray.push(rowspan + (cell.rowspan || 1) + "'");
  126. cell.colspan && columnMarkupArray.push(colspan + (cell.colspan || 1) + "'");
  127. var styleOptions = {
  128. cellElement: cellElement,
  129. cell: cell,
  130. cellsCount: row.length,
  131. cellIndex: j,
  132. rowElement: rowElement,
  133. rowIndex: i,
  134. rowsCount: rowsCount,
  135. rtlEnabled: rtlEnabled,
  136. classArray: [],
  137. cssArray: []
  138. };
  139. that._applyCustomStyles(styleOptions);
  140. if (styleOptions.cssArray.length) {
  141. columnMarkupArray.push("style='");
  142. columnMarkupArray.push(styleOptions.cssArray.join(";"));
  143. columnMarkupArray.push("'")
  144. }
  145. if (styleOptions.classArray.length) {
  146. columnMarkupArray.push("class='");
  147. columnMarkupArray.push(styleOptions.classArray.join(" "));
  148. columnMarkupArray.push("'")
  149. }
  150. columnMarkupArray.push(">");
  151. if ((0, _type.isDefined)(cell.expanded)) {
  152. columnMarkupArray.push("<div class='dx-expand-icon-container'><span class='" + PIVOTGRID_EXPAND_CLASS + "'></span></div>")
  153. }
  154. cellText = this._getCellText(cell, encodeHtml)
  155. } else {
  156. cellText = ""
  157. }
  158. columnMarkupArray.push("<span ");
  159. if ((0, _type.isDefined)(cell.wordWrapEnabled)) {
  160. columnMarkupArray.push("style='white-space:", cell.wordWrapEnabled ? "normal" : "nowrap", ";'")
  161. }
  162. columnMarkupArray.push(">" + cellText + "</span>");
  163. if (cell.sorted) {
  164. columnMarkupArray.push("<span class='dx-icon-sorted'></span>")
  165. }
  166. columnMarkupArray.push("</td>")
  167. }
  168. if (rowClassNames.length) {
  169. markupArray.push("class='");
  170. markupArray.push(rowClassNames.join(" "));
  171. markupArray.push("'")
  172. }
  173. markupArray.push(">");
  174. markupArray.push(columnMarkupArray.join(""));
  175. markupArray.push("</tr>")
  176. }
  177. markupArray.push(this._getCloseMainElementMarkup());
  178. tableElement.append(markupArray.join(""));
  179. this._triggerOnCellPrepared(tableElement, data)
  180. },
  181. _triggerOnCellPrepared: function(tableElement, data) {
  182. var that = this;
  183. var rowElements = tableElement.find("tr");
  184. var areaName = that._getAreaName();
  185. var onCellPrepared = that.option("onCellPrepared");
  186. var hasEvent = that.component.hasEvent("cellPrepared");
  187. var rowElement;
  188. var $cellElement;
  189. var onCellPreparedArgs;
  190. var defaultActionArgs = this.component._defaultActionArgs();
  191. var row;
  192. var cell;
  193. var rowIndex;
  194. var columnIndex;
  195. if (onCellPrepared || hasEvent) {
  196. for (rowIndex = 0; rowIndex < data.length; rowIndex++) {
  197. row = data[rowIndex];
  198. rowElement = rowElements.eq(rowIndex);
  199. for (columnIndex = 0; columnIndex < row.length; columnIndex++) {
  200. cell = row[columnIndex];
  201. $cellElement = rowElement.children().eq(columnIndex);
  202. onCellPreparedArgs = {
  203. area: areaName,
  204. rowIndex: rowIndex,
  205. columnIndex: columnIndex,
  206. cellElement: (0, _dom.getPublicElement)($cellElement),
  207. cell: cell
  208. };
  209. if (hasEvent) {
  210. that.component._trigger("onCellPrepared", onCellPreparedArgs)
  211. } else {
  212. onCellPrepared((0, _extend.extend)(onCellPreparedArgs, defaultActionArgs))
  213. }
  214. }
  215. }
  216. }
  217. },
  218. _getRowHeight: function(index) {
  219. var row = this._getRowElement(index);
  220. var height = 0;
  221. var offsetHeight = row.offsetHeight;
  222. if (row && row.lastChild) {
  223. if (row.getBoundingClientRect) {
  224. var clientRect = row.getBoundingClientRect();
  225. height = clientRect.height;
  226. if (height <= offsetHeight - 1) {
  227. height = offsetHeight
  228. }
  229. }
  230. return height > 0 ? height : offsetHeight
  231. }
  232. return 0
  233. },
  234. _setRowHeight: function(index, value) {
  235. var row = this._getRowElement(index);
  236. if (row) {
  237. row.style.height = value + "px"
  238. }
  239. },
  240. ctor: function(component) {
  241. this.component = component
  242. },
  243. option: function() {
  244. return this.component.option.apply(this.component, arguments)
  245. },
  246. getRowsLength: function() {
  247. var that = this;
  248. if (that._tableElement && that._tableElement.length > 0) {
  249. return that._tableElement[0].rows.length
  250. }
  251. return 0
  252. },
  253. getRowsHeight: function() {
  254. var that = this;
  255. var result = [];
  256. var rowsLength = that.getRowsLength();
  257. var i;
  258. for (i = 0; i < rowsLength; i++) {
  259. result.push(that._getRowHeight(i))
  260. }
  261. return result
  262. },
  263. setRowsHeight: function(values) {
  264. var that = this;
  265. var totalHeight = 0;
  266. var valuesLength = values.length;
  267. var i;
  268. for (i = 0; i < valuesLength; i++) {
  269. totalHeight += values[i];
  270. that._setRowHeight(i, values[i])
  271. }
  272. this._tableHeight = totalHeight;
  273. this._tableElement[0].style.height = totalHeight + "px"
  274. },
  275. getColumnsWidth: function() {
  276. var rowsLength = this.getRowsLength();
  277. var rowIndex;
  278. var row;
  279. var i;
  280. var columnIndex;
  281. var processedCells = [];
  282. var result = [];
  283. var fillCells = function(cells, rowIndex, columnIndex, rowSpan, colSpan) {
  284. var rowOffset;
  285. var columnOffset;
  286. for (rowOffset = 0; rowOffset < rowSpan; rowOffset++) {
  287. for (columnOffset = 0; columnOffset < colSpan; columnOffset++) {
  288. cells[rowIndex + rowOffset] = cells[rowIndex + rowOffset] || [];
  289. cells[rowIndex + rowOffset][columnIndex + columnOffset] = true
  290. }
  291. }
  292. };
  293. if (rowsLength) {
  294. for (rowIndex = 0; rowIndex < rowsLength; rowIndex++) {
  295. processedCells[rowIndex] = processedCells[rowIndex] || [];
  296. row = this._getRowElement(rowIndex);
  297. for (i = 0; i < row.cells.length; i++) {
  298. for (columnIndex = 0; processedCells[rowIndex][columnIndex]; columnIndex++) {}
  299. fillCells(processedCells, rowIndex, columnIndex, row.cells[i].rowSpan, row.cells[i].colSpan);
  300. if (1 === row.cells[i].colSpan) {
  301. result[columnIndex] = result[columnIndex] || getRealElementWidth(row.cells[i])
  302. }
  303. }
  304. }
  305. }
  306. return result
  307. },
  308. setColumnsWidth: function(values) {
  309. var i;
  310. var totalWidth = 0;
  311. var tableElement = this._tableElement[0];
  312. var colgroupElementHTML = "";
  313. var columnsCount = this.getColumnsCount();
  314. var columnWidth = [];
  315. for (i = 0; i < columnsCount; i++) {
  316. columnWidth.push(values[i] || 0)
  317. }
  318. for (i = columnsCount; i < values.length && values; i++) {
  319. columnWidth[columnsCount - 1] += values[i]
  320. }
  321. for (i = 0; i < columnsCount; i++) {
  322. totalWidth += columnWidth[i];
  323. colgroupElementHTML += '<col style="width: ' + columnWidth[i] + 'px">'
  324. }
  325. this._colgroupElement.html(colgroupElementHTML);
  326. this._tableWidth = totalWidth - this._groupWidth > .01 ? Math.ceil(totalWidth) : totalWidth;
  327. tableElement.style.width = this._tableWidth + "px";
  328. tableElement.style.tableLayout = "fixed"
  329. },
  330. resetColumnsWidth: function() {
  331. this._colgroupElement.find("col").width("auto");
  332. this._tableElement.css({
  333. width: "",
  334. tableLayout: ""
  335. })
  336. },
  337. groupWidth: function(value) {
  338. if (void 0 === value) {
  339. return this._groupElement.width()
  340. } else {
  341. if (value >= 0) {
  342. this._groupWidth = value;
  343. return this._groupElement[0].style.width = value + "px"
  344. } else {
  345. return this._groupElement[0].style.width = value
  346. }
  347. }
  348. },
  349. groupHeight: function(value) {
  350. if (void 0 === value) {
  351. return this._groupElement.height()
  352. }
  353. this._groupHeight = null;
  354. if (value >= 0) {
  355. this._groupHeight = value;
  356. this._groupElement[0].style.height = value + "px"
  357. } else {
  358. this._groupElement[0].style.height = value
  359. }
  360. },
  361. groupElement: function() {
  362. return this._groupElement
  363. },
  364. tableElement: function() {
  365. return this._tableElement
  366. },
  367. element: function() {
  368. return this._rootElement
  369. },
  370. headElement: function() {
  371. return this._tableElement.find("thead")
  372. },
  373. _setTableCss: function(styles) {
  374. if (this.option("rtlEnabled")) {
  375. styles.right = styles.left;
  376. delete styles.left
  377. }
  378. this.tableElement().css(styles)
  379. },
  380. setVirtualContentParams: function(params) {
  381. this._virtualContent.css({
  382. width: params.width,
  383. height: params.height
  384. });
  385. this.groupElement().addClass("dx-virtual-mode")
  386. },
  387. disableVirtualMode: function() {
  388. this.groupElement().removeClass("dx-virtual-mode")
  389. },
  390. _renderVirtualContent: function() {
  391. var that = this;
  392. if (!that._virtualContent && "virtual" === that.option("scrolling.mode")) {
  393. that._virtualContent = (0, _renderer2.default)("<div>").addClass("dx-virtual-content").insertBefore(that._tableElement)
  394. }
  395. },
  396. reset: function() {
  397. var that = this;
  398. var tableElement = that._tableElement[0];
  399. that._fakeTable && that._fakeTable.detach();
  400. that._fakeTable = null;
  401. that.disableVirtualMode();
  402. that.groupWidth("100%");
  403. that.groupHeight("auto");
  404. that.resetColumnsWidth();
  405. if (tableElement) {
  406. for (var i = 0; i < tableElement.rows.length; i++) {
  407. tableElement.rows[i].style.height = ""
  408. }
  409. tableElement.style.height = "";
  410. tableElement.style.width = "100%"
  411. }
  412. },
  413. _updateFakeTableVisibility: function() {
  414. var that = this;
  415. var tableElement = that.tableElement()[0];
  416. var horizontalOffsetName = that.option("rtlEnabled") ? "right" : "left";
  417. var fakeTableElement = that._fakeTable[0];
  418. if (tableElement.style.top === fakeTableElement.style.top && fakeTableElement.style[horizontalOffsetName] === tableElement.style[horizontalOffsetName]) {
  419. that._fakeTable.addClass("dx-hidden")
  420. } else {
  421. that._fakeTable.removeClass("dx-hidden")
  422. }
  423. },
  424. _moveFakeTableHorizontally: function(scrollPos) {
  425. var that = this;
  426. var rtlEnabled = that.option("rtlEnabled");
  427. var offsetStyleName = rtlEnabled ? "right" : "left";
  428. var tableElementOffset = parseFloat(that.tableElement()[0].style[offsetStyleName]);
  429. var offset = getFakeTableOffset(scrollPos, tableElementOffset, that._tableWidth, that._groupWidth);
  430. if (parseFloat(that._fakeTable[0].style[offsetStyleName]) !== offset) {
  431. that._fakeTable[0].style[offsetStyleName] = offset + "px"
  432. }
  433. },
  434. _moveFakeTableTop: function(scrollPos) {
  435. var that = this;
  436. var tableElementOffsetTop = parseFloat(that.tableElement()[0].style.top);
  437. var offsetTop = getFakeTableOffset(scrollPos, tableElementOffsetTop, that._tableHeight, that._groupHeight);
  438. if (parseFloat(that._fakeTable[0].style.top) !== offsetTop) {
  439. that._fakeTable[0].style.top = offsetTop + "px"
  440. }
  441. },
  442. _moveFakeTable: function() {
  443. this._updateFakeTableVisibility()
  444. },
  445. _createFakeTable: function() {
  446. var that = this;
  447. if (!that._fakeTable) {
  448. that._fakeTable = that.tableElement().clone().addClass("dx-pivot-grid-fake-table").appendTo(that._virtualContent)
  449. }
  450. },
  451. render: function(rootElement, data) {
  452. var that = this;
  453. if (that._tableElement) {
  454. try {
  455. that._tableElement[0].innerHTML = ""
  456. } catch (e) {
  457. that._tableElement.empty()
  458. }
  459. that._tableElement.attr("style", "")
  460. } else {
  461. that._groupElement = that._createGroupElement();
  462. that._tableElement = that._createTableElement();
  463. that._tableElement.appendTo(that._groupElement);
  464. that._groupElement.appendTo(rootElement);
  465. that._rootElement = rootElement
  466. }
  467. that._colgroupElement = (0, _renderer2.default)("<colgroup>").appendTo(that._tableElement);
  468. that._renderTableContent(that._tableElement, data);
  469. that._renderVirtualContent()
  470. },
  471. _getScrollable: function() {
  472. return this.groupElement().data("dxScrollable")
  473. },
  474. on: function(eventName, handler) {
  475. var that = this;
  476. var scrollable = that._getScrollable();
  477. if (scrollable) {
  478. scrollable.on(eventName, function(e) {
  479. if (that.option("rtlEnabled") && (0, _type.isDefined)(e.scrollOffset.left)) {
  480. e.scrollOffset.left = scrollable.$content().width() - scrollable._container().width() - e.scrollOffset.left
  481. }
  482. handler(e)
  483. })
  484. }
  485. return this
  486. },
  487. off: function(eventName) {
  488. var scrollable = this._getScrollable();
  489. if (scrollable) {
  490. scrollable.off(eventName)
  491. }
  492. return this
  493. },
  494. scrollTo: function(pos) {
  495. var scrollable = this._getScrollable();
  496. var scrollablePos = pos;
  497. if (scrollable) {
  498. if (this.option("rtlEnabled")) {
  499. if ("column" === this._getAreaName()) {
  500. scrollablePos = scrollable.$content().width() - scrollable._container().width() - pos
  501. } else {
  502. if ("data" === this._getAreaName()) {
  503. scrollablePos = {
  504. x: scrollable.$content().width() - scrollable._container().width() - pos.x,
  505. y: pos.y
  506. }
  507. }
  508. }
  509. }
  510. scrollable.scrollTo(scrollablePos);
  511. if (this._virtualContent) {
  512. this._createFakeTable();
  513. this._moveFakeTable(pos)
  514. }
  515. }
  516. },
  517. updateScrollable: function() {
  518. var scrollable = this._getScrollable();
  519. if (scrollable) {
  520. return scrollable.update()
  521. }
  522. },
  523. getColumnsCount: function() {
  524. var columnCount = 0;
  525. var row = this._getRowElement(0);
  526. var cells;
  527. if (row) {
  528. cells = row.cells;
  529. for (var i = 0, len = cells.length; i < len; ++i) {
  530. columnCount += cells[i].colSpan
  531. }
  532. }
  533. return columnCount
  534. },
  535. getData: function() {
  536. var tableElement = this._tableElement;
  537. return tableElement ? tableElement.data("data") : []
  538. }
  539. });