123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- /**
- * This layout manager is used to center contents within a container. As a subclass of
- * {@link Ext.layout.container.Fit fit layout}, CenterLayout expects to have one child
- * item; multiple items will be placed overlapping. The layout does not require any config
- * options. Items in the container can use percentage width or height rather than be fit
- * to the full size of the container.
- *
- * Example usage:
- *
- * // The content panel is centered in the container
- *
- * var p = Ext.create('Ext.Panel', {
- * title: 'Center Layout',
- * layout: 'ux.center',
- * items: [{
- * title: 'Centered Content',
- * width: '75%', // assign 75% of the container width to the item
- * html: 'Some content'
- * }]
- * });
- *
- * If you leave the title blank and specify no border you can create a non-visual, structural
- * container just for centering the contents.
- *
- * var p = Ext.create('Ext.Container', {
- * layout: 'ux.center',
- * items: [{
- * title: 'Centered Content',
- * width: 300,
- * height: '90%', // assign 90% of the container height to the item
- * html: 'Some content'
- * }]
- * });
- */
- Ext.define('Ext.ux.layout.Center', {
- extend: 'Ext.layout.container.Fit',
- alias: 'layout.ux.center',
- percentRe: /^\d+(?:\.\d+)?\%$/,
- itemCls: 'ux-layout-center-item',
- initLayout: function () {
- this.callParent(arguments);
- this.owner.addCls('ux-layout-center');
- },
- getItemSizePolicy: function (item) {
- var policy = this.callParent(arguments);
- if (typeof item.width == 'number') {
- policy = this.sizePolicies[policy.setsHeight ? 2 : 0];
- }
- return policy;
- },
- getPos: function (itemContext, info, dimension) {
- var size = itemContext.props[dimension] + info.margins[dimension],
- pos = Math.round((info.targetSize[dimension] - size) / 2);
- return Math.max(pos, 0);
- },
- getSize: function (item, info, dimension) {
- var ratio = item[dimension];
- if (typeof ratio == 'string' && this.percentRe.test(ratio)) {
- ratio = parseFloat(ratio) / 100;
- } else {
- ratio = item[dimension + 'Ratio']; // backwards compat
- }
- return info.targetSize[dimension] * (ratio || 1) - info.margins[dimension];
- },
- positionItemX: function (itemContext, info) {
- var left = this.getPos(itemContext, info, 'width');
- itemContext.setProp('x', left);
- },
- positionItemY: function (itemContext, info) {
- var top = this.getPos(itemContext, info, 'height');
- itemContext.setProp('y', top);
- },
- setItemHeight: function (itemContext, info) {
- var height = this.getSize(itemContext.target, info, 'height');
- itemContext.setHeight(height);
- },
- setItemWidth: function (itemContext, info) {
- var width = this.getSize(itemContext.target, info, 'width');
- itemContext.setWidth(width);
- }
- });
|