|
50 | 50 | createStylesheet: function(id) {
|
51 | 51 | var style = document.createElement('style');
|
52 | 52 | style.setAttribute('type', 'text/css');
|
53 |
| - style.setAttribute('data-gs-id', id); |
| 53 | + style.setAttribute('data-gs-style-id', id); |
54 | 54 | if (style.styleSheet) {
|
55 | 55 | style.styleSheet.cssText = '';
|
56 | 56 | } else {
|
|
61 | 61 | },
|
62 | 62 |
|
63 | 63 | removeStylesheet: function(id) {
|
64 |
| - $('STYLE[data-gs-id=' + id + ']').remove(); |
| 64 | + $('STYLE[data-gs-style-id=' + id + ']').remove(); |
65 | 65 | },
|
66 | 66 |
|
67 | 67 | insertCSSRule: function(sheet, selector, rules, index) {
|
|
87 | 87 | return n != this.node && Utils.isIntercepted(n, this.nn);
|
88 | 88 | },
|
89 | 89 |
|
90 |
| - _didCollideFloat: function(bn) { |
91 |
| - return this.n != bn && |
92 |
| - Utils.isIntercepted({x: this.n.x, y: this.newY, width: this.n.width, height: this.n.height}, bn); |
93 |
| - }, |
94 |
| - |
95 | 90 | _didCollide: function(bn) {
|
96 | 91 | return Utils.isIntercepted({x: this.n.x, y: this.newY, width: this.n.width, height: this.n.height}, bn);
|
97 | 92 | },
|
|
108 | 103 | if (!match) {
|
109 | 104 | throw new Error('Invalid height');
|
110 | 105 | }
|
111 |
| - heightUnit = match[2]; |
| 106 | + heightUnit = match[2] || 'px'; |
112 | 107 | height = parseFloat(match[1]);
|
113 | 108 | }
|
114 | 109 | return {height: height, unit: heightUnit};
|
|
137 | 132 |
|
138 | 133 | this._updateCounter = 0;
|
139 | 134 | this._float = this.float;
|
| 135 | + |
| 136 | + this._addedNodes = []; |
| 137 | + this._removedNodes = []; |
140 | 138 | };
|
141 | 139 |
|
142 | 140 | GridStackEngine.prototype.batchUpdate = function() {
|
|
182 | 180 | var collisionNode = _.find(this.nodes, _.bind(function(n) {
|
183 | 181 | return Utils.isIntercepted(n, nn);
|
184 | 182 | }, this));
|
185 |
| - return collisionNode === null; |
| 183 | + return collisionNode === null || typeof collisionNode === 'undefined'; |
186 | 184 | };
|
187 | 185 |
|
188 | 186 | GridStackEngine.prototype._sortNodes = function(dir) {
|
|
282 | 280 | if (this._updateCounter) {
|
283 | 281 | return;
|
284 | 282 | }
|
285 |
| - var deletedNodes = Array.prototype.slice.call(arguments, 1).concat(this.getDirtyNodes()); |
| 283 | + var deletedNodes = Array.prototype.slice.call(arguments, 0); |
286 | 284 | deletedNodes = deletedNodes.concat(this.getDirtyNodes());
|
287 | 285 | this.onchange(deletedNodes);
|
288 | 286 | };
|
|
298 | 296 | return _.filter(this.nodes, function(n) { return n._dirty; });
|
299 | 297 | };
|
300 | 298 |
|
301 |
| - GridStackEngine.prototype.addNode = function(node) { |
| 299 | + GridStackEngine.prototype.addNode = function(node, triggerAddEvent) { |
302 | 300 | node = this._prepareNode(node);
|
303 | 301 |
|
304 | 302 | if (typeof node.maxWidth != 'undefined') { node.width = Math.min(node.width, node.maxWidth); }
|
|
327 | 325 | }
|
328 | 326 |
|
329 | 327 | this.nodes.push(node);
|
| 328 | + if (typeof triggerAddEvent != 'undefined' && triggerAddEvent) { |
| 329 | + this._addedNodes.push(_.clone(node)); |
| 330 | + } |
330 | 331 |
|
331 | 332 | this._fixCollisions(node);
|
332 | 333 | this._packNodes();
|
|
335 | 336 | };
|
336 | 337 |
|
337 | 338 | GridStackEngine.prototype.removeNode = function(node) {
|
| 339 | + this._removedNodes.push(_.clone(node)); |
338 | 340 | node._id = null;
|
339 | 341 | this.nodes = _.without(this.nodes, node);
|
340 | 342 | this._packNodes();
|
|
679 | 681 | }
|
680 | 682 | };
|
681 | 683 |
|
| 684 | + GridStack.prototype._triggerAddEvent = function() { |
| 685 | + if (this.grid._addedNodes && this.grid._addedNodes.length > 0) { |
| 686 | + this.container.trigger('added', [_.map(this.grid._addedNodes, _.clone)]); |
| 687 | + this.grid._addedNodes = []; |
| 688 | + } |
| 689 | + }; |
| 690 | + |
| 691 | + GridStack.prototype._triggerRemoveEvent = function() { |
| 692 | + if (this.grid._removedNodes && this.grid._removedNodes.length > 0) { |
| 693 | + this.container.trigger('removed', [_.map(this.grid._removedNodes, _.clone)]); |
| 694 | + this.grid._removedNodes = []; |
| 695 | + } |
| 696 | + }; |
| 697 | + |
682 | 698 | GridStack.prototype._initStyles = function() {
|
683 | 699 | if (this._stylesId) {
|
684 |
| - $('[data-gs-id="' + this._stylesId + '"]').remove(); |
| 700 | + Utils.removeStylesheet(this._stylesId); |
685 | 701 | }
|
686 | 702 | this._stylesId = 'gridstack-style-' + (Math.random() * 100000).toFixed();
|
687 | 703 | this._styles = Utils.createStylesheet(this._stylesId);
|
|
783 | 799 | this.opts.minWidth;
|
784 | 800 | };
|
785 | 801 |
|
786 |
| - GridStack.prototype._prepareElement = function(el) { |
| 802 | + GridStack.prototype._prepareElement = function(el, triggerAddEvent) { |
| 803 | + triggerAddEvent = typeof triggerAddEvent != 'undefined' ? triggerAddEvent : false; |
787 | 804 | var self = this;
|
788 | 805 | el = $(el);
|
789 | 806 |
|
|
801 | 818 | noResize: Utils.toBool(el.attr('data-gs-no-resize')),
|
802 | 819 | noMove: Utils.toBool(el.attr('data-gs-no-move')),
|
803 | 820 | locked: Utils.toBool(el.attr('data-gs-locked')),
|
804 |
| - el: el |
805 |
| - }); |
| 821 | + el: el, |
| 822 | + id: el.attr('data-gs-id') |
| 823 | + }, triggerAddEvent); |
806 | 824 | el.data('_gridstack_node', node);
|
807 | 825 |
|
808 | 826 | var cellWidth;
|
|
985 | 1003 | }
|
986 | 1004 | };
|
987 | 1005 |
|
988 |
| - GridStack.prototype.addWidget = function(el, x, y, width, height, autoPosition) { |
| 1006 | + GridStack.prototype.addWidget = function(el, x, y, width, height, autoPosition, minWidth, maxWidth, |
| 1007 | + minHeight, maxHeight, id) { |
989 | 1008 | el = $(el);
|
990 | 1009 | if (typeof x != 'undefined') { el.attr('data-gs-x', x); }
|
991 | 1010 | if (typeof y != 'undefined') { el.attr('data-gs-y', y); }
|
992 | 1011 | if (typeof width != 'undefined') { el.attr('data-gs-width', width); }
|
993 | 1012 | if (typeof height != 'undefined') { el.attr('data-gs-height', height); }
|
994 | 1013 | if (typeof autoPosition != 'undefined') { el.attr('data-gs-auto-position', autoPosition ? 'yes' : null); }
|
| 1014 | + if (typeof minWidth != 'undefined') { el.attr('data-gs-min-width', minWidth); } |
| 1015 | + if (typeof maxWidth != 'undefined') { el.attr('data-gs-max-width', maxWidth); } |
| 1016 | + if (typeof minHeight != 'undefined') { el.attr('data-gs-min-height', minHeight); } |
| 1017 | + if (typeof maxHeight != 'undefined') { el.attr('data-gs-max-height', maxHeight); } |
| 1018 | + if (typeof id != 'undefined') { el.attr('data-gs-id', id); } |
995 | 1019 | this.container.append(el);
|
996 |
| - this._prepareElement(el); |
| 1020 | + this._prepareElement(el, true); |
| 1021 | + this._triggerAddEvent(); |
997 | 1022 | this._updateContainerHeight();
|
998 | 1023 | this._triggerChangeEvent(true);
|
999 | 1024 |
|
|
1002 | 1027 |
|
1003 | 1028 | GridStack.prototype.makeWidget = function(el) {
|
1004 | 1029 | el = $(el);
|
1005 |
| - this._prepareElement(el); |
| 1030 | + this._prepareElement(el, true); |
| 1031 | + this._triggerAddEvent(); |
1006 | 1032 | this._updateContainerHeight();
|
1007 | 1033 | this._triggerChangeEvent(true);
|
1008 | 1034 |
|
|
1031 | 1057 | el.remove();
|
1032 | 1058 | }
|
1033 | 1059 | this._triggerChangeEvent(true);
|
| 1060 | + this._triggerRemoveEvent(); |
1034 | 1061 | };
|
1035 | 1062 |
|
1036 | 1063 | GridStack.prototype.removeAll = function(detachNode) {
|
|
1041 | 1068 | this._updateContainerHeight();
|
1042 | 1069 | };
|
1043 | 1070 |
|
1044 |
| - GridStack.prototype.destroy = function() { |
| 1071 | + GridStack.prototype.destroy = function(detachGrid) { |
1045 | 1072 | $(window).off('resize', this.onResizeHandler);
|
1046 | 1073 | this.disable();
|
1047 |
| - this.container.remove(); |
| 1074 | + if (typeof detachGrid != 'undefined' && !detachGrid) { |
| 1075 | + this.removeAll(true); |
| 1076 | + } else { |
| 1077 | + this.container.remove(); |
| 1078 | + } |
1048 | 1079 | Utils.removeStylesheet(this._stylesId);
|
1049 | 1080 | if (this.grid) {
|
1050 | 1081 | this.grid = null;
|
|
1296 | 1327 | return Math.ceil(o.outerWidth() / o.attr('data-gs-width'));
|
1297 | 1328 | };
|
1298 | 1329 |
|
1299 |
| - GridStack.prototype.getCellFromPixel = function(position) { |
1300 |
| - var containerPos = this.container.position(); |
| 1330 | + GridStack.prototype.getCellFromPixel = function(position, useOffset) { |
| 1331 | + var containerPos = (typeof useOffset != 'undefined' && useOffset) ? |
| 1332 | + this.container.offset() : this.container.position(); |
1301 | 1333 | var relativeLeft = position.left - containerPos.left;
|
1302 | 1334 | var relativeTop = position.top - containerPos.top;
|
1303 | 1335 |
|
|
1438 | 1470 | scope.GridStackUI = GridStack;
|
1439 | 1471 |
|
1440 | 1472 | scope.GridStackUI.Utils = Utils;
|
| 1473 | + scope.GridStackUI.Engine = GridStackEngine; |
1441 | 1474 |
|
1442 | 1475 | $.fn.gridstack = function(opts) {
|
1443 | 1476 | return this.each(function() {
|
|
0 commit comments