|
7 | 7 | */
|
8 | 8 |
|
9 | 9 | import { GridStackEngine } from './gridstack-engine';
|
10 |
| -import { obsoleteOpts, obsoleteOptsDel, obsoleteAttr, obsolete, Utils } from './utils'; |
| 10 | +import { obsoleteOpts, obsoleteOptsDel, obsoleteAttr, obsolete, Utils, HeightData } from './utils'; |
11 | 11 | import { GridItemHTMLElement, GridStackWidget, GridStackNode, GridStackOptions, numberOrString, ColumnOptions } from './types';
|
12 | 12 | import { GridStackDD } from './gridstack-dd';
|
13 | 13 |
|
@@ -1021,27 +1021,27 @@ export class GridStack {
|
1021 | 1021 | }
|
1022 | 1022 |
|
1023 | 1023 | /**
|
1024 |
| - * Updates the margins which will set all 4 sides at once - see `GridStackOptions.margin` for format options. |
1025 |
| - * @param value new vertical margin value |
1026 |
| - * Note: you can instead use `marginTop | marginBottom | marginLeft | marginRight` GridStackOptions to set the sides separately. |
| 1024 | + * Updates the margins which will set all 4 sides at once - see `GridStackOptions.margin` for format options (CSS string format of 1,2,4 values or single number). |
| 1025 | + * @param value margin value |
1027 | 1026 | */
|
1028 | 1027 | public margin(value: numberOrString): GridStack {
|
1029 |
| - let data = Utils.parseHeight(value); |
1030 |
| - if (this.opts.marginUnit === data.unit && this.opts.margin === data.height) { |
1031 |
| - return; |
1032 |
| - } |
1033 |
| - this.opts.marginUnit = data.unit; |
1034 |
| - this.opts.marginTop = |
1035 |
| - this.opts.marginBottom = |
1036 |
| - this.opts.marginLeft = |
1037 |
| - this.opts.marginRight = |
1038 |
| - this.opts.margin = data.height; |
| 1028 | + let isMultiValue = (typeof value === 'string' && value.split(' ').length > 1); |
| 1029 | + // check if we can skip re-creating our CSS file... won't check if multi values (too much hassle) |
| 1030 | + if (!isMultiValue) { |
| 1031 | + let data = Utils.parseHeight(value); |
| 1032 | + if (this.opts.marginUnit === data.unit && this.opts.margin === data.height) return; |
| 1033 | + } |
| 1034 | + // re-use existing margin handling |
| 1035 | + this.opts.margin = value; |
| 1036 | + this.opts.marginTop = this.opts.marginBottom = this.opts.marginLeft = this.opts.marginRight = undefined; |
| 1037 | + this.initMargin(); |
| 1038 | + |
1039 | 1039 | this._updateStyles(true); // true = force re-create
|
1040 | 1040 |
|
1041 | 1041 | return this;
|
1042 | 1042 | }
|
1043 | 1043 |
|
1044 |
| - /** returns current margin value (undefined if all 4 sides don't match) */ |
| 1044 | + /** returns current margin number value (undefined if 4 sides don't match) */ |
1045 | 1045 | public getMargin(): number { return this.opts.margin as number; }
|
1046 | 1046 |
|
1047 | 1047 | /**
|
@@ -1818,9 +1818,28 @@ export class GridStack {
|
1818 | 1818 |
|
1819 | 1819 | /** @internal initialize margin top/bottom/left/right and units */
|
1820 | 1820 | private initMargin(): GridStack {
|
1821 |
| - let data = Utils.parseHeight(this.opts.margin); |
1822 |
| - this.opts.marginUnit = data.unit; |
1823 |
| - let margin = this.opts.margin = data.height; |
| 1821 | + |
| 1822 | + let data: HeightData; |
| 1823 | + let margin = 0; |
| 1824 | + |
| 1825 | + // support passing multiple values like CSS (ex: '5px 10px 0 20px') |
| 1826 | + let margins: string[] = []; |
| 1827 | + if (typeof this.opts.margin === 'string') { |
| 1828 | + margins = this.opts.margin.split(' ') |
| 1829 | + } |
| 1830 | + if (margins.length === 2) { // top/bot, left/right like CSS |
| 1831 | + this.opts.marginTop = this.opts.marginBottom = margins[0]; |
| 1832 | + this.opts.marginLeft = this.opts.marginRight = margins[1]; |
| 1833 | + } else if (margins.length === 4) { // Clockwise like CSS |
| 1834 | + this.opts.marginTop = margins[0]; |
| 1835 | + this.opts.marginRight = margins[1]; |
| 1836 | + this.opts.marginBottom = margins[2]; |
| 1837 | + this.opts.marginLeft = margins[3]; |
| 1838 | + } else { |
| 1839 | + data = Utils.parseHeight(this.opts.margin); |
| 1840 | + this.opts.marginUnit = data.unit; |
| 1841 | + margin = this.opts.margin = data.height; |
| 1842 | + } |
1824 | 1843 |
|
1825 | 1844 | // see if top/bottom/left/right need to be set as well
|
1826 | 1845 | if (this.opts.marginTop === undefined) {
|
|
0 commit comments