Skip to content

Commit c0eb525

Browse files
committed
v2: fix setting resize handles to not alwaysShowResizeHandle
* fix for #1373 * Utils.defaults() now works recursively which was causing sub items to not e init properly. * also cleaned up some signature calls to TS standards * you can now have resize handles and still auto-hide them.
1 parent 0597e7c commit c0eb525

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Change log
4141
## 2.0.1-dev
4242

4343
- fix `animate` to not re-create CSS style each time (should be faster too) and made it default now since so much nicer. pass `{animate: false}` grid options if you want instant again [937](https://github.com/gridstack/gridstack.js/issues/937)
44+
- fix `resizable: { handles: ...}` forcing `alwaysShowResizeHandle` behavior [1373](https://github.com/gridstack/gridstack.js/issues/1373)
4445

4546
## 2.0.1 (2020-09-26)
4647

spec/utils-spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ describe('gridstack utils', function() {
104104
expect(src).toEqual({x: 1, y: 2, width: undefined});
105105
expect(Utils.defaults(src, {x: 10, width: 3})).toEqual({x: 1, y: 2, width: 3});
106106
expect(Utils.defaults(src, {height: undefined})).toEqual({x: 1, y: 2, width: 3, height: undefined});
107+
src = {x: 1, y: 2, sub: {foo: 1, two: 2}};
108+
expect(src).toEqual({x: 1, y: 2, sub: {foo: 1, two: 2}});
109+
expect(Utils.defaults(src, {x: 10, width: 3})).toEqual({x: 1, y: 2, width: 3, sub: {foo: 1, two: 2}});
110+
expect(Utils.defaults(src, {sub: {three: 3}})).toEqual({x: 1, y: 2, width: 3, sub: {foo: 1, two: 2, three: 3}});
107111
});
108112
});
109113

src/gridstack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export class GridStack {
189189
staticGrid: false,
190190
_class: 'grid-stack-instance-' + (Math.random() * 10000).toFixed(0),
191191
animate: true,
192-
alwaysShowResizeHandle: false,
192+
alwaysShowResizeHandle: opts.alwaysShowResizeHandle || false,
193193
resizable: {
194194
autoHide: !(opts.alwaysShowResizeHandle || false),
195195
handles: 'se'

src/utils.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
import { GridStackWidget, GridStackNode, GridStackOptions, numberOrString } from './types';
1010

11+
export interface HeightData {
12+
height: number;
13+
unit: string;
14+
}
15+
1116
/** checks for obsolete method names */
1217
export function obsolete(self, f, oldName: string, newName: string, rev: string) {
1318
let wrapper = (...args) => {
@@ -132,7 +137,7 @@ export class Utils {
132137
return (value === null || value.length === 0) ? null : Number(value);
133138
}
134139

135-
static parseHeight(val: numberOrString) {
140+
static parseHeight(val: numberOrString): HeightData {
136141
let height: number;
137142
let heightUnit = 'px';
138143
if (typeof val === 'string') {
@@ -145,23 +150,29 @@ export class Utils {
145150
} else {
146151
height = val;
147152
}
148-
return { height: height, unit: heightUnit }
153+
return { height, unit: heightUnit }
149154
}
150155

151-
static defaults(target, ...sources) {
156+
/** copies unset fields in target to use the given default sources values */
157+
static defaults(target, ...sources): {} {
152158

153-
sources.forEach(function (source) {
154-
for (let prop in source) {
155-
if (Object.prototype.hasOwnProperty.call(source, prop) && (target[prop] === null || target[prop] === undefined)) {
156-
target[prop] = source[prop];
159+
sources.forEach(source => {
160+
for (const key in source) {
161+
if (!source.hasOwnProperty(key)) { return; }
162+
if (target[key] === null || target[key] === undefined) {
163+
target[key] = source[key];
164+
} else if (typeof source[key] === 'object' && typeof target[key] === 'object') {
165+
// property is an object, recursively add it's field over... #1373
166+
this.defaults(target[key], source[key]);
157167
}
158168
}
159169
});
160170

161171
return target;
162172
}
163173

164-
static clone(target: {}) {
174+
/** makes a shallow copy of the passed json struct */
175+
static clone(target: {}): {} {
165176
return {...target}; // was $.extend({}, target)
166177
}
167178

@@ -177,11 +188,11 @@ export class Utils {
177188
static throttle(callback: () => void, delay: number) {
178189
let isWaiting = false;
179190

180-
return function (...args) {
191+
return (...args) => {
181192
if (!isWaiting) {
182193
callback.apply(this, args);
183194
isWaiting = true;
184-
setTimeout(function () { isWaiting = false; }, delay);
195+
setTimeout(() => isWaiting = false, delay);
185196
}
186197
}
187198
}

0 commit comments

Comments
 (0)