responsive_box.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /**
  2. * DevExtreme (ui/responsive_box.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 $ = require("../core/renderer");
  11. var eventsEngine = require("../events/core/events_engine");
  12. var commonUtils = require("../core/utils/common");
  13. var typeUtils = require("../core/utils/type");
  14. var errors = require("./widget/ui.errors");
  15. var windowUtils = require("../core/utils/window");
  16. var window = windowUtils.getWindow();
  17. var iteratorUtils = require("../core/utils/iterator");
  18. var extend = require("../core/utils/extend").extend;
  19. var registerComponent = require("../core/component_registrator");
  20. var Box = require("./box");
  21. var CollectionWidget = require("./collection/ui.collection_widget.edit");
  22. var RESPONSIVE_BOX_CLASS = "dx-responsivebox";
  23. var SCREEN_SIZE_CLASS_PREFIX = RESPONSIVE_BOX_CLASS + "-screen-";
  24. var BOX_ITEM_CLASS = "dx-box-item";
  25. var BOX_ITEM_DATA_KEY = "dxBoxItemData";
  26. var HD_SCREEN_WIDTH = 1920;
  27. var ResponsiveBox = CollectionWidget.inherit({
  28. _getDefaultOptions: function() {
  29. return extend(this.callBase(), {
  30. rows: [],
  31. cols: [],
  32. screenByWidth: null,
  33. singleColumnScreen: "",
  34. height: "100%",
  35. width: "100%",
  36. activeStateEnabled: false,
  37. focusStateEnabled: false,
  38. onItemStateChanged: void 0,
  39. onLayoutChanged: null,
  40. currentScreenFactor: void 0,
  41. _layoutStrategy: void 0
  42. })
  43. },
  44. _init: function() {
  45. if (!this.option("screenByWidth")) {
  46. this._options.screenByWidth = windowUtils.defaultScreenFactorFunc
  47. }
  48. this.callBase();
  49. this._initLayoutChangedAction()
  50. },
  51. _initLayoutChangedAction: function() {
  52. this._layoutChangedAction = this._createActionByOption("onLayoutChanged", {
  53. excludeValidators: ["disabled", "readonly"]
  54. })
  55. },
  56. _itemClass: function() {
  57. return BOX_ITEM_CLASS
  58. },
  59. _itemDataKey: function() {
  60. return BOX_ITEM_DATA_KEY
  61. },
  62. _initMarkup: function() {
  63. this.callBase();
  64. this.$element().addClass(RESPONSIVE_BOX_CLASS);
  65. this._updateRootBox()
  66. },
  67. _updateRootBox: function() {
  68. clearTimeout(this._updateTimer);
  69. this._updateTimer = setTimeout(function() {
  70. if (this._$root) {
  71. eventsEngine.triggerHandler(this._$root, "dxupdate")
  72. }
  73. }.bind(this))
  74. },
  75. _renderItems: function() {
  76. this._setScreenSize();
  77. this._screenItems = this._itemsByScreen();
  78. this._prepareGrid();
  79. this._spreadItems();
  80. this._layoutItems();
  81. this._linkNodeToItem()
  82. },
  83. _itemOptionChanged: function(item) {
  84. var $item = this._findItemElementByItem(item);
  85. if (!$item.length) {
  86. return
  87. }
  88. this._refreshItem($item, item);
  89. this._clearItemNodeTemplates();
  90. this._update()
  91. },
  92. _setScreenSize: function() {
  93. var currentScreen = this._getCurrentScreen();
  94. this._removeScreenSizeClass();
  95. this.$element().addClass(SCREEN_SIZE_CLASS_PREFIX + currentScreen);
  96. this.option("currentScreenFactor", currentScreen)
  97. },
  98. _removeScreenSizeClass: function() {
  99. var currentScreenFactor = this.option("currentScreenFactor");
  100. currentScreenFactor && this.$element().removeClass(SCREEN_SIZE_CLASS_PREFIX + currentScreenFactor)
  101. },
  102. _prepareGrid: function() {
  103. var grid = this._grid = [];
  104. this._prepareRowsAndCols();
  105. iteratorUtils.each(this._rows, function() {
  106. var row = [];
  107. grid.push(row);
  108. iteratorUtils.each(this._cols, function() {
  109. row.push(this._createEmptyCell())
  110. }.bind(this))
  111. }.bind(this))
  112. },
  113. getSingleColumnRows: function() {
  114. var rows = this.option("rows");
  115. var screenItemsLength = this._screenItems.length;
  116. if (rows.length) {
  117. var filteredRows = this._filterByScreen(rows);
  118. var result = [];
  119. for (var i = 0; i < screenItemsLength; i++) {
  120. var sizeConfig = this._defaultSizeConfig();
  121. if (i < filteredRows.length && typeUtils.isDefined(filteredRows[i].shrink)) {
  122. sizeConfig.shrink = filteredRows[i].shrink
  123. }
  124. result.push(sizeConfig)
  125. }
  126. return result
  127. } else {
  128. return this._defaultSizeConfig(screenItemsLength)
  129. }
  130. },
  131. _prepareRowsAndCols: function() {
  132. if (this._isSingleColumnScreen()) {
  133. this._prepareSingleColumnScreenItems();
  134. this._rows = this.getSingleColumnRows();
  135. this._cols = this._defaultSizeConfig(1)
  136. } else {
  137. this._rows = this._sizesByScreen(this.option("rows"));
  138. this._cols = this._sizesByScreen(this.option("cols"))
  139. }
  140. },
  141. _isSingleColumnScreen: function() {
  142. return this._screenRegExp().test(this.option("singleColumnScreen")) || !this.option("rows").length || !this.option("cols").length
  143. },
  144. _prepareSingleColumnScreenItems: function() {
  145. this._screenItems.sort(function(item1, item2) {
  146. return item1.location.row - item2.location.row || item1.location.col - item2.location.col
  147. });
  148. iteratorUtils.each(this._screenItems, function(index, item) {
  149. extend(item.location, {
  150. row: index,
  151. col: 0,
  152. rowspan: 1,
  153. colspan: 1
  154. })
  155. })
  156. },
  157. _sizesByScreen: function(sizeConfigs) {
  158. return iteratorUtils.map(this._filterByScreen(sizeConfigs), function(sizeConfig) {
  159. return extend(this._defaultSizeConfig(), sizeConfig)
  160. }.bind(this))
  161. },
  162. _createDefaultSizeConfig: function() {
  163. return {
  164. ratio: 1,
  165. baseSize: 0,
  166. minSize: 0,
  167. maxSize: 0
  168. }
  169. },
  170. _defaultSizeConfig: function(size) {
  171. var defaultSizeConfig = this._createDefaultSizeConfig();
  172. if (!arguments.length) {
  173. return defaultSizeConfig
  174. }
  175. var result = [];
  176. for (var i = 0; i < size; i++) {
  177. result.push(defaultSizeConfig)
  178. }
  179. return result
  180. },
  181. _filterByScreen: function(items) {
  182. var screenRegExp = this._screenRegExp();
  183. return commonUtils.grep(items, function(item) {
  184. return !item.screen || screenRegExp.test(item.screen)
  185. })
  186. },
  187. _screenRegExp: function() {
  188. var screen = this._getCurrentScreen();
  189. return new RegExp("(^|\\s)" + screen + "($|\\s)", "i")
  190. },
  191. _getCurrentScreen: function() {
  192. var width = this._screenWidth();
  193. return this.option("screenByWidth")(width)
  194. },
  195. _screenWidth: function() {
  196. return windowUtils.hasWindow() ? $(window).width() : HD_SCREEN_WIDTH
  197. },
  198. _createEmptyCell: function() {
  199. return {
  200. item: {},
  201. location: {
  202. colspan: 1,
  203. rowspan: 1
  204. }
  205. }
  206. },
  207. _spreadItems: function() {
  208. iteratorUtils.each(this._screenItems, function(_, itemInfo) {
  209. var location = itemInfo.location || {};
  210. var itemCol = location.col;
  211. var itemRow = location.row;
  212. var row = this._grid[itemRow];
  213. var itemCell = row && row[itemCol];
  214. this._occupyCells(itemCell, itemInfo)
  215. }.bind(this))
  216. },
  217. _itemsByScreen: function() {
  218. var _this = this;
  219. return this.option("items").reduce(function(result, item) {
  220. var locations = item.location || {};
  221. locations = typeUtils.isPlainObject(locations) ? [locations] : locations;
  222. _this._filterByScreen(locations).forEach(function(location) {
  223. result.push({
  224. item: item,
  225. location: extend({
  226. rowspan: 1,
  227. colspan: 1
  228. }, location)
  229. })
  230. });
  231. return result
  232. }, [])
  233. },
  234. _occupyCells: function(itemCell, itemInfo) {
  235. if (!itemCell || this._isItemCellOccupied(itemCell, itemInfo)) {
  236. return
  237. }
  238. extend(itemCell, itemInfo);
  239. this._markSpanningCell(itemCell)
  240. },
  241. _isItemCellOccupied: function(itemCell, itemInfo) {
  242. if (!typeUtils.isEmptyObject(itemCell.item)) {
  243. return true
  244. }
  245. var result = false;
  246. this._loopOverSpanning(itemInfo.location, function(cell) {
  247. result = result || !typeUtils.isEmptyObject(cell.item)
  248. });
  249. return result
  250. },
  251. _loopOverSpanning: function(location, callback) {
  252. var rowEnd = location.row + location.rowspan - 1;
  253. var colEnd = location.col + location.colspan - 1;
  254. var boundRowEnd = Math.min(rowEnd, this._rows.length - 1);
  255. var boundColEnd = Math.min(colEnd, this._cols.length - 1);
  256. location.rowspan -= rowEnd - boundRowEnd;
  257. location.colspan -= colEnd - boundColEnd;
  258. for (var rowIndex = location.row; rowIndex <= boundRowEnd; rowIndex++) {
  259. for (var colIndex = location.col; colIndex <= boundColEnd; colIndex++) {
  260. if (rowIndex !== location.row || colIndex !== location.col) {
  261. callback(this._grid[rowIndex][colIndex])
  262. }
  263. }
  264. }
  265. },
  266. _markSpanningCell: function(itemCell) {
  267. this._loopOverSpanning(itemCell.location, function(cell) {
  268. extend(cell, {
  269. item: itemCell.item,
  270. spanningCell: itemCell
  271. })
  272. })
  273. },
  274. _linkNodeToItem: function() {
  275. iteratorUtils.each(this._itemElements(), function(_, itemNode) {
  276. var $item = $(itemNode);
  277. var item = $item.data(BOX_ITEM_DATA_KEY);
  278. if (!item.box) {
  279. item.node = $item.children()
  280. }
  281. })
  282. },
  283. _layoutItems: function() {
  284. var rowsCount = this._grid.length;
  285. var colsCount = rowsCount && this._grid[0].length;
  286. if (!rowsCount && !colsCount) {
  287. return
  288. }
  289. var result = this._layoutBlock({
  290. direction: "col",
  291. row: {
  292. start: 0,
  293. end: rowsCount - 1
  294. },
  295. col: {
  296. start: 0,
  297. end: colsCount - 1
  298. }
  299. });
  300. var rootBox = this._prepareBoxConfig(result.box || {
  301. direction: "row",
  302. items: [extend(result, {
  303. ratio: 1
  304. })]
  305. });
  306. extend(rootBox, this._rootBoxConfig(rootBox.items));
  307. this._$root = $("<div>").appendTo(this._itemContainer());
  308. this._createComponent(this._$root, Box, rootBox)
  309. },
  310. _rootBoxConfig: function(items) {
  311. var rootItems = iteratorUtils.each(items, function(index, item) {
  312. this._needApplyAutoBaseSize(item) && extend(item, {
  313. baseSize: "auto"
  314. })
  315. }.bind(this));
  316. return extend({
  317. width: "100%",
  318. height: "100%",
  319. items: rootItems,
  320. itemTemplate: this._getTemplateByOption("itemTemplate"),
  321. itemHoldTimeout: this.option("itemHoldTimeout"),
  322. onItemHold: this._createActionByOption("onItemHold"),
  323. onItemClick: this._createActionByOption("onItemClick"),
  324. onItemContextMenu: this._createActionByOption("onItemContextMenu"),
  325. onItemRendered: this._createActionByOption("onItemRendered")
  326. }, {
  327. _layoutStrategy: this.option("_layoutStrategy")
  328. })
  329. },
  330. _needApplyAutoBaseSize: function(item) {
  331. return !item.baseSize && (!item.minSize || "auto" === item.minSize) && (!item.maxSize || "auto" === item.maxSize)
  332. },
  333. _prepareBoxConfig: function(config) {
  334. return extend(config || {}, {
  335. crossAlign: "stretch",
  336. onItemStateChanged: this.option("onItemStateChanged")
  337. })
  338. },
  339. _layoutBlock: function(options) {
  340. if (this._isSingleItem(options)) {
  341. return this._itemByCell(options.row.start, options.col.start)
  342. }
  343. return this._layoutDirection(options)
  344. },
  345. _isSingleItem: function(options) {
  346. var firstCellLocation = this._grid[options.row.start][options.col.start].location;
  347. var isItemRowSpanned = options.row.end - options.row.start === firstCellLocation.rowspan - 1;
  348. var isItemColSpanned = options.col.end - options.col.start === firstCellLocation.colspan - 1;
  349. return isItemRowSpanned && isItemColSpanned
  350. },
  351. _itemByCell: function(rowIndex, colIndex) {
  352. var itemCell = this._grid[rowIndex][colIndex];
  353. return itemCell.spanningCell ? null : itemCell.item
  354. },
  355. _layoutDirection: function(options) {
  356. var items = [];
  357. var direction = options.direction;
  358. var crossDirection = this._crossDirection(direction);
  359. var block;
  360. while (block = this._nextBlock(options)) {
  361. if (this._isBlockIndivisible(options.prevBlockOptions, block)) {
  362. throw errors.Error("E1025")
  363. }
  364. var item = this._layoutBlock({
  365. direction: crossDirection,
  366. row: block.row,
  367. col: block.col,
  368. prevBlockOptions: options
  369. });
  370. if (item) {
  371. extend(item, this._blockSize(block, crossDirection));
  372. items.push(item)
  373. }
  374. options[crossDirection].start = block[crossDirection].end + 1
  375. }
  376. return {
  377. box: this._prepareBoxConfig({
  378. direction: direction,
  379. items: items
  380. })
  381. }
  382. },
  383. _isBlockIndivisible: function(options, block) {
  384. return options && options.col.start === block.col.start && options.col.end === block.col.end && options.row.start === block.row.start && options.row.end === block.row.end
  385. },
  386. _crossDirection: function(direction) {
  387. return "col" === direction ? "row" : "col"
  388. },
  389. _nextBlock: function(options) {
  390. var direction = options.direction;
  391. var crossDirection = this._crossDirection(direction);
  392. var startIndex = options[direction].start;
  393. var endIndex = options[direction].end;
  394. var crossStartIndex = options[crossDirection].start;
  395. if (crossStartIndex > options[crossDirection].end) {
  396. return null
  397. }
  398. var crossSpan = 1;
  399. for (var crossIndex = crossStartIndex; crossIndex < crossStartIndex + crossSpan; crossIndex++) {
  400. var lineCrossSpan = 1;
  401. for (var index = startIndex; index <= endIndex; index++) {
  402. var cell = this._cellByDirection(direction, index, crossIndex);
  403. lineCrossSpan = Math.max(lineCrossSpan, cell.location[crossDirection + "span"])
  404. }
  405. var lineCrossEndIndex = crossIndex + lineCrossSpan;
  406. var crossEndIndex = crossStartIndex + crossSpan;
  407. if (lineCrossEndIndex > crossEndIndex) {
  408. crossSpan += lineCrossEndIndex - crossEndIndex
  409. }
  410. }
  411. var result = {};
  412. result[direction] = {
  413. start: startIndex,
  414. end: endIndex
  415. };
  416. result[crossDirection] = {
  417. start: crossStartIndex,
  418. end: crossStartIndex + crossSpan - 1
  419. };
  420. return result
  421. },
  422. _cellByDirection: function(direction, index, crossIndex) {
  423. return "col" === direction ? this._grid[crossIndex][index] : this._grid[index][crossIndex]
  424. },
  425. _blockSize: function(block, direction) {
  426. var defaultMinSize = "row" === direction ? "auto" : 0;
  427. var sizeConfigs = "row" === direction ? this._rows : this._cols;
  428. var result = extend(this._createDefaultSizeConfig(), {
  429. ratio: 0
  430. });
  431. for (var index = block[direction].start; index <= block[direction].end; index++) {
  432. var sizeConfig = sizeConfigs[index];
  433. result.ratio += sizeConfig.ratio;
  434. result.baseSize += sizeConfig.baseSize;
  435. result.minSize += sizeConfig.minSize;
  436. result.maxSize += sizeConfig.maxSize;
  437. if (typeUtils.isDefined(sizeConfig.shrink)) {
  438. result.shrink = sizeConfig.shrink
  439. }
  440. }
  441. result.minSize = result.minSize ? result.minSize : defaultMinSize;
  442. result.maxSize = result.maxSize ? result.maxSize : "auto";
  443. this._isSingleColumnScreen() && (result.baseSize = "auto");
  444. return result
  445. },
  446. _update: function() {
  447. var $existingRoot = this._$root;
  448. this._renderItems();
  449. $existingRoot && $existingRoot.detach();
  450. this._saveAssistantRoot($existingRoot);
  451. this._layoutChangedAction();
  452. this._updateRootBox()
  453. },
  454. _saveAssistantRoot: function($root) {
  455. this._assistantRoots = this._assistantRoots || [];
  456. this._assistantRoots.push($root)
  457. },
  458. _dispose: function() {
  459. clearTimeout(this._updateTimer);
  460. this._clearItemNodeTemplates();
  461. this._cleanUnusedRoots();
  462. this.callBase.apply(this, arguments)
  463. },
  464. _cleanUnusedRoots: function() {
  465. if (!this._assistantRoots) {
  466. return
  467. }
  468. iteratorUtils.each(this._assistantRoots, function(_, item) {
  469. $(item).remove()
  470. })
  471. },
  472. _clearItemNodeTemplates: function() {
  473. iteratorUtils.each(this.option("items"), function() {
  474. delete this.node
  475. })
  476. },
  477. _toggleVisibility: function(visible) {
  478. this.callBase(visible);
  479. if (visible) {
  480. this._updateRootBox()
  481. }
  482. },
  483. _attachClickEvent: commonUtils.noop,
  484. _optionChanged: function(args) {
  485. switch (args.name) {
  486. case "rows":
  487. case "cols":
  488. case "screenByWidth":
  489. case "_layoutStrategy":
  490. case "singleColumnScreen":
  491. this._clearItemNodeTemplates();
  492. this._invalidate();
  493. break;
  494. case "width":
  495. case "height":
  496. this.callBase(args);
  497. this._update();
  498. break;
  499. case "onLayoutChanged":
  500. this._initLayoutChangedAction();
  501. break;
  502. case "itemTemplate":
  503. this._clearItemNodeTemplates();
  504. this.callBase(args);
  505. break;
  506. case "currentScreenFactor":
  507. break;
  508. default:
  509. this.callBase(args)
  510. }
  511. },
  512. _dimensionChanged: function() {
  513. if (this._getCurrentScreen() !== this.option("currentScreenFactor")) {
  514. this._update()
  515. }
  516. },
  517. repaint: function() {
  518. this._update()
  519. }
  520. });
  521. registerComponent("dxResponsiveBox", ResponsiveBox);
  522. module.exports = ResponsiveBox;
  523. module.exports.default = module.exports;