Skip to content

Commit ef8808d

Browse files
committed
added preserveAspectRatio prop to GridItem
1 parent 0783f5d commit ef8808d

File tree

5 files changed

+62
-19
lines changed

5 files changed

+62
-19
lines changed

public/app.css

+5-1
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,8 @@
100100
background-origin: content-box;
101101
box-sizing: border-box;
102102
cursor: pointer;
103-
}
103+
}
104+
105+
#content .vue-grid-item.vue-grid-placeholder {
106+
background-color: green;
107+
}

src/App.vue

+22-8
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
:is-resizable="resizable"
4646
:is-mirrored="mirrored"
4747
:prevent-collision="preventCollision"
48-
:vertical-compact="true"
48+
:vertical-compact="compact"
4949
:use-css-transforms="true"
5050
:responsive="responsive"
5151
@layout-created="layoutCreatedEvent"
@@ -68,14 +68,15 @@
6868
:max-x="item.maxX"
6969
:min-y="item.minY"
7070
:max-y="item.maxY"
71+
:preserve-aspect-ratio="item.preserveAspectRatio"
7172
@resize="resize"
7273
@move="move"
7374
@resized="resized"
7475
@container-resized="containerResized"
7576
@moved="moved"
7677
>
7778
<!--<custom-drag-element :text="item.i"></custom-drag-element>-->
78-
<test-element :text="item.i"></test-element>
79+
<test-element :text="item.i" @removeItem="removeItem($event)"></test-element>
7980
<!--<button @click="clicked">CLICK ME!</button>-->
8081
</grid-item>
8182
</grid-layout>
@@ -119,7 +120,7 @@
119120
let testLayout = [
120121
{"x":0,"y":0,"w":2,"h":2,"i":"0", resizable: true, draggable: true, static: false, minY: 0, maxY: 2},
121122
{"x":2,"y":0,"w":2,"h":4,"i":"1", resizable: null, draggable: null, static: true},
122-
{"x":4,"y":0,"w":2,"h":5,"i":"2", resizable: false, draggable: false, static: false, minX: 4, maxX: 4, minW: 2, maxW: 2},
123+
{"x":4,"y":0,"w":2,"h":5,"i":"2", resizable: false, draggable: false, static: false, minX: 4, maxX: 4, minW: 2, maxW: 2, preserveAspectRatio: true},
123124
{"x":6,"y":0,"w":2,"h":3,"i":"3", resizable: false, draggable: false, static: false},
124125
{"x":8,"y":0,"w":2,"h":3,"i":"4", resizable: false, draggable: false, static: false},
125126
{"x":10,"y":0,"w":2,"h":3,"i":"5", resizable: false, draggable: false, static: false},
@@ -139,6 +140,14 @@
139140
{"x":2,"y":6,"w":2,"h":2,"i":"19", resizable: false, draggable: false, static: false}
140141
];
141142
143+
/*let testLayout = [
144+
{ x: 0, y: 0, w: 2, h: 2, i: "0" },
145+
{ x: 2, y: 0, w: 2, h: 2, i: "1" },
146+
{ x: 4, y: 0, w: 2, h: 2, i: "2" },
147+
{ x: 6, y: 0, w: 2, h: 2, i: "3" },
148+
{ x: 8, y: 0, w: 2, h: 2, i: "4" },
149+
];*/
150+
142151
export default {
143152
name: 'app',
144153
components: {
@@ -156,6 +165,7 @@
156165
mirrored: false,
157166
responsive: true,
158167
preventCollision: false,
168+
compact: true,
159169
rowHeight: 30,
160170
colNum: 12,
161171
index: 0,
@@ -180,9 +190,10 @@
180190
width -= 20;
181191
document.getElementById("content").style.width = width+"px";
182192
},
183-
removeItem: function(item) {
184-
//console.log("### REMOVE " + item.i);
185-
this.layout.splice(this.layout.indexOf(item), 1);
193+
removeItem: function(i) {
194+
console.log("### REMOVE " + i);
195+
const index = this.layout.map(item => item.i).indexOf(i);
196+
this.layout.splice(index, 1);
186197
},
187198
addItem: function() {
188199
// let self = this;
@@ -192,9 +203,12 @@
192203
this.layout.push(item);
193204
},
194205
addItemDynamically: function() {
206+
const x = (this.layout.length * 2) % (this.colNum || 12);
207+
const y = this.layout.length + (this.colNum || 12);
208+
console.log("X=" + x + " Y=" + y)
195209
let item = {
196-
x: (this.layout.length * 2) % (this.colNum || 12),
197-
y: this.layout.length + (this.colNum || 12),
210+
x: x,
211+
y: y,
198212
w: 2,
199213
h: 2,
200214
i: this.index+"",

src/components/GridItem.vue

+13-5
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@
197197
required: false,
198198
default: 'a, button'
199199
},
200+
preserveAspectRatio: {
201+
type: Boolean,
202+
required: false,
203+
default: false,
204+
}
200205
},
201206
inject: ["eventBus", "layout"],
202207
data: function () {
@@ -790,11 +795,6 @@
790795
// console.log("### MIN " + JSON.stringify(minimum));
791796
792797
const opts = {
793-
modifiers: [
794-
interact.modifiers.aspectRatio({
795-
ratio: 'preserve'
796-
}),
797-
],
798798
// allowFrom: "." + this.resizableHandleClass.trim().replace(" ", "."),
799799
edges: {
800800
left: false,
@@ -815,6 +815,14 @@
815815
}
816816
};
817817
818+
if (this.preserveAspectRatio) {
819+
opts.modifiers = [
820+
interact.modifiers.aspectRatio({
821+
ratio: 'preserve'
822+
}),
823+
]
824+
}
825+
818826
this.interactObj.resizable(opts);
819827
if (!this.resizeEventSet) {
820828
this.resizeEventSet = true;

src/components/TestElement.vue

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
<template>
2-
<span class="text">
3-
{{text}}
4-
</span>
2+
<div>
3+
<span class="text">
4+
{{text}}
5+
</span>
6+
<span class="remove" @click="$emit('removeItem', text)">x</span>
7+
</div>
58
</template>
69
<style>
10+
.remove {
11+
position: absolute;
12+
right: 2px;
13+
top: 0;
14+
cursor: pointer;
15+
}
716
</style>
817
<script>
918
export default {
@@ -22,4 +31,4 @@
2231
console.log("### " + this.text + " ready!");
2332
},
2433
}
25-
</script>
34+
</script>

website/docs/guide/properties.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ See also [responsiveLayouts](#responsivelayouts), [breakpoints](#breakpoints) an
133133
* required: `false`
134134
* default: { lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0 }
135135

136-
Breakpoints defined for responsive layout,the parameter represents the width of different devices:lg(large), md(medium), sm(small), xs(extra small). Sets widths on wich column number changes
136+
Breakpoints defined for responsive layout, the parameter represents the width of different devices:lg(large), md(medium), sm(small), xs(extra small). Sets widths on wich column number changes
137137

138138
See also [responsiveLayouts](#responsivelayouts) and [cols](#cols)
139139

@@ -306,3 +306,11 @@ The value is `css-like` selector string.
306306

307307
For more info please refer to `ignoreFrom` in [interact.js docs](http://interactjs.io/docs/#ignorable-selectors).
308308

309+
310+
### preserveAspectRatio
311+
312+
* type: `Boolean`
313+
* required: `false`
314+
* default: `'false'`
315+
316+
If 'true', forces the GridItem to preserve its aspect ratio when resizing.

0 commit comments

Comments
 (0)