Skip to content

Commit 8ec307b

Browse files
committedJun 25, 2014
feat(gridster): move widget up when added if there is space available
1 parent aef185c commit 8ec307b

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed
 

‎src/jquery.gridster.js

+48-14
Original file line numberDiff line numberDiff line change
@@ -791,24 +791,47 @@
791791

792792

793793
/**
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
795816
* mapped array of positions.
796817
*
797818
* @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.
799823
*/
800824
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+
}
812835

813836
if (this.options.avoid_overlapped_widgets &&
814837
!this.can_move_to(
@@ -832,7 +855,7 @@
832855

833856
this.options.resize.enabled && this.add_resize_handle($el);
834857

835-
return this;
858+
return !! empty_upper_row;
836859
};
837860

838861

@@ -2991,6 +3014,17 @@
29913014
this.$widgets.each($.proxy(function(i, widget) {
29923015
this.register_widget($(widget));
29933016
}, 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+
29943028
return this;
29953029
};
29963030

0 commit comments

Comments
 (0)
Please sign in to comment.