|
534 | 534 | }), |
535 | 535 | disableDrag: opts.disableDrag || false, |
536 | 536 | disableResize: opts.disableResize || false, |
537 | | - rtl: 'auto' |
| 537 | + rtl: 'auto', |
| 538 | + removable: false, |
| 539 | + removeTimeout: 2000 |
538 | 540 | }); |
539 | 541 |
|
540 | 542 | if (this.opts.rtl === 'auto') { |
|
805 | 807 |
|
806 | 808 | var cellWidth; |
807 | 809 | var cellHeight; |
| 810 | + var removeTimeout; |
| 811 | + |
| 812 | + var setupRemovingTimeout = function() { |
| 813 | + if (removeTimeout || !self.opts.removable) { |
| 814 | + return; |
| 815 | + } |
| 816 | + removeTimeout = setTimeout(function() { |
| 817 | + el.addClass('grid-stack-item-removing'); |
| 818 | + node._isAboutToRemove = true; |
| 819 | + }, self.opts.removeTimeout); |
| 820 | + }; |
| 821 | + var clearRemovingTimeout = function() { |
| 822 | + if (!removeTimeout) { |
| 823 | + return; |
| 824 | + } |
| 825 | + clearTimeout(removeTimeout); |
| 826 | + removeTimeout = null; |
| 827 | + el.removeClass('grid-stack-item-removing'); |
| 828 | + node._isAboutToRemove = false; |
| 829 | + }; |
808 | 830 |
|
809 | 831 | var dragOrResize = function(event, ui) { |
810 | 832 | var x = Math.round(ui.position.left / cellWidth); |
811 | 833 | var y = Math.floor((ui.position.top + cellHeight / 2) / cellHeight); |
812 | 834 | var width; |
813 | 835 | var height; |
| 836 | + |
814 | 837 | if (event.type != 'drag') { |
815 | 838 | width = Math.round(ui.size.width / cellWidth); |
816 | 839 | height = Math.round(ui.size.height / cellHeight); |
817 | 840 | } |
818 | 841 |
|
| 842 | + if (event.type == 'drag') { |
| 843 | + if (x < 0 || x >= self.grid.width || y < 0) { |
| 844 | + setupRemovingTimeout(); |
| 845 | + |
| 846 | + x = node._beforeDragX; |
| 847 | + y = node._beforeDragY; |
| 848 | + |
| 849 | + self.placeholder.detach(); |
| 850 | + self.placeholder.hide(); |
| 851 | + self.grid.removeNode(node); |
| 852 | + self._updateContainerHeight(); |
| 853 | + |
| 854 | + node._temporaryRemoved = true; |
| 855 | + } else { |
| 856 | + clearRemovingTimeout(); |
| 857 | + |
| 858 | + if (node._temporaryRemoved) { |
| 859 | + self.grid.addNode(node); |
| 860 | + self.placeholder |
| 861 | + .attr('data-gs-x', x) |
| 862 | + .attr('data-gs-y', y) |
| 863 | + .attr('data-gs-width', width) |
| 864 | + .attr('data-gs-height', height) |
| 865 | + .show(); |
| 866 | + self.container.append(self.placeholder); |
| 867 | + node.el = self.placeholder; |
| 868 | + node._temporaryRemoved = false; |
| 869 | + } |
| 870 | + } |
| 871 | + } else if (event.type == 'resize') { |
| 872 | + if (x < 0) { |
| 873 | + return; |
| 874 | + } |
| 875 | + } |
| 876 | + |
819 | 877 | if (!self.grid.canMoveNode(node, x, y, width, height)) { |
820 | 878 | return; |
821 | 879 | } |
|
838 | 896 | .attr('data-gs-height', o.attr('data-gs-height')) |
839 | 897 | .show(); |
840 | 898 | node.el = self.placeholder; |
| 899 | + node._beforeDragX = node.x; |
| 900 | + node._beforeDragY = node.y; |
841 | 901 |
|
842 | 902 | el.resizable('option', 'minWidth', cellWidth * (node.minWidth || 1)); |
843 | 903 | el.resizable('option', 'minHeight', strictCellHeight * (node.minHeight || 1)); |
|
848 | 908 | }; |
849 | 909 |
|
850 | 910 | var onEndMoving = function(event, ui) { |
| 911 | + var forceNotify = false; |
851 | 912 | self.placeholder.detach(); |
852 | 913 | var o = $(this); |
853 | 914 | node.el = o; |
854 | 915 | self.placeholder.hide(); |
855 | | - o |
856 | | - .attr('data-gs-x', node.x) |
857 | | - .attr('data-gs-y', node.y) |
858 | | - .attr('data-gs-width', node.width) |
859 | | - .attr('data-gs-height', node.height) |
860 | | - .removeAttr('style'); |
| 916 | + |
| 917 | + if (node._isAboutToRemove) { |
| 918 | + forceNotify = true; |
| 919 | + el.removeData('_gridstack_node'); |
| 920 | + el.remove(); |
| 921 | + } else { |
| 922 | + clearRemovingTimeout(); |
| 923 | + if (!node._temporaryRemoved) { |
| 924 | + o |
| 925 | + .attr('data-gs-x', node.x) |
| 926 | + .attr('data-gs-y', node.y) |
| 927 | + .attr('data-gs-width', node.width) |
| 928 | + .attr('data-gs-height', node.height) |
| 929 | + .removeAttr('style'); |
| 930 | + } else { |
| 931 | + o |
| 932 | + .attr('data-gs-x', node._beforeDragX) |
| 933 | + .attr('data-gs-y', node._beforeDragY) |
| 934 | + .attr('data-gs-width', node.width) |
| 935 | + .attr('data-gs-height', node.height) |
| 936 | + .removeAttr('style'); |
| 937 | + node.x = node._beforeDragX; |
| 938 | + node.y = node._beforeDragY; |
| 939 | + self.grid.addNode(node); |
| 940 | + } |
| 941 | + } |
861 | 942 | self._updateContainerHeight(); |
862 | | - self._triggerChangeEvent(); |
| 943 | + self._triggerChangeEvent(forceNotify); |
863 | 944 |
|
864 | 945 | self.grid.endUpdate(); |
865 | 946 |
|
|
0 commit comments