Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b92f6a0

Browse files
committed
one-column mode for small devices
1 parent ad192cc commit b92f6a0

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

gridstack.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,17 @@
128128
.grid-stack-item[data-gs-y="17"] { top: 1360px; }
129129
.grid-stack-item[data-gs-y="18"] { top: 1440px; }
130130
.grid-stack-item[data-gs-y="19"] { top: 1520px; }
131+
132+
@media (max-width: 768px) {
133+
.grid-stack-item {
134+
position: relative !important;
135+
width: auto !important;
136+
left: 0 !important;
137+
top: auto !important;
138+
margin-bottom: 20px;
139+
}
140+
141+
.grid-stack {
142+
height: auto !important;
143+
}
144+
}

gridstack.js

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
};
1717

1818
GridStackEngine.prototype._fix_collisions = function (node) {
19-
this.nodes = _.sortBy(this.nodes, function (n) { return -(n.x + n.y * this.width); }, this);
19+
this._sort_nodes(-1);
2020

2121
while (true) {
2222
var collision_node = _.find(this.nodes, function (n) {
@@ -30,8 +30,13 @@
3030
}
3131
};
3232

33+
GridStackEngine.prototype._sort_nodes = function (dir) {
34+
dir = dir != -1 ? 1 : -1;
35+
this.nodes = _.sortBy(this.nodes, function (n) { return dir * (n.x + n.y * this.width); }, this);
36+
};
37+
3338
GridStackEngine.prototype._pack_nodes = function () {
34-
this.nodes = _.sortBy(this.nodes, function (n) { return n.x + n.y * this.width; }, this);
39+
this._sort_nodes();
3540

3641
_.each(this.nodes, function (n, i) {
3742
while (n.y > 0) {
@@ -123,7 +128,7 @@
123128
node._dirty = true;
124129

125130
if (node.auto_position) {
126-
this.nodes = _.sortBy(this.nodes, function (n) { return n.x + n.y * this.width; }, this);
131+
this._sort_nodes();
127132

128133
for (var i = 0; ; ++i) {
129134
var x = i % this.width, y = Math.floor(i / this.width);
@@ -190,7 +195,7 @@
190195
};
191196

192197
var GridStack = function (el, opts) {
193-
var self = this;
198+
var self = this, one_column_mode;
194199

195200
this.container = $(el);
196201

@@ -201,13 +206,14 @@
201206
handle: '.grid-stack-item-content',
202207
cell_height: 60,
203208
vertical_margin: 20,
204-
auto: true
209+
auto: true,
210+
min_width: 768
205211
});
206212

207213
this.grid = new GridStackEngine(this.opts.width, function (nodes) {
208214
_.each(nodes, function (n) {
209215
if (n._id == null) {
210-
//n.el.remove();
216+
n.el.remove();
211217
}
212218
else {
213219
n.el
@@ -228,12 +234,48 @@
228234
this.placeholder = $('<div class="' + this.opts.placeholder_class + ' ' + this.opts.item_class + '"><div class="placeholder-content" /></div>').hide();
229235
this.container.append(this.placeholder);
230236
this.container.height((this.grid.get_grid_height()) * (this.opts.cell_height + this.opts.vertical_margin) - this.opts.vertical_margin);
237+
238+
var on_resize_handler = function () {
239+
if (self._is_one_column_mode()) {
240+
if (one_column_mode)
241+
return;
242+
243+
one_column_mode = true;
244+
245+
_.each(self.grid.nodes, function (node) {
246+
node.el.draggable('disable');
247+
if (!node.el.attr('data-gs-no-resize')) {
248+
node.el.resizable('disable');
249+
}
250+
});
251+
}
252+
else {
253+
if (!one_column_mode)
254+
return;
255+
256+
one_column_mode = false;
257+
258+
_.each(self.grid.nodes, function (node) {
259+
node.el.draggable('enable');
260+
if (!node.el.attr('data-gs-no-resize')) {
261+
node.el.resizable('enable');
262+
}
263+
});
264+
}
265+
};
266+
267+
$(window).resize(on_resize_handler);
268+
on_resize_handler();
231269
};
232270

233271
GridStack.prototype._update_container_height = function () {
234272
this.container.height(this.grid.get_grid_height() * (this.opts.cell_height + this.opts.vertical_margin) - this.opts.vertical_margin);
235273
};
236274

275+
GridStack.prototype._is_one_column_mode = function () {
276+
return $(window).width() <= this.opts.min_width;
277+
};
278+
237279
GridStack.prototype._prepare_element = function (el) {
238280
var self = this;
239281
el = $(el);
@@ -279,6 +321,12 @@
279321
.removeAttr('style');
280322
self._update_container_height();
281323
self.container.trigger('change', [self.grid.get_dirty_nodes()]);
324+
325+
self.grid._sort_nodes();
326+
_.each(self.grid.nodes, function (node) {
327+
node.el.detach();
328+
self.container.append(node.el);
329+
});
282330
};
283331

284332
el.draggable({
@@ -295,6 +343,11 @@
295343
self._update_container_height();
296344
}
297345
});
346+
347+
if (this._is_one_column_mode()) {
348+
el.draggable('disable');
349+
}
350+
298351
if (!el.attr('data-gs-no-resize')) {
299352
el.resizable({
300353
autoHide: true,
@@ -311,6 +364,10 @@
311364
self._update_container_height();
312365
}
313366
});
367+
368+
if (this._is_one_column_mode()) {
369+
el.resizable('disable');
370+
}
314371
}
315372
};
316373

0 commit comments

Comments
 (0)