Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

destroy drag&drop when removing node(s) #1209

Merged
merged 1 commit into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion demo/float.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ <h1>Float grid demo</h1>
<div class="grid-stack"></div>
</div>


<script type="text/javascript">
var grid = GridStack.init({float: true});

grid.on('added removed change', function(e, items) {
var str = '';
items.forEach(function(item) { str += ' (x,y)=' + item.x + ',' + item.y; });
console.log(e.type + ' ' + items.length + ' items:' + str );
});

var items = [
{x: 2, y: 1, width: 1, height: 1},
{x: 2, y: 3, width: 3, height: 1},
Expand Down
1 change: 1 addition & 0 deletions doc/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Change log
## 1.1.0-dev (upcoming)

- fix [1187](https://github.com/gridstack/gridstack.js/issues/1187) IE support for `CustomEvent` polyfill - thanks [@phil-blais](https://github.com/phil-blais)
- fix [1204](https://github.com/gridstack/gridstack.js/issues/1204) destroy drag&drop when removing node(s) instead of just disabling it.
- include SASS source files to npm package again [1193](https://github.com/gridstack/gridstack.js/pull/1193)

## 1.1.0 (2020-02-29)
Expand Down
4 changes: 2 additions & 2 deletions src/gridstack.jQueryUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

JQueryUIGridStackDragDropPlugin.prototype.resizable = function(el, opts) {
el = $(el);
if (opts === 'disable' || opts === 'enable') {
if (opts === 'disable' || opts === 'enable' || opts === 'destroy') {
el.resizable(opts);
} else if (opts === 'option') {
var key = arguments[2];
Expand All @@ -53,7 +53,7 @@

JQueryUIGridStackDragDropPlugin.prototype.draggable = function(el, opts) {
el = $(el);
if (opts === 'disable' || opts === 'enable') {
if (opts === 'disable' || opts === 'enable' || opts === 'destroy') {
el.draggable(opts);
} else {
el.draggable($.extend({}, this.grid.opts.draggable, {
Expand Down
20 changes: 12 additions & 8 deletions src/gridstack.js
Original file line number Diff line number Diff line change
Expand Up @@ -1505,34 +1505,38 @@
};

GridStack.prototype.removeWidget = function(el, detachNode) {
detachNode = (detachNode === undefined ? true : detachNode);
el = $(el);
var node = el.data('_gridstack_node');
// For Meteor support: https://github.com/gridstack/gridstack.js/pull/272
if (!node) {
node = this.engine.getNodeDataByDOMEl(el.get(0));
}

// remove our DOM data (circular link) and drag&drop permanently
el.removeData('_gridstack_node');
this.dd.draggable(el, 'destroy').resizable(el, 'destroy');

this.engine.removeNode(node, detachNode);
this._triggerRemoveEvent();
this._triggerChangeEvent(true); // trigger any other changes
};

GridStack.prototype.removeAll = function(detachNode) {
if (detachNode !== false) {
// remove our data structure before list gets emptied and DOM elements stay behind
this.engine.nodes.forEach(function(node) { $(node.el).removeData('_gridstack_node') });
}
// always remove our DOM data (circular link) before list gets emptied and drag&drop permanently
this.engine.nodes.forEach(function(node) {
var el = $(node.el);
el.removeData('_gridstack_node');
this.dd.draggable(el, 'destroy').resizable(el, 'destroy');
}, this);

this.engine.removeAll(detachNode);
this._triggerRemoveEvent();
};

GridStack.prototype.destroy = function(detachGrid) {
$(window).off('resize', this.onResizeHandler);
this.disable();
if (detachGrid !== undefined && !detachGrid) {
if (detachGrid === false) {
this.removeAll(false);
this.$el.removeClass(this.opts._class);
delete this.$el.get(0).gridstack;
} else {
this.$el.remove();
Expand Down