Skip to content

Instantly share code, notes, and snippets.

@marceloemanoel
Created November 17, 2014 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save marceloemanoel/5be3a550f1e7ad45fa3f to your computer and use it in GitHub Desktop.
Save marceloemanoel/5be3a550f1e7ad45fa3f to your computer and use it in GitHub Desktop.
RequireJs module to handle layout using Gridster
define(["jquery",
"jquery.gridster"], function($) {
var gridster = $(".gridster").gridster({
widget_margins: [5, 5],
widget_base_dimensions: [200, 200],
widget_selector:"section.widget",
draggable: {
handle: '.ui-sortable-handle, .ui-sortable-handle > h5',
stop: function() {
//trigger a serialize event
}
},
resize: {
enabled: true,
resize: function(e, ui, widget) {
//trigger a resize event
},
stop: function(e, ui, widget) {
//trigger a resize event
//trigger a serialize event
}
},
serialize_params: function($w, wgd) {
return {
id: $w.attr("id"),
value: {
col: wgd.col,
row: wgd.row,
sizex: wgd.size_x,
sizey: wgd.size_y
}
};
}
}).data('gridster');
function concatenate(initial, current) {
initial[current.id] = current.value;
return initial;
}
return function(storage, layout) {
this.start = function() {
if(storage.enabled() && storage.contains("grid")) {
var grid = storage.get("grid");
$.each(grid, function(key, value){
gridster.add_widget($("#"+key), value.sizex, value.sizey, value.col, value.row);
});
}
else {
layout.defaultPositions(gridster);
}
//trigger a resize-all-widgets event
};
this.store = function() {
var result = gridster.serialize()
.reduce(concatenate, {});
storage.store("grid", result);
};
this.resizeAll = function(){
layout.widgets.forEach(function(widget) {
if(widget.resize && typeof widget.resize === "function") {
widget.resize();
}
});
};
this.resize = function(target) {
layout.widgets.filter(function(widget) {
return (widget.resize && typeof widget.resize === "function") &&
(widget.element && widget.element[0] === target[0]);
})
.forEach(function(widget) {
widget.resize();
});
};
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment