Center.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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-ux-layout-Center'>/**
  19. </span> * This layout manager is used to center contents within a container. As a subclass of
  20. * {@link Ext.layout.container.Fit fit layout}, CenterLayout expects to have one child
  21. * item; multiple items will be placed overlapping. The layout does not require any config
  22. * options. Items in the container can use percentage width or height rather than be fit
  23. * to the full size of the container.
  24. *
  25. * Example usage:
  26. *
  27. * // The content panel is centered in the container
  28. *
  29. * var p = Ext.create('Ext.Panel', {
  30. * title: 'Center Layout',
  31. * layout: 'ux.center',
  32. * items: [{
  33. * title: 'Centered Content',
  34. * width: '75%', // assign 75% of the container width to the item
  35. * html: 'Some content'
  36. * }]
  37. * });
  38. *
  39. * If you leave the title blank and specify no border you can create a non-visual, structural
  40. * container just for centering the contents.
  41. *
  42. * var p = Ext.create('Ext.Container', {
  43. * layout: 'ux.center',
  44. * items: [{
  45. * title: 'Centered Content',
  46. * width: 300,
  47. * height: '90%', // assign 90% of the container height to the item
  48. * html: 'Some content'
  49. * }]
  50. * });
  51. */
  52. Ext.define('Ext.ux.layout.Center', {
  53. extend: 'Ext.layout.container.Fit',
  54. alias: 'layout.ux.center',
  55. percentRe: /^\d+(?:\.\d+)?\%$/,
  56. itemCls: 'ux-layout-center-item',
  57. initLayout: function () {
  58. this.callParent(arguments);
  59. this.owner.addCls('ux-layout-center');
  60. },
  61. getItemSizePolicy: function (item) {
  62. var policy = this.callParent(arguments);
  63. if (typeof item.width == 'number') {
  64. policy = this.sizePolicies[policy.setsHeight ? 2 : 0];
  65. }
  66. return policy;
  67. },
  68. getPos: function (itemContext, info, dimension) {
  69. var size = itemContext.props[dimension] + info.margins[dimension],
  70. pos = Math.round((info.targetSize[dimension] - size) / 2);
  71. return Math.max(pos, 0);
  72. },
  73. getSize: function (item, info, dimension) {
  74. var ratio = item[dimension];
  75. if (typeof ratio == 'string' &amp;&amp; this.percentRe.test(ratio)) {
  76. ratio = parseFloat(ratio) / 100;
  77. } else {
  78. ratio = item[dimension + 'Ratio']; // backwards compat
  79. }
  80. return info.targetSize[dimension] * (ratio || 1) - info.margins[dimension];
  81. },
  82. positionItemX: function (itemContext, info) {
  83. var left = this.getPos(itemContext, info, 'width');
  84. itemContext.setProp('x', left);
  85. },
  86. positionItemY: function (itemContext, info) {
  87. var top = this.getPos(itemContext, info, 'height');
  88. itemContext.setProp('y', top);
  89. },
  90. setItemHeight: function (itemContext, info) {
  91. var height = this.getSize(itemContext.target, info, 'height');
  92. itemContext.setHeight(height);
  93. },
  94. setItemWidth: function (itemContext, info) {
  95. var width = this.getSize(itemContext.target, info, 'width');
  96. itemContext.setWidth(width);
  97. }
  98. });
  99. </pre>
  100. </body>
  101. </html>