|
16 | 16 | };
|
17 | 17 |
|
18 | 18 | 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); |
20 | 20 |
|
21 | 21 | while (true) {
|
22 | 22 | var collision_node = _.find(this.nodes, function (n) {
|
|
30 | 30 | }
|
31 | 31 | };
|
32 | 32 |
|
| 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 | + |
33 | 38 | 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(); |
35 | 40 |
|
36 | 41 | _.each(this.nodes, function (n, i) {
|
37 | 42 | while (n.y > 0) {
|
|
123 | 128 | node._dirty = true;
|
124 | 129 |
|
125 | 130 | if (node.auto_position) {
|
126 |
| - this.nodes = _.sortBy(this.nodes, function (n) { return n.x + n.y * this.width; }, this); |
| 131 | + this._sort_nodes(); |
127 | 132 |
|
128 | 133 | for (var i = 0; ; ++i) {
|
129 | 134 | var x = i % this.width, y = Math.floor(i / this.width);
|
|
190 | 195 | };
|
191 | 196 |
|
192 | 197 | var GridStack = function (el, opts) {
|
193 |
| - var self = this; |
| 198 | + var self = this, one_column_mode; |
194 | 199 |
|
195 | 200 | this.container = $(el);
|
196 | 201 |
|
|
201 | 206 | handle: '.grid-stack-item-content',
|
202 | 207 | cell_height: 60,
|
203 | 208 | vertical_margin: 20,
|
204 |
| - auto: true |
| 209 | + auto: true, |
| 210 | + min_width: 768 |
205 | 211 | });
|
206 | 212 |
|
207 | 213 | this.grid = new GridStackEngine(this.opts.width, function (nodes) {
|
208 | 214 | _.each(nodes, function (n) {
|
209 | 215 | if (n._id == null) {
|
210 |
| - //n.el.remove(); |
| 216 | + n.el.remove(); |
211 | 217 | }
|
212 | 218 | else {
|
213 | 219 | n.el
|
|
228 | 234 | this.placeholder = $('<div class="' + this.opts.placeholder_class + ' ' + this.opts.item_class + '"><div class="placeholder-content" /></div>').hide();
|
229 | 235 | this.container.append(this.placeholder);
|
230 | 236 | 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(); |
231 | 269 | };
|
232 | 270 |
|
233 | 271 | GridStack.prototype._update_container_height = function () {
|
234 | 272 | this.container.height(this.grid.get_grid_height() * (this.opts.cell_height + this.opts.vertical_margin) - this.opts.vertical_margin);
|
235 | 273 | };
|
236 | 274 |
|
| 275 | + GridStack.prototype._is_one_column_mode = function () { |
| 276 | + return $(window).width() <= this.opts.min_width; |
| 277 | + }; |
| 278 | + |
237 | 279 | GridStack.prototype._prepare_element = function (el) {
|
238 | 280 | var self = this;
|
239 | 281 | el = $(el);
|
|
279 | 321 | .removeAttr('style');
|
280 | 322 | self._update_container_height();
|
281 | 323 | 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 | + }); |
282 | 330 | };
|
283 | 331 |
|
284 | 332 | el.draggable({
|
|
295 | 343 | self._update_container_height();
|
296 | 344 | }
|
297 | 345 | });
|
| 346 | + |
| 347 | + if (this._is_one_column_mode()) { |
| 348 | + el.draggable('disable'); |
| 349 | + } |
| 350 | + |
298 | 351 | if (!el.attr('data-gs-no-resize')) {
|
299 | 352 | el.resizable({
|
300 | 353 | autoHide: true,
|
|
311 | 364 | self._update_container_height();
|
312 | 365 | }
|
313 | 366 | });
|
| 367 | + |
| 368 | + if (this._is_one_column_mode()) { |
| 369 | + el.resizable('disable'); |
| 370 | + } |
314 | 371 | }
|
315 | 372 | };
|
316 | 373 |
|
|
0 commit comments