fix(gridster): fixing resize limits when in fixed width mode

feature(gridster): added fix_to_content function to fit widget dimensions to a given width and height
feature(gridster): added resize_widget_dimensions function
refactor(gridster): moved code to set number of columns in grid to helper function
This commit is contained in:
floyd_hawkes
2015-03-06 14:36:05 -05:00
parent 14e08d9fe0
commit 6bb47dc1ce
7 changed files with 336 additions and 81 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
/*! gridster.js - v0.6.2 - 2015-02-23
/*! gridster.js - v0.6.2 - 2015-03-06
* http://gridster.net/
* Copyright (c) 2015 decksterteam; Licensed */
+111 -26
View File
@@ -1,4 +1,4 @@
/*! gridster.js - v0.6.2 - 2015-02-23
/*! gridster.js - v0.6.2 - 2015-03-06
* http://gridster.net/
* Copyright (c) 2015 decksterteam; Licensed */
@@ -1174,7 +1174,7 @@
* @return {HTMLElement} Returns the jQuery wrapped HTMLElement representing.
* the widget that was just created.
*/
fn.add_widget = function(html, size_x, size_y, col, row, max_size, min_size) {
fn.add_widget = function(html, size_x, size_y, col, row, max_size, min_size, callback) {
var pos;
size_x || (size_x = 1);
size_y || (size_y = 1);
@@ -1218,7 +1218,7 @@
this.drag_api.set_limits((this.cols * this.min_widget_width) + ((this.cols + 1) * this.options.widget_margins[0]));
return $w.fadeIn();
return $w.fadeIn({complete: function () { if(callback) callback.call(this); }});
};
@@ -1348,7 +1348,7 @@
* representing the widget.
* @param {Number} size_x The number of cols that will occupy the widget.
* @param {Number} size_y The number of rows that will occupy the widget.
* @param {Function} [callback] Function executed when the widget is removed.
* @param {Function} [callback] Function executed when the widget is expanded.
* @return {HTMLElement} Returns $widget.
*/
fn.expand_widget = function($widget, size_x, size_y, callback) {
@@ -1392,7 +1392,7 @@
* @method expand_widget
* @param {HTMLElement} $widget The jQuery wrapped HTMLElement
* representing the widget.
* @param {Function} [callback] Function executed when the widget is removed.
* @param {Function} [callback] Function executed when the widget is collapsed.
* @return {HTMLElement} Returns $widget.
*/
fn.collapse_widget = function($widget, callback) {
@@ -1421,6 +1421,44 @@
return $widget;
};
/**
* Fit the size of a widget to its content (best guess)
*
* @method fit_to_content
* @param $widget {HTMLElement} $widget The jQuery wrapped HTMLElement
* @param max_cols {Number} max number of columns a widget can take up
* @param max_rows {Number} max number of rows a widget can take up
* @param {Function} [callback] Function executed when the widget is fit to content.
* @return {HTMLElement} Returns $widget.
*/
fn.fit_to_content = function($widget, max_cols, max_rows, callback) {
var wgd = $widget.coords().grid;
var width = this.$wrapper.width();
var height = this.$wrapper.height();
var col_size = this.options.widget_base_dimensions[0] + (2 * this.options.widget_margins[0]);
var row_size = this.options.widget_base_dimensions[1] + (2 * this.options.widget_margins[1]);
var best_cols = Math.ceil((width + (2 * this.options.widget_margins[0])) / col_size);
var best_rows = Math.ceil((height + (2 * this.options.widget_margins[1])) / row_size);
var new_grid_data = {
col: wgd.col,
row: wgd.row,
size_x: Math.min(max_cols, best_cols),
size_y: Math.min(max_rows, best_rows)
};
this.mutate_widget_in_gridmap($widget, wgd, new_grid_data);
this.set_dom_grid_height();
this.set_dom_grid_width();
if (callback) {
callback.call(this, new_grid_data.size_x, new_grid_data.size_y);
}
return $widget;
};
/**
* Mutate widget dimensions and position in the grid map.
*
@@ -2308,9 +2346,10 @@
var size_x = Math.max(1, this.resize_initial_sizex + inc_units_x);
var size_y = Math.max(1, this.resize_initial_sizey + inc_units_y);
var max_cols = (this.container_width / this.min_widget_width) -
this.resize_initial_col + 1;
var limit_width = (max_cols * this.min_widget_width) + (this.is_responsive() ? (max_cols - 1) * margin_x : -(margin_x * 2));
// Max number of cols this widget can be in width
var max_cols = Math.floor((this.container_width / this.min_widget_width) - this.resize_initial_col + 1);
var limit_width = (max_cols * this.min_widget_width) + ((max_cols - 1) * margin_x);
size_x = Math.max(Math.min(size_x, max_size_x), min_size_x);
size_x = Math.min(max_cols, size_x);
@@ -4073,6 +4112,36 @@
};
/**
* Resize dimensions of widgets in grid based on given options
*
* @method resize_widget_dimensions
* @param options
* @returns {Gridster}
*/
fn.resize_widget_dimensions = function(options) {
if (options.widget_margins) {
this.options.widget_margins = options.widget_margins;
}
if (options.widget_base_dimensions) {
this.options.widget_base_dimensions = options.widget_base_dimensions;
}
this.$widgets.each($.proxy(function(i, widget) {
var $widget = $(widget);
this.resize_widget($widget);
}, this));
this.generate_grid_and_stylesheet();
this.get_widgets_from_DOM();
this.set_dom_grid_height();
this.set_dom_grid_width();
return this;
};
/**
* Get all widgets in the DOM and register them.
*
@@ -4099,6 +4168,39 @@
};
/**
* Helper function used to set the current number of columns in the grid
*
* @param wrapper
*/
fn.set_num_columns = function (wrapper) {
var max_cols = this.options.max_cols;
var cols = Math.floor(wrapper / (this.min_widget_width + this.options.widget_margins[0])) +
this.options.extra_cols;
var actual_cols = this.$widgets.map(function() {
return $(this).attr('data-col');
}).get();
//needed to pass tests with phantomjs
actual_cols.length || (actual_cols = [0]);
var min_cols = Math.max.apply(Math, actual_cols);
this.cols = Math.max(min_cols, cols, this.options.min_cols);
if (max_cols !== Infinity && max_cols >= min_cols && max_cols < this.cols) {
this.cols = max_cols;
}
if (this.drag_api) {
this.drag_api.set_limits((this.cols * this.min_widget_width) + ((this.cols + 1) * this.options.widget_margins[0]));
}
};
/**
* Calculate columns and rows to be set based on the configuration
* parameters, grid dimensions, etc ...
@@ -4108,25 +4210,8 @@
*/
fn.generate_grid_and_stylesheet = function() {
var aw = this.$wrapper.width();
var max_cols = this.options.max_cols;
var cols = Math.floor(aw / this.min_widget_width) +
this.options.extra_cols;
var actual_cols = this.$widgets.map(function() {
return $(this).attr('data-col');
}).get();
//needed to pass tests with phantomjs
actual_cols.length || (actual_cols = [0]);
var min_cols = Math.max.apply(Math, actual_cols);
this.cols = Math.max(min_cols, cols, this.options.min_cols);
if (max_cols !== Infinity && max_cols >= min_cols && max_cols < this.cols) {
this.cols = max_cols;
}
this.set_num_columns(aw);
// get all rows that could be occupied by the current widgets
var max_rows = this.options.extra_rows;
+1 -1
View File
@@ -1,2 +1,2 @@
/*! gridster.js - v0.6.2 - 2015-02-23 - * http://gridster.net/ - Copyright (c) 2015 decksterteam; Licensed */
/*! gridster.js - v0.6.2 - 2015-03-06 - * http://gridster.net/ - Copyright (c) 2015 decksterteam; Licensed */
.gridster{position:relative}.gridster>*{margin:0 auto;-webkit-transition:height .4s,width .4s;-moz-transition:height .4s,width .4s;-o-transition:height .4s,width .4s;-ms-transition:height .4s,width .4s;transition:height .4s,width .4s}.gridster .gs-w{z-index:2;position:absolute}.gridster.collapsed{height:auto!important}.gridster.collapsed .gs-w{position:static!important}.ready .gs-w:not(.preview-holder){-webkit-transition:opacity .3s,left .3s,top .3s;-moz-transition:opacity .3s,left .3s,top .3s;-o-transition:opacity .3s,left .3s,top .3s;transition:opacity .3s,left .3s,top .3s}.ready .gs-w:not(.preview-holder),.ready .resize-preview-holder{-webkit-transition:opacity .3s,left .3s,top .3s,width .3s,height .3s;-moz-transition:opacity .3s,left .3s,top .3s,width .3s,height .3s;-o-transition:opacity .3s,left .3s,top .3s,width .3s,height .3s;transition:opacity .3s,left .3s,top .3s,width .3s,height .3s}.gridster .preview-holder{z-index:1;position:absolute;background-color:#fff;border-color:#fff;opacity:.3}.gridster .player-revert{z-index:10!important;-webkit-transition:left .3s,top .3s!important;-moz-transition:left .3s,top .3s!important;-o-transition:left .3s,top .3s!important;transition:left .3s,top .3s!important}.gridster .dragging,.gridster .resizing{z-index:10!important;-webkit-transition:all 0s!important;-moz-transition:all 0s!important;-o-transition:all 0s!important;transition:all 0s!important}.gs-resize-handle{position:absolute;z-index:1}.gs-resize-handle-both{width:20px;height:20px;bottom:-8px;right:-8px;background-image:url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/Pg08IS0tIEdlbmVyYXRvcjogQWRvYmUgRmlyZXdvcmtzIENTNiwgRXhwb3J0IFNWRyBFeHRlbnNpb24gYnkgQWFyb24gQmVhbGwgKGh0dHA6Ly9maXJld29ya3MuYWJlYWxsLmNvbSkgLiBWZXJzaW9uOiAwLjYuMSAgLS0+DTwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+DTxzdmcgaWQ9IlVudGl0bGVkLVBhZ2UlMjAxIiB2aWV3Qm94PSIwIDAgNiA2IiBzdHlsZT0iYmFja2dyb3VuZC1jb2xvcjojZmZmZmZmMDAiIHZlcnNpb249IjEuMSINCXhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHhtbDpzcGFjZT0icHJlc2VydmUiDQl4PSIwcHgiIHk9IjBweCIgd2lkdGg9IjZweCIgaGVpZ2h0PSI2cHgiDT4NCTxnIG9wYWNpdHk9IjAuMzAyIj4NCQk8cGF0aCBkPSJNIDYgNiBMIDAgNiBMIDAgNC4yIEwgNCA0LjIgTCA0LjIgNC4yIEwgNC4yIDAgTCA2IDAgTCA2IDYgTCA2IDYgWiIgZmlsbD0iIzAwMDAwMCIvPg0JPC9nPg08L3N2Zz4=);background-position:top left;background-repeat:no-repeat;cursor:se-resize;z-index:20}.gs-resize-handle-x{top:0;bottom:13px;right:-5px;width:10px;cursor:e-resize}.gs-resize-handle-y{left:0;right:13px;bottom:-5px;height:10px;cursor:s-resize}.gs-w:hover .gs-resize-handle,.resizing .gs-resize-handle{opacity:1}.gs-resize-handle,.gs-w.dragging .gs-resize-handle{opacity:0}.gs-resize-disabled .gs-resize-handle{display:none!important}[data-max-sizex="1"] .gs-resize-handle-x,[data-max-sizey="1"] .gs-resize-handle-y,[data-max-sizey="1"][data-max-sizex="1"] .gs-resize-handle{display:none!important}
+2 -2
View File
File diff suppressed because one or more lines are too long
+111 -26
View File
@@ -1,4 +1,4 @@
/*! gridster.js - v0.6.2 - 2015-02-23
/*! gridster.js - v0.6.2 - 2015-03-06
* http://gridster.net/
* Copyright (c) 2015 decksterteam; Licensed */
@@ -1174,7 +1174,7 @@
* @return {HTMLElement} Returns the jQuery wrapped HTMLElement representing.
* the widget that was just created.
*/
fn.add_widget = function(html, size_x, size_y, col, row, max_size, min_size) {
fn.add_widget = function(html, size_x, size_y, col, row, max_size, min_size, callback) {
var pos;
size_x || (size_x = 1);
size_y || (size_y = 1);
@@ -1218,7 +1218,7 @@
this.drag_api.set_limits((this.cols * this.min_widget_width) + ((this.cols + 1) * this.options.widget_margins[0]));
return $w.fadeIn();
return $w.fadeIn({complete: function () { if(callback) callback.call(this); }});
};
@@ -1348,7 +1348,7 @@
* representing the widget.
* @param {Number} size_x The number of cols that will occupy the widget.
* @param {Number} size_y The number of rows that will occupy the widget.
* @param {Function} [callback] Function executed when the widget is removed.
* @param {Function} [callback] Function executed when the widget is expanded.
* @return {HTMLElement} Returns $widget.
*/
fn.expand_widget = function($widget, size_x, size_y, callback) {
@@ -1392,7 +1392,7 @@
* @method expand_widget
* @param {HTMLElement} $widget The jQuery wrapped HTMLElement
* representing the widget.
* @param {Function} [callback] Function executed when the widget is removed.
* @param {Function} [callback] Function executed when the widget is collapsed.
* @return {HTMLElement} Returns $widget.
*/
fn.collapse_widget = function($widget, callback) {
@@ -1421,6 +1421,44 @@
return $widget;
};
/**
* Fit the size of a widget to its content (best guess)
*
* @method fit_to_content
* @param $widget {HTMLElement} $widget The jQuery wrapped HTMLElement
* @param max_cols {Number} max number of columns a widget can take up
* @param max_rows {Number} max number of rows a widget can take up
* @param {Function} [callback] Function executed when the widget is fit to content.
* @return {HTMLElement} Returns $widget.
*/
fn.fit_to_content = function($widget, max_cols, max_rows, callback) {
var wgd = $widget.coords().grid;
var width = this.$wrapper.width();
var height = this.$wrapper.height();
var col_size = this.options.widget_base_dimensions[0] + (2 * this.options.widget_margins[0]);
var row_size = this.options.widget_base_dimensions[1] + (2 * this.options.widget_margins[1]);
var best_cols = Math.ceil((width + (2 * this.options.widget_margins[0])) / col_size);
var best_rows = Math.ceil((height + (2 * this.options.widget_margins[1])) / row_size);
var new_grid_data = {
col: wgd.col,
row: wgd.row,
size_x: Math.min(max_cols, best_cols),
size_y: Math.min(max_rows, best_rows)
};
this.mutate_widget_in_gridmap($widget, wgd, new_grid_data);
this.set_dom_grid_height();
this.set_dom_grid_width();
if (callback) {
callback.call(this, new_grid_data.size_x, new_grid_data.size_y);
}
return $widget;
};
/**
* Mutate widget dimensions and position in the grid map.
*
@@ -2308,9 +2346,10 @@
var size_x = Math.max(1, this.resize_initial_sizex + inc_units_x);
var size_y = Math.max(1, this.resize_initial_sizey + inc_units_y);
var max_cols = (this.container_width / this.min_widget_width) -
this.resize_initial_col + 1;
var limit_width = (max_cols * this.min_widget_width) + (this.is_responsive() ? (max_cols - 1) * margin_x : -(margin_x * 2));
// Max number of cols this widget can be in width
var max_cols = Math.floor((this.container_width / this.min_widget_width) - this.resize_initial_col + 1);
var limit_width = (max_cols * this.min_widget_width) + ((max_cols - 1) * margin_x);
size_x = Math.max(Math.min(size_x, max_size_x), min_size_x);
size_x = Math.min(max_cols, size_x);
@@ -4073,6 +4112,36 @@
};
/**
* Resize dimensions of widgets in grid based on given options
*
* @method resize_widget_dimensions
* @param options
* @returns {Gridster}
*/
fn.resize_widget_dimensions = function(options) {
if (options.widget_margins) {
this.options.widget_margins = options.widget_margins;
}
if (options.widget_base_dimensions) {
this.options.widget_base_dimensions = options.widget_base_dimensions;
}
this.$widgets.each($.proxy(function(i, widget) {
var $widget = $(widget);
this.resize_widget($widget);
}, this));
this.generate_grid_and_stylesheet();
this.get_widgets_from_DOM();
this.set_dom_grid_height();
this.set_dom_grid_width();
return this;
};
/**
* Get all widgets in the DOM and register them.
*
@@ -4099,6 +4168,39 @@
};
/**
* Helper function used to set the current number of columns in the grid
*
* @param wrapper
*/
fn.set_num_columns = function (wrapper) {
var max_cols = this.options.max_cols;
var cols = Math.floor(wrapper / (this.min_widget_width + this.options.widget_margins[0])) +
this.options.extra_cols;
var actual_cols = this.$widgets.map(function() {
return $(this).attr('data-col');
}).get();
//needed to pass tests with phantomjs
actual_cols.length || (actual_cols = [0]);
var min_cols = Math.max.apply(Math, actual_cols);
this.cols = Math.max(min_cols, cols, this.options.min_cols);
if (max_cols !== Infinity && max_cols >= min_cols && max_cols < this.cols) {
this.cols = max_cols;
}
if (this.drag_api) {
this.drag_api.set_limits((this.cols * this.min_widget_width) + ((this.cols + 1) * this.options.widget_margins[0]));
}
};
/**
* Calculate columns and rows to be set based on the configuration
* parameters, grid dimensions, etc ...
@@ -4108,25 +4210,8 @@
*/
fn.generate_grid_and_stylesheet = function() {
var aw = this.$wrapper.width();
var max_cols = this.options.max_cols;
var cols = Math.floor(aw / this.min_widget_width) +
this.options.extra_cols;
var actual_cols = this.$widgets.map(function() {
return $(this).attr('data-col');
}).get();
//needed to pass tests with phantomjs
actual_cols.length || (actual_cols = [0]);
var min_cols = Math.max.apply(Math, actual_cols);
this.cols = Math.max(min_cols, cols, this.options.min_cols);
if (max_cols !== Infinity && max_cols >= min_cols && max_cols < this.cols) {
this.cols = max_cols;
}
this.set_num_columns(aw);
// get all rows that could be occupied by the current widgets
var max_rows = this.options.extra_rows;
File diff suppressed because one or more lines are too long
+108 -23
View File
@@ -501,7 +501,7 @@
* representing the widget.
* @param {Number} size_x The number of cols that will occupy the widget.
* @param {Number} size_y The number of rows that will occupy the widget.
* @param {Function} [callback] Function executed when the widget is removed.
* @param {Function} [callback] Function executed when the widget is expanded.
* @return {HTMLElement} Returns $widget.
*/
fn.expand_widget = function($widget, size_x, size_y, callback) {
@@ -545,7 +545,7 @@
* @method expand_widget
* @param {HTMLElement} $widget The jQuery wrapped HTMLElement
* representing the widget.
* @param {Function} [callback] Function executed when the widget is removed.
* @param {Function} [callback] Function executed when the widget is collapsed.
* @return {HTMLElement} Returns $widget.
*/
fn.collapse_widget = function($widget, callback) {
@@ -574,6 +574,44 @@
return $widget;
};
/**
* Fit the size of a widget to its content (best guess)
*
* @method fit_to_content
* @param $widget {HTMLElement} $widget The jQuery wrapped HTMLElement
* @param max_cols {Number} max number of columns a widget can take up
* @param max_rows {Number} max number of rows a widget can take up
* @param {Function} [callback] Function executed when the widget is fit to content.
* @return {HTMLElement} Returns $widget.
*/
fn.fit_to_content = function($widget, max_cols, max_rows, callback) {
var wgd = $widget.coords().grid;
var width = this.$wrapper.width();
var height = this.$wrapper.height();
var col_size = this.options.widget_base_dimensions[0] + (2 * this.options.widget_margins[0]);
var row_size = this.options.widget_base_dimensions[1] + (2 * this.options.widget_margins[1]);
var best_cols = Math.ceil((width + (2 * this.options.widget_margins[0])) / col_size);
var best_rows = Math.ceil((height + (2 * this.options.widget_margins[1])) / row_size);
var new_grid_data = {
col: wgd.col,
row: wgd.row,
size_x: Math.min(max_cols, best_cols),
size_y: Math.min(max_rows, best_rows)
};
this.mutate_widget_in_gridmap($widget, wgd, new_grid_data);
this.set_dom_grid_height();
this.set_dom_grid_width();
if (callback) {
callback.call(this, new_grid_data.size_x, new_grid_data.size_y);
}
return $widget;
};
/**
* Mutate widget dimensions and position in the grid map.
*
@@ -1461,9 +1499,10 @@
var size_x = Math.max(1, this.resize_initial_sizex + inc_units_x);
var size_y = Math.max(1, this.resize_initial_sizey + inc_units_y);
var max_cols = (this.container_width / this.min_widget_width) -
this.resize_initial_col + 1;
var limit_width = (max_cols * this.min_widget_width) + (this.is_responsive() ? (max_cols - 1) * margin_x : -(margin_x * 2));
// Max number of cols this widget can be in width
var max_cols = Math.floor((this.container_width / this.min_widget_width) - this.resize_initial_col + 1);
var limit_width = (max_cols * this.min_widget_width) + ((max_cols - 1) * margin_x);
size_x = Math.max(Math.min(size_x, max_size_x), min_size_x);
size_x = Math.min(max_cols, size_x);
@@ -3226,6 +3265,36 @@
};
/**
* Resize dimensions of widgets in grid based on given options
*
* @method resize_widget_dimensions
* @param options
* @returns {Gridster}
*/
fn.resize_widget_dimensions = function(options) {
if (options.widget_margins) {
this.options.widget_margins = options.widget_margins;
}
if (options.widget_base_dimensions) {
this.options.widget_base_dimensions = options.widget_base_dimensions;
}
this.$widgets.each($.proxy(function(i, widget) {
var $widget = $(widget);
this.resize_widget($widget);
}, this));
this.generate_grid_and_stylesheet();
this.get_widgets_from_DOM();
this.set_dom_grid_height();
this.set_dom_grid_width();
return this;
};
/**
* Get all widgets in the DOM and register them.
*
@@ -3252,6 +3321,39 @@
};
/**
* Helper function used to set the current number of columns in the grid
*
* @param wrapper
*/
fn.set_num_columns = function (wrapper) {
var max_cols = this.options.max_cols;
var cols = Math.floor(wrapper / (this.min_widget_width + this.options.widget_margins[0])) +
this.options.extra_cols;
var actual_cols = this.$widgets.map(function() {
return $(this).attr('data-col');
}).get();
//needed to pass tests with phantomjs
actual_cols.length || (actual_cols = [0]);
var min_cols = Math.max.apply(Math, actual_cols);
this.cols = Math.max(min_cols, cols, this.options.min_cols);
if (max_cols !== Infinity && max_cols >= min_cols && max_cols < this.cols) {
this.cols = max_cols;
}
if (this.drag_api) {
this.drag_api.set_limits((this.cols * this.min_widget_width) + ((this.cols + 1) * this.options.widget_margins[0]));
}
};
/**
* Calculate columns and rows to be set based on the configuration
* parameters, grid dimensions, etc ...
@@ -3261,25 +3363,8 @@
*/
fn.generate_grid_and_stylesheet = function() {
var aw = this.$wrapper.width();
var max_cols = this.options.max_cols;
var cols = Math.floor(aw / this.min_widget_width) +
this.options.extra_cols;
var actual_cols = this.$widgets.map(function() {
return $(this).attr('data-col');
}).get();
//needed to pass tests with phantomjs
actual_cols.length || (actual_cols = [0]);
var min_cols = Math.max.apply(Math, actual_cols);
this.cols = Math.max(min_cols, cols, this.options.min_cols);
if (max_cols !== Infinity && max_cols >= min_cols && max_cols < this.cols) {
this.cols = max_cols;
}
this.set_num_columns(aw);
// get all rows that could be occupied by the current widgets
var max_rows = this.options.extra_rows;