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

Skip to content

Commit 80f3b33

Browse files
committed
locked widgets
1 parent 3c870a8 commit 80f3b33

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/gridstack.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@
207207
};
208208

209209
GridStackEngine.prototype.can_move_node = function (node, x, y, width, height) {
210-
if (!this.height)
210+
var has_locked = Boolean(_.find(this.nodes, function (n) { return n.locked }));
211+
212+
if (!this.height && !has_locked)
211213
return true;
212214

213215
var cloned_node;
@@ -220,7 +222,14 @@
220222

221223
clone.move_node(cloned_node, x, y, width, height);
222224

223-
return clone.get_grid_height() <= this.height;
225+
var res = true;
226+
227+
if (has_locked)
228+
res &= !Boolean(_.find(clone.nodes, function (n) { return n != cloned_node && Boolean(n.locked) && Boolean(n._dirty); }));
229+
if (this.height)
230+
res &= clone.get_grid_height() <= this.height;
231+
232+
return res;
224233
};
225234

226235
GridStackEngine.prototype.can_be_placed_with_respect_to_height = function (node) {
@@ -415,6 +424,7 @@
415424
auto_position: el.attr('data-gs-auto-position'),
416425
no_resize: el.attr('data-gs-no-resize'),
417426
no_move: el.attr('data-gs-no-move'),
427+
locked: el.attr('data-gs-locked'),
418428
el: el
419429
});
420430
el.data('_gridstack_node', node);
@@ -469,7 +479,7 @@
469479
drag: function (event, ui) {
470480
var x = Math.round(ui.position.left / cell_width),
471481
y = Math.floor((ui.position.top + cell_height/2) / cell_height);
472-
if (self.opts.height && !self.grid.can_move_node(node, x, y, node.width, node.height)) {
482+
if (!self.grid.can_move_node(node, x, y, node.width, node.height)) {
473483
return;
474484
}
475485
self.grid.move_node(node, x, y);
@@ -486,7 +496,7 @@
486496
resize: function (event, ui) {
487497
var width = Math.round(ui.size.width / cell_width),
488498
height = Math.round(ui.size.height / cell_height);
489-
if (self.opts.height && !self.grid.can_move_node(node, node.x, node.y, width, height)) {
499+
if (!self.grid.can_move_node(node, node.x, node.y, width, height)) {
490500
return;
491501
}
492502
self.grid.move_node(node, node.x, node.y, width, height);
@@ -501,6 +511,8 @@
501511
if (node.no_resize || this._is_one_column_mode()) {
502512
el.resizable('disable');
503513
}
514+
515+
el.attr('data-gs-locked', node.locked ? 'yes' : null);
504516
};
505517

506518
GridStack.prototype.set_animation = function (enable) {
@@ -585,6 +597,21 @@
585597
return this;
586598
};
587599

600+
GridStack.prototype.locked = function (el, val) {
601+
el = $(el);
602+
el.each(function (index, el) {
603+
el = $(el);
604+
var node = el.data('_gridstack_node');
605+
if (typeof node == 'undefined') {
606+
return;
607+
}
608+
609+
node.locked = (val || false);
610+
el.attr('data-gs-locked', node.locked ? 'yes' : null);
611+
});
612+
return this;
613+
};
614+
588615
GridStack.prototype._update_element = function (el, callback) {
589616
el = $(el).first();
590617
var node = el.data('_gridstack_node');

0 commit comments

Comments
 (0)