Center.js 3.0 KB

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