gridstack.js 30.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//     gridstack.js 0.2.3-dev
//     http://troolee.github.io/gridstack.js/
//     (c) 2014-2015 Pavel Reznikov
//     gridstack.js may be freely distributed under the MIT license.

(function (factory) {
    if (typeof define === 'function' && define.amd) {
        define(['jquery', 'lodash', 'jquery-ui/core', 'jquery-ui/widget', 'jquery-ui/mouse', 'jquery-ui/draggable', 'jquery-ui/resizable'], factory);
    }
    else {
        factory(jQuery, _);
    }
})(function ($, _) {

    var scope = window;
Nicolas Widart's avatar
Nicolas Widart committed
16 17 18 19 20 21 22 23 24 25 26 27

    var Utils = {
        is_intercepted: function (a, b) {
            return !(a.x + a.width <= b.x || b.x + b.width <= a.x || a.y + a.height <= b.y || b.y + b.height <= a.y);
        },

        sort: function (nodes, dir, width) {
            width = width || _.chain(nodes).map(function (node) { return node.x + node.width; }).max().value();
            dir = dir != -1 ? 1 : -1;
            return _.sortBy(nodes, function (n) { return dir * (n.x + n.y * width); });
        },

28
        create_stylesheet: function (id) {
Nicolas Widart's avatar
Nicolas Widart committed
29
            var style = document.createElement("style");
30 31 32 33 34 35 36 37 38
            style.setAttribute("type", "text/css");
            style.setAttribute("data-gs-id", id);
            if (style.styleSheet) {
                style.styleSheet.cssText = "";
            }
            else {
                style.appendChild(document.createTextNode(""));
            }
            document.getElementsByTagName('head')[0].appendChild(style);
Nicolas Widart's avatar
Nicolas Widart committed
39 40 41
            return style.sheet;
        },

42 43 44 45 46 47 48 49 50
        insert_css_rule: function (sheet, selector, rules, index) {
            if(typeof sheet.insertRule === 'function') {
                sheet.insertRule(selector + "{" + rules + "}", index);
            }
            else if(typeof sheet.addRule === 'function') {
                sheet.addRule(selector, rules, index);
            }
        },

Nicolas Widart's avatar
Nicolas Widart committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        toBool: function (v) {
            if (typeof v == 'boolean')
                return v;
            if (typeof v == 'string') {
                v = v.toLowerCase();
                return !(v == '' || v == 'no' || v == 'false' || v == '0');
            }
            return Boolean(v);
        }
    };

    var id_seq = 0;

    var GridStackEngine = function (width, onchange, float, height, items) {
        this.width = width;
        this.float = float || false;
        this.height = height || 0;

        this.nodes = items || [];
        this.onchange = onchange || function () {};
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

        this._update_counter = 0;
        this._float = this.float;
    };

    GridStackEngine.prototype.batch_update = function () {
        this._update_counter = 1;
        this.float = true;
    };

    GridStackEngine.prototype.commit = function () {
        this._update_counter = 0;
        if (this._update_counter == 0) {
            this.float = this._float;
            this._pack_nodes();
            this._notify();
        }
Nicolas Widart's avatar
Nicolas Widart committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    };

    GridStackEngine.prototype._fix_collisions = function (node) {
        this._sort_nodes(-1);

        var nn = node, has_locked = Boolean(_.find(this.nodes, function (n) { return n.locked }));
        if (!this.float && !has_locked) {
            nn = {x: 0, y: node.y, width: this.width, height: node.height};
        }

        while (true) {
            var collision_node = _.find(this.nodes, function (n) {
                return n != node && Utils.is_intercepted(n, nn);
            }, this);
            if (typeof collision_node == 'undefined') {
                return;
            }
            this.move_node(collision_node, collision_node.x, node.y + node.height,
                collision_node.width, collision_node.height, true);
        }
    };

110 111 112 113 114 115 116 117
    GridStackEngine.prototype.is_area_empty = function (x, y, width, height) {
        var nn = {x: x || 0, y: y || 0, width: width || 1, height: height || 1};
        var collision_node = _.find(this.nodes, function (n) {
            return Utils.is_intercepted(n, nn);
        }, this);
        return collision_node == null;
    };

Nicolas Widart's avatar
Nicolas Widart committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    GridStackEngine.prototype._sort_nodes = function (dir) {
        this.nodes = Utils.sort(this.nodes, dir, this.width);
    };

    GridStackEngine.prototype._pack_nodes = function () {
        this._sort_nodes();

        if (this.float) {
            _.each(this.nodes, function (n, i) {
                if (n._updating || typeof n._orig_y == 'undefined' || n.y == n._orig_y)
                    return;

                var new_y = n.y;
                while (new_y >= n._orig_y) {
                    var collision_node = _.chain(this.nodes)
                        .find(function (bn) {
                            return n != bn && Utils.is_intercepted({x: n.x, y: new_y, width: n.width, height: n.height}, bn);
                        })
                        .value();

                    if (!collision_node) {
                        n._dirty = true;
                        n.y = new_y;
                    }
                    --new_y;
                }
            }, this);
        }
        else {
            _.each(this.nodes, function (n, i) {
                if (n.locked)
                    return;
                while (n.y > 0) {
                    var new_y = n.y - 1;
                    var can_be_moved = i == 0;

                    if (i > 0) {
                        var collision_node = _.chain(this.nodes)
156
                            .take(i)
Nicolas Widart's avatar
Nicolas Widart committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
                            .find(function (bn) {
                                return Utils.is_intercepted({x: n.x, y: new_y, width: n.width, height: n.height}, bn);
                            })
                            .value();
                        can_be_moved = typeof collision_node == 'undefined';
                    }

                    if (!can_be_moved) {
                        break;
                    }
                    n._dirty = n.y != new_y;
                    n.y = new_y;
                }
            }, this);
        }
    };

    GridStackEngine.prototype._prepare_node = function (node, resizing) {
        node = _.defaults(node || {}, {width: 1, height: 1, x: 0, y: 0 });

        node.x = parseInt('' + node.x);
        node.y = parseInt('' + node.y);
        node.width = parseInt('' + node.width);
        node.height = parseInt('' + node.height);
        node.auto_position = node.auto_position || false;
        node.no_resize = node.no_resize || false;
        node.no_move = node.no_move || false;

        if (node.width > this.width) {
            node.width = this.width;
        }
        else if (node.width < 1) {
            node.width = 1;
        }

        if (node.height < 1) {
            node.height = 1;
        }

        if (node.x < 0) {
            node.x = 0;
        }

        if (node.x + node.width > this.width) {
            if (resizing) {
                node.width = this.width - node.x;
            }
            else {
                node.x = this.width - node.width;
            }
        }

        if (node.y < 0) {
            node.y = 0;
        }

        return node;
    };

    GridStackEngine.prototype._notify = function () {
217 218 219
        if (this._update_counter) {
            return;
        }
Nicolas Widart's avatar
Nicolas Widart committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
        var deleted_nodes = Array.prototype.slice.call(arguments, 1).concat(this.get_dirty_nodes());
        deleted_nodes = deleted_nodes.concat(this.get_dirty_nodes());
        this.onchange(deleted_nodes);
    };

    GridStackEngine.prototype.clean_nodes = function () {
        _.each(this.nodes, function (n) {n._dirty = false });
    };

    GridStackEngine.prototype.get_dirty_nodes = function () {
        return _.filter(this.nodes, function (n) { return n._dirty; });
    };

    GridStackEngine.prototype.add_node = function(node) {
        node = this._prepare_node(node);

        if (typeof node.max_width != 'undefined') node.width = Math.min(node.width, node.max_width);
        if (typeof node.max_height != 'undefined') node.height = Math.min(node.height, node.max_height);
        if (typeof node.min_width != 'undefined') node.width = Math.max(node.width, node.min_width);
        if (typeof node.min_height != 'undefined') node.height = Math.max(node.height, node.min_height);

        node._id = ++id_seq;
        node._dirty = true;

        if (node.auto_position) {
            this._sort_nodes();

            for (var i = 0; ; ++i) {
                var x = i % this.width, y = Math.floor(i / this.width);
                if (x + node.width > this.width) {
                    continue;
                }
                if (!_.find(this.nodes, function (n) {
                    return Utils.is_intercepted({x: x, y: y, width: node.width, height: node.height}, n);
                })) {
                    node.x = x;
                    node.y = y;
                    break;
                }
            }
        }

        this.nodes.push(node);

        this._fix_collisions(node);
        this._pack_nodes();
        this._notify();
        return node;
    };

    GridStackEngine.prototype.remove_node = function (node) {
        node._id = null;
        this.nodes = _.without(this.nodes, node);
        this._pack_nodes();
        this._notify(node);
    };

    GridStackEngine.prototype.can_move_node = function (node, x, y, width, height) {
        var has_locked = Boolean(_.find(this.nodes, function (n) { return n.locked }));

        if (!this.height && !has_locked)
            return true;

        var cloned_node;
        var clone = new GridStackEngine(
            this.width,
            null,
            this.float,
            0,
            _.map(this.nodes, function (n) { if (n == node) { cloned_node = $.extend({}, n); return cloned_node; } return $.extend({}, n) }));

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

        var res = true;

        if (has_locked)
            res &= !Boolean(_.find(clone.nodes, function (n) { return n != cloned_node && Boolean(n.locked) && Boolean(n._dirty); }));
        if (this.height)
            res &= clone.get_grid_height() <= this.height;

        return res;
    };

    GridStackEngine.prototype.can_be_placed_with_respect_to_height = function (node) {
        if (!this.height)
            return true;

        var clone = new GridStackEngine(
            this.width,
            null,
            this.float,
            0,
            _.map(this.nodes, function (n) { return $.extend({}, n) }));
        clone.add_node(node);
        return clone.get_grid_height() <= this.height;
    };

    GridStackEngine.prototype.move_node = function (node, x, y, width, height, no_pack) {
        if (typeof x != 'number') x = node.x;
        if (typeof y != 'number') y = node.y;
        if (typeof width != 'number') width = node.width;
        if (typeof height != 'number') height = node.height;

        if (typeof node.max_width != 'undefined') width = Math.min(width, node.max_width);
        if (typeof node.max_height != 'undefined') height = Math.min(height, node.max_height);
        if (typeof node.min_width != 'undefined') width = Math.max(width, node.min_width);
        if (typeof node.min_height != 'undefined') height = Math.max(height, node.min_height);

        if (node.x == x && node.y == y && node.width == width && node.height == height) {
            return node;
        }

        var resizing = node.width != width;
        node._dirty = true;

        node.x = x;
        node.y = y;
        node.width = width;
        node.height = height;

        node = this._prepare_node(node, resizing);

        this._fix_collisions(node);
        if (!no_pack) {
            this._pack_nodes();
            this._notify();
        }
        return node;
    };

    GridStackEngine.prototype.get_grid_height = function () {
        return _.reduce(this.nodes, function (memo, n) { return Math.max(memo, n.y + n.height); }, 0);
    };

    GridStackEngine.prototype.begin_update = function (node) {
        _.each(this.nodes, function (n) {
            n._orig_y = n.y;
        });
        node._updating = true;
    };

    GridStackEngine.prototype.end_update = function () {
362 363 364
        _.each(this.nodes, function (n) {
            n._orig_y = n.y;
        });
Nicolas Widart's avatar
Nicolas Widart committed
365 366 367 368 369 370 371 372 373 374 375
        var n = _.find(this.nodes, function (n) { return n._updating; });
        if (n) {
            n._updating = false;
        }
    };

    var GridStack = function (el, opts) {
        var self = this, one_column_mode;

        this.container = $(el);

376 377 378
        opts.item_class = opts.item_class || 'grid-stack-item';
        var is_nested = this.container.closest('.' + opts.item_class).size() > 0;

Nicolas Widart's avatar
Nicolas Widart committed
379 380 381 382 383 384 385 386 387 388 389 390
        this.opts = _.defaults(opts || {}, {
            width: parseInt(this.container.attr('data-gs-width')) || 12,
            height: parseInt(this.container.attr('data-gs-height')) || 0,
            item_class: 'grid-stack-item',
            placeholder_class: 'grid-stack-placeholder',
            handle: '.grid-stack-item-content',
            cell_height: 60,
            vertical_margin: 20,
            auto: true,
            min_width: 768,
            float: false,
            _class: 'grid-stack-' + (Math.random() * 10000).toFixed(0),
391 392 393 394 395 396 397 398 399 400 401
            animate: Boolean(this.container.attr('data-gs-animate')) || false,
            always_show_resize_handle: opts.always_show_resize_handle || false,
            resizable: _.defaults(opts.resizable || {}, {
                autoHide: !(opts.always_show_resize_handle || false),
                handles: 'se'
            }),
            draggable: _.defaults(opts.draggable || {}, {
                handle: '.grid-stack-item-content',
                scroll: false,
                appendTo: 'body'
            })
Nicolas Widart's avatar
Nicolas Widart committed
402
        });
403
        this.opts.is_nested = is_nested;
Nicolas Widart's avatar
Nicolas Widart committed
404 405

        this.container.addClass(this.opts._class);
406 407 408 409 410
        if (is_nested) {
            this.container.addClass('grid-stack-nested');
        }

        this._init_styles();
Nicolas Widart's avatar
Nicolas Widart committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431

        this.grid = new GridStackEngine(this.opts.width, function (nodes) {
            var max_height = 0;
            _.each(nodes, function (n) {
                if (n._id == null) {
                    n.el.remove();
                }
                else {
                    n.el
                        .attr('data-gs-x', n.x)
                        .attr('data-gs-y', n.y)
                        .attr('data-gs-width', n.width)
                        .attr('data-gs-height', n.height);
                    max_height = Math.max(max_height, n.y + n.height);
                }
            });
            self._update_styles(max_height + 10);
        }, this.opts.float, this.opts.height);

        if (this.opts.auto) {
            var elements = [];
432 433
            var _this = this;
            this.container.children('.' + this.opts.item_class).each(function (index, el) {
Nicolas Widart's avatar
Nicolas Widart committed
434 435 436
                el = $(el);
                elements.push({
                    el: el,
437
                    i: parseInt(el.attr('data-gs-x')) + parseInt(el.attr('data-gs-y')) * _this.opts.width // Use opts.width as weight for Y
Nicolas Widart's avatar
Nicolas Widart committed
438 439 440 441
                });
            });
            _.chain(elements).sortBy(function (x) { return x.i; }).each(function (i) {
                self._prepare_element(i.el);
442
            }).value();
Nicolas Widart's avatar
Nicolas Widart committed
443 444 445 446 447 448 449 450 451 452 453 454 455 456
        }

        this.set_animation(this.opts.animate);

        this.placeholder = $('<div class="' + this.opts.placeholder_class + ' ' + this.opts.item_class + '"><div class="placeholder-content" /></div>').hide();
        this.container.height((this.grid.get_grid_height()) * (this.opts.cell_height + this.opts.vertical_margin) - this.opts.vertical_margin);

        var on_resize_handler = function () {
            if (self._is_one_column_mode()) {
                if (one_column_mode)
                    return;

                one_column_mode = true;

457
                self.grid._sort_nodes();
Nicolas Widart's avatar
Nicolas Widart committed
458
                _.each(self.grid.nodes, function (node) {
459 460
                    self.container.append(node.el);

Nicolas Widart's avatar
Nicolas Widart committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
                    if (!node.no_move) {
                        node.el.draggable('disable');
                    }
                    if (!node.no_resize) {
                        node.el.resizable('disable');
                    }
                });
            }
            else {
                if (!one_column_mode)
                    return;

                one_column_mode = false;

                _.each(self.grid.nodes, function (node) {
                    if (!node.no_move) {
                        node.el.draggable('enable');
                    }
                    if (!node.no_resize) {
                        node.el.resizable('enable');
                    }
                });
            }
        };

        $(window).resize(on_resize_handler);
        on_resize_handler();
    };

490 491 492 493 494 495 496 497 498 499
    GridStack.prototype._init_styles = function () {
        if (this._styles_id) {
            $('[data-gs-id="' + this._styles_id + '"]').remove();
        }
        this._styles_id = 'gridstack-style-' + (Math.random() * 100000).toFixed();
        this._styles = Utils.create_stylesheet(this._styles_id);
        if (this._styles != null)
            this._styles._max = 0;
    };

Nicolas Widart's avatar
Nicolas Widart committed
500
    GridStack.prototype._update_styles = function (max_height) {
501 502 503 504 505 506
        if (this._styles == null) {
            return;
        }

        var prefix = '.' + this.opts._class + ' .' + this.opts.item_class;

Nicolas Widart's avatar
Nicolas Widart committed
507 508
        if (typeof max_height == 'undefined') {
            max_height = this._styles._max;
509
            this._init_styles();
Nicolas Widart's avatar
Nicolas Widart committed
510 511 512
            this._update_container_height();
        }

513 514 515 516
        if (this._styles._max == 0) {
            Utils.insert_css_rule(this._styles, prefix, 'min-height: ' + (this.opts.cell_height) + 'px;', 0);
        }

Nicolas Widart's avatar
Nicolas Widart committed
517 518
        if (max_height > this._styles._max) {
            for (var i = this._styles._max; i < max_height; ++i) {
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
                Utils.insert_css_rule(this._styles,
                    prefix + '[data-gs-height="' + (i + 1) + '"]',
                    'height: ' + (this.opts.cell_height * (i + 1) + this.opts.vertical_margin * i) + 'px;',
                    i
                );
                Utils.insert_css_rule(this._styles,
                    prefix + '[data-gs-min-height="' + (i + 1) + '"]',
                    'min-height: ' + (this.opts.cell_height * (i + 1) + this.opts.vertical_margin * i) + 'px;',
                    i
                );
                Utils.insert_css_rule(this._styles,
                    prefix + '[data-gs-max-height="' + (i + 1) + '"]',
                    'max-height: ' + (this.opts.cell_height * (i + 1) + this.opts.vertical_margin * i) + 'px;',
                    i
                );
                Utils.insert_css_rule(this._styles,
                    prefix + '[data-gs-y="' + i + '"]',
                    'top: ' + (this.opts.cell_height * i + this.opts.vertical_margin * i) + 'px;',
                    i
                );
Nicolas Widart's avatar
Nicolas Widart committed
539 540 541 542 543 544
            }
            this._styles._max = max_height;
        }
    };

    GridStack.prototype._update_container_height = function () {
545 546 547
        if (this.grid._update_counter) {
            return;
        }
Nicolas Widart's avatar
Nicolas Widart committed
548 549 550 551
        this.container.height(this.grid.get_grid_height() * (this.opts.cell_height + this.opts.vertical_margin) - this.opts.vertical_margin);
    };

    GridStack.prototype._is_one_column_mode = function () {
552
        return (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth) <= this.opts.min_width;
Nicolas Widart's avatar
Nicolas Widart committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
    };

    GridStack.prototype._prepare_element = function (el) {
        var self = this;
        el = $(el);

        el.addClass(this.opts.item_class);

        var node = self.grid.add_node({
            x: el.attr('data-gs-x'),
            y: el.attr('data-gs-y'),
            width: el.attr('data-gs-width'),
            height: el.attr('data-gs-height'),
            max_width: el.attr('data-gs-max-width'),
            min_width: el.attr('data-gs-min-width'),
568
            max_height: el.attr('data-gs-max-height'),
Nicolas Widart's avatar
Nicolas Widart committed
569 570 571 572 573 574 575 576 577 578 579 580
            min_height: el.attr('data-gs-min-height'),
            auto_position: Utils.toBool(el.attr('data-gs-auto-position')),
            no_resize: Utils.toBool(el.attr('data-gs-no-resize')),
            no_move: Utils.toBool(el.attr('data-gs-no-move')),
            locked: Utils.toBool(el.attr('data-gs-locked')),
            el: el
        });
        el.data('_gridstack_node', node);

        var cell_width, cell_height;

        var on_start_moving = function (event, ui) {
581
            self.container.append(self.placeholder);
Nicolas Widart's avatar
Nicolas Widart committed
582 583 584 585 586 587 588 589 590 591 592 593
            var o = $(this);
            self.grid.clean_nodes();
            self.grid.begin_update(node);
            cell_width = Math.ceil(o.outerWidth() / o.attr('data-gs-width'));
            cell_height = self.opts.cell_height + self.opts.vertical_margin;
            self.placeholder
                .attr('data-gs-x', o.attr('data-gs-x'))
                .attr('data-gs-y', o.attr('data-gs-y'))
                .attr('data-gs-width', o.attr('data-gs-width'))
                .attr('data-gs-height', o.attr('data-gs-height'))
                .show();
            node.el = self.placeholder;
594 595 596

            el.resizable('option', 'minWidth', cell_width * (node.min_width || 1));
            el.resizable('option', 'minHeight', self.opts.cell_height * (node.min_height || 1));
Nicolas Widart's avatar
Nicolas Widart committed
597 598 599
        };

        var on_end_moving = function (event, ui) {
600
            self.placeholder.detach();
Nicolas Widart's avatar
Nicolas Widart committed
601 602 603 604 605 606 607 608 609 610
            var o = $(this);
            node.el = o;
            self.placeholder.hide();
            o
                .attr('data-gs-x', node.x)
                .attr('data-gs-y', node.y)
                .attr('data-gs-width', node.width)
                .attr('data-gs-height', node.height)
                .removeAttr('style');
            self._update_container_height();
611 612 613
            var elements = self.grid.get_dirty_nodes();
            if (elements && elements.length)
                self.container.trigger('change', [elements]);
Nicolas Widart's avatar
Nicolas Widart committed
614 615 616 617

            self.grid.end_update();
        };

618
        el.draggable(_.extend(this.opts.draggable, {
Nicolas Widart's avatar
Nicolas Widart committed
619 620 621 622 623 624 625 626 627 628
            start: on_start_moving,
            stop: on_end_moving,
            drag: function (event, ui) {
                var x = Math.round(ui.position.left / cell_width),
                    y = Math.floor((ui.position.top + cell_height/2) / cell_height);
                if (!self.grid.can_move_node(node, x, y, node.width, node.height)) {
                    return;
                }
                self.grid.move_node(node, x, y);
                self._update_container_height();
629 630 631
            },
            containment: this.opts.is_nested ? this.container.parent() : null
        })).resizable(_.extend(this.opts.resizable, {
Nicolas Widart's avatar
Nicolas Widart committed
632 633 634
            start: on_start_moving,
            stop: on_end_moving,
            resize: function (event, ui) {
635 636 637
                var x = Math.round(ui.position.left / cell_width),
                    y = Math.floor((ui.position.top + cell_height/2) / cell_height),
                    width = Math.round(ui.size.width / cell_width),
Nicolas Widart's avatar
Nicolas Widart committed
638
                    height = Math.round(ui.size.height / cell_height);
639
                if (!self.grid.can_move_node(node, x, y, width, height)) {
Nicolas Widart's avatar
Nicolas Widart committed
640 641
                    return;
                }
642
                self.grid.move_node(node, x, y, width, height);
Nicolas Widart's avatar
Nicolas Widart committed
643 644
                self._update_container_height();
            }
645
        }));
Nicolas Widart's avatar
Nicolas Widart committed
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676

        if (node.no_move || this._is_one_column_mode()) {
            el.draggable('disable');
        }

        if (node.no_resize || this._is_one_column_mode()) {
            el.resizable('disable');
        }

        el.attr('data-gs-locked', node.locked ? 'yes' : null);
    };

    GridStack.prototype.set_animation = function (enable) {
        if (enable) {
            this.container.addClass('grid-stack-animate');
        }
        else {
            this.container.removeClass('grid-stack-animate');
        }
    };

    GridStack.prototype.add_widget = function (el, x, y, width, height, auto_position) {
        el = $(el);
        if (typeof x != 'undefined') el.attr('data-gs-x', x);
        if (typeof y != 'undefined') el.attr('data-gs-y', y);
        if (typeof width != 'undefined') el.attr('data-gs-width', width);
        if (typeof height != 'undefined') el.attr('data-gs-height', height);
        if (typeof auto_position != 'undefined') el.attr('data-gs-auto-position', auto_position ? 'yes' : null);
        this.container.append(el);
        this._prepare_element(el);
        this._update_container_height();
677 678

        return el;
Nicolas Widart's avatar
Nicolas Widart committed
679 680 681 682 683 684 685
    };

    GridStack.prototype.will_it_fit = function (x, y, width, height, auto_position) {
        var node = {x: x, y: y, width: width, height: height, auto_position: auto_position};
        return this.grid.can_be_placed_with_respect_to_height(node);
    };

686 687
    GridStack.prototype.remove_widget = function (el, detach_node) {
        detach_node = typeof detach_node === 'undefined' ? true : detach_node;
Nicolas Widart's avatar
Nicolas Widart committed
688 689 690
        el = $(el);
        var node = el.data('_gridstack_node');
        this.grid.remove_node(node);
691
        el.removeData('_gridstack_node');
Nicolas Widart's avatar
Nicolas Widart committed
692
        this._update_container_height();
693 694
        if (detach_node)
            el.remove();
Nicolas Widart's avatar
Nicolas Widart committed
695 696
    };

697
    GridStack.prototype.remove_all = function (detach_node) {
Nicolas Widart's avatar
Nicolas Widart committed
698
        _.each(this.grid.nodes, function (node) {
699 700
            this.remove_widget(node.el, detach_node);
        }, this);
Nicolas Widart's avatar
Nicolas Widart committed
701 702 703 704 705 706 707 708 709
        this.grid.nodes = [];
        this._update_container_height();
    };

    GridStack.prototype.resizable = function (el, val) {
        el = $(el);
        el.each(function (index, el) {
            el = $(el);
            var node = el.data('_gridstack_node');
710
            if (typeof node == 'undefined' || node == null) {
Nicolas Widart's avatar
Nicolas Widart committed
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
                return;
            }

            node.no_resize = !(val || false);
            if (node.no_resize) {
                el.resizable('disable');
            }
            else {
                el.resizable('enable');
            }
        });
        return this;
    };

    GridStack.prototype.movable = function (el, val) {
        el = $(el);
        el.each(function (index, el) {
            el = $(el);
            var node = el.data('_gridstack_node');
730
            if (typeof node == 'undefined' || node == null) {
Nicolas Widart's avatar
Nicolas Widart committed
731 732 733 734 735 736 737 738 739 740 741 742 743 744
                return;
            }

            node.no_move = !(val || false);
            if (node.no_move) {
                el.draggable('disable');
            }
            else {
                el.draggable('enable');
            }
        });
        return this;
    };

745 746 747 748 749 750 751 752 753 754
    GridStack.prototype.disable = function () {
        this.movable(this.container.children('.' + this.opts.item_class), false);
        this.resizable(this.container.children('.' + this.opts.item_class), false);
    };

    GridStack.prototype.enable = function () {
        this.movable(this.container.children('.' + this.opts.item_class), true);
        this.resizable(this.container.children('.' + this.opts.item_class), true);
    };

Nicolas Widart's avatar
Nicolas Widart committed
755 756 757 758 759
    GridStack.prototype.locked = function (el, val) {
        el = $(el);
        el.each(function (index, el) {
            el = $(el);
            var node = el.data('_gridstack_node');
760
            if (typeof node == 'undefined' || node == null) {
Nicolas Widart's avatar
Nicolas Widart committed
761 762 763 764 765 766 767 768 769 770 771 772
                return;
            }

            node.locked = (val || false);
            el.attr('data-gs-locked', node.locked ? 'yes' : null);
        });
        return this;
    };

    GridStack.prototype._update_element = function (el, callback) {
        el = $(el).first();
        var node = el.data('_gridstack_node');
773
        if (typeof node == 'undefined' || node == null) {
Nicolas Widart's avatar
Nicolas Widart committed
774 775 776 777 778 779 780 781 782 783 784
            return;
        }

        var self = this;

        self.grid.clean_nodes();
        self.grid.begin_update(node);

        callback.call(this, el, node);

        self._update_container_height();
785 786 787
        var elements = self.grid.get_dirty_nodes();
        if (elements && elements.length)
            self.container.trigger('change', [elements]);
Nicolas Widart's avatar
Nicolas Widart committed
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809

        self.grid.end_update();
    };

    GridStack.prototype.resize = function (el, width, height) {
        this._update_element(el, function (el, node) {
            width = (width != null && typeof width != 'undefined') ? width : node.width;
            height = (height != null && typeof height != 'undefined') ? height : node.height;

            this.grid.move_node(node, node.x, node.y, width, height);
        });
    };

    GridStack.prototype.move = function (el, x, y) {
        this._update_element(el, function (el, node) {
            x = (x != null && typeof x != 'undefined') ? x : node.x;
            y = (y != null && typeof y != 'undefined') ? y : node.y;

            this.grid.move_node(node, x, y, node.width, node.height);
        });
    };

810 811 812 813 814 815 816 817 818 819 820
    GridStack.prototype.update = function (el, x, y, width, height) {
        this._update_element(el, function (el, node) {
            x = (x != null && typeof x != 'undefined') ? x : node.x;
            y = (y != null && typeof y != 'undefined') ? y : node.y;
            width = (width != null && typeof width != 'undefined') ? width : node.width;
            height = (height != null && typeof height != 'undefined') ? height : node.height;

            this.grid.move_node(node, x, y, width, height);
        });
    };

Nicolas Widart's avatar
Nicolas Widart committed
821 822 823 824 825 826 827 828 829 830 831 832
    GridStack.prototype.cell_height = function (val) {
        if (typeof val == 'undefined') {
            return this.opts.cell_height;
        }
        val = parseInt(val);
        if (val == this.opts.cell_height)
            return;
        this.opts.cell_height = val || this.opts.cell_height;
        this._update_styles();
    };

    GridStack.prototype.cell_width = function () {
833
        var o = this.container.children('.' + this.opts.item_class).first();
Nicolas Widart's avatar
Nicolas Widart committed
834 835 836
        return Math.ceil(o.outerWidth() / o.attr('data-gs-width'));
    };

837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
    GridStack.prototype.get_cell_from_pixel = function(position) {
        var containerPos = this.container.position();
        var relativeLeft = position.left - containerPos.left;
        var relativeTop = position.top - containerPos.top;

        var column_width = Math.floor(this.container.width() / this.opts.width);
        var row_height = this.opts.cell_height + this.opts.vertical_margin;

        return {x: Math.floor(relativeLeft / column_width), y: Math.floor(relativeTop / row_height)};
    };

    GridStack.prototype.batch_update = function () {
        this.grid.batch_update();
    };

    GridStack.prototype.commit = function () {
        this.grid.commit();
        this._update_container_height()
    };

    GridStack.prototype.is_area_empty = function (x, y, width, height) {
        return this.grid.is_area_empty(x, y, width, height);
    };

Nicolas Widart's avatar
Nicolas Widart committed
861 862 863 864 865 866 867 868 869 870 871 872
    scope.GridStackUI = GridStack;

    scope.GridStackUI.Utils = Utils;

    $.fn.gridstack = function (opts) {
        return this.each(function () {
            if (!$(this).data('gridstack')) {
                $(this).data('gridstack', new GridStack(this, opts));
            }
        });
    };

873 874
    return scope.GridStackUI;
});