You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

303 lines
14 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-layout-HBoxLayout'>/**
  19. </span> * @class Ext.layout.HBoxLayout
  20. * @extends Ext.layout.BoxLayout
  21. * &lt;p&gt;A layout that arranges items horizontally across a Container. This layout optionally divides available horizontal
  22. * space between child items containing a numeric &lt;code&gt;flex&lt;/code&gt; configuration.&lt;/p&gt;
  23. * This layout may also be used to set the heights of child items by configuring it with the {@link #align} option.
  24. */
  25. Ext.layout.HBoxLayout = Ext.extend(Ext.layout.BoxLayout, {
  26. <span id='Ext-layout-HBoxLayout-cfg-align'> /**
  27. </span> * @cfg {String} align
  28. * Controls how the child items of the container are aligned. Acceptable configuration values for this
  29. * property are:
  30. * &lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
  31. * &lt;li&gt;&lt;b&gt;&lt;tt&gt;top&lt;/tt&gt;&lt;/b&gt; : &lt;b&gt;Default&lt;/b&gt;&lt;div class=&quot;sub-desc&quot;&gt;child items are aligned vertically
  32. * at the &lt;b&gt;top&lt;/b&gt; of the container&lt;/div&gt;&lt;/li&gt;
  33. * &lt;li&gt;&lt;b&gt;&lt;tt&gt;middle&lt;/tt&gt;&lt;/b&gt; : &lt;div class=&quot;sub-desc&quot;&gt;child items are aligned vertically in the
  34. * &lt;b&gt;middle&lt;/b&gt; of the container&lt;/div&gt;&lt;/li&gt;
  35. * &lt;li&gt;&lt;b&gt;&lt;tt&gt;stretch&lt;/tt&gt;&lt;/b&gt; : &lt;div class=&quot;sub-desc&quot;&gt;child items are stretched vertically to fill
  36. * the height of the container&lt;/div&gt;&lt;/li&gt;
  37. * &lt;li&gt;&lt;b&gt;&lt;tt&gt;stretchmax&lt;/tt&gt;&lt;/b&gt; : &lt;div class=&quot;sub-desc&quot;&gt;child items are stretched vertically to
  38. * the height of the largest item.&lt;/div&gt;&lt;/li&gt;
  39. * &lt;/ul&gt;&lt;/div&gt;
  40. */
  41. <span id='Ext-layout-HBoxLayout-property-type'> align: 'top', // top, middle, stretch, strechmax
  42. </span>
  43. type : 'hbox',
  44. <span id='Ext-layout-HBoxLayout-cfg-pack'> /**
  45. </span> * @cfg {String} pack
  46. * Controls how the child items of the container are packed together. Acceptable configuration values
  47. * for this property are:
  48. * &lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
  49. * &lt;li&gt;&lt;b&gt;&lt;tt&gt;start&lt;/tt&gt;&lt;/b&gt; : &lt;b&gt;Default&lt;/b&gt;&lt;div class=&quot;sub-desc&quot;&gt;child items are packed together at
  50. * &lt;b&gt;left&lt;/b&gt; side of container&lt;/div&gt;&lt;/li&gt;
  51. * &lt;li&gt;&lt;b&gt;&lt;tt&gt;center&lt;/tt&gt;&lt;/b&gt; : &lt;div class=&quot;sub-desc&quot;&gt;child items are packed together at
  52. * &lt;b&gt;mid-width&lt;/b&gt; of container&lt;/div&gt;&lt;/li&gt;
  53. * &lt;li&gt;&lt;b&gt;&lt;tt&gt;end&lt;/tt&gt;&lt;/b&gt; : &lt;div class=&quot;sub-desc&quot;&gt;child items are packed together at &lt;b&gt;right&lt;/b&gt;
  54. * side of container&lt;/div&gt;&lt;/li&gt;
  55. * &lt;/ul&gt;&lt;/div&gt;
  56. */
  57. <span id='Ext-layout-HBoxLayout-cfg-flex'> /**
  58. </span> * @cfg {Number} flex
  59. * This configuation option is to be applied to &lt;b&gt;child &lt;tt&gt;items&lt;/tt&gt;&lt;/b&gt; of the container managed
  60. * by this layout. Each child item with a &lt;tt&gt;flex&lt;/tt&gt; property will be flexed &lt;b&gt;horizontally&lt;/b&gt;
  61. * according to each item's &lt;b&gt;relative&lt;/b&gt; &lt;tt&gt;flex&lt;/tt&gt; value compared to the sum of all items with
  62. * a &lt;tt&gt;flex&lt;/tt&gt; value specified. Any child items that have either a &lt;tt&gt;flex = 0&lt;/tt&gt; or
  63. * &lt;tt&gt;flex = undefined&lt;/tt&gt; will not be 'flexed' (the initial size will not be changed).
  64. */
  65. <span id='Ext-layout-HBoxLayout-method-calculateChildBoxes'> /**
  66. </span> * @private
  67. * Calculates the size and positioning of each item in the HBox. This iterates over all of the rendered,
  68. * visible items and returns a height, width, top and left for each, as well as a reference to each. Also
  69. * returns meta data such as maxHeight which are useful when resizing layout wrappers such as this.innerCt.
  70. * @param {Array} visibleItems The array of all rendered, visible items to be calculated for
  71. * @param {Object} targetSize Object containing target size and height
  72. * @return {Object} Object containing box measurements for each child, plus meta data
  73. */
  74. calculateChildBoxes: function(visibleItems, targetSize) {
  75. var visibleCount = visibleItems.length,
  76. padding = this.padding,
  77. topOffset = padding.top,
  78. leftOffset = padding.left,
  79. paddingVert = topOffset + padding.bottom,
  80. paddingHoriz = leftOffset + padding.right,
  81. width = targetSize.width - this.scrollOffset,
  82. height = targetSize.height,
  83. availHeight = Math.max(0, height - paddingVert),
  84. isStart = this.pack == 'start',
  85. isCenter = this.pack == 'center',
  86. isEnd = this.pack == 'end',
  87. nonFlexWidth = 0,
  88. maxHeight = 0,
  89. totalFlex = 0,
  90. desiredWidth = 0,
  91. minimumWidth = 0,
  92. //used to cache the calculated size and position values for each child item
  93. boxes = [],
  94. //used in the for loops below, just declared here for brevity
  95. child, childWidth, childHeight, childSize, childMargins, canLayout, i, calcs, flexedWidth,
  96. horizMargins, vertMargins, stretchHeight;
  97. //gather the total flex of all flexed items and the width taken up by fixed width items
  98. for (i = 0; i &lt; visibleCount; i++) {
  99. child = visibleItems[i];
  100. childHeight = child.height;
  101. childWidth = child.width;
  102. canLayout = !child.hasLayout &amp;&amp; typeof child.doLayout == 'function';
  103. // Static width (numeric) requires no calcs
  104. if (typeof childWidth != 'number') {
  105. // flex and not 'auto' width
  106. if (child.flex &amp;&amp; !childWidth) {
  107. totalFlex += child.flex;
  108. // Not flexed or 'auto' width or undefined width
  109. } else {
  110. //Render and layout sub-containers without a flex or width defined, as otherwise we
  111. //don't know how wide the sub-container should be and cannot calculate flexed widths
  112. if (!childWidth &amp;&amp; canLayout) {
  113. child.doLayout();
  114. }
  115. childSize = child.getSize();
  116. childWidth = childSize.width;
  117. childHeight = childSize.height;
  118. }
  119. }
  120. childMargins = child.margins;
  121. horizMargins = childMargins.left + childMargins.right;
  122. nonFlexWidth += horizMargins + (childWidth || 0);
  123. desiredWidth += horizMargins + (child.flex ? child.minWidth || 0 : childWidth);
  124. minimumWidth += horizMargins + (child.minWidth || childWidth || 0);
  125. // Max height for align - force layout of non-laid out subcontainers without a numeric height
  126. if (typeof childHeight != 'number') {
  127. if (canLayout) {
  128. child.doLayout();
  129. }
  130. childHeight = child.getHeight();
  131. }
  132. maxHeight = Math.max(maxHeight, childHeight + childMargins.top + childMargins.bottom);
  133. //cache the size of each child component. Don't set height or width to 0, keep undefined instead
  134. boxes.push({
  135. component: child,
  136. height : childHeight || undefined,
  137. width : childWidth || undefined
  138. });
  139. }
  140. var shortfall = desiredWidth - width,
  141. tooNarrow = minimumWidth &gt; width;
  142. //the width available to the flexed items
  143. var availableWidth = Math.max(0, width - nonFlexWidth - paddingHoriz);
  144. if (tooNarrow) {
  145. for (i = 0; i &lt; visibleCount; i++) {
  146. boxes[i].width = visibleItems[i].minWidth || visibleItems[i].width || boxes[i].width;
  147. }
  148. } else {
  149. //all flexed items should be sized to their minimum width, other items should be shrunk down until
  150. //the shortfall has been accounted for
  151. if (shortfall &gt; 0) {
  152. var minWidths = [];
  153. // When we have a shortfall but are not tooNarrow, we need to shrink the width of each non-flexed item.
  154. // Flexed items are immediately reduced to their minWidth and anything already at minWidth is ignored.
  155. // The remaining items are collected into the minWidths array, which is later used to distribute the shortfall.
  156. for (var index = 0, length = visibleCount; index &lt; length; index++) {
  157. var item = visibleItems[index],
  158. minWidth = item.minWidth || 0;
  159. //shrink each non-flex tab by an equal amount to make them all fit. Flexed items are all
  160. //shrunk to their minWidth because they're flexible and should be the first to lose width
  161. if (item.flex) {
  162. boxes[index].width = minWidth;
  163. } else {
  164. minWidths.push({
  165. minWidth : minWidth,
  166. available: boxes[index].width - minWidth,
  167. index : index
  168. });
  169. }
  170. }
  171. //sort by descending amount of width remaining before minWidth is reached
  172. minWidths.sort(function(a, b) {
  173. return a.available &gt; b.available ? 1 : -1;
  174. });
  175. /*
  176. * Distribute the shortfall (difference between total desired with of all items and actual width available)
  177. * between the non-flexed items. We try to distribute the shortfall evenly, but apply it to items with the
  178. * smallest difference between their width and minWidth first, so that if reducing the width by the average
  179. * amount would make that item less than its minWidth, we carry the remainder over to the next item.
  180. */
  181. for (var i = 0, length = minWidths.length; i &lt; length; i++) {
  182. var itemIndex = minWidths[i].index;
  183. if (itemIndex == undefined) {
  184. continue;
  185. }
  186. var item = visibleItems[itemIndex],
  187. box = boxes[itemIndex],
  188. oldWidth = box.width,
  189. minWidth = item.minWidth,
  190. newWidth = Math.max(minWidth, oldWidth - Math.ceil(shortfall / (length - i))),
  191. reduction = oldWidth - newWidth;
  192. boxes[itemIndex].width = newWidth;
  193. shortfall -= reduction;
  194. }
  195. } else {
  196. //temporary variables used in the flex width calculations below
  197. var remainingWidth = availableWidth,
  198. remainingFlex = totalFlex;
  199. //calculate the widths of each flexed item
  200. for (i = 0; i &lt; visibleCount; i++) {
  201. child = visibleItems[i];
  202. calcs = boxes[i];
  203. childMargins = child.margins;
  204. vertMargins = childMargins.top + childMargins.bottom;
  205. if (isStart &amp;&amp; child.flex &amp;&amp; !child.width) {
  206. flexedWidth = Math.ceil((child.flex / remainingFlex) * remainingWidth);
  207. remainingWidth -= flexedWidth;
  208. remainingFlex -= child.flex;
  209. calcs.width = flexedWidth;
  210. calcs.dirtySize = true;
  211. }
  212. }
  213. }
  214. }
  215. if (isCenter) {
  216. leftOffset += availableWidth / 2;
  217. } else if (isEnd) {
  218. leftOffset += availableWidth;
  219. }
  220. //finally, calculate the left and top position of each item
  221. for (i = 0; i &lt; visibleCount; i++) {
  222. child = visibleItems[i];
  223. calcs = boxes[i];
  224. childMargins = child.margins;
  225. leftOffset += childMargins.left;
  226. vertMargins = childMargins.top + childMargins.bottom;
  227. calcs.left = leftOffset;
  228. calcs.top = topOffset + childMargins.top;
  229. switch (this.align) {
  230. case 'stretch':
  231. stretchHeight = availHeight - vertMargins;
  232. calcs.height = stretchHeight.constrain(child.minHeight || 0, child.maxHeight || 1000000);
  233. calcs.dirtySize = true;
  234. break;
  235. case 'stretchmax':
  236. stretchHeight = maxHeight - vertMargins;
  237. calcs.height = stretchHeight.constrain(child.minHeight || 0, child.maxHeight || 1000000);
  238. calcs.dirtySize = true;
  239. break;
  240. case 'middle':
  241. var diff = availHeight - calcs.height - vertMargins;
  242. if (diff &gt; 0) {
  243. calcs.top = topOffset + vertMargins + (diff / 2);
  244. }
  245. }
  246. leftOffset += calcs.width + childMargins.right;
  247. }
  248. return {
  249. boxes: boxes,
  250. meta : {
  251. maxHeight : maxHeight,
  252. nonFlexWidth: nonFlexWidth,
  253. desiredWidth: desiredWidth,
  254. minimumWidth: minimumWidth,
  255. shortfall : desiredWidth - width,
  256. tooNarrow : tooNarrow
  257. }
  258. };
  259. }
  260. });
  261. Ext.Container.LAYOUTS.hbox = Ext.layout.HBoxLayout;</pre>
  262. </body>
  263. </html>