box.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /**
  2. * DevExtreme (ui/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. function _typeof(obj) {
  11. "@babel/helpers - typeof";
  12. return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(obj) {
  13. return typeof obj
  14. } : function(obj) {
  15. return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj
  16. }, _typeof(obj)
  17. }
  18. var _renderer = require("../core/renderer");
  19. var _renderer2 = _interopRequireDefault(_renderer);
  20. var _events_engine = require("../events/core/events_engine");
  21. var _events_engine2 = _interopRequireDefault(_events_engine);
  22. var _component_registrator = require("../core/component_registrator");
  23. var _component_registrator2 = _interopRequireDefault(_component_registrator);
  24. var _extend = require("../core/utils/extend");
  25. var _common = require("../core/utils/common");
  26. var _window = require("../core/utils/window");
  27. var _window2 = _interopRequireDefault(_window);
  28. var _inflector = require("../core/utils/inflector");
  29. var _inflector2 = _interopRequireDefault(_inflector);
  30. var _type = require("../core/utils/type");
  31. var _style = require("../core/utils/style");
  32. var _style2 = _interopRequireDefault(_style);
  33. var _iterator = require("../core/utils/iterator");
  34. var _browser = require("../core/utils/browser");
  35. var _browser2 = _interopRequireDefault(_browser);
  36. var _item = require("./collection/item");
  37. var _item2 = _interopRequireDefault(_item);
  38. var _devices = require("../core/devices");
  39. var _devices2 = _interopRequireDefault(_devices);
  40. var _uiCollection_widget = require("./collection/ui.collection_widget.edit");
  41. var _uiCollection_widget2 = _interopRequireDefault(_uiCollection_widget);
  42. function _interopRequireDefault(obj) {
  43. return obj && obj.__esModule ? obj : {
  44. "default": obj
  45. }
  46. }
  47. function _classCallCheck(instance, Constructor) {
  48. if (!(instance instanceof Constructor)) {
  49. throw new TypeError("Cannot call a class as a function")
  50. }
  51. }
  52. function _defineProperties(target, props) {
  53. for (var i = 0; i < props.length; i++) {
  54. var descriptor = props[i];
  55. descriptor.enumerable = descriptor.enumerable || false;
  56. descriptor.configurable = true;
  57. if ("value" in descriptor) {
  58. descriptor.writable = true
  59. }
  60. Object.defineProperty(target, descriptor.key, descriptor)
  61. }
  62. }
  63. function _createClass(Constructor, protoProps, staticProps) {
  64. if (protoProps) {
  65. _defineProperties(Constructor.prototype, protoProps)
  66. }
  67. if (staticProps) {
  68. _defineProperties(Constructor, staticProps)
  69. }
  70. Object.defineProperty(Constructor, "prototype", {
  71. writable: false
  72. });
  73. return Constructor
  74. }
  75. function _get() {
  76. if ("undefined" !== typeof Reflect && Reflect.get) {
  77. _get = Reflect.get.bind()
  78. } else {
  79. _get = function(target, property, receiver) {
  80. var base = _superPropBase(target, property);
  81. if (!base) {
  82. return
  83. }
  84. var desc = Object.getOwnPropertyDescriptor(base, property);
  85. if (desc.get) {
  86. return desc.get.call(arguments.length < 3 ? target : receiver)
  87. }
  88. return desc.value
  89. }
  90. }
  91. return _get.apply(this, arguments)
  92. }
  93. function _superPropBase(object, property) {
  94. while (!Object.prototype.hasOwnProperty.call(object, property)) {
  95. object = _getPrototypeOf(object);
  96. if (null === object) {
  97. break
  98. }
  99. }
  100. return object
  101. }
  102. function _inherits(subClass, superClass) {
  103. if ("function" !== typeof superClass && null !== superClass) {
  104. throw new TypeError("Super expression must either be null or a function")
  105. }
  106. subClass.prototype = Object.create(superClass && superClass.prototype, {
  107. constructor: {
  108. value: subClass,
  109. writable: true,
  110. configurable: true
  111. }
  112. });
  113. Object.defineProperty(subClass, "prototype", {
  114. writable: false
  115. });
  116. if (superClass) {
  117. _setPrototypeOf(subClass, superClass)
  118. }
  119. }
  120. function _setPrototypeOf(o, p) {
  121. _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
  122. o.__proto__ = p;
  123. return o
  124. };
  125. return _setPrototypeOf(o, p)
  126. }
  127. function _createSuper(Derived) {
  128. var hasNativeReflectConstruct = _isNativeReflectConstruct();
  129. return function() {
  130. var result, Super = _getPrototypeOf(Derived);
  131. if (hasNativeReflectConstruct) {
  132. var NewTarget = _getPrototypeOf(this).constructor;
  133. result = Reflect.construct(Super, arguments, NewTarget)
  134. } else {
  135. result = Super.apply(this, arguments)
  136. }
  137. return _possibleConstructorReturn(this, result)
  138. }
  139. }
  140. function _possibleConstructorReturn(self, call) {
  141. if (call && ("object" === _typeof(call) || "function" === typeof call)) {
  142. return call
  143. } else {
  144. if (void 0 !== call) {
  145. throw new TypeError("Derived constructors may only return object or undefined")
  146. }
  147. }
  148. return _assertThisInitialized(self)
  149. }
  150. function _assertThisInitialized(self) {
  151. if (void 0 === self) {
  152. throw new ReferenceError("this hasn't been initialised - super() hasn't been called")
  153. }
  154. return self
  155. }
  156. function _isNativeReflectConstruct() {
  157. if ("undefined" === typeof Reflect || !Reflect.construct) {
  158. return false
  159. }
  160. if (Reflect.construct.sham) {
  161. return false
  162. }
  163. if ("function" === typeof Proxy) {
  164. return true
  165. }
  166. try {
  167. Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
  168. return true
  169. } catch (e) {
  170. return false
  171. }
  172. }
  173. function _getPrototypeOf(o) {
  174. _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function(o) {
  175. return o.__proto__ || Object.getPrototypeOf(o)
  176. };
  177. return _getPrototypeOf(o)
  178. }
  179. var BOX_CLASS = "dx-box";
  180. var BOX_SELECTOR = ".dx-box";
  181. var BOX_ITEM_CLASS = "dx-box-item";
  182. var BOX_ITEM_DATA_KEY = "dxBoxItemData";
  183. var MINSIZE_MAP = {
  184. row: "minWidth",
  185. col: "minHeight"
  186. };
  187. var MAXSIZE_MAP = {
  188. row: "maxWidth",
  189. col: "maxHeight"
  190. };
  191. var SHRINK = 1;
  192. var FLEX_JUSTIFY_CONTENT_MAP = {
  193. start: "flex-start",
  194. end: "flex-end",
  195. center: "center",
  196. "space-between": "space-between",
  197. "space-around": "space-around"
  198. };
  199. var FLEX_ALIGN_ITEMS_MAP = {
  200. start: "flex-start",
  201. end: "flex-end",
  202. center: "center",
  203. stretch: "stretch"
  204. };
  205. var FLEX_DIRECTION_MAP = {
  206. row: "row",
  207. col: "column"
  208. };
  209. var setFlexProp = function(element, prop, value) {
  210. value = _style2.default.normalizeStyleProp(prop, value);
  211. element.style[_style2.default.styleProp(prop)] = value;
  212. if (!_window2.default.hasWindow()) {
  213. if ("" === value || !(0, _type.isDefined)(value)) {
  214. return
  215. }
  216. var cssName = _inflector2.default.dasherize(prop);
  217. var styleExpr = cssName + ": " + value + ";";
  218. if (!element.attributes.style) {
  219. element.setAttribute("style", styleExpr)
  220. } else {
  221. if (element.attributes.style.value.indexOf(styleExpr) < 0) {
  222. element.attributes.style.value += " " + styleExpr
  223. }
  224. }
  225. }
  226. };
  227. var BOX_EVENTNAMESPACE = "dxBox";
  228. var UPDATE_EVENT = "dxupdate." + BOX_EVENTNAMESPACE;
  229. var FALLBACK_BOX_ITEM = "dx-box-fallback-item";
  230. var FALLBACK_WRAP_MAP = {
  231. row: "nowrap",
  232. col: "normal"
  233. };
  234. var FALLBACK_MAIN_SIZE_MAP = {
  235. row: "width",
  236. col: "height"
  237. };
  238. var FALLBACK_CROSS_SIZE_MAP = {
  239. row: "height",
  240. col: "width"
  241. };
  242. var FALLBACK_PRE_MARGIN_MAP = {
  243. row: "marginLeft",
  244. col: "marginTop"
  245. };
  246. var FALLBACK_POST_MARGIN_MAP = {
  247. row: "marginRight",
  248. col: "marginBottom"
  249. };
  250. var FALLBACK_CROSS_PRE_MARGIN_MAP = {
  251. row: "marginTop",
  252. col: "marginLeft"
  253. };
  254. var FALLBACK_CROSS_POST_MARGIN_MAP = {
  255. row: "marginBottom",
  256. col: "marginRight"
  257. };
  258. var MARGINS_RTL_FLIP_MAP = {
  259. marginLeft: "marginRight",
  260. marginRight: "marginLeft"
  261. };
  262. var BoxItem = function(_CollectionWidgetItem) {
  263. _inherits(BoxItem, _CollectionWidgetItem);
  264. var _super = _createSuper(BoxItem);
  265. function BoxItem() {
  266. _classCallCheck(this, BoxItem);
  267. return _super.apply(this, arguments)
  268. }
  269. _createClass(BoxItem, [{
  270. key: "_renderVisible",
  271. value: function(value, oldValue) {
  272. _get(_getPrototypeOf(BoxItem.prototype), "_renderVisible", this).call(this, value);
  273. if ((0, _type.isDefined)(oldValue)) {
  274. this._options.fireItemStateChangedAction({
  275. name: "visible",
  276. state: value,
  277. oldState: oldValue
  278. })
  279. }
  280. }
  281. }]);
  282. return BoxItem
  283. }(_item2.default);
  284. var FlexLayoutStrategy = function() {
  285. function FlexLayoutStrategy($element, option) {
  286. _classCallCheck(this, FlexLayoutStrategy);
  287. this._$element = $element;
  288. this._option = option;
  289. this.initSize = _common.noop;
  290. this.update = _common.noop
  291. }
  292. _createClass(FlexLayoutStrategy, [{
  293. key: "renderBox",
  294. value: function() {
  295. this._$element.css({
  296. display: _style2.default.stylePropPrefix("flexDirection") + "flex"
  297. });
  298. setFlexProp(this._$element.get(0), "flexDirection", FLEX_DIRECTION_MAP[this._option("direction")])
  299. }
  300. }, {
  301. key: "renderAlign",
  302. value: function() {
  303. this._$element.css({
  304. justifyContent: this._normalizedAlign()
  305. })
  306. }
  307. }, {
  308. key: "_normalizedAlign",
  309. value: function() {
  310. var align = this._option("align");
  311. return align in FLEX_JUSTIFY_CONTENT_MAP ? FLEX_JUSTIFY_CONTENT_MAP[align] : align
  312. }
  313. }, {
  314. key: "renderCrossAlign",
  315. value: function() {
  316. this._$element.css({
  317. alignItems: this._normalizedCrossAlign()
  318. })
  319. }
  320. }, {
  321. key: "_normalizedCrossAlign",
  322. value: function() {
  323. var crossAlign = this._option("crossAlign");
  324. return crossAlign in FLEX_ALIGN_ITEMS_MAP ? FLEX_ALIGN_ITEMS_MAP[crossAlign] : crossAlign
  325. }
  326. }, {
  327. key: "renderItems",
  328. value: function($items) {
  329. var flexPropPrefix = _style2.default.stylePropPrefix("flexDirection");
  330. var direction = this._option("direction");
  331. (0, _iterator.each)($items, function() {
  332. var $item = (0, _renderer2.default)(this);
  333. var item = $item.data(BOX_ITEM_DATA_KEY);
  334. $item.css({
  335. display: flexPropPrefix + "flex"
  336. }).css(MAXSIZE_MAP[direction], item.maxSize || "none").css(MINSIZE_MAP[direction], item.minSize || "0");
  337. setFlexProp($item.get(0), "flexBasis", item.baseSize || 0);
  338. setFlexProp($item.get(0), "flexGrow", item.ratio);
  339. setFlexProp($item.get(0), "flexShrink", (0, _type.isDefined)(item.shrink) ? item.shrink : SHRINK);
  340. $item.children().each(function(_, itemContent) {
  341. (0, _renderer2.default)(itemContent).css({
  342. width: "auto",
  343. height: "auto",
  344. display: _style2.default.stylePropPrefix("flexDirection") + "flex",
  345. flexBasis: 0
  346. });
  347. setFlexProp(itemContent, "flexGrow", 1);
  348. setFlexProp(itemContent, "flexDirection", (0, _renderer2.default)(itemContent)[0].style.flexDirection || "column")
  349. })
  350. })
  351. }
  352. }]);
  353. return FlexLayoutStrategy
  354. }();
  355. var FallbackLayoutStrategy = function() {
  356. function FallbackLayoutStrategy($element, option) {
  357. _classCallCheck(this, FallbackLayoutStrategy);
  358. this._$element = $element;
  359. this._option = option
  360. }
  361. _createClass(FallbackLayoutStrategy, [{
  362. key: "renderBox",
  363. value: function() {
  364. this._$element.css({
  365. fontSize: 0,
  366. whiteSpace: FALLBACK_WRAP_MAP[this._option("direction")],
  367. verticalAlign: "top"
  368. });
  369. _events_engine2.default.off(this._$element, UPDATE_EVENT);
  370. _events_engine2.default.on(this._$element, UPDATE_EVENT, this.update.bind(this))
  371. }
  372. }, {
  373. key: "renderAlign",
  374. value: function() {
  375. var $items = this._$items;
  376. if (!$items) {
  377. return
  378. }
  379. var align = this._option("align");
  380. var totalItemSize = this.totalItemSize;
  381. var direction = this._option("direction");
  382. var boxSize = this._$element[FALLBACK_MAIN_SIZE_MAP[direction]]();
  383. var freeSpace = boxSize - totalItemSize;
  384. var shift = 0;
  385. this._setItemsMargins($items, direction, 0);
  386. switch (align) {
  387. case "start":
  388. break;
  389. case "end":
  390. shift = freeSpace;
  391. $items.first().css(this._chooseMarginSide(FALLBACK_PRE_MARGIN_MAP[direction]), shift);
  392. break;
  393. case "center":
  394. shift = .5 * freeSpace;
  395. $items.first().css(this._chooseMarginSide(FALLBACK_PRE_MARGIN_MAP[direction]), shift);
  396. $items.last().css(this._chooseMarginSide(FALLBACK_POST_MARGIN_MAP[direction]), shift);
  397. break;
  398. case "space-between":
  399. shift = .5 * freeSpace / ($items.length - 1);
  400. this._setItemsMargins($items, direction, shift);
  401. $items.first().css(this._chooseMarginSide(FALLBACK_PRE_MARGIN_MAP[direction]), 0);
  402. $items.last().css(this._chooseMarginSide(FALLBACK_POST_MARGIN_MAP[direction]), 0);
  403. break;
  404. case "space-around":
  405. shift = .5 * freeSpace / $items.length;
  406. this._setItemsMargins($items, direction, shift)
  407. }
  408. }
  409. }, {
  410. key: "_setItemsMargins",
  411. value: function($items, direction, shift) {
  412. $items.css(this._chooseMarginSide(FALLBACK_PRE_MARGIN_MAP[direction]), shift).css(this._chooseMarginSide(FALLBACK_POST_MARGIN_MAP[direction]), shift)
  413. }
  414. }, {
  415. key: "renderCrossAlign",
  416. value: function() {
  417. var $items = this._$items;
  418. if (!$items) {
  419. return
  420. }
  421. var crossAlign = this._option("crossAlign");
  422. var direction = this._option("direction");
  423. var size = this._$element[FALLBACK_CROSS_SIZE_MAP[direction]]();
  424. var that = this;
  425. switch (crossAlign) {
  426. case "start":
  427. break;
  428. case "end":
  429. (0, _iterator.each)($items, function() {
  430. var $item = (0, _renderer2.default)(this);
  431. var itemSize = $item[FALLBACK_CROSS_SIZE_MAP[direction]]();
  432. var shift = size - itemSize;
  433. $item.css(that._chooseMarginSide(FALLBACK_CROSS_PRE_MARGIN_MAP[direction]), shift)
  434. });
  435. break;
  436. case "center":
  437. (0, _iterator.each)($items, function() {
  438. var $item = (0, _renderer2.default)(this);
  439. var itemSize = $item[FALLBACK_CROSS_SIZE_MAP[direction]]();
  440. var shift = .5 * (size - itemSize);
  441. $item.css(that._chooseMarginSide(FALLBACK_CROSS_PRE_MARGIN_MAP[direction]), shift).css(that._chooseMarginSide(FALLBACK_CROSS_POST_MARGIN_MAP[direction]), shift)
  442. });
  443. break;
  444. case "stretch":
  445. $items.css(that._chooseMarginSide(FALLBACK_CROSS_PRE_MARGIN_MAP[direction]), 0).css(that._chooseMarginSide(FALLBACK_CROSS_POST_MARGIN_MAP[direction]), 0).css(FALLBACK_CROSS_SIZE_MAP[direction], "100%")
  446. }
  447. }
  448. }, {
  449. key: "_chooseMarginSide",
  450. value: function(value) {
  451. if (!this._option("rtlEnabled")) {
  452. return value
  453. }
  454. return MARGINS_RTL_FLIP_MAP[value] || value
  455. }
  456. }, {
  457. key: "renderItems",
  458. value: function($items) {
  459. var _this = this;
  460. this._$items = $items;
  461. var direction = this._option("direction");
  462. var totalRatio = 0;
  463. var totalWeightedShrink = 0;
  464. var totalBaseSize = 0;
  465. (0, _iterator.each)($items, function(_, item) {
  466. var $item = (0, _renderer2.default)(item);
  467. $item.css({
  468. display: "inline-block",
  469. verticalAlign: "top"
  470. });
  471. $item[FALLBACK_MAIN_SIZE_MAP[direction]]("auto");
  472. $item.removeClass(FALLBACK_BOX_ITEM);
  473. var itemData = $item.data(BOX_ITEM_DATA_KEY);
  474. var ratio = itemData.ratio || 0;
  475. var size = _this._baseSize($item);
  476. var shrink = (0, _type.isDefined)(itemData.shrink) ? itemData.shrink : SHRINK;
  477. totalRatio += ratio;
  478. totalWeightedShrink += shrink * size;
  479. totalBaseSize += size
  480. });
  481. var freeSpaceSize = this._boxSize() - totalBaseSize;
  482. var itemSize = function($item) {
  483. var itemData = $item.data(BOX_ITEM_DATA_KEY);
  484. var size = _this._baseSize($item);
  485. var factor = freeSpaceSize >= 0 ? itemData.ratio || 0 : ((0, _type.isDefined)(itemData.shrink) ? itemData.shrink : SHRINK) * size;
  486. var totalFactor = freeSpaceSize >= 0 ? totalRatio : totalWeightedShrink;
  487. var shift = totalFactor ? Math.round(freeSpaceSize * factor / totalFactor) : 0;
  488. return size + shift
  489. };
  490. var totalItemSize = 0;
  491. (0, _iterator.each)($items, function(_, item) {
  492. var $item = (0, _renderer2.default)(item);
  493. var itemData = (0, _renderer2.default)(item).data(BOX_ITEM_DATA_KEY);
  494. var size = itemSize($item);
  495. totalItemSize += size;
  496. $item.css(MAXSIZE_MAP[direction], itemData.maxSize || "none").css(MINSIZE_MAP[direction], itemData.minSize || "0").css(FALLBACK_MAIN_SIZE_MAP[direction], size);
  497. $item.addClass(FALLBACK_BOX_ITEM)
  498. });
  499. this.totalItemSize = totalItemSize
  500. }
  501. }, {
  502. key: "_baseSize",
  503. value: function(item) {
  504. var itemData = (0, _renderer2.default)(item).data(BOX_ITEM_DATA_KEY);
  505. return null == itemData.baseSize ? 0 : "auto" === itemData.baseSize ? this._contentSize(item) : this._parseSize(itemData.baseSize)
  506. }
  507. }, {
  508. key: "_contentSize",
  509. value: function(item) {
  510. return (0, _renderer2.default)(item)[FALLBACK_MAIN_SIZE_MAP[this._option("direction")]]()
  511. }
  512. }, {
  513. key: "_parseSize",
  514. value: function(size) {
  515. return String(size).match(/.+%$/) ? .01 * parseFloat(size) * this._boxSizeValue : size
  516. }
  517. }, {
  518. key: "_boxSize",
  519. value: function(value) {
  520. if (!arguments.length) {
  521. this._boxSizeValue = this._boxSizeValue || this._totalBaseSize();
  522. return this._boxSizeValue
  523. }
  524. this._boxSizeValue = value
  525. }
  526. }, {
  527. key: "_totalBaseSize",
  528. value: function() {
  529. var _this2 = this;
  530. var result = 0;
  531. (0, _iterator.each)(this._$items, function(_, item) {
  532. result += _this2._baseSize(item)
  533. });
  534. return result
  535. }
  536. }, {
  537. key: "initSize",
  538. value: function() {
  539. this._boxSize(this._$element[FALLBACK_MAIN_SIZE_MAP[this._option("direction")]]())
  540. }
  541. }, {
  542. key: "update",
  543. value: function() {
  544. if (!this._$items || this._$element.is(":hidden")) {
  545. return
  546. }
  547. this._$items.detach();
  548. this.initSize();
  549. this._$element.append(this._$items);
  550. this.renderItems(this._$items);
  551. this.renderAlign();
  552. this.renderCrossAlign();
  553. var element = this._$element.get(0);
  554. this._$items.find(BOX_SELECTOR).each(function() {
  555. if (element === (0, _renderer2.default)(this).parent().closest(BOX_SELECTOR).get(0)) {
  556. _events_engine2.default.triggerHandler(this, UPDATE_EVENT)
  557. }
  558. })
  559. }
  560. }]);
  561. return FallbackLayoutStrategy
  562. }();
  563. var Box = function(_CollectionWidget) {
  564. _inherits(Box, _CollectionWidget);
  565. var _super2 = _createSuper(Box);
  566. function Box() {
  567. _classCallCheck(this, Box);
  568. return _super2.apply(this, arguments)
  569. }
  570. _createClass(Box, [{
  571. key: "_getDefaultOptions",
  572. value: function() {
  573. return (0, _extend.extend)(_get(_getPrototypeOf(Box.prototype), "_getDefaultOptions", this).call(this), {
  574. direction: "row",
  575. align: "start",
  576. crossAlign: "stretch",
  577. activeStateEnabled: false,
  578. focusStateEnabled: false,
  579. onItemStateChanged: void 0,
  580. _layoutStrategy: "flex",
  581. _queue: void 0
  582. })
  583. }
  584. }, {
  585. key: "_defaultOptionsRules",
  586. value: function() {
  587. return _get(_getPrototypeOf(Box.prototype), "_defaultOptionsRules", this).call(this).concat([{
  588. device: function device() {
  589. var device = _devices2.default.real();
  590. var isOldAndroid = "android" === device.platform && (device.version[0] < 4 || 4 === device.version[0] && device.version[1] < 4);
  591. var isOldIos = "ios" === device.platform && device.version[0] < 7;
  592. return "win" === device.platform || _browser2.default.msie || isOldAndroid || isOldIos
  593. },
  594. options: {
  595. _layoutStrategy: "fallback"
  596. }
  597. }])
  598. }
  599. }, {
  600. key: "_itemClass",
  601. value: function() {
  602. return BOX_ITEM_CLASS
  603. }
  604. }, {
  605. key: "_itemDataKey",
  606. value: function() {
  607. return BOX_ITEM_DATA_KEY
  608. }
  609. }, {
  610. key: "_itemElements",
  611. value: function() {
  612. return this._itemContainer().children(this._itemSelector())
  613. }
  614. }, {
  615. key: "_init",
  616. value: function() {
  617. _get(_getPrototypeOf(Box.prototype), "_init", this).call(this);
  618. this.$element().addClass("".concat(BOX_CLASS, "-").concat(this.option("_layoutStrategy")));
  619. this._initLayout();
  620. this._initBoxQueue()
  621. }
  622. }, {
  623. key: "_initLayout",
  624. value: function() {
  625. this._layout = "fallback" === this.option("_layoutStrategy") ? new FallbackLayoutStrategy(this.$element(), this.option.bind(this)) : new FlexLayoutStrategy(this.$element(), this.option.bind(this))
  626. }
  627. }, {
  628. key: "_initBoxQueue",
  629. value: function() {
  630. this._queue = this.option("_queue") || []
  631. }
  632. }, {
  633. key: "_queueIsNotEmpty",
  634. value: function() {
  635. return this.option("_queue") ? false : !!this._queue.length
  636. }
  637. }, {
  638. key: "_pushItemToQueue",
  639. value: function($item, config) {
  640. this._queue.push({
  641. $item: $item,
  642. config: config
  643. })
  644. }
  645. }, {
  646. key: "_shiftItemFromQueue",
  647. value: function() {
  648. return this._queue.shift()
  649. }
  650. }, {
  651. key: "_initMarkup",
  652. value: function() {
  653. this.$element().addClass(BOX_CLASS);
  654. this._layout.renderBox();
  655. _get(_getPrototypeOf(Box.prototype), "_initMarkup", this).call(this);
  656. this._renderAlign();
  657. this._renderActions()
  658. }
  659. }, {
  660. key: "_renderActions",
  661. value: function() {
  662. this._onItemStateChanged = this._createActionByOption("onItemStateChanged")
  663. }
  664. }, {
  665. key: "_renderAlign",
  666. value: function() {
  667. this._layout.renderAlign();
  668. this._layout.renderCrossAlign()
  669. }
  670. }, {
  671. key: "_renderItems",
  672. value: function(items) {
  673. var _this3 = this;
  674. this._layout.initSize();
  675. _get(_getPrototypeOf(Box.prototype), "_renderItems", this).call(this, items);
  676. while (this._queueIsNotEmpty()) {
  677. var item = this._shiftItemFromQueue();
  678. this._createComponent(item.$item, Box, (0, _extend.extend)({
  679. _layoutStrategy: this.option("_layoutStrategy"),
  680. itemTemplate: this.option("itemTemplate"),
  681. itemHoldTimeout: this.option("itemHoldTimeout"),
  682. onItemHold: this.option("onItemHold"),
  683. onItemClick: this.option("onItemClick"),
  684. onItemContextMenu: this.option("onItemContextMenu"),
  685. onItemRendered: this.option("onItemRendered"),
  686. _queue: this._queue
  687. }, item.config))
  688. }
  689. this._layout.renderItems(this._itemElements());
  690. clearTimeout(this._updateTimer);
  691. this._updateTimer = setTimeout(function() {
  692. if (!_this3._isUpdated) {
  693. _this3._layout.update()
  694. }
  695. _this3._isUpdated = false;
  696. _this3._updateTimer = null
  697. })
  698. }
  699. }, {
  700. key: "_renderItemContent",
  701. value: function(args) {
  702. var $itemNode = args.itemData && args.itemData.node;
  703. if ($itemNode) {
  704. return this._renderItemContentByNode(args, $itemNode)
  705. }
  706. return _get(_getPrototypeOf(Box.prototype), "_renderItemContent", this).call(this, args)
  707. }
  708. }, {
  709. key: "_postprocessRenderItem",
  710. value: function(args) {
  711. var boxConfig = args.itemData.box;
  712. if (!boxConfig) {
  713. return
  714. }
  715. this._pushItemToQueue(args.itemContent, boxConfig)
  716. }
  717. }, {
  718. key: "_createItemByTemplate",
  719. value: function(itemTemplate, args) {
  720. if (args.itemData.box) {
  721. return itemTemplate.source ? itemTemplate.source() : (0, _renderer2.default)()
  722. }
  723. return _get(_getPrototypeOf(Box.prototype), "_createItemByTemplate", this).call(this, itemTemplate, args)
  724. }
  725. }, {
  726. key: "_visibilityChanged",
  727. value: function(visible) {
  728. if (visible) {
  729. this._dimensionChanged()
  730. }
  731. }
  732. }, {
  733. key: "_dimensionChanged",
  734. value: function() {
  735. if (this._updateTimer) {
  736. return
  737. }
  738. this._isUpdated = true;
  739. this._layout.update()
  740. }
  741. }, {
  742. key: "_dispose",
  743. value: function() {
  744. clearTimeout(this._updateTimer);
  745. _get(_getPrototypeOf(Box.prototype), "_dispose", this).apply(this, arguments)
  746. }
  747. }, {
  748. key: "_itemOptionChanged",
  749. value: function(item, property, value, oldValue) {
  750. if ("visible" === property) {
  751. this._onItemStateChanged({
  752. name: property,
  753. state: value,
  754. oldState: false !== oldValue
  755. })
  756. }
  757. _get(_getPrototypeOf(Box.prototype), "_itemOptionChanged", this).call(this, item, property, value)
  758. }
  759. }, {
  760. key: "_optionChanged",
  761. value: function(args) {
  762. switch (args.name) {
  763. case "_layoutStrategy":
  764. case "_queue":
  765. case "direction":
  766. this._invalidate();
  767. break;
  768. case "align":
  769. this._layout.renderAlign();
  770. break;
  771. case "crossAlign":
  772. this._layout.renderCrossAlign();
  773. break;
  774. default:
  775. _get(_getPrototypeOf(Box.prototype), "_optionChanged", this).call(this, args)
  776. }
  777. }
  778. }, {
  779. key: "_itemOptions",
  780. value: function() {
  781. var _this4 = this;
  782. var options = _get(_getPrototypeOf(Box.prototype), "_itemOptions", this).call(this);
  783. options.fireItemStateChangedAction = function(e) {
  784. _this4._onItemStateChanged(e)
  785. };
  786. return options
  787. }
  788. }, {
  789. key: "repaint",
  790. value: function() {
  791. this._dimensionChanged()
  792. }
  793. }]);
  794. return Box
  795. }(_uiCollection_widget2.default);
  796. Box.ItemClass = BoxItem;
  797. (0, _component_registrator2.default)("dxBox", Box);
  798. module.exports = Box;
  799. module.exports.default = module.exports;