|
1 |
| -/*! gridster.js - v0.6.2 - 2015-02-23 |
| 1 | +/*! gridster.js - v0.6.2 - 2015-03-06 |
2 | 2 | * http://gridster.net/
|
3 | 3 | * Copyright (c) 2015 decksterteam; Licensed */
|
4 | 4 |
|
|
1174 | 1174 | * @return {HTMLElement} Returns the jQuery wrapped HTMLElement representing.
|
1175 | 1175 | * the widget that was just created.
|
1176 | 1176 | */
|
1177 |
| - fn.add_widget = function(html, size_x, size_y, col, row, max_size, min_size) { |
| 1177 | + fn.add_widget = function(html, size_x, size_y, col, row, max_size, min_size, callback) { |
1178 | 1178 | var pos;
|
1179 | 1179 | size_x || (size_x = 1);
|
1180 | 1180 | size_y || (size_y = 1);
|
|
1218 | 1218 |
|
1219 | 1219 | this.drag_api.set_limits((this.cols * this.min_widget_width) + ((this.cols + 1) * this.options.widget_margins[0]));
|
1220 | 1220 |
|
1221 |
| - return $w.fadeIn(); |
| 1221 | + return $w.fadeIn({complete: function () { if(callback) callback.call(this); }}); |
1222 | 1222 | };
|
1223 | 1223 |
|
1224 | 1224 |
|
|
1348 | 1348 | * representing the widget.
|
1349 | 1349 | * @param {Number} size_x The number of cols that will occupy the widget.
|
1350 | 1350 | * @param {Number} size_y The number of rows that will occupy the widget.
|
1351 |
| - * @param {Function} [callback] Function executed when the widget is removed. |
| 1351 | + * @param {Function} [callback] Function executed when the widget is expanded. |
1352 | 1352 | * @return {HTMLElement} Returns $widget.
|
1353 | 1353 | */
|
1354 | 1354 | fn.expand_widget = function($widget, size_x, size_y, callback) {
|
|
1392 | 1392 | * @method expand_widget
|
1393 | 1393 | * @param {HTMLElement} $widget The jQuery wrapped HTMLElement
|
1394 | 1394 | * representing the widget.
|
1395 |
| - * @param {Function} [callback] Function executed when the widget is removed. |
| 1395 | + * @param {Function} [callback] Function executed when the widget is collapsed. |
1396 | 1396 | * @return {HTMLElement} Returns $widget.
|
1397 | 1397 | */
|
1398 | 1398 | fn.collapse_widget = function($widget, callback) {
|
|
1421 | 1421 | return $widget;
|
1422 | 1422 | };
|
1423 | 1423 |
|
| 1424 | + /** |
| 1425 | + * Fit the size of a widget to its content (best guess) |
| 1426 | + * |
| 1427 | + * @method fit_to_content |
| 1428 | + * @param $widget {HTMLElement} $widget The jQuery wrapped HTMLElement |
| 1429 | + * @param max_cols {Number} max number of columns a widget can take up |
| 1430 | + * @param max_rows {Number} max number of rows a widget can take up |
| 1431 | + * @param {Function} [callback] Function executed when the widget is fit to content. |
| 1432 | + * @return {HTMLElement} Returns $widget. |
| 1433 | + */ |
| 1434 | + fn.fit_to_content = function($widget, max_cols, max_rows, callback) { |
| 1435 | + var wgd = $widget.coords().grid; |
| 1436 | + var width = this.$wrapper.width(); |
| 1437 | + var height = this.$wrapper.height(); |
| 1438 | + var col_size = this.options.widget_base_dimensions[0] + (2 * this.options.widget_margins[0]); |
| 1439 | + var row_size = this.options.widget_base_dimensions[1] + (2 * this.options.widget_margins[1]); |
| 1440 | + var best_cols = Math.ceil((width + (2 * this.options.widget_margins[0])) / col_size); |
| 1441 | + var best_rows = Math.ceil((height + (2 * this.options.widget_margins[1])) / row_size); |
| 1442 | + |
| 1443 | + var new_grid_data = { |
| 1444 | + col: wgd.col, |
| 1445 | + row: wgd.row, |
| 1446 | + size_x: Math.min(max_cols, best_cols), |
| 1447 | + size_y: Math.min(max_rows, best_rows) |
| 1448 | + }; |
| 1449 | + |
| 1450 | + this.mutate_widget_in_gridmap($widget, wgd, new_grid_data); |
| 1451 | + |
| 1452 | + this.set_dom_grid_height(); |
| 1453 | + this.set_dom_grid_width(); |
| 1454 | + |
| 1455 | + if (callback) { |
| 1456 | + callback.call(this, new_grid_data.size_x, new_grid_data.size_y); |
| 1457 | + } |
| 1458 | + |
| 1459 | + return $widget; |
| 1460 | + }; |
| 1461 | + |
1424 | 1462 | /**
|
1425 | 1463 | * Mutate widget dimensions and position in the grid map.
|
1426 | 1464 | *
|
|
2308 | 2346 | var size_x = Math.max(1, this.resize_initial_sizex + inc_units_x);
|
2309 | 2347 | var size_y = Math.max(1, this.resize_initial_sizey + inc_units_y);
|
2310 | 2348 |
|
2311 |
| - var max_cols = (this.container_width / this.min_widget_width) - |
2312 |
| - this.resize_initial_col + 1; |
2313 |
| - var limit_width = (max_cols * this.min_widget_width) + (this.is_responsive() ? (max_cols - 1) * margin_x : -(margin_x * 2)); |
| 2349 | + // Max number of cols this widget can be in width |
| 2350 | + var max_cols = Math.floor((this.container_width / this.min_widget_width) - this.resize_initial_col + 1); |
| 2351 | + |
| 2352 | + var limit_width = (max_cols * this.min_widget_width) + ((max_cols - 1) * margin_x); |
2314 | 2353 |
|
2315 | 2354 | size_x = Math.max(Math.min(size_x, max_size_x), min_size_x);
|
2316 | 2355 | size_x = Math.min(max_cols, size_x);
|
|
4073 | 4112 | };
|
4074 | 4113 |
|
4075 | 4114 |
|
| 4115 | + /** |
| 4116 | + * Resize dimensions of widgets in grid based on given options |
| 4117 | + * |
| 4118 | + * @method resize_widget_dimensions |
| 4119 | + * @param options |
| 4120 | + * @returns {Gridster} |
| 4121 | + */ |
| 4122 | + fn.resize_widget_dimensions = function(options) { |
| 4123 | + if (options.widget_margins) { |
| 4124 | + this.options.widget_margins = options.widget_margins; |
| 4125 | + } |
| 4126 | + |
| 4127 | + if (options.widget_base_dimensions) { |
| 4128 | + this.options.widget_base_dimensions = options.widget_base_dimensions; |
| 4129 | + } |
| 4130 | + |
| 4131 | + this.$widgets.each($.proxy(function(i, widget) { |
| 4132 | + var $widget = $(widget); |
| 4133 | + this.resize_widget($widget); |
| 4134 | + }, this)); |
| 4135 | + |
| 4136 | + this.generate_grid_and_stylesheet(); |
| 4137 | + this.get_widgets_from_DOM(); |
| 4138 | + this.set_dom_grid_height(); |
| 4139 | + this.set_dom_grid_width(); |
| 4140 | + |
| 4141 | + return this; |
| 4142 | + }; |
| 4143 | + |
| 4144 | + |
4076 | 4145 | /**
|
4077 | 4146 | * Get all widgets in the DOM and register them.
|
4078 | 4147 | *
|
|
4099 | 4168 | };
|
4100 | 4169 |
|
4101 | 4170 |
|
| 4171 | + /** |
| 4172 | + * Helper function used to set the current number of columns in the grid |
| 4173 | + * |
| 4174 | + * @param wrapper |
| 4175 | + */ |
| 4176 | + fn.set_num_columns = function (wrapper) { |
| 4177 | + |
| 4178 | + var max_cols = this.options.max_cols; |
| 4179 | + |
| 4180 | + var cols = Math.floor(wrapper / (this.min_widget_width + this.options.widget_margins[0])) + |
| 4181 | + this.options.extra_cols; |
| 4182 | + |
| 4183 | + var actual_cols = this.$widgets.map(function() { |
| 4184 | + return $(this).attr('data-col'); |
| 4185 | + }).get(); |
| 4186 | + |
| 4187 | + //needed to pass tests with phantomjs |
| 4188 | + actual_cols.length || (actual_cols = [0]); |
| 4189 | + |
| 4190 | + var min_cols = Math.max.apply(Math, actual_cols); |
| 4191 | + |
| 4192 | + this.cols = Math.max(min_cols, cols, this.options.min_cols); |
| 4193 | + |
| 4194 | + if (max_cols !== Infinity && max_cols >= min_cols && max_cols < this.cols) { |
| 4195 | + this.cols = max_cols; |
| 4196 | + } |
| 4197 | + |
| 4198 | + if (this.drag_api) { |
| 4199 | + this.drag_api.set_limits((this.cols * this.min_widget_width) + ((this.cols + 1) * this.options.widget_margins[0])); |
| 4200 | + } |
| 4201 | + }; |
| 4202 | + |
| 4203 | + |
4102 | 4204 | /**
|
4103 | 4205 | * Calculate columns and rows to be set based on the configuration
|
4104 | 4206 | * parameters, grid dimensions, etc ...
|
|
4108 | 4210 | */
|
4109 | 4211 | fn.generate_grid_and_stylesheet = function() {
|
4110 | 4212 | var aw = this.$wrapper.width();
|
4111 |
| - var max_cols = this.options.max_cols; |
4112 |
| - |
4113 |
| - var cols = Math.floor(aw / this.min_widget_width) + |
4114 |
| - this.options.extra_cols; |
4115 |
| - |
4116 |
| - var actual_cols = this.$widgets.map(function() { |
4117 |
| - return $(this).attr('data-col'); |
4118 |
| - }).get(); |
4119 |
| - |
4120 |
| - //needed to pass tests with phantomjs |
4121 |
| - actual_cols.length || (actual_cols = [0]); |
4122 | 4213 |
|
4123 |
| - var min_cols = Math.max.apply(Math, actual_cols); |
4124 |
| - |
4125 |
| - this.cols = Math.max(min_cols, cols, this.options.min_cols); |
4126 |
| - |
4127 |
| - if (max_cols !== Infinity && max_cols >= min_cols && max_cols < this.cols) { |
4128 |
| - this.cols = max_cols; |
4129 |
| - } |
| 4214 | + this.set_num_columns(aw); |
4130 | 4215 |
|
4131 | 4216 | // get all rows that could be occupied by the current widgets
|
4132 | 4217 | var max_rows = this.options.extra_rows;
|
|
0 commit comments