|
| 1 | +(function (scope, _) { |
| 2 | + |
| 3 | + var Utils = { |
| 4 | + is_intercepted: function (a, b) { |
| 5 | + return !(a.x + a.width <= b.x || b.x + b.width <= a.x || a.y + a.height <= b.y || b.y + b.height <= a.y); |
| 6 | + } |
| 7 | + }; |
| 8 | + |
| 9 | + var id_seq = 0; |
| 10 | + |
| 11 | + var GridStackEngine = function (width, onchange) { |
| 12 | + this.width = width; |
| 13 | + |
| 14 | + this.nodes = []; |
| 15 | + this.onchange = onchange || function () {}; |
| 16 | + }; |
| 17 | + |
| 18 | + GridStackEngine.prototype._fix_collisions = function (node) { |
| 19 | + this.nodes = _.sortBy(this.nodes, function (n) { return -(n.x + n.y * this.width); }, this); |
| 20 | + |
| 21 | + while (true) { |
| 22 | + var collision_node = _.find(this.nodes, function (n) { |
| 23 | + return n != node && Utils.is_intercepted(n, node); |
| 24 | + }, this); |
| 25 | + if (typeof collision_node == 'undefined') { |
| 26 | + return; |
| 27 | + } |
| 28 | + this.move_node(collision_node, collision_node.x, node.y + node.height, |
| 29 | + collision_node.width, collision_node.height, true); |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + GridStackEngine.prototype._pack_nodes = function () { |
| 34 | + this.nodes = _.sortBy(this.nodes, function (n) { return n.x + n.y * this.width; }, this); |
| 35 | + |
| 36 | + _.each(this.nodes, function (n, i) { |
| 37 | + while (n.y > 0) { |
| 38 | + var new_y = n.y - 1; |
| 39 | + var can_be_moved = i == 0; |
| 40 | + |
| 41 | + if (i > 0) { |
| 42 | + var collision_node = _.chain(this.nodes) |
| 43 | + .first(i) |
| 44 | + .find(function (bn) { |
| 45 | + return Utils.is_intercepted({x: n.x, y: new_y, width: n.width, height: n.height}, bn); |
| 46 | + }) |
| 47 | + .value(); |
| 48 | + can_be_moved = typeof collision_node == 'undefined'; |
| 49 | + } |
| 50 | + |
| 51 | + if (!can_be_moved) { |
| 52 | + break; |
| 53 | + } |
| 54 | + n.y = new_y; |
| 55 | + } |
| 56 | + }, this); |
| 57 | + }; |
| 58 | + |
| 59 | + GridStackEngine.prototype._prepare_node = function (node, moving) { |
| 60 | + node = _.defaults(node || {}, {width: 1, height: 1, x: 0, y: 0 }); |
| 61 | + |
| 62 | + node.x = parseInt('' + node.x); |
| 63 | + node.y = parseInt('' + node.y); |
| 64 | + node.width = parseInt('' + node.width); |
| 65 | + node.height = parseInt('' + node.height); |
| 66 | + |
| 67 | + if (node.width > this.width) { |
| 68 | + node.width = this.width; |
| 69 | + } |
| 70 | + else if (node.width < 1) { |
| 71 | + node.width = 1; |
| 72 | + } |
| 73 | + |
| 74 | + if (node.height < 1) { |
| 75 | + node.height = 1; |
| 76 | + } |
| 77 | + |
| 78 | + if (node.x < 0) { |
| 79 | + node.x = 0; |
| 80 | + } |
| 81 | + |
| 82 | + if (node.x + node.width > this.width) { |
| 83 | + if (moving) { |
| 84 | + node.x = this.width - node.width; |
| 85 | + } |
| 86 | + else { |
| 87 | + node.width = this.width - node.x; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if (node.y < 0) { |
| 92 | + node.y = 0; |
| 93 | + } |
| 94 | + |
| 95 | + return node; |
| 96 | + }; |
| 97 | + |
| 98 | + GridStackEngine.prototype._notify = function () { |
| 99 | + this.onchange(this.nodes); |
| 100 | + }; |
| 101 | + |
| 102 | + GridStackEngine.prototype.add_node = function(node) { |
| 103 | + node = this._prepare_node(node); |
| 104 | + |
| 105 | + if (typeof node.max_width != 'undefined') node.width = Math.min(node.width, node.max_width); |
| 106 | + if (typeof node.max_height != 'undefined') node.height = Math.min(node.height, node.max_height); |
| 107 | + if (typeof node.min_width != 'undefined') node.width = Math.max(node.width, node.min_width); |
| 108 | + if (typeof node.min_height != 'undefined') node.height = Math.max(node.height, node.min_height); |
| 109 | + |
| 110 | + node._id = ++id_seq; |
| 111 | + this.nodes.push(node); |
| 112 | + this._fix_collisions(node); |
| 113 | + this._pack_nodes(); |
| 114 | + this._notify(); |
| 115 | + return node; |
| 116 | + }; |
| 117 | + |
| 118 | + GridStackEngine.prototype.remove_node = function (node) { |
| 119 | + node._id = null; |
| 120 | + this.nodes = _.without(this.nodes, node); |
| 121 | + this._pack_nodes(); |
| 122 | + this._notify(node); |
| 123 | + }; |
| 124 | + |
| 125 | + GridStackEngine.prototype.move_node = function (node, x, y, width, height, no_pack) { |
| 126 | + if (typeof x == 'undefined') x = node.x; |
| 127 | + if (typeof y == 'undefined') y = node.y; |
| 128 | + if (typeof width == 'undefined') width = node.width; |
| 129 | + if (typeof height == 'undefined') height = node.height; |
| 130 | + |
| 131 | + if (typeof node.max_width != 'undefined') width = Math.min(width, node.max_width); |
| 132 | + if (typeof node.max_height != 'undefined') height = Math.min(height, node.max_height); |
| 133 | + if (typeof node.min_width != 'undefined') width = Math.max(width, node.min_width); |
| 134 | + if (typeof node.min_height != 'undefined') height = Math.max(height, node.min_height); |
| 135 | + |
| 136 | + if (node.x == x && node.y == y && node.width == width && node.height == height) { |
| 137 | + return node; |
| 138 | + } |
| 139 | + |
| 140 | + var moving = node.x != x; |
| 141 | + |
| 142 | + node.x = x; |
| 143 | + node.y = y; |
| 144 | + node.width = width; |
| 145 | + node.height = height; |
| 146 | + |
| 147 | + node = this._prepare_node(node, moving); |
| 148 | + |
| 149 | + this._fix_collisions(node); |
| 150 | + if (!no_pack) { |
| 151 | + this._pack_nodes(); |
| 152 | + this._notify(); |
| 153 | + } |
| 154 | + return node; |
| 155 | + }; |
| 156 | + |
| 157 | + GridStackEngine.prototype.get_grid_height = function () { |
| 158 | + return _.reduce(this.nodes, function (memo, n) { return Math.max(memo, n.y + n.height); }, 0); |
| 159 | + }; |
| 160 | + |
| 161 | + var GridStack = function (el, opts) { |
| 162 | + var self = this; |
| 163 | + |
| 164 | + this.container = $(el); |
| 165 | + |
| 166 | + this.opts = _.defaults(opts || {}, { |
| 167 | + width: 12, |
| 168 | + item_class: 'grid-stack-item', |
| 169 | + placeholder_class: 'grid-stack-placeholder', |
| 170 | + handle: '.grid-stack-item-content', |
| 171 | + cell_height: 60, |
| 172 | + vertical_margin: 20 |
| 173 | + }); |
| 174 | + |
| 175 | + this.grid = new GridStackEngine(this.opts.width, function (nodes) { |
| 176 | + _.each(nodes, function (n) { |
| 177 | + n.el |
| 178 | + .attr('data-gs-x', n.x) |
| 179 | + .attr('data-gs-y', n.y) |
| 180 | + .attr('data-gs-width', n.width) |
| 181 | + .attr('data-gs-height', n.height); |
| 182 | + }); |
| 183 | + }); |
| 184 | + |
| 185 | + this.container.find('.' + this.opts.item_class).each(function (index, el) { |
| 186 | + self._prepare_element(el); |
| 187 | + }); |
| 188 | + |
| 189 | + this.placeholder = $('<div class="' + this.opts.placeholder_class + ' ' + this.opts.item_class + '"><div class="placeholder-content" /></div>').hide(); |
| 190 | + this.container.append(this.placeholder); |
| 191 | + this.container.height((this.grid.get_grid_height()) * (this.opts.cell_height + this.opts.vertical_margin) - this.opts.vertical_margin); |
| 192 | + }; |
| 193 | + |
| 194 | + GridStack.prototype._update_container_height = function () { |
| 195 | + this.container.height(this.grid.get_grid_height() * (this.opts.cell_height + this.opts.vertical_margin) - this.opts.vertical_margin); |
| 196 | + }; |
| 197 | + |
| 198 | + GridStack.prototype._prepare_element = function (el) { |
| 199 | + var self = this; |
| 200 | + el = $(el); |
| 201 | + |
| 202 | + var node = self.grid.add_node({ |
| 203 | + x: el.attr('data-gs-x'), |
| 204 | + y: el.attr('data-gs-y'), |
| 205 | + width: el.attr('data-gs-width'), |
| 206 | + height: el.attr('data-gs-height'), |
| 207 | + max_width: el.attr('data-gs-max-width'), |
| 208 | + min_width: el.attr('data-gs-min-width'), |
| 209 | + max_height: el.attr('data-gs-max-height'), |
| 210 | + min_height: el.attr('data-gs-min-height'), |
| 211 | + el: el |
| 212 | + }); |
| 213 | + el.data('_gridstack_node', node); |
| 214 | + |
| 215 | + var cell_width, cell_height = this.opts.cell_height + this.opts.vertical_margin / 2; |
| 216 | + |
| 217 | + var on_start_moving = function (event, ui) { |
| 218 | + var o = $(this); |
| 219 | + cell_width = Math.ceil(o.outerWidth() / o.attr('data-gs-width')); |
| 220 | + self.placeholder |
| 221 | + .attr('data-gs-x', o.attr('data-gs-x')) |
| 222 | + .attr('data-gs-y', o.attr('data-gs-y')) |
| 223 | + .attr('data-gs-width', o.attr('data-gs-width')) |
| 224 | + .attr('data-gs-height', o.attr('data-gs-height')) |
| 225 | + .show(); |
| 226 | + node.el = self.placeholder; |
| 227 | + }; |
| 228 | + |
| 229 | + var on_end_moving = function (event, ui) { |
| 230 | + var o = $(this); |
| 231 | + node.el = o; |
| 232 | + self.placeholder.hide(); |
| 233 | + o |
| 234 | + .attr('data-gs-x', node.x) |
| 235 | + .attr('data-gs-y', node.y) |
| 236 | + .attr('data-gs-width', node.width) |
| 237 | + .attr('data-gs-height', node.height) |
| 238 | + .removeAttr('style'); |
| 239 | + self._update_container_height(); |
| 240 | + }; |
| 241 | + |
| 242 | + el.draggable({ |
| 243 | + handle: this.opts.handle, |
| 244 | + scroll: true, |
| 245 | + appendTo: 'body', |
| 246 | + |
| 247 | + start: on_start_moving, |
| 248 | + stop: on_end_moving, |
| 249 | + drag: function (event, ui) { |
| 250 | + var x = Math.round(ui.position.left / cell_width), |
| 251 | + y = Math.floor(ui.position.top / cell_height); |
| 252 | + self.grid.move_node(node, x, y); |
| 253 | + self._update_container_height(); |
| 254 | + } |
| 255 | + }).resizable({ |
| 256 | + autoHide: true, |
| 257 | + handles: 'se', |
| 258 | + minHeight: this.opts.cell_height - 10, |
| 259 | + minWidth: 70, |
| 260 | + |
| 261 | + start: on_start_moving, |
| 262 | + stop: on_end_moving, |
| 263 | + resize: function (event, ui) { |
| 264 | + var width = Math.round(ui.size.width / cell_width), |
| 265 | + height = Math.round(ui.size.height / cell_height); |
| 266 | + self.grid.move_node(node, node.x, node.y, width, height); |
| 267 | + self._update_container_height(); |
| 268 | + } |
| 269 | + }); |
| 270 | + }; |
| 271 | + |
| 272 | + GridStack.prototype.add_widget = function (el, x, y, width, height) { |
| 273 | + el = $(el); |
| 274 | + if (typeof x != 'undefined') el.attr('data-gs-x', x); |
| 275 | + if (typeof y != 'undefined') el.attr('data-gs-y', y); |
| 276 | + if (typeof width != 'undefined') el.attr('data-gs-width', width); |
| 277 | + if (typeof height != 'undefined') el.attr('data-gs-height', height); |
| 278 | + this.container.append(el); |
| 279 | + this._prepare_element(el); |
| 280 | + }; |
| 281 | + |
| 282 | + GridStack.prototype.remove_widget = function (el) { |
| 283 | + var node = $(el).data('_gridstack_node'); |
| 284 | + this.grid.remove_node(node); |
| 285 | + el.remove(); |
| 286 | + }; |
| 287 | + |
| 288 | + scope.GridStackUI = GridStack; |
| 289 | + |
| 290 | + $.fn.gridstack = function (opts) { |
| 291 | + return this.each(function () { |
| 292 | + if (!$(this).data('_gridstack')) { |
| 293 | + $(this).data('_gridstack', new GridStack(this, opts)); |
| 294 | + } |
| 295 | + }); |
| 296 | + }; |
| 297 | + |
| 298 | +})(window, _); |
0 commit comments