├── .bowerrc ├── .gitignore ├── LICENSE ├── package.json ├── test ├── jquery.gridder_test.js └── jquery.gridder.html ├── README.md ├── src ├── utils.js ├── jquery.coords.js ├── jquery.gridster.css ├── jquery.gridster.extras.js ├── jquery.collision.js ├── jquery.draggable.js └── jquery.gridster.js ├── CONTRIBUTING.md ├── Gruntfile.js └── CHANGELOG.md /.bowerrc: -------------------------------------------------------------------------------- 1 | { 2 | "directory": "libs", 3 | "json": "package.json" 4 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | dist/ 3 | libs/ 4 | gh-pages/ 5 | demo/ 6 | .idea 7 | .DS_Store 8 | .idea 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Ducksboard 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gridster", 3 | "title": "gridster.js", 4 | "description": "a drag-and-drop multi-column jQuery grid plugin", 5 | "version": "0.5.6", 6 | "homepage": "http://gridster.net/", 7 | "author": { 8 | "name": "ducksboard", 9 | "email": "hackers@ducksboard.com" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git://github.com/ducksboard/gridster.js.git" 14 | }, 15 | "bugs": { 16 | "url": "https://github.com/ducksboard/gridster.js/issues" 17 | }, 18 | "licenses": [ 19 | { 20 | "type": "MIT", 21 | "url": "https://github.com/ducksboard/gridster.js/blob/master/LICENSE" 22 | } 23 | ], 24 | "keywords": [], 25 | "dependencies": { 26 | "jquery": "git+https://github.com/jquery/jquery.git#2.0.3" 27 | }, 28 | "devDependencies": { 29 | "grunt": "~0.4.1", 30 | "grunt-contrib-uglify": "~0.2.0", 31 | "grunt-contrib-jshint": "~0.3.0", 32 | "grunt-contrib-concat": "~0.1.3", 33 | "grunt-contrib-watch": "~0.3.1", 34 | "grunt-contrib-cssmin": "~0.5.0", 35 | "grunt-contrib-yuidoc": "~0.4.0", 36 | "bower": "~0.9.2", 37 | "qunitjs": "~1.11.0", 38 | "grunt-bump": "0.0.11", 39 | "grunt-conventional-changelog": "~1.0.0" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /test/jquery.gridder_test.js: -------------------------------------------------------------------------------- 1 | /*global QUnit:false, module:false, test:false, asyncTest:false, expect:false*/ 2 | /*global start:false, stop:false ok:false, equal:false, notEqual:false, deepEqual:false*/ 3 | /*global notDeepEqual:false, strictEqual:false, notStrictEqual:false, raises:false*/ 4 | (function($) { 5 | 6 | /* 7 | ======== A Handy Little QUnit Reference ======== 8 | http://docs.jquery.com/QUnit 9 | 10 | Test methods: 11 | expect(numAssertions) 12 | stop(increment) 13 | start(decrement) 14 | Test assertions: 15 | ok(value, [message]) 16 | equal(actual, expected, [message]) 17 | notEqual(actual, expected, [message]) 18 | deepEqual(actual, expected, [message]) 19 | notDeepEqual(actual, expected, [message]) 20 | strictEqual(actual, expected, [message]) 21 | notStrictEqual(actual, expected, [message]) 22 | raises(block, [expected], [message]) 23 | */ 24 | 25 | module('jQuery#gridster', { 26 | setup: function() { 27 | 28 | this.el = $('#qunit-fixture').find(".wrapper ul"); 29 | 30 | } 31 | }); 32 | 33 | // test('is chainable', 1, function() { 34 | // // Not a bad test to run on collection methods. 35 | // strictEqual(this.el, this.el.gridster(), 'should be chaninable'); 36 | // }); 37 | 38 | }(jQuery)); 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Gridster.js 2 | =========== 3 | 4 | [](https://bitdeli.com/free "Bitdeli Badge") 5 | 6 | Gridster is a jQuery plugin that makes building intuitive draggable 7 | layouts from elements spanning multiple columns. You can even 8 | dynamically add and remove elements from the grid. 9 | 10 | More at [http://gridster.net/](http://gridster.net/). 11 | 12 | [Releases](https://github.com/ducksboard/gridster.js/releases) 13 | 14 | [CHANGELOG](https://github.com/ducksboard/gridster.js/blob/master/CHANGELOG.md) 15 | 16 | Gridster is maintained by Ducksboard occasionally but not actively. 17 | @dustmoo and @pushmatrix have also write permissions as Gridster maintainers 18 | they are. Thank you guys! 19 | 20 | ## Forks 21 | 22 | Mr @dustmoo (maintainer of Gridster) has his own fork of gridster.js 23 | with some new interesting features like widget-swapping and static widgets. 24 | 25 | Can be found here: [dustmoo/gridster.js](https://github.com/dustmoo/gridster.js) 26 | 27 | @dustmoo is working in his spare time to merge all these changes into 28 | ducksboard/gridster.js 29 | 30 | If anyone would like to help @dustmoo improve his fork and reconcile 31 | it with the main library he would be happy for the help. 32 | 33 | 34 | ## Contributing to this project 35 | 36 | Anyone and everyone is welcome to contribute. Please take a moment to review the guidelines for contributing. 37 | 38 | * [Bug reports](CONTRIBUTING.md#bugs) 39 | * [Feature requests](CONTRIBUTING.md#features) 40 | * [Pull requests](CONTRIBUTING.md#pull-requests) 41 | 42 | 43 | ## License 44 | 45 | Distributed under the MIT license. 46 | 47 | ## Whodunit 48 | 49 | Gridster is built by [Ducksboard](http://ducksboard.com/) with the help of all 50 | these [wonderful people](https://github.com/ducksboard/gridster.js/graphs/contributors). 51 | -------------------------------------------------------------------------------- /test/jquery.gridder.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |size_x is limited to the space available from
447 | * the column where the widget begins, until the last column to the right.
448 | * @param {Number} size_y The number of rows that will occupy the widget.
449 | * @param {Function} [callback] Function executed when the widget is removed.
450 | * @return {HTMLElement} Returns $widget.
451 | */
452 | fn.resize_widget = function($widget, size_x, size_y, callback) {
453 | var wgd = $widget.coords().grid;
454 | var col = wgd.col;
455 | var max_cols = this.options.max_cols;
456 | var old_size_y = wgd.size_y;
457 | var old_col = wgd.col;
458 | var new_col = old_col;
459 |
460 | size_x || (size_x = wgd.size_x);
461 | size_y || (size_y = wgd.size_y);
462 |
463 | if (max_cols !== Infinity) {
464 | size_x = Math.min(size_x, max_cols - col + 1);
465 | }
466 |
467 | if (size_y > old_size_y) {
468 | this.add_faux_rows(Math.max(size_y - old_size_y, 0));
469 | }
470 |
471 | var player_rcol = (col + size_x - 1);
472 | if (player_rcol > this.cols) {
473 | this.add_faux_cols(player_rcol - this.cols);
474 | }
475 |
476 | var new_grid_data = {
477 | col: new_col,
478 | row: wgd.row,
479 | size_x: size_x,
480 | size_y: size_y
481 | };
482 |
483 | this.mutate_widget_in_gridmap($widget, wgd, new_grid_data);
484 |
485 | this.set_dom_grid_height();
486 | this.set_dom_grid_width();
487 |
488 | if (callback) {
489 | callback.call(this, new_grid_data.size_x, new_grid_data.size_y);
490 | }
491 |
492 | return $widget;
493 | };
494 |
495 | /**
496 | * Change the dimensions of widgets.
497 | *
498 | * @method resize_widget_dimensions
499 | * @param {Object} [options] An Object with all options you want to
500 | * overwrite:
501 | * @param {Array} [options.widget_margins] Margin between widgets.
502 | * The first index for the horizontal margin (left, right) and
503 | * the second for the vertical margin (top, bottom).
504 | * @param {Array} [options.widget_base_dimensions] Base widget dimensions
505 | * in pixels. The first index for the width and the second for the
506 | * height.
507 | * @return {Class} Returns the instance of the Gridster Class.
508 | */
509 | fn.resize_widget_dimensions = function(options) {
510 | if (options.widget_margins) {
511 | this.options.widget_margins = options.widget_margins;
512 | }
513 |
514 | if (options.widget_base_dimensions) {
515 | this.options.widget_base_dimensions = options.widget_base_dimensions;
516 | }
517 |
518 | this.min_widget_width = (this.options.widget_margins[0] * 2) + this.options.widget_base_dimensions[0];
519 | this.min_widget_height = (this.options.widget_margins[1] * 2) + this.options.widget_base_dimensions[1];
520 |
521 | var serializedGrid = this.serialize();
522 | this.$widgets.each($.proxy(function(i, widget) {
523 | var $widget = $(widget);
524 | this.resize_widget($widget);
525 | }, this));
526 |
527 | this.generate_grid_and_stylesheet();
528 | this.get_widgets_from_DOM();
529 | this.set_dom_grid_height();
530 |
531 | return this;
532 | };
533 |
534 |
535 |
536 | /**
537 | * Mutate widget dimensions and position in the grid map.
538 | *
539 | * @method mutate_widget_in_gridmap
540 | * @param {HTMLElement} $widget The jQuery wrapped HTMLElement
541 | * representing the widget to mutate.
542 | * @param {Object} wgd Current widget grid data (col, row, size_x, size_y).
543 | * @param {Object} new_wgd New widget grid data.
544 | * @return {HTMLElement} Returns instance of gridster Class.
545 | */
546 | fn.mutate_widget_in_gridmap = function($widget, wgd, new_wgd) {
547 | var old_size_x = wgd.size_x;
548 | var old_size_y = wgd.size_y;
549 |
550 | var old_cells_occupied = this.get_cells_occupied(wgd);
551 | var new_cells_occupied = this.get_cells_occupied(new_wgd);
552 |
553 | var empty_cols = [];
554 | $.each(old_cells_occupied.cols, function(i, col) {
555 | if ($.inArray(col, new_cells_occupied.cols) === -1) {
556 | empty_cols.push(col);
557 | }
558 | });
559 |
560 | var occupied_cols = [];
561 | $.each(new_cells_occupied.cols, function(i, col) {
562 | if ($.inArray(col, old_cells_occupied.cols) === -1) {
563 | occupied_cols.push(col);
564 | }
565 | });
566 |
567 | var empty_rows = [];
568 | $.each(old_cells_occupied.rows, function(i, row) {
569 | if ($.inArray(row, new_cells_occupied.rows) === -1) {
570 | empty_rows.push(row);
571 | }
572 | });
573 |
574 | var occupied_rows = [];
575 | $.each(new_cells_occupied.rows, function(i, row) {
576 | if ($.inArray(row, old_cells_occupied.rows) === -1) {
577 | occupied_rows.push(row);
578 | }
579 | });
580 |
581 | this.remove_from_gridmap(wgd);
582 |
583 | if (occupied_cols.length) {
584 | var cols_to_empty = [
585 | new_wgd.col, new_wgd.row, new_wgd.size_x, Math.min(old_size_y, new_wgd.size_y), $widget
586 | ];
587 | this.empty_cells.apply(this, cols_to_empty);
588 | }
589 |
590 | if (occupied_rows.length) {
591 | var rows_to_empty = [new_wgd.col, new_wgd.row, new_wgd.size_x, new_wgd.size_y, $widget];
592 | this.empty_cells.apply(this, rows_to_empty);
593 | }
594 |
595 | // not the same that wgd = new_wgd;
596 | wgd.col = new_wgd.col;
597 | wgd.row = new_wgd.row;
598 | wgd.size_x = new_wgd.size_x;
599 | wgd.size_y = new_wgd.size_y;
600 |
601 | this.add_to_gridmap(new_wgd, $widget);
602 |
603 | $widget.removeClass('player-revert');
604 |
605 | //update coords instance attributes
606 | $widget.data('coords').update({
607 | width: (new_wgd.size_x * this.options.widget_base_dimensions[0] +
608 | ((new_wgd.size_x - 1) * this.options.widget_margins[0]) * 2),
609 | height: (new_wgd.size_y * this.options.widget_base_dimensions[1] +
610 | ((new_wgd.size_y - 1) * this.options.widget_margins[1]) * 2)
611 | });
612 |
613 | $widget.attr({
614 | 'data-col': new_wgd.col,
615 | 'data-row': new_wgd.row,
616 | 'data-sizex': new_wgd.size_x,
617 | 'data-sizey': new_wgd.size_y
618 | });
619 |
620 | if (empty_cols.length) {
621 | var cols_to_remove_holes = [
622 | empty_cols[0], new_wgd.row,
623 | empty_cols.length,
624 | Math.min(old_size_y, new_wgd.size_y),
625 | $widget
626 | ];
627 |
628 | this.remove_empty_cells.apply(this, cols_to_remove_holes);
629 | }
630 |
631 | if (empty_rows.length) {
632 | var rows_to_remove_holes = [
633 | new_wgd.col, new_wgd.row, new_wgd.size_x, new_wgd.size_y, $widget
634 | ];
635 | this.remove_empty_cells.apply(this, rows_to_remove_holes);
636 | }
637 |
638 | this.move_widget_up($widget);
639 |
640 | return this;
641 | };
642 |
643 |
644 | /**
645 | * Move down widgets in cells represented by the arguments col, row, size_x,
646 | * size_y
647 | *
648 | * @method empty_cells
649 | * @param {Number} col The column where the group of cells begin.
650 | * @param {Number} row The row where the group of cells begin.
651 | * @param {Number} size_x The number of columns that the group of cells
652 | * occupy.
653 | * @param {Number} size_y The number of rows that the group of cells
654 | * occupy.
655 | * @param {HTMLElement} $exclude Exclude widgets from being moved.
656 | * @return {Class} Returns the instance of the Gridster Class.
657 | */
658 | fn.empty_cells = function(col, row, size_x, size_y, $exclude) {
659 | var $nexts = this.widgets_below({
660 | col: col,
661 | row: row - size_y,
662 | size_x: size_x,
663 | size_y: size_y
664 | });
665 |
666 | $nexts.not($exclude).each($.proxy(function(i, w) {
667 | var wgd = $(w).coords().grid;
668 | if ( !(wgd.row <= (row + size_y - 1))) { return; }
669 | var diff = (row + size_y) - wgd.row;
670 | this.move_widget_down($(w), diff);
671 | }, this));
672 |
673 | this.set_dom_grid_height();
674 |
675 | return this;
676 | };
677 |
678 |
679 | /**
680 | * Move up widgets below cells represented by the arguments col, row, size_x,
681 | * size_y.
682 | *
683 | * @method remove_empty_cells
684 | * @param {Number} col The column where the group of cells begin.
685 | * @param {Number} row The row where the group of cells begin.
686 | * @param {Number} size_x The number of columns that the group of cells
687 | * occupy.
688 | * @param {Number} size_y The number of rows that the group of cells
689 | * occupy.
690 | * @param {HTMLElement} exclude Exclude widgets from being moved.
691 | * @return {Class} Returns the instance of the Gridster Class.
692 | */
693 | fn.remove_empty_cells = function(col, row, size_x, size_y, exclude) {
694 | var $nexts = this.widgets_below({
695 | col: col,
696 | row: row,
697 | size_x: size_x,
698 | size_y: size_y
699 | });
700 |
701 | $nexts.not(exclude).each($.proxy(function(i, widget) {
702 | this.move_widget_up( $(widget), size_y );
703 | }, this));
704 |
705 | this.set_dom_grid_height();
706 |
707 | return this;
708 | };
709 |
710 |
711 | /**
712 | * Get the most left column below to add a new widget.
713 | *
714 | * @method next_position
715 | * @param {Number} size_x The nº of rows the widget occupies horizontally.
716 | * @param {Number} size_y The nº of columns the widget occupies vertically.
717 | * @return {Object} Returns a grid coords object representing the future
718 | * widget coords.
719 | */
720 | fn.next_position = function(size_x, size_y) {
721 | size_x || (size_x = 1);
722 | size_y || (size_y = 1);
723 | var ga = this.gridmap;
724 | var cols_l = ga.length;
725 | var valid_pos = [];
726 | var rows_l;
727 |
728 | for (var c = 1; c < cols_l; c++) {
729 | rows_l = ga[c].length;
730 | for (var r = 1; r <= rows_l; r++) {
731 | var can_move_to = this.can_move_to({
732 | size_x: size_x,
733 | size_y: size_y
734 | }, c, r);
735 |
736 | if (can_move_to) {
737 | valid_pos.push({
738 | col: c,
739 | row: r,
740 | size_y: size_y,
741 | size_x: size_x
742 | });
743 | }
744 | }
745 | }
746 |
747 | if (valid_pos.length) {
748 | return Gridster.sort_by_row_and_col_asc(valid_pos)[0];
749 | }
750 | return false;
751 | };
752 |
753 |
754 | /**
755 | * Remove a widget from the grid.
756 | *
757 | * @method remove_widget
758 | * @param {HTMLElement} el The jQuery wrapped HTMLElement you want to remove.
759 | * @param {Boolean|Function} silent If true, widgets below the removed one
760 | * will not move up. If a Function is passed it will be used as callback.
761 | * @param {Function} callback Function executed when the widget is removed.
762 | * @return {Class} Returns the instance of the Gridster Class.
763 | */
764 | fn.remove_widget = function(el, silent, callback) {
765 | var $el = el instanceof $ ? el : $(el);
766 | var wgd = $el.coords().grid;
767 |
768 | // if silent is a function assume it's a callback
769 | if ($.isFunction(silent)) {
770 | callback = silent;
771 | silent = false;
772 | }
773 |
774 | this.cells_occupied_by_placeholder = {};
775 | this.$widgets = this.$widgets.not($el);
776 |
777 | var $nexts = this.widgets_below($el);
778 |
779 | this.remove_from_gridmap(wgd);
780 |
781 | $el.fadeOut($.proxy(function() {
782 | $el.remove();
783 |
784 | if (!silent) {
785 | $nexts.each($.proxy(function(i, widget) {
786 | this.move_widget_up( $(widget), wgd.size_y );
787 | }, this));
788 | }
789 |
790 | this.set_dom_grid_height();
791 |
792 | if (callback) {
793 | callback.call(this, el);
794 | }
795 | }, this));
796 |
797 | return this;
798 | };
799 |
800 |
801 | /**
802 | * Remove all widgets from the grid.
803 | *
804 | * @method remove_all_widgets
805 | * @param {Function} callback Function executed for each widget removed.
806 | * @return {Class} Returns the instance of the Gridster Class.
807 | */
808 | fn.remove_all_widgets = function(callback) {
809 | this.$widgets.each($.proxy(function(i, el){
810 | this.remove_widget(el, true, callback);
811 | }, this));
812 |
813 | return this;
814 | };
815 |
816 |
817 | /**
818 | * Returns a serialized array of the widgets in the grid.
819 | *
820 | * @method serialize
821 | * @param {HTMLElement} [$widgets] The collection of jQuery wrapped
822 | * HTMLElements you want to serialize. If no argument is passed all widgets
823 | * will be serialized.
824 | * @return {Array} Returns an Array of Objects with the data specified in
825 | * the serialize_params option.
826 | */
827 | fn.serialize = function($widgets) {
828 | $widgets || ($widgets = this.$widgets);
829 |
830 | return $widgets.map($.proxy(function(i, widget) {
831 | var $w = $(widget);
832 | return this.options.serialize_params($w, $w.coords().grid);
833 | }, this)).get();
834 | };
835 |
836 |
837 | /**
838 | * Returns a serialized array of the widgets that have changed their
839 | * position.
840 | *
841 | * @method serialize_changed
842 | * @return {Array} Returns an Array of Objects with the data specified in
843 | * the serialize_params option.
844 | */
845 | fn.serialize_changed = function() {
846 | return this.serialize(this.$changed);
847 | };
848 |
849 |
850 | /**
851 | * Convert widgets from DOM elements to "widget grid data" Objects.
852 | *
853 | * @method dom_to_coords
854 | * @param {HTMLElement} $widget The widget to be converted.
855 | */
856 | fn.dom_to_coords = function($widget) {
857 | return {
858 | 'col': parseInt($widget.attr('data-col'), 10),
859 | 'row': parseInt($widget.attr('data-row'), 10),
860 | 'size_x': parseInt($widget.attr('data-sizex'), 10) || 1,
861 | 'size_y': parseInt($widget.attr('data-sizey'), 10) || 1,
862 | 'max_size_x': parseInt($widget.attr('data-max-sizex'), 10) || false,
863 | 'max_size_y': parseInt($widget.attr('data-max-sizey'), 10) || false,
864 | 'min_size_x': parseInt($widget.attr('data-min-sizex'), 10) || false,
865 | 'min_size_y': parseInt($widget.attr('data-min-sizey'), 10) || false,
866 | 'el': $widget
867 | };
868 | };
869 |
870 |
871 | /**
872 | * Creates the grid coords object representing the widget an add it to the
873 | * mapped array of positions.
874 | *
875 | * @method register_widget
876 | * @param {HTMLElement|Object} $el jQuery wrapped HTMLElement representing
877 | * the widget, or an "widget grid data" Object with (col, row, el ...).
878 | * @return {Boolean} Returns true if the widget final position is different
879 | * than the original.
880 | */
881 | fn.register_widget = function($el) {
882 | var isDOM = $el instanceof jQuery;
883 | var wgd = isDOM ? this.dom_to_coords($el) : $el;
884 | var posChanged = false;
885 | isDOM || ($el = wgd.el);
886 |
887 | var empty_upper_row = this.can_go_widget_up(wgd);
888 | if (empty_upper_row) {
889 | wgd.row = empty_upper_row;
890 | $el.attr('data-row', empty_upper_row);
891 | this.$el.trigger('gridster:positionchanged', [wgd]);
892 | posChanged = true;
893 | }
894 |
895 | if (this.options.avoid_overlapped_widgets &&
896 | !this.can_move_to(
897 | {size_x: wgd.size_x, size_y: wgd.size_y}, wgd.col, wgd.row)
898 | ) {
899 | $.extend(wgd, this.next_position(wgd.size_x, wgd.size_y));
900 | $el.attr({
901 | 'data-col': wgd.col,
902 | 'data-row': wgd.row,
903 | 'data-sizex': wgd.size_x,
904 | 'data-sizey': wgd.size_y
905 | });
906 | posChanged = true;
907 | }
908 |
909 | // attach Coord object to player data-coord attribute
910 | $el.data('coords', $el.coords());
911 | // Extend Coord object with grid position info
912 | $el.data('coords').grid = wgd;
913 |
914 | this.add_to_gridmap(wgd, $el);
915 |
916 | this.options.resize.enabled && this.add_resize_handle($el);
917 |
918 | return posChanged;
919 | };
920 |
921 |
922 | /**
923 | * Update in the mapped array of positions the value of cells represented by
924 | * the grid coords object passed in the `grid_data` param.
925 | *
926 | * @param {Object} grid_data The grid coords object representing the cells
927 | * to update in the mapped array.
928 | * @param {HTMLElement|Boolean} value Pass `false` or the jQuery wrapped
929 | * HTMLElement, depends if you want to delete an existing position or add
930 | * a new one.
931 | * @method update_widget_position
932 | * @return {Class} Returns the instance of the Gridster Class.
933 | */
934 | fn.update_widget_position = function(grid_data, value) {
935 | this.for_each_cell_occupied(grid_data, function(col, row) {
936 | if (!this.gridmap[col]) { return this; }
937 | this.gridmap[col][row] = value;
938 | });
939 | return this;
940 | };
941 |
942 |
943 | /**
944 | * Remove a widget from the mapped array of positions.
945 | *
946 | * @method remove_from_gridmap
947 | * @param {Object} grid_data The grid coords object representing the cells
948 | * to update in the mapped array.
949 | * @return {Class} Returns the instance of the Gridster Class.
950 | */
951 | fn.remove_from_gridmap = function(grid_data) {
952 | return this.update_widget_position(grid_data, false);
953 | };
954 |
955 |
956 | /**
957 | * Add a widget to the mapped array of positions.
958 | *
959 | * @method add_to_gridmap
960 | * @param {Object} grid_data The grid coords object representing the cells
961 | * to update in the mapped array.
962 | * @param {HTMLElement|Boolean} value The value to set in the specified
963 | * position .
964 | * @return {Class} Returns the instance of the Gridster Class.
965 | */
966 | fn.add_to_gridmap = function(grid_data, value) {
967 | this.update_widget_position(grid_data, value || grid_data.el);
968 |
969 | if (grid_data.el) {
970 | var $widgets = this.widgets_below(grid_data.el);
971 | $widgets.each($.proxy(function(i, widget) {
972 | this.move_widget_up( $(widget));
973 | }, this));
974 | }
975 | };
976 |
977 |
978 | /**
979 | * Make widgets draggable.
980 | *
981 | * @uses Draggable
982 | * @method draggable
983 | * @return {Class} Returns the instance of the Gridster Class.
984 | */
985 | fn.draggable = function() {
986 | var self = this;
987 | var draggable_options = $.extend(true, {}, this.options.draggable, {
988 | offset_left: this.options.widget_margins[0],
989 | offset_top: this.options.widget_margins[1],
990 | container_width: this.cols * this.min_widget_width,
991 | limit: true,
992 | start: function(event, ui) {
993 | self.$widgets.filter('.player-revert')
994 | .removeClass('player-revert');
995 |
996 | self.$player = $(this);
997 | self.$helper = $(ui.$helper);
998 |
999 | self.helper = !self.$helper.is(self.$player);
1000 |
1001 | self.on_start_drag.call(self, event, ui);
1002 | self.$el.trigger('gridster:dragstart');
1003 | },
1004 | stop: function(event, ui) {
1005 | self.on_stop_drag.call(self, event, ui);
1006 | self.$el.trigger('gridster:dragstop');
1007 | },
1008 | drag: throttle(function(event, ui) {
1009 | self.on_drag.call(self, event, ui);
1010 | self.$el.trigger('gridster:drag');
1011 | }, 60)
1012 | });
1013 |
1014 | this.drag_api = this.$el.drag(draggable_options);
1015 | return this;
1016 | };
1017 |
1018 |
1019 | /**
1020 | * Bind resize events to get resize working.
1021 | *
1022 | * @method resizable
1023 | * @return {Class} Returns instance of gridster Class.
1024 | */
1025 | fn.resizable = function() {
1026 | this.resize_api = this.$el.drag({
1027 | items: '.' + this.options.resize.handle_class,
1028 | offset_left: this.options.widget_margins[0],
1029 | container_width: this.container_width,
1030 | move_element: false,
1031 | resize: true,
1032 | limit: this.options.autogrow_cols ? false : true,
1033 | start: $.proxy(this.on_start_resize, this),
1034 | stop: $.proxy(function(event, ui) {
1035 | delay($.proxy(function() {
1036 | this.on_stop_resize(event, ui);
1037 | }, this), 120);
1038 | }, this),
1039 | drag: throttle($.proxy(this.on_resize, this), 60)
1040 | });
1041 |
1042 | return this;
1043 | };
1044 |
1045 |
1046 | /**
1047 | * Setup things required for resizing. Like build templates for drag handles.
1048 | *
1049 | * @method setup_resize
1050 | * @return {Class} Returns instance of gridster Class.
1051 | */
1052 | fn.setup_resize = function() {
1053 | this.resize_handle_class = this.options.resize.handle_class;
1054 | var axes = this.options.resize.axes;
1055 | var handle_tpl = '';
1057 |
1058 | this.resize_handle_tpl = $.map(axes, function(type) {
1059 | return handle_tpl.replace('{type}', type);
1060 | }).join('');
1061 |
1062 | if ($.isArray(this.options.draggable.ignore_dragging)) {
1063 | this.options.draggable.ignore_dragging.push(
1064 | '.' + this.resize_handle_class);
1065 | }
1066 |
1067 | return this;
1068 | };
1069 |
1070 |
1071 | /**
1072 | * This function is executed when the player begins to be dragged.
1073 | *
1074 | * @method on_start_drag
1075 | * @param {Event} event The original browser event
1076 | * @param {Object} ui A prepared ui object with useful drag-related data
1077 | */
1078 | fn.on_start_drag = function(event, ui) {
1079 | this.$helper.add(this.$player).add(this.$wrapper).addClass('dragging');
1080 |
1081 | this.highest_col = this.get_highest_occupied_cell().col;
1082 |
1083 | this.$player.addClass('player');
1084 | this.player_grid_data = this.$player.coords().grid;
1085 | this.placeholder_grid_data = $.extend({}, this.player_grid_data);
1086 |
1087 | this.set_dom_grid_height(this.$el.height() +
1088 | (this.player_grid_data.size_y * this.min_widget_height));
1089 |
1090 | this.set_dom_grid_width(this.cols);
1091 |
1092 | var pgd_sizex = this.player_grid_data.size_x;
1093 | var cols_diff = this.cols - this.highest_col;
1094 |
1095 | if (this.options.autogrow_cols && cols_diff <= pgd_sizex) {
1096 | this.add_faux_cols(Math.min(pgd_sizex - cols_diff, 1));
1097 | }
1098 |
1099 | var colliders = this.faux_grid;
1100 | var coords = this.$player.data('coords').coords;
1101 |
1102 | this.cells_occupied_by_player = this.get_cells_occupied(
1103 | this.player_grid_data);
1104 | this.cells_occupied_by_placeholder = this.get_cells_occupied(
1105 | this.placeholder_grid_data);
1106 |
1107 | this.last_cols = [];
1108 | this.last_rows = [];
1109 |
1110 | // see jquery.collision.js
1111 | this.collision_api = this.$helper.collision(
1112 | colliders, this.options.collision);
1113 |
1114 | this.$preview_holder = $('<' + this.$player.get(0).tagName + ' />', {
1115 | 'class': 'preview-holder',
1116 | 'data-row': this.$player.attr('data-row'),
1117 | 'data-col': this.$player.attr('data-col'),
1118 | css: {
1119 | width: coords.width,
1120 | height: coords.height
1121 | }
1122 | }).appendTo(this.$el);
1123 |
1124 | if (this.options.draggable.start) {
1125 | this.options.draggable.start.call(this, event, ui);
1126 | }
1127 | };
1128 |
1129 |
1130 | /**
1131 | * This function is executed when the player is being dragged.
1132 | *
1133 | * @method on_drag
1134 | * @param {Event} event The original browser event
1135 | * @param {Object} ui A prepared ui object with useful drag-related data
1136 | */
1137 | fn.on_drag = function(event, ui) {
1138 | //break if dragstop has been fired
1139 | if (this.$player === null) {
1140 | return false;
1141 | }
1142 |
1143 | var abs_offset = {
1144 | left: ui.position.left + this.baseX,
1145 | top: ui.position.top + this.baseY
1146 | };
1147 |
1148 | // auto grow cols
1149 | if (this.options.autogrow_cols) {
1150 | var prcol = this.placeholder_grid_data.col +
1151 | this.placeholder_grid_data.size_x - 1;
1152 |
1153 | // "- 1" due to adding at least 1 column in on_start_drag
1154 | if (prcol >= this.cols - 1 && this.options.max_cols >= this.cols + 1) {
1155 | this.add_faux_cols(1);
1156 | this.set_dom_grid_width(this.cols + 1);
1157 | this.drag_api.set_limits(this.container_width);
1158 | }
1159 |
1160 | this.collision_api.set_colliders(this.faux_grid);
1161 | }
1162 |
1163 | this.colliders_data = this.collision_api.get_closest_colliders(
1164 | abs_offset);
1165 |
1166 | this.on_overlapped_column_change(
1167 | this.on_start_overlapping_column, this.on_stop_overlapping_column);
1168 |
1169 | this.on_overlapped_row_change(
1170 | this.on_start_overlapping_row, this.on_stop_overlapping_row);
1171 |
1172 |
1173 | if (this.helper && this.$player) {
1174 | this.$player.css({
1175 | 'left': ui.position.left,
1176 | 'top': ui.position.top
1177 | });
1178 | }
1179 |
1180 | if (this.options.draggable.drag) {
1181 | this.options.draggable.drag.call(this, event, ui);
1182 | }
1183 | };
1184 |
1185 |
1186 | /**
1187 | * This function is executed when the player stops being dragged.
1188 | *
1189 | * @method on_stop_drag
1190 | * @param {Event} event The original browser event
1191 | * @param {Object} ui A prepared ui object with useful drag-related data
1192 | */
1193 | fn.on_stop_drag = function(event, ui) {
1194 | this.$helper.add(this.$player).add(this.$wrapper)
1195 | .removeClass('dragging');
1196 |
1197 | ui.position.left = ui.position.left + this.baseX;
1198 | ui.position.top = ui.position.top + this.baseY;
1199 | this.colliders_data = this.collision_api.get_closest_colliders(
1200 | ui.position);
1201 |
1202 | this.on_overlapped_column_change(
1203 | this.on_start_overlapping_column,
1204 | this.on_stop_overlapping_column
1205 | );
1206 |
1207 | this.on_overlapped_row_change(
1208 | this.on_start_overlapping_row,
1209 | this.on_stop_overlapping_row
1210 | );
1211 |
1212 | this.$player.addClass('player-revert').removeClass('player')
1213 | .attr({
1214 | 'data-col': this.placeholder_grid_data.col,
1215 | 'data-row': this.placeholder_grid_data.row
1216 | }).css({
1217 | 'left': '',
1218 | 'top': ''
1219 | });
1220 |
1221 | this.$changed = this.$changed.add(this.$player);
1222 |
1223 | this.cells_occupied_by_player = this.get_cells_occupied(
1224 | this.placeholder_grid_data);
1225 | this.set_cells_player_occupies(
1226 | this.placeholder_grid_data.col, this.placeholder_grid_data.row);
1227 |
1228 | this.$player.coords().grid.row = this.placeholder_grid_data.row;
1229 | this.$player.coords().grid.col = this.placeholder_grid_data.col;
1230 |
1231 | if (this.options.draggable.stop) {
1232 | this.options.draggable.stop.call(this, event, ui);
1233 | }
1234 |
1235 | this.$preview_holder.remove();
1236 |
1237 | this.$player = null;
1238 | this.$helper = null;
1239 | this.placeholder_grid_data = {};
1240 | this.player_grid_data = {};
1241 | this.cells_occupied_by_placeholder = {};
1242 | this.cells_occupied_by_player = {};
1243 |
1244 | this.set_dom_grid_height();
1245 | this.set_dom_grid_width();
1246 |
1247 | if (this.options.autogrow_cols) {
1248 | this.drag_api.set_limits(this.cols * this.min_widget_width);
1249 | }
1250 | };
1251 |
1252 |
1253 | /**
1254 | * This function is executed every time a widget starts to be resized.
1255 | *
1256 | * @method on_start_resize
1257 | * @param {Event} event The original browser event
1258 | * @param {Object} ui A prepared ui object with useful drag-related data
1259 | */
1260 | fn.on_start_resize = function(event, ui) {
1261 | this.$resized_widget = ui.$player.closest('.gs-w');
1262 | this.resize_coords = this.$resized_widget.coords();
1263 | this.resize_wgd = this.resize_coords.grid;
1264 | this.resize_initial_width = this.resize_coords.coords.width;
1265 | this.resize_initial_height = this.resize_coords.coords.height;
1266 | this.resize_initial_sizex = this.resize_coords.grid.size_x;
1267 | this.resize_initial_sizey = this.resize_coords.grid.size_y;
1268 | this.resize_initial_col = this.resize_coords.grid.col;
1269 | this.resize_last_sizex = this.resize_initial_sizex;
1270 | this.resize_last_sizey = this.resize_initial_sizey;
1271 |
1272 | this.resize_max_size_x = Math.min(this.resize_wgd.max_size_x ||
1273 | this.options.resize.max_size[0],
1274 | this.options.max_cols - this.resize_initial_col + 1);
1275 | this.resize_max_size_y = this.resize_wgd.max_size_y ||
1276 | this.options.resize.max_size[1];
1277 |
1278 | this.resize_min_size_x = (this.resize_wgd.min_size_x ||
1279 | this.options.resize.min_size[0] || 1);
1280 | this.resize_min_size_y = (this.resize_wgd.min_size_y ||
1281 | this.options.resize.min_size[1] || 1);
1282 |
1283 | this.resize_initial_last_col = this.get_highest_occupied_cell().col;
1284 |
1285 | this.set_dom_grid_width(this.cols);
1286 |
1287 | this.resize_dir = {
1288 | right: ui.$player.is('.' + this.resize_handle_class + '-x'),
1289 | bottom: ui.$player.is('.' + this.resize_handle_class + '-y')
1290 | };
1291 |
1292 | this.$resized_widget.css({
1293 | 'min-width': this.options.widget_base_dimensions[0],
1294 | 'min-height': this.options.widget_base_dimensions[1]
1295 | });
1296 |
1297 | var nodeName = this.$resized_widget.get(0).tagName;
1298 | this.$resize_preview_holder = $('<' + nodeName + ' />', {
1299 | 'class': 'preview-holder resize-preview-holder',
1300 | 'data-row': this.$resized_widget.attr('data-row'),
1301 | 'data-col': this.$resized_widget.attr('data-col'),
1302 | 'css': {
1303 | 'width': this.resize_initial_width,
1304 | 'height': this.resize_initial_height
1305 | }
1306 | }).appendTo(this.$el);
1307 |
1308 | this.$resized_widget.addClass('resizing');
1309 |
1310 | if (this.options.resize.start) {
1311 | this.options.resize.start.call(this, event, ui, this.$resized_widget);
1312 | }
1313 |
1314 | this.$el.trigger('gridster:resizestart');
1315 | };
1316 |
1317 |
1318 | /**
1319 | * This function is executed every time a widget stops being resized.
1320 | *
1321 | * @method on_stop_resize
1322 | * @param {Event} event The original browser event
1323 | * @param {Object} ui A prepared ui object with useful drag-related data
1324 | */
1325 | fn.on_stop_resize = function(event, ui) {
1326 | this.$resized_widget
1327 | .removeClass('resizing')
1328 | .css({
1329 | 'width': '',
1330 | 'height': ''
1331 | });
1332 |
1333 | delay($.proxy(function() {
1334 | this.$resize_preview_holder
1335 | .remove()
1336 | .css({
1337 | 'min-width': '',
1338 | 'min-height': ''
1339 | });
1340 |
1341 | if (this.options.resize.stop) {
1342 | this.options.resize.stop.call(this, event, ui, this.$resized_widget);
1343 | }
1344 |
1345 | this.$el.trigger('gridster:resizestop');
1346 | }, this), 300);
1347 |
1348 | this.set_dom_grid_width();
1349 |
1350 | if (this.options.autogrow_cols) {
1351 | this.drag_api.set_limits(this.cols * this.min_widget_width);
1352 | }
1353 | };
1354 |
1355 |
1356 | /**
1357 | * This function is executed when a widget is being resized.
1358 | *
1359 | * @method on_resize
1360 | * @param {Event} event The original browser event
1361 | * @param {Object} ui A prepared ui object with useful drag-related data
1362 | */
1363 | fn.on_resize = function(event, ui) {
1364 | var rel_x = (ui.pointer.diff_left);
1365 | var rel_y = (ui.pointer.diff_top);
1366 | var wbd_x = this.options.widget_base_dimensions[0];
1367 | var wbd_y = this.options.widget_base_dimensions[1];
1368 | var margin_x = this.options.widget_margins[0];
1369 | var margin_y = this.options.widget_margins[1];
1370 | var max_size_x = this.resize_max_size_x;
1371 | var min_size_x = this.resize_min_size_x;
1372 | var max_size_y = this.resize_max_size_y;
1373 | var min_size_y = this.resize_min_size_y;
1374 | var autogrow = this.options.autogrow_cols;
1375 | var width;
1376 | var max_width = Infinity;
1377 | var max_height = Infinity;
1378 |
1379 | var inc_units_x = Math.ceil((rel_x / (wbd_x + margin_x * 2)) - 0.2);
1380 | var inc_units_y = Math.ceil((rel_y / (wbd_y + margin_y * 2)) - 0.2);
1381 |
1382 | var size_x = Math.max(1, this.resize_initial_sizex + inc_units_x);
1383 | var size_y = Math.max(1, this.resize_initial_sizey + inc_units_y);
1384 |
1385 | var max_cols = (this.container_width / this.min_widget_width) -
1386 | this.resize_initial_col + 1;
1387 | var limit_width = ((max_cols * this.min_widget_width) - margin_x * 2);
1388 |
1389 | size_x = Math.max(Math.min(size_x, max_size_x), min_size_x);
1390 | size_x = Math.min(max_cols, size_x);
1391 | width = (max_size_x * wbd_x) + ((size_x - 1) * margin_x * 2);
1392 | max_width = Math.min(width, limit_width);
1393 | min_width = (min_size_x * wbd_x) + ((size_x - 1) * margin_x * 2);
1394 |
1395 | size_y = Math.max(Math.min(size_y, max_size_y), min_size_y);
1396 | max_height = (max_size_y * wbd_y) + ((size_y - 1) * margin_y * 2);
1397 | min_height = (min_size_y * wbd_y) + ((size_y - 1) * margin_y * 2);
1398 |
1399 | if (this.resize_dir.right) {
1400 | size_y = this.resize_initial_sizey;
1401 | } else if (this.resize_dir.bottom) {
1402 | size_x = this.resize_initial_sizex;
1403 | }
1404 |
1405 | if (autogrow) {
1406 | var last_widget_col = this.resize_initial_col + size_x - 1;
1407 | if (autogrow && this.resize_initial_last_col <= last_widget_col) {
1408 | this.set_dom_grid_width(Math.max(last_widget_col + 1, this.cols));
1409 |
1410 | if (this.cols < last_widget_col) {
1411 | this.add_faux_cols(last_widget_col - this.cols);
1412 | }
1413 | }
1414 | }
1415 |
1416 | var css_props = {};
1417 | !this.resize_dir.bottom && (css_props.width = Math.max(Math.min(
1418 | this.resize_initial_width + rel_x, max_width), min_width));
1419 | !this.resize_dir.right && (css_props.height = Math.max(Math.min(
1420 | this.resize_initial_height + rel_y, max_height), min_height));
1421 |
1422 | this.$resized_widget.css(css_props);
1423 |
1424 | if (size_x !== this.resize_last_sizex ||
1425 | size_y !== this.resize_last_sizey) {
1426 |
1427 | this.resize_widget(this.$resized_widget, size_x, size_y);
1428 | this.set_dom_grid_width(this.cols);
1429 |
1430 | this.$resize_preview_holder.css({
1431 | 'width': '',
1432 | 'height': ''
1433 | }).attr({
1434 | 'data-row': this.$resized_widget.attr('data-row'),
1435 | 'data-sizex': size_x,
1436 | 'data-sizey': size_y
1437 | });
1438 | }
1439 |
1440 | if (this.options.resize.resize) {
1441 | this.options.resize.resize.call(this, event, ui, this.$resized_widget);
1442 | }
1443 |
1444 | this.$el.trigger('gridster:resize');
1445 |
1446 | this.resize_last_sizex = size_x;
1447 | this.resize_last_sizey = size_y;
1448 | };
1449 |
1450 |
1451 | /**
1452 | * Executes the callbacks passed as arguments when a column begins to be
1453 | * overlapped or stops being overlapped.
1454 | *
1455 | * @param {Function} start_callback Function executed when a new column
1456 | * begins to be overlapped. The column is passed as first argument.
1457 | * @param {Function} stop_callback Function executed when a column stops
1458 | * being overlapped. The column is passed as first argument.
1459 | * @method on_overlapped_column_change
1460 | * @return {Class} Returns the instance of the Gridster Class.
1461 | */
1462 | fn.on_overlapped_column_change = function(start_callback, stop_callback) {
1463 | if (!this.colliders_data.length) {
1464 | return this;
1465 | }
1466 | var cols = this.get_targeted_columns(
1467 | this.colliders_data[0].el.data.col);
1468 |
1469 | var last_n_cols = this.last_cols.length;
1470 | var n_cols = cols.length;
1471 | var i;
1472 |
1473 | for (i = 0; i < n_cols; i++) {
1474 | if ($.inArray(cols[i], this.last_cols) === -1) {
1475 | (start_callback || $.noop).call(this, cols[i]);
1476 | }
1477 | }
1478 |
1479 | for (i = 0; i< last_n_cols; i++) {
1480 | if ($.inArray(this.last_cols[i], cols) === -1) {
1481 | (stop_callback || $.noop).call(this, this.last_cols[i]);
1482 | }
1483 | }
1484 |
1485 | this.last_cols = cols;
1486 |
1487 | return this;
1488 | };
1489 |
1490 |
1491 | /**
1492 | * Executes the callbacks passed as arguments when a row starts to be
1493 | * overlapped or stops being overlapped.
1494 | *
1495 | * @param {Function} start_callback Function executed when a new row begins
1496 | * to be overlapped. The row is passed as first argument.
1497 | * @param {Function} end_callback Function executed when a row stops being
1498 | * overlapped. The row is passed as first argument.
1499 | * @method on_overlapped_row_change
1500 | * @return {Class} Returns the instance of the Gridster Class.
1501 | */
1502 | fn.on_overlapped_row_change = function(start_callback, end_callback) {
1503 | if (!this.colliders_data.length) {
1504 | return this;
1505 | }
1506 | var rows = this.get_targeted_rows(this.colliders_data[0].el.data.row);
1507 | var last_n_rows = this.last_rows.length;
1508 | var n_rows = rows.length;
1509 | var i;
1510 |
1511 | for (i = 0; i < n_rows; i++) {
1512 | if ($.inArray(rows[i], this.last_rows) === -1) {
1513 | (start_callback || $.noop).call(this, rows[i]);
1514 | }
1515 | }
1516 |
1517 | for (i = 0; i < last_n_rows; i++) {
1518 | if ($.inArray(this.last_rows[i], rows) === -1) {
1519 | (end_callback || $.noop).call(this, this.last_rows[i]);
1520 | }
1521 | }
1522 |
1523 | this.last_rows = rows;
1524 | };
1525 |
1526 |
1527 | /**
1528 | * Sets the current position of the player
1529 | *
1530 | * @param {Number} col
1531 | * @param {Number} row
1532 | * @param {Boolean} no_player
1533 | * @method set_player
1534 | * @return {object}
1535 | */
1536 | fn.set_player = function(col, row, no_player) {
1537 | var self = this;
1538 | if (!no_player) {
1539 | this.empty_cells_player_occupies();
1540 | }
1541 | var cell = !no_player ? self.colliders_data[0].el.data : {col: col};
1542 | var to_col = cell.col;
1543 | var to_row = row || cell.row;
1544 |
1545 | this.player_grid_data = {
1546 | col: to_col,
1547 | row: to_row,
1548 | size_y : this.player_grid_data.size_y,
1549 | size_x : this.player_grid_data.size_x
1550 | };
1551 |
1552 | this.cells_occupied_by_player = this.get_cells_occupied(
1553 | this.player_grid_data);
1554 |
1555 | var $overlapped_widgets = this.get_widgets_overlapped(
1556 | this.player_grid_data);
1557 |
1558 | var constraints = this.widgets_constraints($overlapped_widgets);
1559 |
1560 | this.manage_movements(constraints.can_go_up, to_col, to_row);
1561 | this.manage_movements(constraints.can_not_go_up, to_col, to_row);
1562 |
1563 | /* if there is not widgets overlapping in the new player position,
1564 | * update the new placeholder position. */
1565 | if (!$overlapped_widgets.length) {
1566 | var pp = this.can_go_player_up(this.player_grid_data);
1567 | if (pp !== false) {
1568 | to_row = pp;
1569 | }
1570 | this.set_placeholder(to_col, to_row);
1571 | }
1572 |
1573 | return {
1574 | col: to_col,
1575 | row: to_row
1576 | };
1577 | };
1578 |
1579 |
1580 | /**
1581 | * See which of the widgets in the $widgets param collection can go to
1582 | * a upper row and which not.
1583 | *
1584 | * @method widgets_contraints
1585 | * @param {jQuery} $widgets A jQuery wrapped collection of
1586 | * HTMLElements.
1587 | * @return {object} Returns a literal Object with two keys: `can_go_up` &
1588 | * `can_not_go_up`. Each contains a set of HTMLElements.
1589 | */
1590 | fn.widgets_constraints = function($widgets) {
1591 | var $widgets_can_go_up = $([]);
1592 | var $widgets_can_not_go_up;
1593 | var wgd_can_go_up = [];
1594 | var wgd_can_not_go_up = [];
1595 |
1596 | $widgets.each($.proxy(function(i, w) {
1597 | var $w = $(w);
1598 | var wgd = $w.coords().grid;
1599 | if (this.can_go_widget_up(wgd)) {
1600 | $widgets_can_go_up = $widgets_can_go_up.add($w);
1601 | wgd_can_go_up.push(wgd);
1602 | } else {
1603 | wgd_can_not_go_up.push(wgd);
1604 | }
1605 | }, this));
1606 |
1607 | $widgets_can_not_go_up = $widgets.not($widgets_can_go_up);
1608 |
1609 | return {
1610 | can_go_up: Gridster.sort_by_row_asc(wgd_can_go_up),
1611 | can_not_go_up: Gridster.sort_by_row_desc(wgd_can_not_go_up)
1612 | };
1613 | };
1614 |
1615 |
1616 | /**
1617 | * Sorts an Array of grid coords objects (representing the grid coords of
1618 | * each widget) in descending way.
1619 | *
1620 | * @method manage_movements
1621 | * @param {jQuery} $widgets A jQuery collection of HTMLElements
1622 | * representing the widgets you want to move.
1623 | * @param {Number} to_col The column to which we want to move the widgets.
1624 | * @param {Number} to_row The row to which we want to move the widgets.
1625 | * @return {Class} Returns the instance of the Gridster Class.
1626 | */
1627 | fn.manage_movements = function($widgets, to_col, to_row) {
1628 | $.each($widgets, $.proxy(function(i, w) {
1629 | var wgd = w;
1630 | var $w = wgd.el;
1631 |
1632 | var can_go_widget_up = this.can_go_widget_up(wgd);
1633 |
1634 | if (can_go_widget_up) {
1635 | //target CAN go up
1636 | //so move widget up
1637 | this.move_widget_to($w, can_go_widget_up);
1638 | this.set_placeholder(to_col, can_go_widget_up + wgd.size_y);
1639 |
1640 | } else {
1641 | //target can't go up
1642 | var can_go_player_up = this.can_go_player_up(
1643 | this.player_grid_data);
1644 |
1645 | if (!can_go_player_up) {
1646 | // target can't go up
1647 | // player cant't go up
1648 | // so we need to move widget down to a position that dont
1649 | // overlaps player
1650 | var y = (to_row + this.player_grid_data.size_y) - wgd.row;
1651 |
1652 | this.move_widget_down($w, y);
1653 | this.set_placeholder(to_col, to_row);
1654 | }
1655 | }
1656 | }, this));
1657 |
1658 | return this;
1659 | };
1660 |
1661 | /**
1662 | * Determines if there is a widget in the row and col given. Or if the
1663 | * HTMLElement passed as first argument is the player.
1664 | *
1665 | * @method is_player
1666 | * @param {Number|HTMLElement} col_or_el A jQuery wrapped collection of
1667 | * HTMLElements.
1668 | * @param {Number} [row] The column to which we want to move the widgets.
1669 | * @return {Boolean} Returns true or false.
1670 | */
1671 | fn.is_player = function(col_or_el, row) {
1672 | if (row && !this.gridmap[col_or_el]) { return false; }
1673 | var $w = row ? this.gridmap[col_or_el][row] : col_or_el;
1674 | return $w && ($w.is(this.$player) || $w.is(this.$helper));
1675 | };
1676 |
1677 |
1678 | /**
1679 | * Determines if the widget that is being dragged is currently over the row
1680 | * and col given.
1681 | *
1682 | * @method is_player_in
1683 | * @param {Number} col The column to check.
1684 | * @param {Number} row The row to check.
1685 | * @return {Boolean} Returns true or false.
1686 | */
1687 | fn.is_player_in = function(col, row) {
1688 | var c = this.cells_occupied_by_player || {};
1689 | return $.inArray(col, c.cols) >= 0 && $.inArray(row, c.rows) >= 0;
1690 | };
1691 |
1692 |
1693 | /**
1694 | * Determines if the placeholder is currently over the row and col given.
1695 | *
1696 | * @method is_placeholder_in
1697 | * @param {Number} col The column to check.
1698 | * @param {Number} row The row to check.
1699 | * @return {Boolean} Returns true or false.
1700 | */
1701 | fn.is_placeholder_in = function(col, row) {
1702 | var c = this.cells_occupied_by_placeholder || {};
1703 | return this.is_placeholder_in_col(col) && $.inArray(row, c.rows) >= 0;
1704 | };
1705 |
1706 |
1707 | /**
1708 | * Determines if the placeholder is currently over the column given.
1709 | *
1710 | * @method is_placeholder_in_col
1711 | * @param {Number} col The column to check.
1712 | * @return {Boolean} Returns true or false.
1713 | */
1714 | fn.is_placeholder_in_col = function(col) {
1715 | var c = this.cells_occupied_by_placeholder || [];
1716 | return $.inArray(col, c.cols) >= 0;
1717 | };
1718 |
1719 |
1720 | /**
1721 | * Determines if the cell represented by col and row params is empty.
1722 | *
1723 | * @method is_empty
1724 | * @param {Number} col The column to check.
1725 | * @param {Number} row The row to check.
1726 | * @return {Boolean} Returns true or false.
1727 | */
1728 | fn.is_empty = function(col, row) {
1729 | if (typeof this.gridmap[col] !== 'undefined') {
1730 | if(typeof this.gridmap[col][row] !== 'undefined' &&
1731 | this.gridmap[col][row] === false
1732 | ) {
1733 | return true;
1734 | }
1735 | return false;
1736 | }
1737 | return true;
1738 | };
1739 |
1740 |
1741 | /**
1742 | * Determines if the cell represented by col and row params is occupied.
1743 | *
1744 | * @method is_occupied
1745 | * @param {Number} col The column to check.
1746 | * @param {Number} row The row to check.
1747 | * @return {Boolean} Returns true or false.
1748 | */
1749 | fn.is_occupied = function(col, row) {
1750 | if (!this.gridmap[col]) {
1751 | return false;
1752 | }
1753 |
1754 | if (this.gridmap[col][row]) {
1755 | return true;
1756 | }
1757 | return false;
1758 | };
1759 |
1760 |
1761 | /**
1762 | * Determines if there is a widget in the cell represented by col/row params.
1763 | *
1764 | * @method is_widget
1765 | * @param {Number} col The column to check.
1766 | * @param {Number} row The row to check.
1767 | * @return {Boolean|HTMLElement} Returns false if there is no widget,
1768 | * else returns the jQuery HTMLElement
1769 | */
1770 | fn.is_widget = function(col, row) {
1771 | var cell = this.gridmap[col];
1772 | if (!cell) {
1773 | return false;
1774 | }
1775 |
1776 | cell = cell[row];
1777 |
1778 | if (cell) {
1779 | return cell;
1780 | }
1781 |
1782 | return false;
1783 | };
1784 |
1785 |
1786 | /**
1787 | * Determines if there is a widget in the cell represented by col/row
1788 | * params and if this is under the widget that is being dragged.
1789 | *
1790 | * @method is_widget_under_player
1791 | * @param {Number} col The column to check.
1792 | * @param {Number} row The row to check.
1793 | * @return {Boolean} Returns true or false.
1794 | */
1795 | fn.is_widget_under_player = function(col, row) {
1796 | if (this.is_widget(col, row)) {
1797 | return this.is_player_in(col, row);
1798 | }
1799 | return false;
1800 | };
1801 |
1802 |
1803 | /**
1804 | * Get widgets overlapping with the player or with the object passed
1805 | * representing the grid cells.
1806 | *
1807 | * @method get_widgets_under_player
1808 | * @return {HTMLElement} Returns a jQuery collection of HTMLElements
1809 | */
1810 | fn.get_widgets_under_player = function(cells) {
1811 | cells || (cells = this.cells_occupied_by_player || {cols: [], rows: []});
1812 | var $widgets = $([]);
1813 |
1814 | $.each(cells.cols, $.proxy(function(i, col) {
1815 | $.each(cells.rows, $.proxy(function(i, row) {
1816 | if(this.is_widget(col, row)) {
1817 | $widgets = $widgets.add(this.gridmap[col][row]);
1818 | }
1819 | }, this));
1820 | }, this));
1821 |
1822 | return $widgets;
1823 | };
1824 |
1825 |
1826 | /**
1827 | * Put placeholder at the row and column specified.
1828 | *
1829 | * @method set_placeholder
1830 | * @param {Number} col The column to which we want to move the
1831 | * placeholder.
1832 | * @param {Number} row The row to which we want to move the
1833 | * placeholder.
1834 | * @return {Class} Returns the instance of the Gridster Class.
1835 | */
1836 | fn.set_placeholder = function(col, row) {
1837 | var phgd = $.extend({}, this.placeholder_grid_data);
1838 | var $nexts = this.widgets_below({
1839 | col: phgd.col,
1840 | row: phgd.row,
1841 | size_y: phgd.size_y,
1842 | size_x: phgd.size_x
1843 | });
1844 |
1845 | // Prevents widgets go out of the grid
1846 | var right_col = (col + phgd.size_x - 1);
1847 | if (right_col > this.cols) {
1848 | col = col - (right_col - col);
1849 | }
1850 |
1851 | var moved_down = this.placeholder_grid_data.row < row;
1852 | var changed_column = this.placeholder_grid_data.col !== col;
1853 |
1854 | this.placeholder_grid_data.col = col;
1855 | this.placeholder_grid_data.row = row;
1856 |
1857 | this.cells_occupied_by_placeholder = this.get_cells_occupied(
1858 | this.placeholder_grid_data);
1859 |
1860 | this.$preview_holder.attr({
1861 | 'data-row' : row,
1862 | 'data-col' : col
1863 | });
1864 |
1865 | if (moved_down || changed_column) {
1866 | $nexts.each($.proxy(function(i, widget) {
1867 | this.move_widget_up(
1868 | $(widget), this.placeholder_grid_data.col - col + phgd.size_y);
1869 | }, this));
1870 | }
1871 |
1872 | var $widgets_under_ph = this.get_widgets_under_player(
1873 | this.cells_occupied_by_placeholder);
1874 |
1875 | if ($widgets_under_ph.length) {
1876 | $widgets_under_ph.each($.proxy(function(i, widget) {
1877 | var $w = $(widget);
1878 | this.move_widget_down(
1879 | $w, row + phgd.size_y - $w.data('coords').grid.row);
1880 | }, this));
1881 | }
1882 |
1883 | };
1884 |
1885 |
1886 | /**
1887 | * Determines whether the player can move to a position above.
1888 | *
1889 | * @method can_go_player_up
1890 | * @param {Object} widget_grid_data The actual grid coords object of the
1891 | * player.
1892 | * @return {Number|Boolean} If the player can be moved to an upper row
1893 | * returns the row number, else returns false.
1894 | */
1895 | fn.can_go_player_up = function(widget_grid_data) {
1896 | var p_bottom_row = widget_grid_data.row + widget_grid_data.size_y - 1;
1897 | var result = true;
1898 | var upper_rows = [];
1899 | var min_row = 10000;
1900 | var $widgets_under_player = this.get_widgets_under_player();
1901 |
1902 | /* generate an array with columns as index and array with upper rows
1903 | * empty as value */
1904 | this.for_each_column_occupied(widget_grid_data, function(tcol) {
1905 | var grid_col = this.gridmap[tcol];
1906 | var r = p_bottom_row + 1;
1907 | upper_rows[tcol] = [];
1908 |
1909 | while (--r > 0) {
1910 | if (this.is_empty(tcol, r) || this.is_player(tcol, r) ||
1911 | this.is_widget(tcol, r) &&
1912 | grid_col[r].is($widgets_under_player)
1913 | ) {
1914 | upper_rows[tcol].push(r);
1915 | min_row = r < min_row ? r : min_row;
1916 | } else {
1917 | break;
1918 | }
1919 | }
1920 |
1921 | if (upper_rows[tcol].length === 0) {
1922 | result = false;
1923 | return true; //break
1924 | }
1925 |
1926 | upper_rows[tcol].sort(function(a, b) {
1927 | return a - b;
1928 | });
1929 | });
1930 |
1931 | if (!result) { return false; }
1932 |
1933 | return this.get_valid_rows(widget_grid_data, upper_rows, min_row);
1934 | };
1935 |
1936 |
1937 | /**
1938 | * Determines whether a widget can move to a position above.
1939 | *
1940 | * @method can_go_widget_up
1941 | * @param {Object} widget_grid_data The actual grid coords object of the
1942 | * widget we want to check.
1943 | * @return {Number|Boolean} If the widget can be moved to an upper row
1944 | * returns the row number, else returns false.
1945 | */
1946 | fn.can_go_widget_up = function(widget_grid_data) {
1947 | var p_bottom_row = widget_grid_data.row + widget_grid_data.size_y - 1;
1948 | var result = true;
1949 | var upper_rows = [];
1950 | var min_row = 10000;
1951 |
1952 | /* generate an array with columns as index and array with topmost rows
1953 | * empty as value */
1954 | this.for_each_column_occupied(widget_grid_data, function(tcol) {
1955 | var grid_col = this.gridmap[tcol];
1956 | upper_rows[tcol] = [];
1957 |
1958 | var r = p_bottom_row + 1;
1959 | // iterate over each row
1960 | while (--r > 0) {
1961 | if (this.is_widget(tcol, r) && !this.is_player_in(tcol, r)) {
1962 | if (!grid_col[r].is(widget_grid_data.el)) {
1963 | break;
1964 | }
1965 | }
1966 |
1967 | if (!this.is_player(tcol, r) &&
1968 | !this.is_placeholder_in(tcol, r) &&
1969 | !this.is_player_in(tcol, r)) {
1970 | upper_rows[tcol].push(r);
1971 | }
1972 |
1973 | if (r < min_row) {
1974 | min_row = r;
1975 | }
1976 | }
1977 |
1978 | if (upper_rows[tcol].length === 0) {
1979 | result = false;
1980 | return true; //break
1981 | }
1982 |
1983 | upper_rows[tcol].sort(function(a, b) {
1984 | return a - b;
1985 | });
1986 | });
1987 |
1988 | if (!result) { return false; }
1989 |
1990 | return this.get_valid_rows(widget_grid_data, upper_rows, min_row);
1991 | };
1992 |
1993 |
1994 | /**
1995 | * Search a valid row for the widget represented by `widget_grid_data' in
1996 | * the `upper_rows` array. Iteration starts from row specified in `min_row`.
1997 | *
1998 | * @method get_valid_rows
1999 | * @param {Object} widget_grid_data The actual grid coords object of the
2000 | * player.
2001 | * @param {Array} upper_rows An array with columns as index and arrays
2002 | * of valid rows as values.
2003 | * @param {Number} min_row The upper row from which the iteration will start.
2004 | * @return {Number|Boolean} Returns the upper row valid from the `upper_rows`
2005 | * for the widget in question.
2006 | */
2007 | fn.get_valid_rows = function(widget_grid_data, upper_rows, min_row) {
2008 | var p_top_row = widget_grid_data.row;
2009 | var p_bottom_row = widget_grid_data.row + widget_grid_data.size_y - 1;
2010 | var size_y = widget_grid_data.size_y;
2011 | var r = min_row - 1;
2012 | var valid_rows = [];
2013 |
2014 | while (++r <= p_bottom_row ) {
2015 | var common = true;
2016 | $.each(upper_rows, function(col, rows) {
2017 | if ($.isArray(rows) && $.inArray(r, rows) === -1) {
2018 | common = false;
2019 | }
2020 | });
2021 |
2022 | if (common === true) {
2023 | valid_rows.push(r);
2024 | if (valid_rows.length === size_y) {
2025 | break;
2026 | }
2027 | }
2028 | }
2029 |
2030 | var new_row = false;
2031 | if (size_y === 1) {
2032 | if (valid_rows[0] !== p_top_row) {
2033 | new_row = valid_rows[0] || false;
2034 | }
2035 | } else {
2036 | if (valid_rows[0] !== p_top_row) {
2037 | new_row = this.get_consecutive_numbers_index(
2038 | valid_rows, size_y);
2039 | }
2040 | }
2041 |
2042 | return new_row;
2043 | };
2044 |
2045 |
2046 | fn.get_consecutive_numbers_index = function(arr, size_y) {
2047 | var max = arr.length;
2048 | var result = [];
2049 | var first = true;
2050 | var prev = -1; // or null?
2051 |
2052 | for (var i=0; i < max; i++) {
2053 | if (first || arr[i] === prev + 1) {
2054 | result.push(i);
2055 | if (result.length === size_y) {
2056 | break;
2057 | }
2058 | first = false;
2059 | } else {
2060 | result = [];
2061 | first = true;
2062 | }
2063 |
2064 | prev = arr[i];
2065 | }
2066 |
2067 | return result.length >= size_y ? arr[result[0]] : false;
2068 | };
2069 |
2070 |
2071 | /**
2072 | * Get widgets overlapping with the player.
2073 | *
2074 | * @method get_widgets_overlapped
2075 | * @return {jQuery} Returns a jQuery collection of HTMLElements.
2076 | */
2077 | fn.get_widgets_overlapped = function() {
2078 | var $w;
2079 | var $widgets = $([]);
2080 | var used = [];
2081 | var rows_from_bottom = this.cells_occupied_by_player.rows.slice(0);
2082 | rows_from_bottom.reverse();
2083 |
2084 | $.each(this.cells_occupied_by_player.cols, $.proxy(function(i, col) {
2085 | $.each(rows_from_bottom, $.proxy(function(i, row) {
2086 | // if there is a widget in the player position
2087 | if (!this.gridmap[col]) { return true; } //next iteration
2088 | var $w = this.gridmap[col][row];
2089 | if (this.is_occupied(col, row) && !this.is_player($w) &&
2090 | $.inArray($w, used) === -1
2091 | ) {
2092 | $widgets = $widgets.add($w);
2093 | used.push($w);
2094 | }
2095 |
2096 | }, this));
2097 | }, this));
2098 |
2099 | return $widgets;
2100 | };
2101 |
2102 |
2103 | /**
2104 | * This callback is executed when the player begins to collide with a column.
2105 | *
2106 | * @method on_start_overlapping_column
2107 | * @param {Number} col The collided column.
2108 | * @return {jQuery} Returns a jQuery collection of HTMLElements.
2109 | */
2110 | fn.on_start_overlapping_column = function(col) {
2111 | this.set_player(col, false);
2112 | };
2113 |
2114 |
2115 | /**
2116 | * A callback executed when the player begins to collide with a row.
2117 | *
2118 | * @method on_start_overlapping_row
2119 | * @param {Number} row The collided row.
2120 | * @return {jQuery} Returns a jQuery collection of HTMLElements.
2121 | */
2122 | fn.on_start_overlapping_row = function(row) {
2123 | this.set_player(false, row);
2124 | };
2125 |
2126 |
2127 | /**
2128 | * A callback executed when the the player ends to collide with a column.
2129 | *
2130 | * @method on_stop_overlapping_column
2131 | * @param {Number} col The collided row.
2132 | * @return {jQuery} Returns a jQuery collection of HTMLElements.
2133 | */
2134 | fn.on_stop_overlapping_column = function(col) {
2135 | this.set_player(col, false);
2136 |
2137 | var self = this;
2138 | this.for_each_widget_below(col, this.cells_occupied_by_player.rows[0],
2139 | function(tcol, trow) {
2140 | self.move_widget_up(this, self.player_grid_data.size_y);
2141 | });
2142 | };
2143 |
2144 |
2145 | /**
2146 | * This callback is executed when the player ends to collide with a row.
2147 | *
2148 | * @method on_stop_overlapping_row
2149 | * @param {Number} row The collided row.
2150 | * @return {jQuery} Returns a jQuery collection of HTMLElements.
2151 | */
2152 | fn.on_stop_overlapping_row = function(row) {
2153 | this.set_player(false, row);
2154 |
2155 | var self = this;
2156 | var cols = this.cells_occupied_by_player.cols;
2157 | for (var c = 0, cl = cols.length; c < cl; c++) {
2158 | this.for_each_widget_below(cols[c], row, function(tcol, trow) {
2159 | self.move_widget_up(this, self.player_grid_data.size_y);
2160 | });
2161 | }
2162 | };
2163 |
2164 |
2165 | /**
2166 | * Move a widget to a specific row. The cell or cells must be empty.
2167 | * If the widget has widgets below, all of these widgets will be moved also
2168 | * if they can.
2169 | *
2170 | * @method move_widget_to
2171 | * @param {HTMLElement} $widget The jQuery wrapped HTMLElement of the
2172 | * widget is going to be moved.
2173 | * @return {Class} Returns the instance of the Gridster Class.
2174 | */
2175 | fn.move_widget_to = function($widget, row) {
2176 | var self = this;
2177 | var widget_grid_data = $widget.coords().grid;
2178 | var diff = row - widget_grid_data.row;
2179 | var $next_widgets = this.widgets_below($widget);
2180 |
2181 | var can_move_to_new_cell = this.can_move_to(
2182 | widget_grid_data, widget_grid_data.col, row, $widget);
2183 |
2184 | if (can_move_to_new_cell === false) {
2185 | return false;
2186 | }
2187 |
2188 | this.remove_from_gridmap(widget_grid_data);
2189 | widget_grid_data.row = row;
2190 | this.add_to_gridmap(widget_grid_data);
2191 | $widget.attr('data-row', row);
2192 | this.$changed = this.$changed.add($widget);
2193 |
2194 |
2195 | $next_widgets.each(function(i, widget) {
2196 | var $w = $(widget);
2197 | var wgd = $w.coords().grid;
2198 | var can_go_up = self.can_go_widget_up(wgd);
2199 | if (can_go_up && can_go_up !== wgd.row) {
2200 | self.move_widget_to($w, can_go_up);
2201 | }
2202 | });
2203 |
2204 | return this;
2205 | };
2206 |
2207 |
2208 | /**
2209 | * Move up the specified widget and all below it.
2210 | *
2211 | * @method move_widget_up
2212 | * @param {HTMLElement} $widget The widget you want to move.
2213 | * @param {Number} [y_units] The number of cells that the widget has to move.
2214 | * @return {Class} Returns the instance of the Gridster Class.
2215 | */
2216 | fn.move_widget_up = function($widget, y_units) {
2217 | var el_grid_data = $widget.coords().grid;
2218 | var actual_row = el_grid_data.row;
2219 | var moved = [];
2220 | var can_go_up = true;
2221 | y_units || (y_units = 1);
2222 |
2223 | if (!this.can_go_up($widget)) { return false; } //break;
2224 |
2225 | this.for_each_column_occupied(el_grid_data, function(col) {
2226 | // can_go_up
2227 | if ($.inArray($widget, moved) === -1) {
2228 | var widget_grid_data = $widget.coords().grid;
2229 | var next_row = actual_row - y_units;
2230 | next_row = this.can_go_up_to_row(
2231 | widget_grid_data, col, next_row);
2232 |
2233 | if (!next_row) {
2234 | return true;
2235 | }
2236 |
2237 | var $next_widgets = this.widgets_below($widget);
2238 |
2239 | this.remove_from_gridmap(widget_grid_data);
2240 | widget_grid_data.row = next_row;
2241 | this.add_to_gridmap(widget_grid_data);
2242 | $widget.attr('data-row', widget_grid_data.row);
2243 | this.$changed = this.$changed.add($widget);
2244 |
2245 | moved.push($widget);
2246 |
2247 | $next_widgets.each($.proxy(function(i, widget) {
2248 | this.move_widget_up($(widget), y_units);
2249 | }, this));
2250 | }
2251 | });
2252 |
2253 | };
2254 |
2255 |
2256 | /**
2257 | * Move down the specified widget and all below it.
2258 | *
2259 | * @method move_widget_down
2260 | * @param {jQuery} $widget The jQuery object representing the widget
2261 | * you want to move.
2262 | * @param {Number} y_units The number of cells that the widget has to move.
2263 | * @return {Class} Returns the instance of the Gridster Class.
2264 | */
2265 | fn.move_widget_down = function($widget, y_units) {
2266 | var el_grid_data, actual_row, moved, y_diff;
2267 |
2268 | if (y_units <= 0) { return false; }
2269 |
2270 | el_grid_data = $widget.coords().grid;
2271 | actual_row = el_grid_data.row;
2272 | moved = [];
2273 | y_diff = y_units;
2274 |
2275 | if (!$widget) { return false; }
2276 |
2277 | if ($.inArray($widget, moved) === -1) {
2278 |
2279 | var widget_grid_data = $widget.coords().grid;
2280 | var next_row = actual_row + y_units;
2281 | var $next_widgets = this.widgets_below($widget);
2282 |
2283 | this.remove_from_gridmap(widget_grid_data);
2284 |
2285 | $next_widgets.each($.proxy(function(i, widget) {
2286 | var $w = $(widget);
2287 | var wd = $w.coords().grid;
2288 | var tmp_y = this.displacement_diff(
2289 | wd, widget_grid_data, y_diff);
2290 |
2291 | if (tmp_y > 0) {
2292 | this.move_widget_down($w, tmp_y);
2293 | }
2294 | }, this));
2295 |
2296 | widget_grid_data.row = next_row;
2297 | this.update_widget_position(widget_grid_data, $widget);
2298 | $widget.attr('data-row', widget_grid_data.row);
2299 | this.$changed = this.$changed.add($widget);
2300 |
2301 | moved.push($widget);
2302 | }
2303 | };
2304 |
2305 |
2306 | /**
2307 | * Check if the widget can move to the specified row, else returns the
2308 | * upper row possible.
2309 | *
2310 | * @method can_go_up_to_row
2311 | * @param {Number} widget_grid_data The current grid coords object of the
2312 | * widget.
2313 | * @param {Number} col The target column.
2314 | * @param {Number} row The target row.
2315 | * @return {Boolean|Number} Returns the row number if the widget can move
2316 | * to the target position, else returns false.
2317 | */
2318 | fn.can_go_up_to_row = function(widget_grid_data, col, row) {
2319 | var ga = this.gridmap;
2320 | var result = true;
2321 | var urc = []; // upper_rows_in_columns
2322 | var actual_row = widget_grid_data.row;
2323 | var r;
2324 |
2325 | /* generate an array with columns as index and array with
2326 | * upper rows empty in the column */
2327 | this.for_each_column_occupied(widget_grid_data, function(tcol) {
2328 | var grid_col = ga[tcol];
2329 | urc[tcol] = [];
2330 |
2331 | r = actual_row;
2332 | while (r--) {
2333 | if (this.is_empty(tcol, r) &&
2334 | !this.is_placeholder_in(tcol, r)
2335 | ) {
2336 | urc[tcol].push(r);
2337 | } else {
2338 | break;
2339 | }
2340 | }
2341 |
2342 | if (!urc[tcol].length) {
2343 | result = false;
2344 | return true;
2345 | }
2346 |
2347 | });
2348 |
2349 | if (!result) { return false; }
2350 |
2351 | /* get common rows starting from upper position in all the columns
2352 | * that widget occupies */
2353 | r = row;
2354 | for (r = 1; r < actual_row; r++) {
2355 | var common = true;
2356 |
2357 | for (var uc = 0, ucl = urc.length; uc < ucl; uc++) {
2358 | if (urc[uc] && $.inArray(r, urc[uc]) === -1) {
2359 | common = false;
2360 | }
2361 | }
2362 |
2363 | if (common === true) {
2364 | result = r;
2365 | break;
2366 | }
2367 | }
2368 |
2369 | return result;
2370 | };
2371 |
2372 |
2373 | fn.displacement_diff = function(widget_grid_data, parent_bgd, y_units) {
2374 | var actual_row = widget_grid_data.row;
2375 | var diffs = [];
2376 | var parent_max_y = parent_bgd.row + parent_bgd.size_y;
2377 |
2378 | this.for_each_column_occupied(widget_grid_data, function(col) {
2379 | var temp_y_units = 0;
2380 |
2381 | for (var r = parent_max_y; r < actual_row; r++) {
2382 | if (this.is_empty(col, r)) {
2383 | temp_y_units = temp_y_units + 1;
2384 | }
2385 | }
2386 |
2387 | diffs.push(temp_y_units);
2388 | });
2389 |
2390 | var max_diff = Math.max.apply(Math, diffs);
2391 | y_units = (y_units - max_diff);
2392 |
2393 | return y_units > 0 ? y_units : 0;
2394 | };
2395 |
2396 |
2397 | /**
2398 | * Get widgets below a widget.
2399 | *
2400 | * @method widgets_below
2401 | * @param {HTMLElement} $el The jQuery wrapped HTMLElement.
2402 | * @return {jQuery} A jQuery collection of HTMLElements.
2403 | */
2404 | fn.widgets_below = function($el) {
2405 | var el_grid_data = $.isPlainObject($el) ? $el : $el.coords().grid;
2406 | var self = this;
2407 | var ga = this.gridmap;
2408 | var next_row = el_grid_data.row + el_grid_data.size_y - 1;
2409 | var $nexts = $([]);
2410 |
2411 | this.for_each_column_occupied(el_grid_data, function(col) {
2412 | self.for_each_widget_below(col, next_row, function(tcol, trow) {
2413 | if (!self.is_player(this) && $.inArray(this, $nexts) === -1) {
2414 | $nexts = $nexts.add(this);
2415 | return true; // break
2416 | }
2417 | });
2418 | });
2419 |
2420 | return Gridster.sort_by_row_asc($nexts);
2421 | };
2422 |
2423 |
2424 | /**
2425 | * Update the array of mapped positions with the new player position.
2426 | *
2427 | * @method set_cells_player_occupies
2428 | * @param {Number} col The new player col.
2429 | * @param {Number} col The new player row.
2430 | * @return {Class} Returns the instance of the Gridster Class.
2431 | */
2432 | fn.set_cells_player_occupies = function(col, row) {
2433 | this.remove_from_gridmap(this.placeholder_grid_data);
2434 | this.placeholder_grid_data.col = col;
2435 | this.placeholder_grid_data.row = row;
2436 | this.add_to_gridmap(this.placeholder_grid_data, this.$player);
2437 | return this;
2438 | };
2439 |
2440 |
2441 | /**
2442 | * Remove from the array of mapped positions the reference to the player.
2443 | *
2444 | * @method empty_cells_player_occupies
2445 | * @return {Class} Returns the instance of the Gridster Class.
2446 | */
2447 | fn.empty_cells_player_occupies = function() {
2448 | this.remove_from_gridmap(this.placeholder_grid_data);
2449 | return this;
2450 | };
2451 |
2452 |
2453 | fn.can_go_up = function($el) {
2454 | var el_grid_data = $el.coords().grid;
2455 | var initial_row = el_grid_data.row;
2456 | var prev_row = initial_row - 1;
2457 | var ga = this.gridmap;
2458 | var upper_rows_by_column = [];
2459 |
2460 | var result = true;
2461 | if (initial_row === 1) { return false; }
2462 |
2463 | this.for_each_column_occupied(el_grid_data, function(col) {
2464 | var $w = this.is_widget(col, prev_row);
2465 |
2466 | if (this.is_occupied(col, prev_row) ||
2467 | this.is_player(col, prev_row) ||
2468 | this.is_placeholder_in(col, prev_row) ||
2469 | this.is_player_in(col, prev_row)
2470 | ) {
2471 | result = false;
2472 | return true; //break
2473 | }
2474 | });
2475 |
2476 | return result;
2477 | };
2478 |
2479 |
2480 | /**
2481 | * Check if it's possible to move a widget to a specific col/row. It takes
2482 | * into account the dimensions (`size_y` and `size_x` attrs. of the grid
2483 | * coords object) the widget occupies.
2484 | *
2485 | * @method can_move_to
2486 | * @param {Object} widget_grid_data The grid coords object that represents
2487 | * the widget.
2488 | * @param {Object} col The col to check.
2489 | * @param {Object} row The row to check.
2490 | * @param {Number} [max_row] The max row allowed.
2491 | * @return {Boolean} Returns true if all cells are empty, else return false.
2492 | */
2493 | fn.can_move_to = function(widget_grid_data, col, row, max_row) {
2494 | var ga = this.gridmap;
2495 | var $w = widget_grid_data.el;
2496 | var future_wd = {
2497 | size_y: widget_grid_data.size_y,
2498 | size_x: widget_grid_data.size_x,
2499 | col: col,
2500 | row: row
2501 | };
2502 | var result = true;
2503 |
2504 | //Prevents widgets go out of the grid
2505 | var right_col = col + widget_grid_data.size_x - 1;
2506 | if (right_col > this.cols) {
2507 | return false;
2508 | }
2509 |
2510 | if (max_row && max_row < row + widget_grid_data.size_y - 1) {
2511 | return false;
2512 | }
2513 |
2514 | this.for_each_cell_occupied(future_wd, function(tcol, trow) {
2515 | var $tw = this.is_widget(tcol, trow);
2516 | if ($tw && (!widget_grid_data.el || $tw.is($w))) {
2517 | result = false;
2518 | }
2519 | });
2520 |
2521 | return result;
2522 | };
2523 |
2524 |
2525 | /**
2526 | * Given the leftmost column returns all columns that are overlapping
2527 | * with the player.
2528 | *
2529 | * @method get_targeted_columns
2530 | * @param {Number} [from_col] The leftmost column.
2531 | * @return {Array} Returns an array with column numbers.
2532 | */
2533 | fn.get_targeted_columns = function(from_col) {
2534 | var max = (from_col || this.player_grid_data.col) +
2535 | (this.player_grid_data.size_x - 1);
2536 | var cols = [];
2537 | for (var col = from_col; col <= max; col++) {
2538 | cols.push(col);
2539 | }
2540 | return cols;
2541 | };
2542 |
2543 |
2544 | /**
2545 | * Given the upper row returns all rows that are overlapping with the player.
2546 | *
2547 | * @method get_targeted_rows
2548 | * @param {Number} [from_row] The upper row.
2549 | * @return {Array} Returns an array with row numbers.
2550 | */
2551 | fn.get_targeted_rows = function(from_row) {
2552 | var max = (from_row || this.player_grid_data.row) +
2553 | (this.player_grid_data.size_y - 1);
2554 | var rows = [];
2555 | for (var row = from_row; row <= max; row++) {
2556 | rows.push(row);
2557 | }
2558 | return rows;
2559 | };
2560 |
2561 | /**
2562 | * Get all columns and rows that a widget occupies.
2563 | *
2564 | * @method get_cells_occupied
2565 | * @param {Object} el_grid_data The grid coords object of the widget.
2566 | * @return {Object} Returns an object like `{ cols: [], rows: []}`.
2567 | */
2568 | fn.get_cells_occupied = function(el_grid_data) {
2569 | var cells = { cols: [], rows: []};
2570 | var i;
2571 | if (arguments[1] instanceof $) {
2572 | el_grid_data = arguments[1].coords().grid;
2573 | }
2574 |
2575 | for (i = 0; i < el_grid_data.size_x; i++) {
2576 | var col = el_grid_data.col + i;
2577 | cells.cols.push(col);
2578 | }
2579 |
2580 | for (i = 0; i < el_grid_data.size_y; i++) {
2581 | var row = el_grid_data.row + i;
2582 | cells.rows.push(row);
2583 | }
2584 |
2585 | return cells;
2586 | };
2587 |
2588 |
2589 | /**
2590 | * Iterate over the cells occupied by a widget executing a function for
2591 | * each one.
2592 | *
2593 | * @method for_each_cell_occupied
2594 | * @param {Object} el_grid_data The grid coords object that represents the
2595 | * widget.
2596 | * @param {Function} callback The function to execute on each column
2597 | * iteration. Column and row are passed as arguments.
2598 | * @return {Class} Returns the instance of the Gridster Class.
2599 | */
2600 | fn.for_each_cell_occupied = function(grid_data, callback) {
2601 | this.for_each_column_occupied(grid_data, function(col) {
2602 | this.for_each_row_occupied(grid_data, function(row) {
2603 | callback.call(this, col, row);
2604 | });
2605 | });
2606 | return this;
2607 | };
2608 |
2609 |
2610 | /**
2611 | * Iterate over the columns occupied by a widget executing a function for
2612 | * each one.
2613 | *
2614 | * @method for_each_column_occupied
2615 | * @param {Object} el_grid_data The grid coords object that represents
2616 | * the widget.
2617 | * @param {Function} callback The function to execute on each column
2618 | * iteration. The column number is passed as first argument.
2619 | * @return {Class} Returns the instance of the Gridster Class.
2620 | */
2621 | fn.for_each_column_occupied = function(el_grid_data, callback) {
2622 | for (var i = 0; i < el_grid_data.size_x; i++) {
2623 | var col = el_grid_data.col + i;
2624 | callback.call(this, col, el_grid_data);
2625 | }
2626 | };
2627 |
2628 |
2629 | /**
2630 | * Iterate over the rows occupied by a widget executing a function for
2631 | * each one.
2632 | *
2633 | * @method for_each_row_occupied
2634 | * @param {Object} el_grid_data The grid coords object that represents
2635 | * the widget.
2636 | * @param {Function} callback The function to execute on each column
2637 | * iteration. The row number is passed as first argument.
2638 | * @return {Class} Returns the instance of the Gridster Class.
2639 | */
2640 | fn.for_each_row_occupied = function(el_grid_data, callback) {
2641 | for (var i = 0; i < el_grid_data.size_y; i++) {
2642 | var row = el_grid_data.row + i;
2643 | callback.call(this, row, el_grid_data);
2644 | }
2645 | };
2646 |
2647 |
2648 |
2649 | fn._traversing_widgets = function(type, direction, col, row, callback) {
2650 | var ga = this.gridmap;
2651 | if (!ga[col]) { return; }
2652 |
2653 | var cr, max;
2654 | var action = type + '/' + direction;
2655 | if (arguments[2] instanceof $) {
2656 | var el_grid_data = arguments[2].coords().grid;
2657 | col = el_grid_data.col;
2658 | row = el_grid_data.row;
2659 | callback = arguments[3];
2660 | }
2661 | var matched = [];
2662 | var trow = row;
2663 |
2664 |
2665 | var methods = {
2666 | 'for_each/above': function() {
2667 | while (trow--) {
2668 | if (trow > 0 && this.is_widget(col, trow) &&
2669 | $.inArray(ga[col][trow], matched) === -1
2670 | ) {
2671 | cr = callback.call(ga[col][trow], col, trow);
2672 | matched.push(ga[col][trow]);
2673 | if (cr) { break; }
2674 | }
2675 | }
2676 | },
2677 | 'for_each/below': function() {
2678 | for (trow = row + 1, max = ga[col].length; trow < max; trow++) {
2679 | if (this.is_widget(col, trow) &&
2680 | $.inArray(ga[col][trow], matched) === -1
2681 | ) {
2682 | cr = callback.call(ga[col][trow], col, trow);
2683 | matched.push(ga[col][trow]);
2684 | if (cr) { break; }
2685 | }
2686 | }
2687 | }
2688 | };
2689 |
2690 | if (methods[action]) {
2691 | methods[action].call(this);
2692 | }
2693 | };
2694 |
2695 |
2696 | /**
2697 | * Iterate over each widget above the column and row specified.
2698 | *
2699 | * @method for_each_widget_above
2700 | * @param {Number} col The column to start iterating.
2701 | * @param {Number} row The row to start iterating.
2702 | * @param {Function} callback The function to execute on each widget
2703 | * iteration. The value of `this` inside the function is the jQuery
2704 | * wrapped HTMLElement.
2705 | * @return {Class} Returns the instance of the Gridster Class.
2706 | */
2707 | fn.for_each_widget_above = function(col, row, callback) {
2708 | this._traversing_widgets('for_each', 'above', col, row, callback);
2709 | return this;
2710 | };
2711 |
2712 |
2713 | /**
2714 | * Iterate over each widget below the column and row specified.
2715 | *
2716 | * @method for_each_widget_below
2717 | * @param {Number} col The column to start iterating.
2718 | * @param {Number} row The row to start iterating.
2719 | * @param {Function} callback The function to execute on each widget
2720 | * iteration. The value of `this` inside the function is the jQuery wrapped
2721 | * HTMLElement.
2722 | * @return {Class} Returns the instance of the Gridster Class.
2723 | */
2724 | fn.for_each_widget_below = function(col, row, callback) {
2725 | this._traversing_widgets('for_each', 'below', col, row, callback);
2726 | return this;
2727 | };
2728 |
2729 |
2730 | /**
2731 | * Returns the highest occupied cell in the grid.
2732 | *
2733 | * @method get_highest_occupied_cell
2734 | * @return {Object} Returns an object with `col` and `row` numbers.
2735 | */
2736 | fn.get_highest_occupied_cell = function() {
2737 | var r;
2738 | var gm = this.gridmap;
2739 | var rl = gm[1].length;
2740 | var rows = [], cols = [];
2741 | var row_in_col = [];
2742 | for (var c = gm.length - 1; c >= 1; c--) {
2743 | for (r = rl - 1; r >= 1; r--) {
2744 | if (this.is_widget(c, r)) {
2745 | rows.push(r);
2746 | cols.push(c);
2747 | break;
2748 | }
2749 | }
2750 | }
2751 |
2752 | return {
2753 | col: Math.max.apply(Math, cols),
2754 | row: Math.max.apply(Math, rows)
2755 | };
2756 | };
2757 |
2758 |
2759 | fn.get_widgets_from = function(col, row) {
2760 | var ga = this.gridmap;
2761 | var $widgets = $();
2762 |
2763 | if (col) {
2764 | $widgets = $widgets.add(
2765 | this.$widgets.filter(function() {
2766 | var tcol = $(this).attr('data-col');
2767 | return (tcol === col || tcol > col);
2768 | })
2769 | );
2770 | }
2771 |
2772 | if (row) {
2773 | $widgets = $widgets.add(
2774 | this.$widgets.filter(function() {
2775 | var trow = $(this).attr('data-row');
2776 | return (trow === row || trow > row);
2777 | })
2778 | );
2779 | }
2780 |
2781 | return $widgets;
2782 | };
2783 |
2784 |
2785 | /**
2786 | * Set the current height of the parent grid.
2787 | *
2788 | * @method set_dom_grid_height
2789 | * @return {Object} Returns the instance of the Gridster class.
2790 | */
2791 | fn.set_dom_grid_height = function(height) {
2792 | if (typeof height === 'undefined') {
2793 | var r = this.get_highest_occupied_cell().row;
2794 | height = r * this.min_widget_height;
2795 | }
2796 |
2797 | this.container_height = height;
2798 | this.$el.css('height', this.container_height);
2799 | return this;
2800 | };
2801 |
2802 | /**
2803 | * Set the current width of the parent grid.
2804 | *
2805 | * @method set_dom_grid_width
2806 | * @return {Object} Returns the instance of the Gridster class.
2807 | */
2808 | fn.set_dom_grid_width = function(cols) {
2809 | if (typeof cols === 'undefined') {
2810 | cols = this.get_highest_occupied_cell().col;
2811 | }
2812 |
2813 | var max_cols = (this.options.autogrow_cols ? this.options.max_cols :
2814 | this.cols);
2815 |
2816 | cols = Math.min(max_cols, Math.max(cols, this.options.min_cols));
2817 | this.container_width = cols * this.min_widget_width;
2818 | this.$el.css('width', this.container_width);
2819 | return this;
2820 | };
2821 |
2822 |
2823 | /**
2824 | * It generates the neccessary styles to position the widgets.
2825 | *
2826 | * @method generate_stylesheet
2827 | * @param {Number} rows Number of columns.
2828 | * @param {Number} cols Number of rows.
2829 | * @return {Object} Returns the instance of the Gridster class.
2830 | */
2831 | fn.generate_stylesheet = function(opts) {
2832 | var styles = '';
2833 | var max_size_x = this.options.max_size_x || this.cols;
2834 | var max_rows = 0;
2835 | var max_cols = 0;
2836 | var i;
2837 | var rules;
2838 |
2839 | opts || (opts = {});
2840 | opts.cols || (opts.cols = this.cols);
2841 | opts.rows || (opts.rows = this.rows);
2842 | opts.namespace || (opts.namespace = this.options.namespace);
2843 | opts.widget_base_dimensions ||
2844 | (opts.widget_base_dimensions = this.options.widget_base_dimensions);
2845 | opts.widget_margins ||
2846 | (opts.widget_margins = this.options.widget_margins);
2847 | opts.min_widget_width = (opts.widget_margins[0] * 2) +
2848 | opts.widget_base_dimensions[0];
2849 | opts.min_widget_height = (opts.widget_margins[1] * 2) +
2850 | opts.widget_base_dimensions[1];
2851 |
2852 | // don't duplicate stylesheets for the same configuration
2853 | var serialized_opts = $.param(opts);
2854 | if ($.inArray(serialized_opts, Gridster.generated_stylesheets) >= 0) {
2855 | return false;
2856 | }
2857 |
2858 | this.generated_stylesheets.push(serialized_opts);
2859 | Gridster.generated_stylesheets.push(serialized_opts);
2860 |
2861 | /* generate CSS styles for cols */
2862 | for (i = opts.cols; i >= 0; i--) {
2863 | styles += (opts.namespace + ' [data-col="'+ (i + 1) + '"] { left:' +
2864 | ((i * opts.widget_base_dimensions[0]) +
2865 | (i * opts.widget_margins[0]) +
2866 | ((i + 1) * opts.widget_margins[0])) + 'px; }\n');
2867 | }
2868 |
2869 | /* generate CSS styles for rows */
2870 | for (i = opts.rows; i >= 0; i--) {
2871 | styles += (opts.namespace + ' [data-row="' + (i + 1) + '"] { top:' +
2872 | ((i * opts.widget_base_dimensions[1]) +
2873 | (i * opts.widget_margins[1]) +
2874 | ((i + 1) * opts.widget_margins[1]) ) + 'px; }\n');
2875 | }
2876 |
2877 | for (var y = 1; y <= opts.rows; y++) {
2878 | styles += (opts.namespace + ' [data-sizey="' + y + '"] { height:' +
2879 | (y * opts.widget_base_dimensions[1] +
2880 | (y - 1) * (opts.widget_margins[1] * 2)) + 'px; }\n');
2881 | }
2882 |
2883 | for (var x = 1; x <= max_size_x; x++) {
2884 | styles += (opts.namespace + ' [data-sizex="' + x + '"] { width:' +
2885 | (x * opts.widget_base_dimensions[0] +
2886 | (x - 1) * (opts.widget_margins[0] * 2)) + 'px; }\n');
2887 | }
2888 |
2889 | this.remove_style_tags();
2890 |
2891 | return this.add_style_tag(styles);
2892 | };
2893 |
2894 |
2895 | /**
2896 | * Injects the given CSS as string to the head of the document.
2897 | *
2898 | * @method add_style_tag
2899 | * @param {String} css The styles to apply.
2900 | * @return {Object} Returns the instance of the Gridster class.
2901 | */
2902 | fn.add_style_tag = function(css) {
2903 | var d = document;
2904 | var tag = d.createElement('style');
2905 |
2906 | d.getElementsByTagName('head')[0].appendChild(tag);
2907 | tag.setAttribute('type', 'text/css');
2908 |
2909 | if (tag.styleSheet) {
2910 | tag.styleSheet.cssText = css;
2911 | }
2912 | else{
2913 | tag.appendChild(document.createTextNode(css));
2914 | }
2915 |
2916 | this.remove_style_tags();
2917 | this.$style_tags = this.$style_tags.add(tag);
2918 |
2919 | return this;
2920 | };
2921 |
2922 |
2923 | /**
2924 | * Remove the style tag with the associated id from the head of the document
2925 | *
2926 | * @method remove_style_tag
2927 | * @return {Object} Returns the instance of the Gridster class.
2928 | */
2929 | fn.remove_style_tags = function() {
2930 | var all_styles = Gridster.generated_stylesheets;
2931 | var ins_styles = this.generated_stylesheets;
2932 |
2933 | this.$style_tags.remove();
2934 |
2935 | Gridster.generated_stylesheets = $.map(all_styles, function(s) {
2936 | if ($.inArray(s, ins_styles) === -1) { return s; }
2937 | });
2938 | };
2939 |
2940 |
2941 | /**
2942 | * Generates a faux grid to collide with it when a widget is dragged and
2943 | * detect row or column that we want to go.
2944 | *
2945 | * @method generate_faux_grid
2946 | * @param {Number} rows Number of columns.
2947 | * @param {Number} cols Number of rows.
2948 | * @return {Object} Returns the instance of the Gridster class.
2949 | */
2950 | fn.generate_faux_grid = function(rows, cols) {
2951 | this.faux_grid = [];
2952 | this.gridmap = [];
2953 | var col;
2954 | var row;
2955 | for (col = cols; col > 0; col--) {
2956 | this.gridmap[col] = [];
2957 | for (row = rows; row > 0; row--) {
2958 | this.add_faux_cell(row, col);
2959 | }
2960 | }
2961 | return this;
2962 | };
2963 |
2964 |
2965 | /**
2966 | * Add cell to the faux grid.
2967 | *
2968 | * @method add_faux_cell
2969 | * @param {Number} row The row for the new faux cell.
2970 | * @param {Number} col The col for the new faux cell.
2971 | * @return {Object} Returns the instance of the Gridster class.
2972 | */
2973 | fn.add_faux_cell = function(row, col) {
2974 | var coords = $({
2975 | left: this.baseX + ((col - 1) * this.min_widget_width),
2976 | top: this.baseY + (row -1) * this.min_widget_height,
2977 | width: this.min_widget_width,
2978 | height: this.min_widget_height,
2979 | col: col,
2980 | row: row,
2981 | original_col: col,
2982 | original_row: row
2983 | }).coords();
2984 |
2985 | if (!$.isArray(this.gridmap[col])) {
2986 | this.gridmap[col] = [];
2987 | }
2988 |
2989 | this.gridmap[col][row] = false;
2990 | this.faux_grid.push(coords);
2991 |
2992 | return this;
2993 | };
2994 |
2995 |
2996 | /**
2997 | * Add rows to the faux grid.
2998 | *
2999 | * @method add_faux_rows
3000 | * @param {Number} rows The number of rows you want to add to the faux grid.
3001 | * @return {Object} Returns the instance of the Gridster class.
3002 | */
3003 | fn.add_faux_rows = function(rows) {
3004 | var actual_rows = this.rows;
3005 | var max_rows = actual_rows + (rows || 1);
3006 |
3007 | for (var r = max_rows; r > actual_rows; r--) {
3008 | for (var c = this.cols; c >= 1; c--) {
3009 | this.add_faux_cell(r, c);
3010 | }
3011 | }
3012 |
3013 | this.rows = max_rows;
3014 |
3015 | if (this.options.autogenerate_stylesheet) {
3016 | this.generate_stylesheet();
3017 | }
3018 |
3019 | return this;
3020 | };
3021 |
3022 | /**
3023 | * Add cols to the faux grid.
3024 | *
3025 | * @method add_faux_cols
3026 | * @param {Number} cols The number of cols you want to add to the faux grid.
3027 | * @return {Object} Returns the instance of the Gridster class.
3028 | */
3029 | fn.add_faux_cols = function(cols) {
3030 | var actual_cols = this.cols;
3031 | var max_cols = actual_cols + (cols || 1);
3032 | max_cols = Math.min(max_cols, this.options.max_cols);
3033 |
3034 | for (var c = actual_cols + 1; c <= max_cols; c++) {
3035 | for (var r = this.rows; r >= 1; r--) {
3036 | this.add_faux_cell(r, c);
3037 | }
3038 | }
3039 |
3040 | this.cols = max_cols;
3041 |
3042 | if (this.options.autogenerate_stylesheet) {
3043 | this.generate_stylesheet();
3044 | }
3045 |
3046 | return this;
3047 | };
3048 |
3049 |
3050 | /**
3051 | * Recalculates the offsets for the faux grid. You need to use it when
3052 | * the browser is resized.
3053 | *
3054 | * @method recalculate_faux_grid
3055 | * @return {Object} Returns the instance of the Gridster class.
3056 | */
3057 | fn.recalculate_faux_grid = function() {
3058 | var aw = this.$wrapper.width();
3059 | this.baseX = ($(window).width() - aw) / 2;
3060 | this.baseY = this.$wrapper.offset().top;
3061 |
3062 | $.each(this.faux_grid, $.proxy(function(i, coords) {
3063 | this.faux_grid[i] = coords.update({
3064 | left: this.baseX + (coords.data.col -1) * this.min_widget_width,
3065 | top: this.baseY + (coords.data.row -1) * this.min_widget_height
3066 | });
3067 | }, this));
3068 |
3069 | return this;
3070 | };
3071 |
3072 |
3073 | /**
3074 | * Get all widgets in the DOM and register them.
3075 | *
3076 | * @method get_widgets_from_DOM
3077 | * @return {Object} Returns the instance of the Gridster class.
3078 | */
3079 | fn.get_widgets_from_DOM = function() {
3080 | var widgets_coords = this.$widgets.map($.proxy(function(i, widget) {
3081 | var $w = $(widget);
3082 | return this.dom_to_coords($w);
3083 | }, this));
3084 |
3085 | widgets_coords = Gridster.sort_by_row_and_col_asc(widgets_coords);
3086 |
3087 | var changes = $(widgets_coords).map($.proxy(function(i, wgd) {
3088 | return this.register_widget(wgd) || null;
3089 | }, this));
3090 |
3091 | if (changes.length) {
3092 | this.$el.trigger('gridster:positionschanged');
3093 | }
3094 |
3095 | return this;
3096 | };
3097 |
3098 |
3099 | /**
3100 | * Calculate columns and rows to be set based on the configuration
3101 | * parameters, grid dimensions, etc ...
3102 | *
3103 | * @method generate_grid_and_stylesheet
3104 | * @return {Object} Returns the instance of the Gridster class.
3105 | */
3106 | fn.generate_grid_and_stylesheet = function() {
3107 | var aw = this.$wrapper.width();
3108 | var max_cols = this.options.max_cols;
3109 |
3110 | var cols = Math.floor(aw / this.min_widget_width) +
3111 | this.options.extra_cols;
3112 |
3113 | var actual_cols = this.$widgets.map(function() {
3114 | return $(this).attr('data-col');
3115 | }).get();
3116 |
3117 | //needed to pass tests with phantomjs
3118 | actual_cols.length || (actual_cols = [0]);
3119 |
3120 | var min_cols = Math.max.apply(Math, actual_cols);
3121 |
3122 | this.cols = Math.max(min_cols, cols, this.options.min_cols);
3123 |
3124 | if (max_cols !== Infinity && max_cols >= min_cols && max_cols < this.cols) {
3125 | this.cols = max_cols;
3126 | }
3127 |
3128 | // get all rows that could be occupied by the current widgets
3129 | var max_rows = this.options.extra_rows;
3130 | this.$widgets.each(function(i, w) {
3131 | max_rows += (+$(w).attr('data-sizey'));
3132 | });
3133 |
3134 | this.rows = Math.max(max_rows, this.options.min_rows);
3135 |
3136 | this.baseX = ($(window).width() - aw) / 2;
3137 | this.baseY = this.$wrapper.offset().top;
3138 |
3139 | if (this.options.autogenerate_stylesheet) {
3140 | this.generate_stylesheet();
3141 | }
3142 |
3143 | return this.generate_faux_grid(this.rows, this.cols);
3144 | };
3145 |
3146 | /**
3147 | * Destroy this gridster by removing any sign of its presence, making it easy to avoid memory leaks
3148 | *
3149 | * @method destroy
3150 | * @param {Boolean} remove If true, remove gridster from DOM.
3151 | * @return {Object} Returns the instance of the Gridster class.
3152 | */
3153 | fn.destroy = function(remove) {
3154 | this.$el.removeData('gridster');
3155 |
3156 | // remove bound callback on window resize
3157 | $(window).unbind('.gridster');
3158 |
3159 | if (this.drag_api) {
3160 | this.drag_api.destroy();
3161 | }
3162 |
3163 | this.remove_style_tags();
3164 |
3165 | remove && this.$el.remove();
3166 |
3167 | return this;
3168 | };
3169 |
3170 |
3171 | //jQuery adapter
3172 | $.fn.gridster = function(options) {
3173 | return this.each(function() {
3174 | if (! $(this).data('gridster')) {
3175 | $(this).data('gridster', new Gridster( this, options ));
3176 | }
3177 | });
3178 | };
3179 |
3180 | return Gridster;
3181 |
3182 | }));
3183 |
--------------------------------------------------------------------------------