From fbeb2ac05c2801eafccd42d8076183a1f2ae748f Mon Sep 17 00:00:00 2001 From: vieron Date: Fri, 20 Jul 2012 14:03:24 +0200 Subject: [PATCH] fixed add_widget method --- src/jquery.gridster.js | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/jquery.gridster.js b/src/jquery.gridster.js index 8e6a5d7440..380f8cc7d3 100644 --- a/src/jquery.gridster.js +++ b/src/jquery.gridster.js @@ -206,9 +206,7 @@ } if (valid_pos.length) { - var next_position = this.sort_by_row_desc(valid_pos); - next_position = this.sort_by_col_asc(next_position)[0]; - return next_position; + return this.sort_by_row_and_col_asc(valid_pos)[0]; } return false; }; @@ -376,6 +374,7 @@ self.$player = $(this); self.$helper = self.options.draggable.helper === 'clone' ? $(ui.helper) : self.$player; + self.helper = !self.$helper.is(self.$player); self.on_start_drag.call(self, event, ui); self.$el.trigger('gridster:dragstart'); @@ -391,8 +390,7 @@ }); this.$widgets.draggable(draggable_options); - - return this; + return this; }; @@ -466,6 +464,13 @@ this.on_stop_overlapping_row ); + if (this.helper && this.$player) { + this.$player.css({ + 'left': ui.position.left, + 'top': ui.position.top + }); + } + if (this.options.draggable.drag) { this.options.draggable.drag.call(this, event, ui); } @@ -480,6 +485,7 @@ * See http://jqueryui.com/demos/draggable/ for more info. */ fn.on_stop_drag = function(event, ui) { + this.$wrapper.removeClass('dragging'); this.colliders_data = this.drag_api.get_closest_colliders(); this.on_overlapped_column_change( @@ -707,6 +713,26 @@ }; + /** + * Sorts an Array of grid coords objects (representing the grid coords of + * each widget) placing first the empty cells upper left. + * + * @method sort_by_row_asc + * @param {Array} widgets Array of grid coords objects + * @return {Array} Returns the array sorted. + */ + fn.sort_by_row_and_col_asc = function(widgets) { + widgets = widgets.sort(function(a, b){ + if (a.row > b.row || a.row == b.row && a.col > b.col) { + return 1; + } + return -1; + }); + + return widgets; + }; + + /** * Sorts an Array of grid coords objects by column (representing the grid * coords of each widget) in ascending way. @@ -2027,14 +2053,9 @@ cols = Math.max(min_cols, cols, this.options.min_cols); rows = Math.max(min_rows, rows, this.options.min_rows); - //this.support_grid_width = cols * this.min_widget_width; - // this.support_grid_width = this.wrapper_width; - - // this.support_grid_height = rows * this.min_widget_height; this.baseX = ($(window).width() - aw) / 2; this.baseY = this.$wrapper.offset().top; - //this.baseX = 0; if (this.options.autogenerate_stylesheet) { this.generate_stylesheet(rows, cols); }