|
791 | 791 |
|
792 | 792 |
|
793 | 793 | /**
|
794 |
| - * Creates the grid coords object representing the widget a add it to the |
| 794 | + * Convert widgets from DOM elements to "widget grid data" Objects. |
| 795 | + * |
| 796 | + * @method dom_to_coords |
| 797 | + * @param {HTMLElement} $widget The widget to be converted. |
| 798 | + */ |
| 799 | + fn.dom_to_coords = function($widget) { |
| 800 | + return { |
| 801 | + 'col': parseInt($widget.attr('data-col'), 10), |
| 802 | + 'row': parseInt($widget.attr('data-row'), 10), |
| 803 | + 'size_x': parseInt($widget.attr('data-sizex'), 10) || 1, |
| 804 | + 'size_y': parseInt($widget.attr('data-sizey'), 10) || 1, |
| 805 | + 'max_size_x': parseInt($widget.attr('data-max-sizex'), 10) || false, |
| 806 | + 'max_size_y': parseInt($widget.attr('data-max-sizey'), 10) || false, |
| 807 | + 'min_size_x': parseInt($widget.attr('data-min-sizex'), 10) || false, |
| 808 | + 'min_size_y': parseInt($widget.attr('data-min-sizey'), 10) || false, |
| 809 | + 'el': $widget |
| 810 | + }; |
| 811 | + }; |
| 812 | + |
| 813 | + |
| 814 | + /** |
| 815 | + * Creates the grid coords object representing the widget an add it to the |
795 | 816 | * mapped array of positions.
|
796 | 817 | *
|
797 | 818 | * @method register_widget
|
798 |
| - * @return {Array} Returns the instance of the Gridster class. |
| 819 | + * @param {HTMLElement|Object} $el jQuery wrapped HTMLElement representing |
| 820 | + * the widget, or an "widget grid data" Object with (col, row, el ...). |
| 821 | + * @return {Boolean} Returns true if the widget final position is different |
| 822 | + * than the original. |
799 | 823 | */
|
800 | 824 | fn.register_widget = function($el) {
|
801 |
| - var wgd = { |
802 |
| - 'col': parseInt($el.attr('data-col'), 10), |
803 |
| - 'row': parseInt($el.attr('data-row'), 10), |
804 |
| - 'size_x': parseInt($el.attr('data-sizex'), 10), |
805 |
| - 'size_y': parseInt($el.attr('data-sizey'), 10), |
806 |
| - 'max_size_x': parseInt($el.attr('data-max-sizex'), 10) || false, |
807 |
| - 'max_size_y': parseInt($el.attr('data-max-sizey'), 10) || false, |
808 |
| - 'min_size_x': parseInt($el.attr('data-min-sizex'), 10) || false, |
809 |
| - 'min_size_y': parseInt($el.attr('data-min-sizey'), 10) || false, |
810 |
| - 'el': $el |
811 |
| - }; |
| 825 | + var isDOM = $el instanceof jQuery; |
| 826 | + var wgd = isDOM ? this.dom_to_coords($el) : $el; |
| 827 | + isDOM || ($el = wgd.el); |
| 828 | + |
| 829 | + var empty_upper_row = this.can_go_widget_up(wgd); |
| 830 | + if (empty_upper_row) { |
| 831 | + wgd.row = empty_upper_row; |
| 832 | + $el.attr('data-row', empty_upper_row); |
| 833 | + this.$el.trigger('gridster:positionchanged', [wgd]); |
| 834 | + } |
812 | 835 |
|
813 | 836 | if (this.options.avoid_overlapped_widgets &&
|
814 | 837 | !this.can_move_to(
|
|
832 | 855 |
|
833 | 856 | this.options.resize.enabled && this.add_resize_handle($el);
|
834 | 857 |
|
835 |
| - return this; |
| 858 | + return !! empty_upper_row; |
836 | 859 | };
|
837 | 860 |
|
838 | 861 |
|
|
2991 | 3014 | this.$widgets.each($.proxy(function(i, widget) {
|
2992 | 3015 | this.register_widget($(widget));
|
2993 | 3016 | }, this));
|
| 3017 | + |
| 3018 | + widgets_coords = Gridster.sort_by_row_and_col_asc(widgets_coords); |
| 3019 | + |
| 3020 | + var changes = $(widgets_coords).map($.proxy(function(i, wgd) { |
| 3021 | + return this.register_widget(wgd) || null; |
| 3022 | + }, this)); |
| 3023 | + |
| 3024 | + if (changes.length) { |
| 3025 | + this.$el.trigger('gridster:positionschanged'); |
| 3026 | + } |
| 3027 | + |
2994 | 3028 | return this;
|
2995 | 3029 | };
|
2996 | 3030 |
|
|
0 commit comments