Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to specify scroll container for drag/resize #464

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/jquery.gridster.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! gridster.js - v0.5.6 - 2014-09-25
/*! gridster.js - v0.5.6 - 2014-12-02
* http://gridster.net/
* Copyright (c) 2014 ducksboard; Licensed MIT */

Expand Down
88 changes: 58 additions & 30 deletions dist/jquery.gridster.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! gridster.js - v0.5.6 - 2014-09-25
/*! gridster.js - v0.5.6 - 2014-12-02
* http://gridster.net/
* Copyright (c) 2014 ducksboard; Licensed MIT */

Expand Down Expand Up @@ -466,7 +466,7 @@
var idCounter = 0;
var uniqId = function() {
return ++idCounter + '';
}
};

/**
* Basic drag implementation for DOM elements inside a container.
Expand Down Expand Up @@ -498,15 +498,17 @@
* @constructor
*/
function Draggable(el, options) {
this.options = $.extend({}, defaults, options);
this.$document = $(document);
this.$container = $(el);
this.$dragitems = $(this.options.items, this.$container);
this.is_dragging = false;
this.player_min_left = 0 + this.options.offset_left;
this.id = uniqId();
this.ns = '.gridster-draggable-' + this.id;
this.init();
this.options = $.extend({}, defaults, options);
this.$document = $(document);
this.$container = $(el);
this.$scroll_container = this.options.scroll_container == window ?
$(window) : this.$container.closest(this.options.scroll_container);
this.$dragitems = $(this.options.items, this.$container);
this.is_dragging = false;
this.player_min_left = 0 + this.options.offset_left;
this.id = uniqId();
this.ns = '.gridster-draggable-' + this.id;
this.init();
}

Draggable.defaults = defaults;
Expand Down Expand Up @@ -578,9 +580,13 @@
var diff_y = Math.round(mouse_actual_pos.top - this.mouse_init_pos.top);

var left = Math.round(this.el_init_offset.left +
diff_x - this.baseX + $(window).scrollLeft() - this.win_offset_x);
diff_x - this.baseX +
this.$scroll_container.scrollLeft() -
this.scroll_container_offset_x);
var top = Math.round(this.el_init_offset.top +
diff_y - this.baseY + $(window).scrollTop() - this.win_offset_y);
diff_y - this.baseY +
this.$scroll_container.scrollTop() -
this.scroll_container_offset_y);

if (this.options.limit) {
if (left > this.player_max_left) {
Expand All @@ -598,8 +604,10 @@
pointer: {
left: mouse_actual_pos.left,
top: mouse_actual_pos.top,
diff_left: diff_x + ($(window).scrollLeft() - this.win_offset_x),
diff_top: diff_y + ($(window).scrollTop() - this.win_offset_y)
diff_left: diff_x + (this.$scroll_container.scrollLeft() -
this.scroll_container_offset_x),
diff_top: diff_y + (this.$scroll_container.scrollTop() -
this.scroll_container_offset_y)
}
};
};
Expand Down Expand Up @@ -630,36 +638,44 @@

var area_size = 50;
var scroll_inc = 30;
var scrollDir = 'scroll' + capitalize(dir_prop);

var is_x = axis === 'x';
var window_size = is_x ? this.window_width : this.window_height;
var doc_size = is_x ? $(document).width() : $(document).height();
var scroller_size = is_x ? this.scroller_width : this.scroller_height;
var doc_size;
if (this.$scroll_container == window){
doc_size = is_x ? this.$scroll_container.width() :
this.$scroll_container.height();
}else{
doc_size = is_x ? this.$scroll_container[0].scrollWidth :
this.$scroll_container[0].scrollHeight;
}
var player_size = is_x ? this.$player.width() : this.$player.height();

var next_scroll;
var scroll_offset = $window['scroll' + capitalize(dir_prop)]();
var min_window_pos = scroll_offset;
var max_window_pos = min_window_pos + window_size;
var scroll_offset = this.$scroll_container[scrollDir]();
var min_scroll_pos = scroll_offset;
var max_scroll_pos = min_scroll_pos + scroller_size;

var mouse_next_zone = max_window_pos - area_size; // down/right
var mouse_prev_zone = min_window_pos + area_size; // up/left
var mouse_next_zone = max_scroll_pos - area_size; // down/right
var mouse_prev_zone = min_scroll_pos + area_size; // up/left

var abs_mouse_pos = min_window_pos + data.pointer[dir_prop];
var abs_mouse_pos = min_scroll_pos + data.pointer[dir_prop];

var max_player_pos = (doc_size - window_size + player_size);
var max_player_pos = (doc_size - scroller_size + player_size);

if (abs_mouse_pos >= mouse_next_zone) {
next_scroll = scroll_offset + scroll_inc;
if (next_scroll < max_player_pos) {
$window['scroll' + capitalize(dir_prop)](next_scroll);
this.$scroll_container[scrollDir](next_scroll);
this['scroll_offset_' + axis] += scroll_inc;
}
}

if (abs_mouse_pos <= mouse_prev_zone) {
next_scroll = scroll_offset - scroll_inc;
if (next_scroll > 0) {
$window['scroll' + capitalize(dir_prop)](next_scroll);
this.$scroll_container[scrollDir](next_scroll);
this['scroll_offset_' + axis] -= scroll_inc;
}
}
Expand All @@ -675,8 +691,8 @@


fn.calculate_dimensions = function(e) {
this.window_height = $window.height();
this.window_width = $window.width();
this.scroller_height = this.$scroll_container.height();
this.scroller_width = this.$scroll_container.width();
};


Expand Down Expand Up @@ -747,8 +763,8 @@
this.helper = false;
}

this.win_offset_y = $(window).scrollTop();
this.win_offset_x = $(window).scrollLeft();
this.scroll_container_offset_y = this.$scroll_container.scrollTop();
this.scroll_container_offset_x = this.$scroll_container.scrollLeft();
this.scroll_offset_y = 0;
this.scroll_offset_x = 0;
this.el_init_offset = this.$player.offset();
Expand Down Expand Up @@ -879,6 +895,7 @@
autogenerate_stylesheet: true,
avoid_overlapped_widgets: true,
auto_init: true,
scroll_container: window,
serialize_params: function($w, wgd) {
return {
col: wgd.col,
Expand Down Expand Up @@ -979,7 +996,12 @@
*/
function Gridster(el, options) {
this.options = $.extend(true, {}, defaults, options);
this.options.draggable = this.options.draggable || {};
this.options.draggable = $.extend(true, {}, this.options.draggable,
{scroll_container: this.options.scroll_container});
this.$el = $(el);
this.$scroll_container = this.options.scroll_container == window ?
$(window) : this.$el.closest(this.options.scroll_container);
this.$wrapper = this.$el.parent();
this.$widgets = this.$el.children(
this.options.widget_selector).addClass('gs-w');
Expand Down Expand Up @@ -1837,6 +1859,7 @@
move_element: false,
resize: true,
limit: this.options.autogrow_cols ? false : true,
scroll_container: this.options.scroll_container,
start: $.proxy(this.on_start_resize, this),
stop: $.proxy(function(event, ui) {
delay($.proxy(function() {
Expand Down Expand Up @@ -3964,6 +3987,11 @@
if (this.drag_api) {
this.drag_api.destroy();
}
if (this.resize_api) {
this.resize_api.destroy();
}

this.$widgets.each(function(i, el) { $(el).coords().destroy(); });

this.remove_style_tags();

Expand Down
2 changes: 1 addition & 1 deletion dist/jquery.gridster.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/jquery.gridster.min.js

Large diffs are not rendered by default.

88 changes: 58 additions & 30 deletions dist/jquery.gridster.with-extras.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! gridster.js - v0.5.6 - 2014-09-25
/*! gridster.js - v0.5.6 - 2014-12-02
* http://gridster.net/
* Copyright (c) 2014 ducksboard; Licensed MIT */

Expand Down Expand Up @@ -466,7 +466,7 @@
var idCounter = 0;
var uniqId = function() {
return ++idCounter + '';
}
};

/**
* Basic drag implementation for DOM elements inside a container.
Expand Down Expand Up @@ -498,15 +498,17 @@
* @constructor
*/
function Draggable(el, options) {
this.options = $.extend({}, defaults, options);
this.$document = $(document);
this.$container = $(el);
this.$dragitems = $(this.options.items, this.$container);
this.is_dragging = false;
this.player_min_left = 0 + this.options.offset_left;
this.id = uniqId();
this.ns = '.gridster-draggable-' + this.id;
this.init();
this.options = $.extend({}, defaults, options);
this.$document = $(document);
this.$container = $(el);
this.$scroll_container = this.options.scroll_container == window ?
$(window) : this.$container.closest(this.options.scroll_container);
this.$dragitems = $(this.options.items, this.$container);
this.is_dragging = false;
this.player_min_left = 0 + this.options.offset_left;
this.id = uniqId();
this.ns = '.gridster-draggable-' + this.id;
this.init();
}

Draggable.defaults = defaults;
Expand Down Expand Up @@ -578,9 +580,13 @@
var diff_y = Math.round(mouse_actual_pos.top - this.mouse_init_pos.top);

var left = Math.round(this.el_init_offset.left +
diff_x - this.baseX + $(window).scrollLeft() - this.win_offset_x);
diff_x - this.baseX +
this.$scroll_container.scrollLeft() -
this.scroll_container_offset_x);
var top = Math.round(this.el_init_offset.top +
diff_y - this.baseY + $(window).scrollTop() - this.win_offset_y);
diff_y - this.baseY +
this.$scroll_container.scrollTop() -
this.scroll_container_offset_y);

if (this.options.limit) {
if (left > this.player_max_left) {
Expand All @@ -598,8 +604,10 @@
pointer: {
left: mouse_actual_pos.left,
top: mouse_actual_pos.top,
diff_left: diff_x + ($(window).scrollLeft() - this.win_offset_x),
diff_top: diff_y + ($(window).scrollTop() - this.win_offset_y)
diff_left: diff_x + (this.$scroll_container.scrollLeft() -
this.scroll_container_offset_x),
diff_top: diff_y + (this.$scroll_container.scrollTop() -
this.scroll_container_offset_y)
}
};
};
Expand Down Expand Up @@ -630,36 +638,44 @@

var area_size = 50;
var scroll_inc = 30;
var scrollDir = 'scroll' + capitalize(dir_prop);

var is_x = axis === 'x';
var window_size = is_x ? this.window_width : this.window_height;
var doc_size = is_x ? $(document).width() : $(document).height();
var scroller_size = is_x ? this.scroller_width : this.scroller_height;
var doc_size;
if (this.$scroll_container == window){
doc_size = is_x ? this.$scroll_container.width() :
this.$scroll_container.height();
}else{
doc_size = is_x ? this.$scroll_container[0].scrollWidth :
this.$scroll_container[0].scrollHeight;
}
var player_size = is_x ? this.$player.width() : this.$player.height();

var next_scroll;
var scroll_offset = $window['scroll' + capitalize(dir_prop)]();
var min_window_pos = scroll_offset;
var max_window_pos = min_window_pos + window_size;
var scroll_offset = this.$scroll_container[scrollDir]();
var min_scroll_pos = scroll_offset;
var max_scroll_pos = min_scroll_pos + scroller_size;

var mouse_next_zone = max_window_pos - area_size; // down/right
var mouse_prev_zone = min_window_pos + area_size; // up/left
var mouse_next_zone = max_scroll_pos - area_size; // down/right
var mouse_prev_zone = min_scroll_pos + area_size; // up/left

var abs_mouse_pos = min_window_pos + data.pointer[dir_prop];
var abs_mouse_pos = min_scroll_pos + data.pointer[dir_prop];

var max_player_pos = (doc_size - window_size + player_size);
var max_player_pos = (doc_size - scroller_size + player_size);

if (abs_mouse_pos >= mouse_next_zone) {
next_scroll = scroll_offset + scroll_inc;
if (next_scroll < max_player_pos) {
$window['scroll' + capitalize(dir_prop)](next_scroll);
this.$scroll_container[scrollDir](next_scroll);
this['scroll_offset_' + axis] += scroll_inc;
}
}

if (abs_mouse_pos <= mouse_prev_zone) {
next_scroll = scroll_offset - scroll_inc;
if (next_scroll > 0) {
$window['scroll' + capitalize(dir_prop)](next_scroll);
this.$scroll_container[scrollDir](next_scroll);
this['scroll_offset_' + axis] -= scroll_inc;
}
}
Expand All @@ -675,8 +691,8 @@


fn.calculate_dimensions = function(e) {
this.window_height = $window.height();
this.window_width = $window.width();
this.scroller_height = this.$scroll_container.height();
this.scroller_width = this.$scroll_container.width();
};


Expand Down Expand Up @@ -747,8 +763,8 @@
this.helper = false;
}

this.win_offset_y = $(window).scrollTop();
this.win_offset_x = $(window).scrollLeft();
this.scroll_container_offset_y = this.$scroll_container.scrollTop();
this.scroll_container_offset_x = this.$scroll_container.scrollLeft();
this.scroll_offset_y = 0;
this.scroll_offset_x = 0;
this.el_init_offset = this.$player.offset();
Expand Down Expand Up @@ -879,6 +895,7 @@
autogenerate_stylesheet: true,
avoid_overlapped_widgets: true,
auto_init: true,
scroll_container: window,
serialize_params: function($w, wgd) {
return {
col: wgd.col,
Expand Down Expand Up @@ -979,7 +996,12 @@
*/
function Gridster(el, options) {
this.options = $.extend(true, {}, defaults, options);
this.options.draggable = this.options.draggable || {};
this.options.draggable = $.extend(true, {}, this.options.draggable,
{scroll_container: this.options.scroll_container});
this.$el = $(el);
this.$scroll_container = this.options.scroll_container == window ?
$(window) : this.$el.closest(this.options.scroll_container);
this.$wrapper = this.$el.parent();
this.$widgets = this.$el.children(
this.options.widget_selector).addClass('gs-w');
Expand Down Expand Up @@ -1837,6 +1859,7 @@
move_element: false,
resize: true,
limit: this.options.autogrow_cols ? false : true,
scroll_container: this.options.scroll_container,
start: $.proxy(this.on_start_resize, this),
stop: $.proxy(function(event, ui) {
delay($.proxy(function() {
Expand Down Expand Up @@ -3964,6 +3987,11 @@
if (this.drag_api) {
this.drag_api.destroy();
}
if (this.resize_api) {
this.resize_api.destroy();
}

this.$widgets.each(function(i, el) { $(el).coords().destroy(); });

this.remove_style_tags();

Expand Down
4 changes: 2 additions & 2 deletions dist/jquery.gridster.with-extras.min.js

Large diffs are not rendered by default.

Loading