baseboard 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/README.md +4 -0
- data/bin/baseboard +9 -0
- data/javascripts/batman.jquery.js +163 -0
- data/javascripts/batman.js +13680 -0
- data/javascripts/dashing.coffee +125 -0
- data/javascripts/es5-shim.js +1021 -0
- data/javascripts/jquery.js +4 -0
- data/lib/baseboard/app.rb +178 -0
- data/lib/baseboard/cli.rb +109 -0
- data/lib/baseboard/downloader.rb +18 -0
- data/lib/baseboard.rb +6 -0
- data/templates/dashboard/%name%.erb.tt +7 -0
- data/templates/job/%name%.rb +4 -0
- data/templates/project/.gitignore +2 -0
- data/templates/project/Gemfile +6 -0
- data/templates/project/README.md +1 -0
- data/templates/project/assets/fonts/fontawesome-webfont.eot +0 -0
- data/templates/project/assets/fonts/fontawesome-webfont.svg +399 -0
- data/templates/project/assets/fonts/fontawesome-webfont.ttf +0 -0
- data/templates/project/assets/fonts/fontawesome-webfont.woff +0 -0
- data/templates/project/assets/images/logo.png +0 -0
- data/templates/project/assets/javascripts/application.coffee +25 -0
- data/templates/project/assets/javascripts/d3-3.2.8.js +5 -0
- data/templates/project/assets/javascripts/dashing.gridster.coffee +37 -0
- data/templates/project/assets/javascripts/gridster/jquery.gridster.js +3263 -0
- data/templates/project/assets/javascripts/gridster/jquery.leanModal.min.js +5 -0
- data/templates/project/assets/javascripts/jquery.knob.js +646 -0
- data/templates/project/assets/javascripts/rickshaw-1.4.3.min.js +2 -0
- data/templates/project/assets/stylesheets/application.scss +258 -0
- data/templates/project/assets/stylesheets/font-awesome.css +1479 -0
- data/templates/project/assets/stylesheets/jquery.gridster.css +57 -0
- data/templates/project/config.ru +18 -0
- data/templates/project/dashboards/layout.erb +32 -0
- data/templates/project/dashboards/sample.erb +25 -0
- data/templates/project/dashboards/sampletv.erb +56 -0
- data/templates/project/jobs/buzzwords.rb +9 -0
- data/templates/project/jobs/convergence.rb +14 -0
- data/templates/project/jobs/sample.rb +13 -0
- data/templates/project/jobs/twitter.rb +28 -0
- data/templates/project/lib/.empty_directory +1 -0
- data/templates/project/public/404.html +26 -0
- data/templates/project/public/favicon.ico +0 -0
- data/templates/project/widgets/clock/clock.coffee +18 -0
- data/templates/project/widgets/clock/clock.html +2 -0
- data/templates/project/widgets/clock/clock.scss +13 -0
- data/templates/project/widgets/comments/comments.coffee +24 -0
- data/templates/project/widgets/comments/comments.html +7 -0
- data/templates/project/widgets/comments/comments.scss +33 -0
- data/templates/project/widgets/graph/graph.coffee +37 -0
- data/templates/project/widgets/graph/graph.html +5 -0
- data/templates/project/widgets/graph/graph.scss +65 -0
- data/templates/project/widgets/iframe/iframe.coffee +9 -0
- data/templates/project/widgets/iframe/iframe.html +1 -0
- data/templates/project/widgets/iframe/iframe.scss +8 -0
- data/templates/project/widgets/image/image.coffee +9 -0
- data/templates/project/widgets/image/image.html +1 -0
- data/templates/project/widgets/image/image.scss +13 -0
- data/templates/project/widgets/list/list.coffee +6 -0
- data/templates/project/widgets/list/list.html +18 -0
- data/templates/project/widgets/list/list.scss +60 -0
- data/templates/project/widgets/meter/meter.coffee +14 -0
- data/templates/project/widgets/meter/meter.html +7 -0
- data/templates/project/widgets/meter/meter.scss +35 -0
- data/templates/project/widgets/number/number.coffee +24 -0
- data/templates/project/widgets/number/number.html +11 -0
- data/templates/project/widgets/number/number.scss +39 -0
- data/templates/project/widgets/text/text.coffee +1 -0
- data/templates/project/widgets/text/text.html +7 -0
- data/templates/project/widgets/text/text.scss +32 -0
- data/templates/widget/%name%/%name%.coffee.tt +9 -0
- data/templates/widget/%name%/%name%.html +1 -0
- data/templates/widget/%name%/%name%.scss.tt +3 -0
- metadata +339 -0
@@ -0,0 +1,3263 @@
|
|
1
|
+
/*! gridster.js - v0.1.0 - 2013-02-15
|
2
|
+
* http://gridster.net/
|
3
|
+
* Copyright (c) 2013 ducksboard; Licensed MIT */
|
4
|
+
|
5
|
+
;(function($, window, document, undefined){
|
6
|
+
/**
|
7
|
+
* Creates objects with coordinates (x1, y1, x2, y2, cx, cy, width, height)
|
8
|
+
* to simulate DOM elements on the screen.
|
9
|
+
* Coords is used by Gridster to create a faux grid with any DOM element can
|
10
|
+
* collide.
|
11
|
+
*
|
12
|
+
* @class Coords
|
13
|
+
* @param {HTMLElement|Object} obj The jQuery HTMLElement or a object with: left,
|
14
|
+
* top, width and height properties.
|
15
|
+
* @return {Object} Coords instance.
|
16
|
+
* @constructor
|
17
|
+
*/
|
18
|
+
function Coords(obj) {
|
19
|
+
if (obj[0] && $.isPlainObject(obj[0])) {
|
20
|
+
this.data = obj[0];
|
21
|
+
}else {
|
22
|
+
this.el = obj;
|
23
|
+
}
|
24
|
+
|
25
|
+
this.isCoords = true;
|
26
|
+
this.coords = {};
|
27
|
+
this.init();
|
28
|
+
return this;
|
29
|
+
}
|
30
|
+
|
31
|
+
|
32
|
+
var fn = Coords.prototype;
|
33
|
+
|
34
|
+
|
35
|
+
fn.init = function(){
|
36
|
+
this.set();
|
37
|
+
this.original_coords = this.get();
|
38
|
+
};
|
39
|
+
|
40
|
+
|
41
|
+
fn.set = function(update, not_update_offsets) {
|
42
|
+
var el = this.el;
|
43
|
+
|
44
|
+
if (el && !update) {
|
45
|
+
this.data = el.offset();
|
46
|
+
this.data.width = el.width();
|
47
|
+
this.data.height = el.height();
|
48
|
+
}
|
49
|
+
|
50
|
+
if (el && update && !not_update_offsets) {
|
51
|
+
var offset = el.offset();
|
52
|
+
this.data.top = offset.top;
|
53
|
+
this.data.left = offset.left;
|
54
|
+
}
|
55
|
+
|
56
|
+
var d = this.data;
|
57
|
+
|
58
|
+
this.coords.x1 = d.left;
|
59
|
+
this.coords.y1 = d.top;
|
60
|
+
this.coords.x2 = d.left + d.width;
|
61
|
+
this.coords.y2 = d.top + d.height;
|
62
|
+
this.coords.cx = d.left + (d.width / 2);
|
63
|
+
this.coords.cy = d.top + (d.height / 2);
|
64
|
+
this.coords.width = d.width;
|
65
|
+
this.coords.height = d.height;
|
66
|
+
this.coords.el = el || false ;
|
67
|
+
|
68
|
+
return this;
|
69
|
+
};
|
70
|
+
|
71
|
+
|
72
|
+
fn.update = function(data){
|
73
|
+
if (!data && !this.el) {
|
74
|
+
return this;
|
75
|
+
}
|
76
|
+
|
77
|
+
if (data) {
|
78
|
+
var new_data = $.extend({}, this.data, data);
|
79
|
+
this.data = new_data;
|
80
|
+
return this.set(true, true);
|
81
|
+
}
|
82
|
+
|
83
|
+
this.set(true);
|
84
|
+
return this;
|
85
|
+
};
|
86
|
+
|
87
|
+
|
88
|
+
fn.get = function(){
|
89
|
+
return this.coords;
|
90
|
+
};
|
91
|
+
|
92
|
+
|
93
|
+
//jQuery adapter
|
94
|
+
$.fn.coords = function() {
|
95
|
+
if (this.data('coords') ) {
|
96
|
+
return this.data('coords');
|
97
|
+
}
|
98
|
+
|
99
|
+
var ins = new Coords(this, arguments[0]);
|
100
|
+
this.data('coords', ins);
|
101
|
+
return ins;
|
102
|
+
};
|
103
|
+
|
104
|
+
}(jQuery, window, document));
|
105
|
+
|
106
|
+
;(function($, window, document, undefined){
|
107
|
+
|
108
|
+
var defaults = {
|
109
|
+
colliders_context: document.body
|
110
|
+
// ,on_overlap: function(collider_data){},
|
111
|
+
// on_overlap_start : function(collider_data){},
|
112
|
+
// on_overlap_stop : function(collider_data){}
|
113
|
+
};
|
114
|
+
|
115
|
+
|
116
|
+
/**
|
117
|
+
* Detects collisions between a DOM element against other DOM elements or
|
118
|
+
* Coords objects.
|
119
|
+
*
|
120
|
+
* @class Collision
|
121
|
+
* @uses Coords
|
122
|
+
* @param {HTMLElement} el The jQuery wrapped HTMLElement.
|
123
|
+
* @param {HTMLElement|Array} colliders Can be a jQuery collection
|
124
|
+
* of HTMLElements or an Array of Coords instances.
|
125
|
+
* @param {Object} [options] An Object with all options you want to
|
126
|
+
* overwrite:
|
127
|
+
* @param {Function} [options.on_overlap_start] Executes a function the first
|
128
|
+
* time each `collider ` is overlapped.
|
129
|
+
* @param {Function} [options.on_overlap_stop] Executes a function when a
|
130
|
+
* `collider` is no longer collided.
|
131
|
+
* @param {Function} [options.on_overlap] Executes a function when the
|
132
|
+
* mouse is moved during the collision.
|
133
|
+
* @return {Object} Collision instance.
|
134
|
+
* @constructor
|
135
|
+
*/
|
136
|
+
function Collision(el, colliders, options) {
|
137
|
+
this.options = $.extend(defaults, options);
|
138
|
+
this.$element = el;
|
139
|
+
this.last_colliders = [];
|
140
|
+
this.last_colliders_coords = [];
|
141
|
+
if (typeof colliders === 'string' || colliders instanceof jQuery) {
|
142
|
+
this.$colliders = $(colliders,
|
143
|
+
this.options.colliders_context).not(this.$element);
|
144
|
+
}else{
|
145
|
+
this.colliders = $(colliders);
|
146
|
+
}
|
147
|
+
|
148
|
+
this.init();
|
149
|
+
}
|
150
|
+
|
151
|
+
|
152
|
+
var fn = Collision.prototype;
|
153
|
+
|
154
|
+
|
155
|
+
fn.init = function() {
|
156
|
+
this.find_collisions();
|
157
|
+
};
|
158
|
+
|
159
|
+
|
160
|
+
fn.overlaps = function(a, b) {
|
161
|
+
var x = false;
|
162
|
+
var y = false;
|
163
|
+
|
164
|
+
if ((b.x1 >= a.x1 && b.x1 <= a.x2) ||
|
165
|
+
(b.x2 >= a.x1 && b.x2 <= a.x2) ||
|
166
|
+
(a.x1 >= b.x1 && a.x2 <= b.x2)
|
167
|
+
) { x = true; }
|
168
|
+
|
169
|
+
if ((b.y1 >= a.y1 && b.y1 <= a.y2) ||
|
170
|
+
(b.y2 >= a.y1 && b.y2 <= a.y2) ||
|
171
|
+
(a.y1 >= b.y1 && a.y2 <= b.y2)
|
172
|
+
) { y = true; }
|
173
|
+
|
174
|
+
return (x && y);
|
175
|
+
};
|
176
|
+
|
177
|
+
|
178
|
+
fn.detect_overlapping_region = function(a, b){
|
179
|
+
var regionX = '';
|
180
|
+
var regionY = '';
|
181
|
+
|
182
|
+
if (a.y1 > b.cy && a.y1 < b.y2) { regionX = 'N'; }
|
183
|
+
if (a.y2 > b.y1 && a.y2 < b.cy) { regionX = 'S'; }
|
184
|
+
if (a.x1 > b.cx && a.x1 < b.x2) { regionY = 'W'; }
|
185
|
+
if (a.x2 > b.x1 && a.x2 < b.cx) { regionY = 'E'; }
|
186
|
+
|
187
|
+
return (regionX + regionY) || 'C';
|
188
|
+
};
|
189
|
+
|
190
|
+
|
191
|
+
fn.calculate_overlapped_area_coords = function(a, b){
|
192
|
+
var x1 = Math.max(a.x1, b.x1);
|
193
|
+
var y1 = Math.max(a.y1, b.y1);
|
194
|
+
var x2 = Math.min(a.x2, b.x2);
|
195
|
+
var y2 = Math.min(a.y2, b.y2);
|
196
|
+
|
197
|
+
return $({
|
198
|
+
left: x1,
|
199
|
+
top: y1,
|
200
|
+
width : (x2 - x1),
|
201
|
+
height: (y2 - y1)
|
202
|
+
}).coords().get();
|
203
|
+
};
|
204
|
+
|
205
|
+
|
206
|
+
fn.calculate_overlapped_area = function(coords){
|
207
|
+
return (coords.width * coords.height);
|
208
|
+
};
|
209
|
+
|
210
|
+
|
211
|
+
fn.manage_colliders_start_stop = function(new_colliders_coords, start_callback, stop_callback){
|
212
|
+
var last = this.last_colliders_coords;
|
213
|
+
|
214
|
+
for (var i = 0, il = last.length; i < il; i++) {
|
215
|
+
if ($.inArray(last[i], new_colliders_coords) === -1) {
|
216
|
+
start_callback.call(this, last[i]);
|
217
|
+
}
|
218
|
+
}
|
219
|
+
|
220
|
+
for (var j = 0, jl = new_colliders_coords.length; j < jl; j++) {
|
221
|
+
if ($.inArray(new_colliders_coords[j], last) === -1) {
|
222
|
+
stop_callback.call(this, new_colliders_coords[j]);
|
223
|
+
}
|
224
|
+
|
225
|
+
}
|
226
|
+
};
|
227
|
+
|
228
|
+
|
229
|
+
fn.find_collisions = function(player_data_coords){
|
230
|
+
var self = this;
|
231
|
+
var colliders_coords = [];
|
232
|
+
var colliders_data = [];
|
233
|
+
var $colliders = (this.colliders || this.$colliders);
|
234
|
+
var count = $colliders.length;
|
235
|
+
var player_coords = self.$element.coords()
|
236
|
+
.update(player_data_coords || false).get();
|
237
|
+
|
238
|
+
while(count--){
|
239
|
+
var $collider = self.$colliders ?
|
240
|
+
$($colliders[count]) : $colliders[count];
|
241
|
+
var $collider_coords_ins = ($collider.isCoords) ?
|
242
|
+
$collider : $collider.coords();
|
243
|
+
var collider_coords = $collider_coords_ins.get();
|
244
|
+
var overlaps = self.overlaps(player_coords, collider_coords);
|
245
|
+
|
246
|
+
if (!overlaps) {
|
247
|
+
continue;
|
248
|
+
}
|
249
|
+
|
250
|
+
var region = self.detect_overlapping_region(
|
251
|
+
player_coords, collider_coords);
|
252
|
+
|
253
|
+
//todo: make this an option
|
254
|
+
if (region === 'C'){
|
255
|
+
var area_coords = self.calculate_overlapped_area_coords(
|
256
|
+
player_coords, collider_coords);
|
257
|
+
var area = self.calculate_overlapped_area(area_coords);
|
258
|
+
var collider_data = {
|
259
|
+
area: area,
|
260
|
+
area_coords : area_coords,
|
261
|
+
region: region,
|
262
|
+
coords: collider_coords,
|
263
|
+
player_coords: player_coords,
|
264
|
+
el: $collider
|
265
|
+
};
|
266
|
+
|
267
|
+
if (self.options.on_overlap) {
|
268
|
+
self.options.on_overlap.call(this, collider_data);
|
269
|
+
}
|
270
|
+
colliders_coords.push($collider_coords_ins);
|
271
|
+
colliders_data.push(collider_data);
|
272
|
+
}
|
273
|
+
}
|
274
|
+
|
275
|
+
if (self.options.on_overlap_stop || self.options.on_overlap_start) {
|
276
|
+
this.manage_colliders_start_stop(colliders_coords,
|
277
|
+
self.options.on_overlap_stop, self.options.on_overlap_start);
|
278
|
+
}
|
279
|
+
|
280
|
+
this.last_colliders_coords = colliders_coords;
|
281
|
+
|
282
|
+
return colliders_data;
|
283
|
+
};
|
284
|
+
|
285
|
+
|
286
|
+
fn.get_closest_colliders = function(player_data_coords){
|
287
|
+
var colliders = this.find_collisions(player_data_coords);
|
288
|
+
|
289
|
+
colliders.sort(function(a, b) {
|
290
|
+
/* if colliders are being overlapped by the "C" (center) region,
|
291
|
+
* we have to set a lower index in the array to which they are placed
|
292
|
+
* above in the grid. */
|
293
|
+
if (a.region === 'C' && b.region === 'C') {
|
294
|
+
if (a.coords.y1 < b.coords.y1 || a.coords.x1 < b.coords.x1) {
|
295
|
+
return - 1;
|
296
|
+
}else{
|
297
|
+
return 1;
|
298
|
+
}
|
299
|
+
}
|
300
|
+
|
301
|
+
if (a.area < b.area) {
|
302
|
+
return 1;
|
303
|
+
}
|
304
|
+
|
305
|
+
return 1;
|
306
|
+
});
|
307
|
+
return colliders;
|
308
|
+
};
|
309
|
+
|
310
|
+
|
311
|
+
//jQuery adapter
|
312
|
+
$.fn.collision = function(collider, options) {
|
313
|
+
return new Collision( this, collider, options );
|
314
|
+
};
|
315
|
+
|
316
|
+
|
317
|
+
}(jQuery, window, document));
|
318
|
+
|
319
|
+
;(function(window, undefined) {
|
320
|
+
/* Debounce and throttle functions taken from underscore.js */
|
321
|
+
window.debounce = function(func, wait, immediate) {
|
322
|
+
var timeout;
|
323
|
+
return function() {
|
324
|
+
var context = this, args = arguments;
|
325
|
+
var later = function() {
|
326
|
+
timeout = null;
|
327
|
+
if (!immediate) func.apply(context, args);
|
328
|
+
};
|
329
|
+
if (immediate && !timeout) func.apply(context, args);
|
330
|
+
clearTimeout(timeout);
|
331
|
+
timeout = setTimeout(later, wait);
|
332
|
+
};
|
333
|
+
};
|
334
|
+
|
335
|
+
|
336
|
+
window.throttle = function(func, wait) {
|
337
|
+
var context, args, timeout, throttling, more, result;
|
338
|
+
var whenDone = debounce(
|
339
|
+
function(){ more = throttling = false; }, wait);
|
340
|
+
return function() {
|
341
|
+
context = this; args = arguments;
|
342
|
+
var later = function() {
|
343
|
+
timeout = null;
|
344
|
+
if (more) func.apply(context, args);
|
345
|
+
whenDone();
|
346
|
+
};
|
347
|
+
if (!timeout) timeout = setTimeout(later, wait);
|
348
|
+
if (throttling) {
|
349
|
+
more = true;
|
350
|
+
} else {
|
351
|
+
result = func.apply(context, args);
|
352
|
+
}
|
353
|
+
whenDone();
|
354
|
+
throttling = true;
|
355
|
+
return result;
|
356
|
+
};
|
357
|
+
};
|
358
|
+
|
359
|
+
})(window);
|
360
|
+
|
361
|
+
;(function($, window, document, undefined){
|
362
|
+
|
363
|
+
var defaults = {
|
364
|
+
items: '.gs_w',
|
365
|
+
distance: 1,
|
366
|
+
limit: true,
|
367
|
+
offset_left: 0,
|
368
|
+
autoscroll: true,
|
369
|
+
ignore_dragging: ['INPUT', 'TEXTAREA', 'SELECT', 'BUTTON'],
|
370
|
+
handle: null
|
371
|
+
// drag: function(e){},
|
372
|
+
// start : function(e, ui){},
|
373
|
+
// stop : function(e){}
|
374
|
+
};
|
375
|
+
|
376
|
+
var $window = $(window);
|
377
|
+
var isTouch = !!('ontouchstart' in window);
|
378
|
+
var pointer_events = {
|
379
|
+
start: isTouch ? 'touchstart' : 'mousedown.draggable',
|
380
|
+
move: isTouch ? 'touchmove' : 'mousemove.draggable',
|
381
|
+
end: isTouch ? 'touchend' : 'mouseup.draggable'
|
382
|
+
};
|
383
|
+
|
384
|
+
/**
|
385
|
+
* Basic drag implementation for DOM elements inside a container.
|
386
|
+
* Provide start/stop/drag callbacks.
|
387
|
+
*
|
388
|
+
* @class Draggable
|
389
|
+
* @param {HTMLElement} el The HTMLelement that contains all the widgets
|
390
|
+
* to be dragged.
|
391
|
+
* @param {Object} [options] An Object with all options you want to
|
392
|
+
* overwrite:
|
393
|
+
* @param {HTMLElement|String} [options.items] Define who will
|
394
|
+
* be the draggable items. Can be a CSS Selector String or a
|
395
|
+
* collection of HTMLElements.
|
396
|
+
* @param {Number} [options.distance] Distance in pixels after mousedown
|
397
|
+
* the mouse must move before dragging should start.
|
398
|
+
* @param {Boolean} [options.limit] Constrains dragging to the width of
|
399
|
+
* the container
|
400
|
+
* @param {offset_left} [options.offset_left] Offset added to the item
|
401
|
+
* that is being dragged.
|
402
|
+
* @param {Number} [options.drag] Executes a callback when the mouse is
|
403
|
+
* moved during the dragging.
|
404
|
+
* @param {Number} [options.start] Executes a callback when the drag
|
405
|
+
* starts.
|
406
|
+
* @param {Number} [options.stop] Executes a callback when the drag stops.
|
407
|
+
* @return {Object} Returns `el`.
|
408
|
+
* @constructor
|
409
|
+
*/
|
410
|
+
function Draggable(el, options) {
|
411
|
+
this.options = $.extend({}, defaults, options);
|
412
|
+
this.$body = $(document.body);
|
413
|
+
this.$container = $(el);
|
414
|
+
this.$dragitems = $(this.options.items, this.$container);
|
415
|
+
this.is_dragging = false;
|
416
|
+
this.player_min_left = 0 + this.options.offset_left;
|
417
|
+
this.init();
|
418
|
+
}
|
419
|
+
|
420
|
+
var fn = Draggable.prototype;
|
421
|
+
|
422
|
+
fn.init = function() {
|
423
|
+
this.calculate_positions();
|
424
|
+
this.$container.css('position', 'relative');
|
425
|
+
this.disabled = false;
|
426
|
+
this.events();
|
427
|
+
|
428
|
+
this.on_window_resize = throttle($.proxy(this.calculate_positions, this), 200);
|
429
|
+
$(window).bind('resize', this.on_window_resize);
|
430
|
+
};
|
431
|
+
|
432
|
+
fn.events = function() {
|
433
|
+
this.proxied_on_select_start = $.proxy(this.on_select_start, this);
|
434
|
+
this.$container.on('selectstart', this.proxied_on_select_start);
|
435
|
+
|
436
|
+
this.proxied_drag_handler = $.proxy(this.drag_handler, this);
|
437
|
+
this.$container.on(pointer_events.start, this.options.items, this.proxied_drag_handler);
|
438
|
+
|
439
|
+
this.proxied_pointer_events_end = $.proxy(function(e) {
|
440
|
+
this.is_dragging = false;
|
441
|
+
if (this.disabled) { return; }
|
442
|
+
this.$body.off(pointer_events.move);
|
443
|
+
if (this.drag_start) {
|
444
|
+
this.on_dragstop(e);
|
445
|
+
}
|
446
|
+
}, this);
|
447
|
+
this.$body.on(pointer_events.end, this.proxied_pointer_events_end);
|
448
|
+
};
|
449
|
+
|
450
|
+
fn.get_actual_pos = function($el) {
|
451
|
+
var pos = $el.position();
|
452
|
+
return pos;
|
453
|
+
};
|
454
|
+
|
455
|
+
|
456
|
+
fn.get_mouse_pos = function(e) {
|
457
|
+
if (isTouch) {
|
458
|
+
var oe = e.originalEvent;
|
459
|
+
e = oe.touches.length ? oe.touches[0] : oe.changedTouches[0];
|
460
|
+
}
|
461
|
+
|
462
|
+
return {
|
463
|
+
left: e.clientX,
|
464
|
+
top: e.clientY
|
465
|
+
};
|
466
|
+
};
|
467
|
+
|
468
|
+
|
469
|
+
fn.get_offset = function(e) {
|
470
|
+
e.preventDefault();
|
471
|
+
var mouse_actual_pos = this.get_mouse_pos(e);
|
472
|
+
var diff_x = Math.round(
|
473
|
+
mouse_actual_pos.left - this.mouse_init_pos.left);
|
474
|
+
var diff_y = Math.round(mouse_actual_pos.top - this.mouse_init_pos.top);
|
475
|
+
|
476
|
+
var left = Math.round(this.el_init_offset.left + diff_x - this.baseX);
|
477
|
+
var top = Math.round(
|
478
|
+
this.el_init_offset.top + diff_y - this.baseY + this.scrollOffset);
|
479
|
+
|
480
|
+
if (this.options.limit) {
|
481
|
+
if (left > this.player_max_left) {
|
482
|
+
left = this.player_max_left;
|
483
|
+
}else if(left < this.player_min_left) {
|
484
|
+
left = this.player_min_left;
|
485
|
+
}
|
486
|
+
}
|
487
|
+
|
488
|
+
return {
|
489
|
+
left: left,
|
490
|
+
top: top,
|
491
|
+
mouse_left: mouse_actual_pos.left,
|
492
|
+
mouse_top: mouse_actual_pos.top
|
493
|
+
};
|
494
|
+
};
|
495
|
+
|
496
|
+
|
497
|
+
fn.manage_scroll = function(offset) {
|
498
|
+
/* scroll document */
|
499
|
+
var nextScrollTop;
|
500
|
+
var scrollTop = $window.scrollTop();
|
501
|
+
var min_window_y = scrollTop;
|
502
|
+
var max_window_y = min_window_y + this.window_height;
|
503
|
+
|
504
|
+
var mouse_down_zone = max_window_y - 50;
|
505
|
+
var mouse_up_zone = min_window_y + 50;
|
506
|
+
|
507
|
+
var abs_mouse_left = offset.mouse_left;
|
508
|
+
var abs_mouse_top = min_window_y + offset.mouse_top;
|
509
|
+
|
510
|
+
var max_player_y = (this.doc_height - this.window_height +
|
511
|
+
this.player_height);
|
512
|
+
|
513
|
+
if (abs_mouse_top >= mouse_down_zone) {
|
514
|
+
nextScrollTop = scrollTop + 30;
|
515
|
+
if (nextScrollTop < max_player_y) {
|
516
|
+
$window.scrollTop(nextScrollTop);
|
517
|
+
this.scrollOffset = this.scrollOffset + 30;
|
518
|
+
}
|
519
|
+
}
|
520
|
+
|
521
|
+
if (abs_mouse_top <= mouse_up_zone) {
|
522
|
+
nextScrollTop = scrollTop - 30;
|
523
|
+
if (nextScrollTop > 0) {
|
524
|
+
$window.scrollTop(nextScrollTop);
|
525
|
+
this.scrollOffset = this.scrollOffset - 30;
|
526
|
+
}
|
527
|
+
}
|
528
|
+
};
|
529
|
+
|
530
|
+
|
531
|
+
fn.calculate_positions = function(e) {
|
532
|
+
this.window_height = $window.height();
|
533
|
+
};
|
534
|
+
|
535
|
+
|
536
|
+
fn.drag_handler = function(e) {
|
537
|
+
var node = e.target.nodeName;
|
538
|
+
if (this.disabled || e.which !== 1 && !isTouch) {
|
539
|
+
return;
|
540
|
+
}
|
541
|
+
|
542
|
+
if (this.ignore_drag(e)) {
|
543
|
+
return;
|
544
|
+
}
|
545
|
+
|
546
|
+
var self = this;
|
547
|
+
var first = true;
|
548
|
+
this.$player = $(e.currentTarget);
|
549
|
+
|
550
|
+
this.el_init_pos = this.get_actual_pos(this.$player);
|
551
|
+
this.mouse_init_pos = this.get_mouse_pos(e);
|
552
|
+
this.offsetY = this.mouse_init_pos.top - this.el_init_pos.top;
|
553
|
+
|
554
|
+
this.on_pointer_events_move = function(mme){
|
555
|
+
var mouse_actual_pos = self.get_mouse_pos(mme);
|
556
|
+
var diff_x = Math.abs(
|
557
|
+
mouse_actual_pos.left - self.mouse_init_pos.left);
|
558
|
+
var diff_y = Math.abs(
|
559
|
+
mouse_actual_pos.top - self.mouse_init_pos.top);
|
560
|
+
if (!(diff_x > self.options.distance ||
|
561
|
+
diff_y > self.options.distance)
|
562
|
+
) {
|
563
|
+
return false;
|
564
|
+
}
|
565
|
+
|
566
|
+
if (first) {
|
567
|
+
first = false;
|
568
|
+
self.on_dragstart.call(self, mme);
|
569
|
+
return false;
|
570
|
+
}
|
571
|
+
|
572
|
+
if (self.is_dragging === true) {
|
573
|
+
self.on_dragmove.call(self, mme);
|
574
|
+
}
|
575
|
+
|
576
|
+
return false;
|
577
|
+
};
|
578
|
+
|
579
|
+
this.$body.on(pointer_events.move, this.on_pointer_events_move);
|
580
|
+
|
581
|
+
return false;
|
582
|
+
};
|
583
|
+
|
584
|
+
|
585
|
+
fn.on_dragstart = function(e) {
|
586
|
+
e.preventDefault();
|
587
|
+
this.drag_start = true;
|
588
|
+
this.is_dragging = true;
|
589
|
+
var offset = this.$container.offset();
|
590
|
+
this.baseX = Math.round(offset.left);
|
591
|
+
this.baseY = Math.round(offset.top);
|
592
|
+
this.doc_height = $(document).height();
|
593
|
+
|
594
|
+
if (this.options.helper === 'clone') {
|
595
|
+
this.$helper = this.$player.clone()
|
596
|
+
.appendTo(this.$container).addClass('helper');
|
597
|
+
this.helper = true;
|
598
|
+
}else{
|
599
|
+
this.helper = false;
|
600
|
+
}
|
601
|
+
this.scrollOffset = 0;
|
602
|
+
this.el_init_offset = this.$player.offset();
|
603
|
+
this.player_width = this.$player.width();
|
604
|
+
this.player_height = this.$player.height();
|
605
|
+
this.player_max_left = (this.$container.width() - this.player_width +
|
606
|
+
this.options.offset_left);
|
607
|
+
|
608
|
+
if (this.options.start) {
|
609
|
+
this.options.start.call(this.$player, e, {
|
610
|
+
helper: this.helper ? this.$helper : this.$player
|
611
|
+
});
|
612
|
+
}
|
613
|
+
return false;
|
614
|
+
};
|
615
|
+
|
616
|
+
|
617
|
+
fn.on_dragmove = function(e) {
|
618
|
+
var offset = this.get_offset(e);
|
619
|
+
|
620
|
+
this.options.autoscroll && this.manage_scroll(offset);
|
621
|
+
|
622
|
+
(this.helper ? this.$helper : this.$player).css({
|
623
|
+
'position': 'absolute',
|
624
|
+
'left' : offset.left,
|
625
|
+
'top' : offset.top
|
626
|
+
});
|
627
|
+
|
628
|
+
var ui = {
|
629
|
+
'position': {
|
630
|
+
'left': offset.left,
|
631
|
+
'top': offset.top
|
632
|
+
}
|
633
|
+
};
|
634
|
+
|
635
|
+
if (this.options.drag) {
|
636
|
+
this.options.drag.call(this.$player, e, ui);
|
637
|
+
}
|
638
|
+
return false;
|
639
|
+
};
|
640
|
+
|
641
|
+
|
642
|
+
fn.on_dragstop = function(e) {
|
643
|
+
var offset = this.get_offset(e);
|
644
|
+
this.drag_start = false;
|
645
|
+
|
646
|
+
var ui = {
|
647
|
+
'position': {
|
648
|
+
'left': offset.left,
|
649
|
+
'top': offset.top
|
650
|
+
}
|
651
|
+
};
|
652
|
+
|
653
|
+
if (this.options.stop) {
|
654
|
+
this.options.stop.call(this.$player, e, ui);
|
655
|
+
}
|
656
|
+
|
657
|
+
if (this.helper) {
|
658
|
+
this.$helper.remove();
|
659
|
+
}
|
660
|
+
|
661
|
+
return false;
|
662
|
+
};
|
663
|
+
|
664
|
+
fn.on_select_start = function(e) {
|
665
|
+
if (this.disabled) { return; }
|
666
|
+
|
667
|
+
if (this.ignore_drag(e)) {
|
668
|
+
return;
|
669
|
+
}
|
670
|
+
|
671
|
+
return false;
|
672
|
+
};
|
673
|
+
|
674
|
+
fn.enable = function() {
|
675
|
+
this.disabled = false;
|
676
|
+
};
|
677
|
+
|
678
|
+
fn.disable = function() {
|
679
|
+
this.disabled = true;
|
680
|
+
};
|
681
|
+
|
682
|
+
|
683
|
+
fn.destroy = function(){
|
684
|
+
this.disable();
|
685
|
+
|
686
|
+
this.$container.off('selectstart', this.proxied_on_select_start);
|
687
|
+
this.$container.off(pointer_events.start, this.proxied_drag_handler);
|
688
|
+
this.$body.off(pointer_events.end, this.proxied_pointer_events_end);
|
689
|
+
this.$body.off(pointer_events.move, this.on_pointer_events_move);
|
690
|
+
$(window).unbind('resize', this.on_window_resize);
|
691
|
+
|
692
|
+
$.removeData(this.$container, 'drag');
|
693
|
+
};
|
694
|
+
|
695
|
+
fn.ignore_drag = function(event) {
|
696
|
+
if (this.options.handle) {
|
697
|
+
return !$(event.target).is(this.options.handle);
|
698
|
+
}
|
699
|
+
|
700
|
+
return $.inArray(event.target.nodeName, this.options.ignore_dragging) >= 0;
|
701
|
+
};
|
702
|
+
|
703
|
+
//jQuery adapter
|
704
|
+
$.fn.drag = function ( options ) {
|
705
|
+
return this.each(function () {
|
706
|
+
if (!$.data(this, 'drag')) {
|
707
|
+
$.data(this, 'drag', new Draggable( this, options ));
|
708
|
+
}
|
709
|
+
});
|
710
|
+
};
|
711
|
+
|
712
|
+
|
713
|
+
}(jQuery, window, document));
|
714
|
+
|
715
|
+
;(function($, window, document, undefined) {
|
716
|
+
|
717
|
+
var defaults = {
|
718
|
+
namespace: '',
|
719
|
+
widget_selector: 'li',
|
720
|
+
widget_margins: [10, 10],
|
721
|
+
widget_base_dimensions: [400, 225],
|
722
|
+
extra_rows: 0,
|
723
|
+
extra_cols: 0,
|
724
|
+
min_cols: 1,
|
725
|
+
min_rows: 15,
|
726
|
+
max_size_x: 6,
|
727
|
+
autogenerate_stylesheet: true,
|
728
|
+
avoid_overlapped_widgets: true,
|
729
|
+
serialize_params: function($w, wgd) {
|
730
|
+
return {
|
731
|
+
col: wgd.col,
|
732
|
+
row: wgd.row,
|
733
|
+
size_x: wgd.size_x,
|
734
|
+
size_y: wgd.size_y
|
735
|
+
};
|
736
|
+
},
|
737
|
+
collision: {},
|
738
|
+
draggable: {
|
739
|
+
distance: 4
|
740
|
+
}
|
741
|
+
};
|
742
|
+
|
743
|
+
|
744
|
+
/**
|
745
|
+
* @class Gridster
|
746
|
+
* @uses Draggable
|
747
|
+
* @uses Collision
|
748
|
+
* @param {HTMLElement} el The HTMLelement that contains all the widgets.
|
749
|
+
* @param {Object} [options] An Object with all options you want to
|
750
|
+
* overwrite:
|
751
|
+
* @param {HTMLElement|String} [options.widget_selector] Define who will
|
752
|
+
* be the draggable widgets. Can be a CSS Selector String or a
|
753
|
+
* collection of HTMLElements
|
754
|
+
* @param {Array} [options.widget_margins] Margin between widgets.
|
755
|
+
* The first index for the horizontal margin (left, right) and
|
756
|
+
* the second for the vertical margin (top, bottom).
|
757
|
+
* @param {Array} [options.widget_base_dimensions] Base widget dimensions
|
758
|
+
* in pixels. The first index for the width and the second for the
|
759
|
+
* height.
|
760
|
+
* @param {Number} [options.extra_cols] Add more columns in addition to
|
761
|
+
* those that have been calculated.
|
762
|
+
* @param {Number} [options.extra_rows] Add more rows in addition to
|
763
|
+
* those that have been calculated.
|
764
|
+
* @param {Number} [options.min_cols] The minimum required columns.
|
765
|
+
* @param {Number} [options.min_rows] The minimum required rows.
|
766
|
+
* @param {Number} [options.max_size_x] The maximum number of columns
|
767
|
+
* that a widget can span.
|
768
|
+
* @param {Boolean} [options.autogenerate_stylesheet] If true, all the
|
769
|
+
* CSS required to position all widgets in their respective columns
|
770
|
+
* and rows will be generated automatically and injected to the
|
771
|
+
* `<head>` of the document. You can set this to false, and write
|
772
|
+
* your own CSS targeting rows and cols via data-attributes like so:
|
773
|
+
* `[data-col="1"] { left: 10px; }`
|
774
|
+
* @param {Boolean} [options.avoid_overlapped_widgets] Avoid that widgets loaded
|
775
|
+
* from the DOM can be overlapped. It is helpful if the positions were
|
776
|
+
* bad stored in the database or if there was any conflict.
|
777
|
+
* @param {Function} [options.serialize_params] Return the data you want
|
778
|
+
* for each widget in the serialization. Two arguments are passed:
|
779
|
+
* `$w`: the jQuery wrapped HTMLElement, and `wgd`: the grid
|
780
|
+
* coords object (`col`, `row`, `size_x`, `size_y`).
|
781
|
+
* @param {Object} [options.collision] An Object with all options for
|
782
|
+
* Collision class you want to overwrite. See Collision docs for
|
783
|
+
* more info.
|
784
|
+
* @param {Object} [options.draggable] An Object with all options for
|
785
|
+
* Draggable class you want to overwrite. See Draggable docs for more
|
786
|
+
* info.
|
787
|
+
*
|
788
|
+
* @constructor
|
789
|
+
*/
|
790
|
+
function Gridster(el, options) {
|
791
|
+
this.options = $.extend(true, defaults, options);
|
792
|
+
this.$el = $(el);
|
793
|
+
this.$wrapper = this.$el.parent();
|
794
|
+
this.$widgets = this.$el.children(this.options.widget_selector).addClass('gs_w');
|
795
|
+
this.widgets = [];
|
796
|
+
this.$changed = $([]);
|
797
|
+
this.wrapper_width = this.$wrapper.width();
|
798
|
+
this.min_widget_width = (this.options.widget_margins[0] * 2) +
|
799
|
+
this.options.widget_base_dimensions[0];
|
800
|
+
this.min_widget_height = (this.options.widget_margins[1] * 2) +
|
801
|
+
this.options.widget_base_dimensions[1];
|
802
|
+
this.init();
|
803
|
+
}
|
804
|
+
|
805
|
+
Gridster.generated_stylesheets = [];
|
806
|
+
|
807
|
+
var fn = Gridster.prototype;
|
808
|
+
|
809
|
+
fn.init = function() {
|
810
|
+
this.generate_grid_and_stylesheet();
|
811
|
+
this.get_widgets_from_DOM();
|
812
|
+
this.set_dom_grid_height();
|
813
|
+
this.$wrapper.addClass('ready');
|
814
|
+
this.draggable();
|
815
|
+
|
816
|
+
this.on_window_resize = throttle($.proxy(this.recalculate_faux_grid, this), 200);
|
817
|
+
|
818
|
+
$(window).bind('resize', this.on_window_resize);
|
819
|
+
};
|
820
|
+
|
821
|
+
|
822
|
+
/**
|
823
|
+
* Disables dragging.
|
824
|
+
*
|
825
|
+
* @method disable
|
826
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
827
|
+
*/
|
828
|
+
fn.disable = function() {
|
829
|
+
this.$wrapper.find('.player-revert').removeClass('player-revert');
|
830
|
+
this.drag_api.disable();
|
831
|
+
return this;
|
832
|
+
};
|
833
|
+
|
834
|
+
|
835
|
+
/**
|
836
|
+
* Enables dragging.
|
837
|
+
*
|
838
|
+
* @method enable
|
839
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
840
|
+
*/
|
841
|
+
fn.enable = function() {
|
842
|
+
this.drag_api.enable();
|
843
|
+
return this;
|
844
|
+
};
|
845
|
+
|
846
|
+
|
847
|
+
/**
|
848
|
+
* Add a new widget to the grid.
|
849
|
+
*
|
850
|
+
* @method add_widget
|
851
|
+
* @param {String|HTMLElement} html The string representing the HTML of the widget
|
852
|
+
* or the HTMLElement.
|
853
|
+
* @param {Number} [size_x] The nº of rows the widget occupies horizontally.
|
854
|
+
* @param {Number} [size_y] The nº of columns the widget occupies vertically.
|
855
|
+
* @param {Number} [col] The column the widget should start in.
|
856
|
+
* @param {Number} [row] The row the widget should start in.
|
857
|
+
* @return {HTMLElement} Returns the jQuery wrapped HTMLElement representing.
|
858
|
+
* the widget that was just created.
|
859
|
+
*/
|
860
|
+
fn.add_widget = function(html, size_x, size_y, col, row) {
|
861
|
+
var pos;
|
862
|
+
size_x || (size_x = 1);
|
863
|
+
size_y || (size_y = 1);
|
864
|
+
|
865
|
+
if (!col & !row) {
|
866
|
+
pos = this.next_position(size_x, size_y);
|
867
|
+
}else{
|
868
|
+
pos = {
|
869
|
+
col: col,
|
870
|
+
row: row
|
871
|
+
};
|
872
|
+
|
873
|
+
this.empty_cells(col, row, size_x, size_y);
|
874
|
+
}
|
875
|
+
|
876
|
+
var $w = $(html).attr({
|
877
|
+
'data-col': pos.col,
|
878
|
+
'data-row': pos.row,
|
879
|
+
'data-sizex' : size_x,
|
880
|
+
'data-sizey' : size_y
|
881
|
+
}).addClass('gs_w').appendTo(this.$el).hide();
|
882
|
+
|
883
|
+
this.$widgets = this.$widgets.add($w);
|
884
|
+
|
885
|
+
this.register_widget($w);
|
886
|
+
|
887
|
+
this.add_faux_rows(pos.size_y);
|
888
|
+
//this.add_faux_cols(pos.size_x);
|
889
|
+
|
890
|
+
this.set_dom_grid_height();
|
891
|
+
|
892
|
+
return $w.fadeIn();
|
893
|
+
};
|
894
|
+
|
895
|
+
|
896
|
+
|
897
|
+
/**
|
898
|
+
* Change the size of a widget.
|
899
|
+
*
|
900
|
+
* @method resize_widget
|
901
|
+
* @param {HTMLElement} $widget The jQuery wrapped HTMLElement
|
902
|
+
* representing the widget.
|
903
|
+
* @param {Number} size_x The number of columns that will occupy the widget.
|
904
|
+
* @param {Number} size_y The number of rows that will occupy the widget.
|
905
|
+
* @return {HTMLElement} Returns $widget.
|
906
|
+
*/
|
907
|
+
fn.resize_widget = function($widget, size_x, size_y) {
|
908
|
+
var wgd = $widget.coords().grid;
|
909
|
+
size_x || (size_x = wgd.size_x);
|
910
|
+
size_y || (size_y = wgd.size_y);
|
911
|
+
|
912
|
+
if (size_x > this.cols) {
|
913
|
+
size_x = this.cols;
|
914
|
+
}
|
915
|
+
|
916
|
+
var old_cells_occupied = this.get_cells_occupied(wgd);
|
917
|
+
var old_size_x = wgd.size_x;
|
918
|
+
var old_size_y = wgd.size_y;
|
919
|
+
var old_col = wgd.col;
|
920
|
+
var new_col = old_col;
|
921
|
+
var wider = size_x > old_size_x;
|
922
|
+
var taller = size_y > old_size_y;
|
923
|
+
|
924
|
+
if (old_col + size_x - 1 > this.cols) {
|
925
|
+
var diff = old_col + (size_x - 1) - this.cols;
|
926
|
+
var c = old_col - diff;
|
927
|
+
new_col = Math.max(1, c);
|
928
|
+
}
|
929
|
+
|
930
|
+
var new_grid_data = {
|
931
|
+
col: new_col,
|
932
|
+
row: wgd.row,
|
933
|
+
size_x: size_x,
|
934
|
+
size_y: size_y
|
935
|
+
};
|
936
|
+
|
937
|
+
var new_cells_occupied = this.get_cells_occupied(new_grid_data);
|
938
|
+
|
939
|
+
var empty_cols = [];
|
940
|
+
$.each(old_cells_occupied.cols, function(i, col) {
|
941
|
+
if ($.inArray(col, new_cells_occupied.cols) === -1) {
|
942
|
+
empty_cols.push(col);
|
943
|
+
}
|
944
|
+
});
|
945
|
+
|
946
|
+
var occupied_cols = [];
|
947
|
+
$.each(new_cells_occupied.cols, function(i, col) {
|
948
|
+
if ($.inArray(col, old_cells_occupied.cols) === -1) {
|
949
|
+
occupied_cols.push(col);
|
950
|
+
}
|
951
|
+
});
|
952
|
+
|
953
|
+
var empty_rows = [];
|
954
|
+
$.each(old_cells_occupied.rows, function(i, row) {
|
955
|
+
if ($.inArray(row, new_cells_occupied.rows) === -1) {
|
956
|
+
empty_rows.push(row);
|
957
|
+
}
|
958
|
+
});
|
959
|
+
|
960
|
+
var occupied_rows = [];
|
961
|
+
$.each(new_cells_occupied.rows, function(i, row) {
|
962
|
+
if ($.inArray(row, old_cells_occupied.rows) === -1) {
|
963
|
+
occupied_rows.push(row);
|
964
|
+
}
|
965
|
+
});
|
966
|
+
|
967
|
+
this.remove_from_gridmap(wgd);
|
968
|
+
|
969
|
+
if (occupied_cols.length) {
|
970
|
+
var cols_to_empty = [
|
971
|
+
new_col, wgd.row, size_x, Math.min(old_size_y, size_y), $widget
|
972
|
+
];
|
973
|
+
this.empty_cells.apply(this, cols_to_empty);
|
974
|
+
}
|
975
|
+
|
976
|
+
if (occupied_rows.length) {
|
977
|
+
var rows_to_empty = [new_col, wgd.row, size_x, size_y, $widget];
|
978
|
+
this.empty_cells.apply(this, rows_to_empty);
|
979
|
+
}
|
980
|
+
|
981
|
+
wgd.col = new_col;
|
982
|
+
wgd.size_x = size_x;
|
983
|
+
wgd.size_y = size_y;
|
984
|
+
this.add_to_gridmap(new_grid_data, $widget);
|
985
|
+
|
986
|
+
//update coords instance attributes
|
987
|
+
$widget.data('coords').update({
|
988
|
+
width: (size_x * this.options.widget_base_dimensions[0] +
|
989
|
+
((size_x - 1) * this.options.widget_margins[0]) * 2),
|
990
|
+
height: (size_y * this.options.widget_base_dimensions[1] +
|
991
|
+
((size_y - 1) * this.options.widget_margins[1]) * 2)
|
992
|
+
});
|
993
|
+
|
994
|
+
if (size_y > old_size_y) {
|
995
|
+
this.add_faux_rows(size_y - old_size_y);
|
996
|
+
}
|
997
|
+
|
998
|
+
if (size_x > old_size_x) {
|
999
|
+
this.add_faux_cols(size_x - old_size_x);
|
1000
|
+
}
|
1001
|
+
|
1002
|
+
$widget.attr({
|
1003
|
+
'data-col': new_col,
|
1004
|
+
'data-sizex': size_x,
|
1005
|
+
'data-sizey': size_y
|
1006
|
+
});
|
1007
|
+
|
1008
|
+
if (empty_cols.length) {
|
1009
|
+
var cols_to_remove_holes = [
|
1010
|
+
empty_cols[0], wgd.row,
|
1011
|
+
empty_cols.length,
|
1012
|
+
Math.min(old_size_y, size_y),
|
1013
|
+
$widget
|
1014
|
+
];
|
1015
|
+
|
1016
|
+
this.remove_empty_cells.apply(this, cols_to_remove_holes);
|
1017
|
+
}
|
1018
|
+
|
1019
|
+
if (empty_rows.length) {
|
1020
|
+
var rows_to_remove_holes = [
|
1021
|
+
new_col, wgd.row, size_x, size_y, $widget
|
1022
|
+
];
|
1023
|
+
this.remove_empty_cells.apply(this, rows_to_remove_holes);
|
1024
|
+
}
|
1025
|
+
|
1026
|
+
return $widget;
|
1027
|
+
};
|
1028
|
+
|
1029
|
+
/**
|
1030
|
+
* Move down widgets in cells represented by the arguments col, row, size_x,
|
1031
|
+
* size_y
|
1032
|
+
*
|
1033
|
+
* @method empty_cells
|
1034
|
+
* @param {Number} col The column where the group of cells begin.
|
1035
|
+
* @param {Number} row The row where the group of cells begin.
|
1036
|
+
* @param {Number} size_x The number of columns that the group of cells
|
1037
|
+
* occupy.
|
1038
|
+
* @param {Number} size_y The number of rows that the group of cells
|
1039
|
+
* occupy.
|
1040
|
+
* @param {HTMLElement} $exclude Exclude widgets from being moved.
|
1041
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
1042
|
+
*/
|
1043
|
+
fn.empty_cells = function(col, row, size_x, size_y, $exclude) {
|
1044
|
+
var $nexts = this.widgets_below({
|
1045
|
+
col: col,
|
1046
|
+
row: row - size_y,
|
1047
|
+
size_x: size_x,
|
1048
|
+
size_y: size_y
|
1049
|
+
});
|
1050
|
+
|
1051
|
+
$nexts.not($exclude).each($.proxy(function(i, w) {
|
1052
|
+
var wgd = $(w).coords().grid;
|
1053
|
+
if (!(wgd.row <= (row + size_y - 1))) { return; }
|
1054
|
+
var diff = (row + size_y) - wgd.row;
|
1055
|
+
this.move_widget_down($(w), diff);
|
1056
|
+
}, this));
|
1057
|
+
|
1058
|
+
this.set_dom_grid_height();
|
1059
|
+
|
1060
|
+
return this;
|
1061
|
+
};
|
1062
|
+
|
1063
|
+
|
1064
|
+
/**
|
1065
|
+
* Move up widgets below cells represented by the arguments col, row, size_x,
|
1066
|
+
* size_y.
|
1067
|
+
*
|
1068
|
+
* @method remove_empty_cells
|
1069
|
+
* @param {Number} col The column where the group of cells begin.
|
1070
|
+
* @param {Number} row The row where the group of cells begin.
|
1071
|
+
* @param {Number} size_x The number of columns that the group of cells
|
1072
|
+
* occupy.
|
1073
|
+
* @param {Number} size_y The number of rows that the group of cells
|
1074
|
+
* occupy.
|
1075
|
+
* @param {HTMLElement} exclude Exclude widgets from being moved.
|
1076
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
1077
|
+
*/
|
1078
|
+
fn.remove_empty_cells = function(col, row, size_x, size_y, exclude) {
|
1079
|
+
var $nexts = this.widgets_below({
|
1080
|
+
col: col,
|
1081
|
+
row: row,
|
1082
|
+
size_x: size_x,
|
1083
|
+
size_y: size_y
|
1084
|
+
});
|
1085
|
+
|
1086
|
+
$nexts.not(exclude).each($.proxy(function(i, widget) {
|
1087
|
+
this.move_widget_up( $(widget), size_y );
|
1088
|
+
}, this));
|
1089
|
+
|
1090
|
+
this.set_dom_grid_height();
|
1091
|
+
|
1092
|
+
return this;
|
1093
|
+
};
|
1094
|
+
|
1095
|
+
|
1096
|
+
/**
|
1097
|
+
* Get the most left column below to add a new widget.
|
1098
|
+
*
|
1099
|
+
* @method next_position
|
1100
|
+
* @param {Number} size_x The nº of rows the widget occupies horizontally.
|
1101
|
+
* @param {Number} size_y The nº of columns the widget occupies vertically.
|
1102
|
+
* @return {Object} Returns a grid coords object representing the future
|
1103
|
+
* widget coords.
|
1104
|
+
*/
|
1105
|
+
fn.next_position = function(size_x, size_y) {
|
1106
|
+
size_x || (size_x = 1);
|
1107
|
+
size_y || (size_y = 1);
|
1108
|
+
var ga = this.gridmap;
|
1109
|
+
var cols_l = ga.length;
|
1110
|
+
var valid_pos = [];
|
1111
|
+
var rows_l;
|
1112
|
+
|
1113
|
+
for (var c = 1; c < cols_l; c++) {
|
1114
|
+
rows_l = ga[c].length;
|
1115
|
+
for (var r = 1; r <= rows_l; r++) {
|
1116
|
+
var can_move_to = this.can_move_to({
|
1117
|
+
size_x: size_x,
|
1118
|
+
size_y: size_y
|
1119
|
+
}, c, r);
|
1120
|
+
|
1121
|
+
if (can_move_to) {
|
1122
|
+
valid_pos.push({
|
1123
|
+
col: c,
|
1124
|
+
row: r,
|
1125
|
+
size_y: size_y,
|
1126
|
+
size_x: size_x
|
1127
|
+
});
|
1128
|
+
}
|
1129
|
+
}
|
1130
|
+
}
|
1131
|
+
|
1132
|
+
if (valid_pos.length) {
|
1133
|
+
return this.sort_by_row_and_col_asc(valid_pos)[0];
|
1134
|
+
}
|
1135
|
+
return false;
|
1136
|
+
};
|
1137
|
+
|
1138
|
+
|
1139
|
+
/**
|
1140
|
+
* Remove a widget from the grid.
|
1141
|
+
*
|
1142
|
+
* @method remove_widget
|
1143
|
+
* @param {HTMLElement} el The jQuery wrapped HTMLElement you want to remove.
|
1144
|
+
* @param {Boolean|Function} silent If true, widgets below the removed one
|
1145
|
+
* will not move up. If a Function is passed it will be used as callback.
|
1146
|
+
* @param {Function} callback Function executed when the widget is removed.
|
1147
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
1148
|
+
*/
|
1149
|
+
fn.remove_widget = function(el, silent, callback) {
|
1150
|
+
var $el = el instanceof jQuery ? el : $(el);
|
1151
|
+
var wgd = $el.coords().grid;
|
1152
|
+
|
1153
|
+
// if silent is a function assume it's a callback
|
1154
|
+
if ($.isFunction(silent)) {
|
1155
|
+
callback = silent;
|
1156
|
+
silent = false;
|
1157
|
+
}
|
1158
|
+
|
1159
|
+
this.cells_occupied_by_placeholder = {};
|
1160
|
+
this.$widgets = this.$widgets.not($el);
|
1161
|
+
|
1162
|
+
var $nexts = this.widgets_below($el);
|
1163
|
+
|
1164
|
+
this.remove_from_gridmap(wgd);
|
1165
|
+
|
1166
|
+
$el.fadeOut($.proxy(function() {
|
1167
|
+
$el.remove();
|
1168
|
+
|
1169
|
+
if (!silent) {
|
1170
|
+
$nexts.each($.proxy(function(i, widget) {
|
1171
|
+
this.move_widget_up( $(widget), wgd.size_y );
|
1172
|
+
}, this));
|
1173
|
+
}
|
1174
|
+
|
1175
|
+
this.set_dom_grid_height();
|
1176
|
+
|
1177
|
+
if (callback) {
|
1178
|
+
callback.call(this, el);
|
1179
|
+
}
|
1180
|
+
}, this));
|
1181
|
+
};
|
1182
|
+
|
1183
|
+
|
1184
|
+
/**
|
1185
|
+
* Remove all widgets from the grid.
|
1186
|
+
*
|
1187
|
+
* @method remove_all_widgets
|
1188
|
+
* @param {Function} callback Function executed for each widget removed.
|
1189
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
1190
|
+
*/
|
1191
|
+
fn.remove_all_widgets = function(callback) {
|
1192
|
+
this.$widgets.each($.proxy(function(i, el){
|
1193
|
+
this.remove_widget(el, true, callback);
|
1194
|
+
}, this));
|
1195
|
+
|
1196
|
+
return this;
|
1197
|
+
};
|
1198
|
+
|
1199
|
+
|
1200
|
+
/**
|
1201
|
+
* Returns a serialized array of the widgets in the grid.
|
1202
|
+
*
|
1203
|
+
* @method serialize
|
1204
|
+
* @param {HTMLElement} [$widgets] The collection of jQuery wrapped
|
1205
|
+
* HTMLElements you want to serialize. If no argument is passed all widgets
|
1206
|
+
* will be serialized.
|
1207
|
+
* @return {Array} Returns an Array of Objects with the data specified in
|
1208
|
+
* the serialize_params option.
|
1209
|
+
*/
|
1210
|
+
fn.serialize = function($widgets) {
|
1211
|
+
$widgets || ($widgets = this.$widgets);
|
1212
|
+
var result = [];
|
1213
|
+
$widgets.each($.proxy(function(i, widget) {
|
1214
|
+
result.push(this.options.serialize_params(
|
1215
|
+
$(widget), $(widget).coords().grid ) );
|
1216
|
+
}, this));
|
1217
|
+
|
1218
|
+
return result;
|
1219
|
+
};
|
1220
|
+
|
1221
|
+
|
1222
|
+
/**
|
1223
|
+
* Returns a serialized array of the widgets that have changed their
|
1224
|
+
* position.
|
1225
|
+
*
|
1226
|
+
* @method serialize_changed
|
1227
|
+
* @return {Array} Returns an Array of Objects with the data specified in
|
1228
|
+
* the serialize_params option.
|
1229
|
+
*/
|
1230
|
+
fn.serialize_changed = function() {
|
1231
|
+
return this.serialize(this.$changed);
|
1232
|
+
};
|
1233
|
+
|
1234
|
+
|
1235
|
+
/**
|
1236
|
+
* Creates the grid coords object representing the widget a add it to the
|
1237
|
+
* mapped array of positions.
|
1238
|
+
*
|
1239
|
+
* @method register_widget
|
1240
|
+
* @return {Array} Returns the instance of the Gridster class.
|
1241
|
+
*/
|
1242
|
+
fn.register_widget = function($el) {
|
1243
|
+
|
1244
|
+
var wgd = {
|
1245
|
+
'col': parseInt($el.attr('data-col'), 10),
|
1246
|
+
'row': parseInt($el.attr('data-row'), 10),
|
1247
|
+
'size_x': parseInt($el.attr('data-sizex'), 10),
|
1248
|
+
'size_y': parseInt($el.attr('data-sizey'), 10),
|
1249
|
+
'el': $el
|
1250
|
+
};
|
1251
|
+
|
1252
|
+
if (this.options.avoid_overlapped_widgets &&
|
1253
|
+
!this.can_move_to(
|
1254
|
+
{size_x: wgd.size_x, size_y: wgd.size_y}, wgd.col, wgd.row)
|
1255
|
+
) {
|
1256
|
+
wgd = this.next_position(wgd.size_x, wgd.size_y);
|
1257
|
+
wgd.el = $el;
|
1258
|
+
$el.attr({
|
1259
|
+
'data-col': wgd.col,
|
1260
|
+
'data-row': wgd.row,
|
1261
|
+
'data-sizex': wgd.size_x,
|
1262
|
+
'data-sizey': wgd.size_y
|
1263
|
+
});
|
1264
|
+
}
|
1265
|
+
|
1266
|
+
// attach Coord object to player data-coord attribute
|
1267
|
+
$el.data('coords', $el.coords());
|
1268
|
+
|
1269
|
+
// Extend Coord object with grid position info
|
1270
|
+
$el.data('coords').grid = wgd;
|
1271
|
+
|
1272
|
+
this.add_to_gridmap(wgd, $el);
|
1273
|
+
|
1274
|
+
return this;
|
1275
|
+
};
|
1276
|
+
|
1277
|
+
|
1278
|
+
/**
|
1279
|
+
* Update in the mapped array of positions the value of cells represented by
|
1280
|
+
* the grid coords object passed in the `grid_data` param.
|
1281
|
+
*
|
1282
|
+
* @param {Object} grid_data The grid coords object representing the cells
|
1283
|
+
* to update in the mapped array.
|
1284
|
+
* @param {HTMLElement|Boolean} value Pass `false` or the jQuery wrapped
|
1285
|
+
* HTMLElement, depends if you want to delete an existing position or add
|
1286
|
+
* a new one.
|
1287
|
+
* @method update_widget_position
|
1288
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
1289
|
+
*/
|
1290
|
+
fn.update_widget_position = function(grid_data, value) {
|
1291
|
+
this.for_each_cell_occupied(grid_data, function(col, row) {
|
1292
|
+
if (!this.gridmap[col]) { return this; }
|
1293
|
+
this.gridmap[col][row] = value;
|
1294
|
+
});
|
1295
|
+
return this;
|
1296
|
+
};
|
1297
|
+
|
1298
|
+
|
1299
|
+
/**
|
1300
|
+
* Remove a widget from the mapped array of positions.
|
1301
|
+
*
|
1302
|
+
* @method remove_from_gridmap
|
1303
|
+
* @param {Object} grid_data The grid coords object representing the cells
|
1304
|
+
* to update in the mapped array.
|
1305
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
1306
|
+
*/
|
1307
|
+
fn.remove_from_gridmap = function(grid_data) {
|
1308
|
+
return this.update_widget_position(grid_data, false);
|
1309
|
+
};
|
1310
|
+
|
1311
|
+
|
1312
|
+
/**
|
1313
|
+
* Add a widget to the mapped array of positions.
|
1314
|
+
*
|
1315
|
+
* @method add_to_gridmap
|
1316
|
+
* @param {Object} grid_data The grid coords object representing the cells
|
1317
|
+
* to update in the mapped array.
|
1318
|
+
* @param {HTMLElement|Boolean} value The value to set in the specified
|
1319
|
+
* position .
|
1320
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
1321
|
+
*/
|
1322
|
+
fn.add_to_gridmap = function(grid_data, value) {
|
1323
|
+
this.update_widget_position(grid_data, value || grid_data.el);
|
1324
|
+
|
1325
|
+
if (grid_data.el) {
|
1326
|
+
var $widgets = this.widgets_below(grid_data.el);
|
1327
|
+
$widgets.each($.proxy(function(i, widget) {
|
1328
|
+
this.move_widget_up( $(widget));
|
1329
|
+
}, this));
|
1330
|
+
}
|
1331
|
+
};
|
1332
|
+
|
1333
|
+
|
1334
|
+
/**
|
1335
|
+
* Make widgets draggable.
|
1336
|
+
*
|
1337
|
+
* @uses Draggable
|
1338
|
+
* @method draggable
|
1339
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
1340
|
+
*/
|
1341
|
+
fn.draggable = function() {
|
1342
|
+
var self = this;
|
1343
|
+
var draggable_options = $.extend(true, {}, this.options.draggable, {
|
1344
|
+
offset_left: this.options.widget_margins[0],
|
1345
|
+
start: function(event, ui) {
|
1346
|
+
self.$widgets.filter('.player-revert')
|
1347
|
+
.removeClass('player-revert');
|
1348
|
+
|
1349
|
+
self.$player = $(this);
|
1350
|
+
self.$helper = self.options.draggable.helper === 'clone' ?
|
1351
|
+
$(ui.helper) : self.$player;
|
1352
|
+
self.helper = !self.$helper.is(self.$player);
|
1353
|
+
|
1354
|
+
self.on_start_drag.call(self, event, ui);
|
1355
|
+
self.$el.trigger('gridster:dragstart');
|
1356
|
+
},
|
1357
|
+
stop: function(event, ui) {
|
1358
|
+
self.on_stop_drag.call(self, event, ui);
|
1359
|
+
self.$el.trigger('gridster:dragstop');
|
1360
|
+
},
|
1361
|
+
drag: throttle(function(event, ui) {
|
1362
|
+
self.on_drag.call(self, event, ui);
|
1363
|
+
self.$el.trigger('gridster:drag');
|
1364
|
+
}, 60)
|
1365
|
+
});
|
1366
|
+
|
1367
|
+
this.drag_api = this.$el.drag(draggable_options).data('drag');
|
1368
|
+
return this;
|
1369
|
+
};
|
1370
|
+
|
1371
|
+
|
1372
|
+
/**
|
1373
|
+
* This function is executed when the player begins to be dragged.
|
1374
|
+
*
|
1375
|
+
* @method on_start_drag
|
1376
|
+
* @param {Event} event The original browser event
|
1377
|
+
* @param {Object} ui A prepared ui object.
|
1378
|
+
*/
|
1379
|
+
fn.on_start_drag = function(event, ui) {
|
1380
|
+
|
1381
|
+
this.$helper.add(this.$player).add(this.$wrapper).addClass('dragging');
|
1382
|
+
|
1383
|
+
this.$player.addClass('player');
|
1384
|
+
this.player_grid_data = this.$player.coords().grid;
|
1385
|
+
this.placeholder_grid_data = $.extend({}, this.player_grid_data);
|
1386
|
+
|
1387
|
+
//set new grid height along the dragging period
|
1388
|
+
this.$el.css('height', this.$el.height() +
|
1389
|
+
(this.player_grid_data.size_y * this.min_widget_height));
|
1390
|
+
|
1391
|
+
var colliders = this.faux_grid;
|
1392
|
+
var coords = this.$player.data('coords').coords;
|
1393
|
+
|
1394
|
+
this.cells_occupied_by_player = this.get_cells_occupied(
|
1395
|
+
this.player_grid_data);
|
1396
|
+
this.cells_occupied_by_placeholder = this.get_cells_occupied(
|
1397
|
+
this.placeholder_grid_data);
|
1398
|
+
|
1399
|
+
this.last_cols = [];
|
1400
|
+
this.last_rows = [];
|
1401
|
+
|
1402
|
+
|
1403
|
+
// see jquery.collision.js
|
1404
|
+
this.collision_api = this.$helper.collision(
|
1405
|
+
colliders, this.options.collision);
|
1406
|
+
|
1407
|
+
this.$preview_holder = $('<li />', {
|
1408
|
+
'class': 'preview-holder',
|
1409
|
+
'data-row': this.$player.attr('data-row'),
|
1410
|
+
'data-col': this.$player.attr('data-col'),
|
1411
|
+
css: {
|
1412
|
+
width: coords.width,
|
1413
|
+
height: coords.height
|
1414
|
+
}
|
1415
|
+
}).appendTo(this.$el);
|
1416
|
+
|
1417
|
+
if (this.options.draggable.start) {
|
1418
|
+
this.options.draggable.start.call(this, event, ui);
|
1419
|
+
}
|
1420
|
+
};
|
1421
|
+
|
1422
|
+
|
1423
|
+
/**
|
1424
|
+
* This function is executed when the player is being dragged.
|
1425
|
+
*
|
1426
|
+
* @method on_drag
|
1427
|
+
* @param {Event} event The original browser event
|
1428
|
+
* @param {Object} ui A prepared ui object.
|
1429
|
+
*/
|
1430
|
+
fn.on_drag = function(event, ui) {
|
1431
|
+
//break if dragstop has been fired
|
1432
|
+
if (this.$player === null) {
|
1433
|
+
return false;
|
1434
|
+
}
|
1435
|
+
|
1436
|
+
var abs_offset = {
|
1437
|
+
left: ui.position.left + this.baseX,
|
1438
|
+
top: ui.position.top + this.baseY
|
1439
|
+
};
|
1440
|
+
|
1441
|
+
this.colliders_data = this.collision_api.get_closest_colliders(
|
1442
|
+
abs_offset);
|
1443
|
+
|
1444
|
+
this.on_overlapped_column_change(
|
1445
|
+
this.on_start_overlapping_column,
|
1446
|
+
this.on_stop_overlapping_column
|
1447
|
+
);
|
1448
|
+
|
1449
|
+
this.on_overlapped_row_change(
|
1450
|
+
this.on_start_overlapping_row,
|
1451
|
+
this.on_stop_overlapping_row
|
1452
|
+
);
|
1453
|
+
|
1454
|
+
if (this.helper && this.$player) {
|
1455
|
+
this.$player.css({
|
1456
|
+
'left': ui.position.left,
|
1457
|
+
'top': ui.position.top
|
1458
|
+
});
|
1459
|
+
}
|
1460
|
+
|
1461
|
+
if (this.options.draggable.drag) {
|
1462
|
+
this.options.draggable.drag.call(this, event, ui);
|
1463
|
+
}
|
1464
|
+
};
|
1465
|
+
|
1466
|
+
/**
|
1467
|
+
* This function is executed when the player stops being dragged.
|
1468
|
+
*
|
1469
|
+
* @method on_stop_drag
|
1470
|
+
* @param {Event} event The original browser event
|
1471
|
+
* @param {Object} ui A prepared ui object.
|
1472
|
+
*/
|
1473
|
+
fn.on_stop_drag = function(event, ui) {
|
1474
|
+
this.$helper.add(this.$player).add(this.$wrapper)
|
1475
|
+
.removeClass('dragging');
|
1476
|
+
|
1477
|
+
ui.position.left = ui.position.left + this.baseX;
|
1478
|
+
ui.position.top = ui.position.top + this.baseY;
|
1479
|
+
this.colliders_data = this.collision_api.get_closest_colliders(ui.position);
|
1480
|
+
|
1481
|
+
this.on_overlapped_column_change(
|
1482
|
+
this.on_start_overlapping_column,
|
1483
|
+
this.on_stop_overlapping_column
|
1484
|
+
);
|
1485
|
+
|
1486
|
+
this.on_overlapped_row_change(
|
1487
|
+
this.on_start_overlapping_row,
|
1488
|
+
this.on_stop_overlapping_row
|
1489
|
+
);
|
1490
|
+
|
1491
|
+
this.$player.addClass('player-revert').removeClass('player')
|
1492
|
+
.attr({
|
1493
|
+
'data-col': this.placeholder_grid_data.col,
|
1494
|
+
'data-row': this.placeholder_grid_data.row
|
1495
|
+
}).css({
|
1496
|
+
'left': '',
|
1497
|
+
'top': ''
|
1498
|
+
});
|
1499
|
+
|
1500
|
+
this.$changed = this.$changed.add(this.$player);
|
1501
|
+
|
1502
|
+
this.cells_occupied_by_player = this.get_cells_occupied(
|
1503
|
+
this.placeholder_grid_data);
|
1504
|
+
this.set_cells_player_occupies(
|
1505
|
+
this.placeholder_grid_data.col, this.placeholder_grid_data.row);
|
1506
|
+
|
1507
|
+
this.$player.coords().grid.row = this.placeholder_grid_data.row;
|
1508
|
+
this.$player.coords().grid.col = this.placeholder_grid_data.col;
|
1509
|
+
|
1510
|
+
if (this.options.draggable.stop) {
|
1511
|
+
this.options.draggable.stop.call(this, event, ui);
|
1512
|
+
}
|
1513
|
+
|
1514
|
+
this.$preview_holder.remove();
|
1515
|
+
|
1516
|
+
this.$player = null;
|
1517
|
+
this.$helper = null;
|
1518
|
+
this.placeholder_grid_data = {};
|
1519
|
+
this.player_grid_data = {};
|
1520
|
+
this.cells_occupied_by_placeholder = {};
|
1521
|
+
this.cells_occupied_by_player = {};
|
1522
|
+
|
1523
|
+
this.set_dom_grid_height();
|
1524
|
+
};
|
1525
|
+
|
1526
|
+
|
1527
|
+
/**
|
1528
|
+
* Executes the callbacks passed as arguments when a column begins to be
|
1529
|
+
* overlapped or stops being overlapped.
|
1530
|
+
*
|
1531
|
+
* @param {Function} start_callback Function executed when a new column
|
1532
|
+
* begins to be overlapped. The column is passed as first argument.
|
1533
|
+
* @param {Function} stop_callback Function executed when a column stops
|
1534
|
+
* being overlapped. The column is passed as first argument.
|
1535
|
+
* @method on_overlapped_column_change
|
1536
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
1537
|
+
*/
|
1538
|
+
fn.on_overlapped_column_change = function(start_callback, stop_callback) {
|
1539
|
+
if (!this.colliders_data.length) {
|
1540
|
+
return this;
|
1541
|
+
}
|
1542
|
+
var cols = this.get_targeted_columns(
|
1543
|
+
this.colliders_data[0].el.data.col);
|
1544
|
+
|
1545
|
+
var last_n_cols = this.last_cols.length;
|
1546
|
+
var n_cols = cols.length;
|
1547
|
+
var i;
|
1548
|
+
|
1549
|
+
for (i = 0; i < n_cols; i++) {
|
1550
|
+
if ($.inArray(cols[i], this.last_cols) === -1) {
|
1551
|
+
(start_callback || $.noop).call(this, cols[i]);
|
1552
|
+
}
|
1553
|
+
}
|
1554
|
+
|
1555
|
+
for (i = 0; i< last_n_cols; i++) {
|
1556
|
+
if ($.inArray(this.last_cols[i], cols) === -1) {
|
1557
|
+
(stop_callback || $.noop).call(this, this.last_cols[i]);
|
1558
|
+
}
|
1559
|
+
}
|
1560
|
+
|
1561
|
+
this.last_cols = cols;
|
1562
|
+
|
1563
|
+
return this;
|
1564
|
+
};
|
1565
|
+
|
1566
|
+
|
1567
|
+
/**
|
1568
|
+
* Executes the callbacks passed as arguments when a row starts to be
|
1569
|
+
* overlapped or stops being overlapped.
|
1570
|
+
*
|
1571
|
+
* @param {Function} start_callback Function executed when a new row begins
|
1572
|
+
* to be overlapped. The row is passed as first argument.
|
1573
|
+
* @param {Function} end_callback Function executed when a row stops being
|
1574
|
+
* overlapped. The row is passed as first argument.
|
1575
|
+
* @method on_overlapped_row_change
|
1576
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
1577
|
+
*/
|
1578
|
+
fn.on_overlapped_row_change = function(start_callback, end_callback) {
|
1579
|
+
if (!this.colliders_data.length) {
|
1580
|
+
return this;
|
1581
|
+
}
|
1582
|
+
var rows = this.get_targeted_rows(this.colliders_data[0].el.data.row);
|
1583
|
+
var last_n_rows = this.last_rows.length;
|
1584
|
+
var n_rows = rows.length;
|
1585
|
+
var i;
|
1586
|
+
|
1587
|
+
for (i = 0; i < n_rows; i++) {
|
1588
|
+
if ($.inArray(rows[i], this.last_rows) === -1) {
|
1589
|
+
(start_callback || $.noop).call(this, rows[i]);
|
1590
|
+
}
|
1591
|
+
}
|
1592
|
+
|
1593
|
+
for (i = 0; i < last_n_rows; i++) {
|
1594
|
+
if ($.inArray(this.last_rows[i], rows) === -1) {
|
1595
|
+
(end_callback || $.noop).call(this, this.last_rows[i]);
|
1596
|
+
}
|
1597
|
+
}
|
1598
|
+
|
1599
|
+
this.last_rows = rows;
|
1600
|
+
};
|
1601
|
+
|
1602
|
+
|
1603
|
+
/**
|
1604
|
+
* Sets the current position of the player
|
1605
|
+
*
|
1606
|
+
* @param {Number} col
|
1607
|
+
* @param {Number} row
|
1608
|
+
* @param {Boolean} no_player
|
1609
|
+
* @method set_player
|
1610
|
+
* @return {object}
|
1611
|
+
*/
|
1612
|
+
fn.set_player = function(col, row, no_player) {
|
1613
|
+
var self = this;
|
1614
|
+
if (!no_player) {
|
1615
|
+
this.empty_cells_player_occupies();
|
1616
|
+
}
|
1617
|
+
var cell = !no_player ? self.colliders_data[0].el.data : {col: col};
|
1618
|
+
var to_col = cell.col;
|
1619
|
+
var to_row = row || cell.row;
|
1620
|
+
|
1621
|
+
this.player_grid_data = {
|
1622
|
+
col: to_col,
|
1623
|
+
row: to_row,
|
1624
|
+
size_y : this.player_grid_data.size_y,
|
1625
|
+
size_x : this.player_grid_data.size_x
|
1626
|
+
};
|
1627
|
+
|
1628
|
+
this.cells_occupied_by_player = this.get_cells_occupied(
|
1629
|
+
this.player_grid_data);
|
1630
|
+
|
1631
|
+
var $overlapped_widgets = this.get_widgets_overlapped(
|
1632
|
+
this.player_grid_data);
|
1633
|
+
|
1634
|
+
var constraints = this.widgets_constraints($overlapped_widgets);
|
1635
|
+
|
1636
|
+
this.manage_movements(constraints.can_go_up, to_col, to_row);
|
1637
|
+
this.manage_movements(constraints.can_not_go_up, to_col, to_row);
|
1638
|
+
|
1639
|
+
/* if there is not widgets overlapping in the new player position,
|
1640
|
+
* update the new placeholder position. */
|
1641
|
+
if (!$overlapped_widgets.length) {
|
1642
|
+
var pp = this.can_go_player_up(this.player_grid_data);
|
1643
|
+
if (pp !== false) {
|
1644
|
+
to_row = pp;
|
1645
|
+
}
|
1646
|
+
this.set_placeholder(to_col, to_row);
|
1647
|
+
}
|
1648
|
+
|
1649
|
+
return {
|
1650
|
+
col: to_col,
|
1651
|
+
row: to_row
|
1652
|
+
};
|
1653
|
+
};
|
1654
|
+
|
1655
|
+
|
1656
|
+
/**
|
1657
|
+
* See which of the widgets in the $widgets param collection can go to
|
1658
|
+
* a upper row and which not.
|
1659
|
+
*
|
1660
|
+
* @method widgets_contraints
|
1661
|
+
* @param {jQuery} $widgets A jQuery wrapped collection of
|
1662
|
+
* HTMLElements.
|
1663
|
+
* @return {object} Returns a literal Object with two keys: `can_go_up` &
|
1664
|
+
* `can_not_go_up`. Each contains a set of HTMLElements.
|
1665
|
+
*/
|
1666
|
+
fn.widgets_constraints = function($widgets) {
|
1667
|
+
var $widgets_can_go_up = $([]);
|
1668
|
+
var $widgets_can_not_go_up;
|
1669
|
+
var wgd_can_go_up = [];
|
1670
|
+
var wgd_can_not_go_up = [];
|
1671
|
+
|
1672
|
+
$widgets.each($.proxy(function(i, w) {
|
1673
|
+
var $w = $(w);
|
1674
|
+
var wgd = $w.coords().grid;
|
1675
|
+
if (this.can_go_widget_up(wgd)) {
|
1676
|
+
$widgets_can_go_up = $widgets_can_go_up.add($w);
|
1677
|
+
wgd_can_go_up.push(wgd);
|
1678
|
+
}else{
|
1679
|
+
wgd_can_not_go_up.push(wgd);
|
1680
|
+
}
|
1681
|
+
}, this));
|
1682
|
+
|
1683
|
+
$widgets_can_not_go_up = $widgets.not($widgets_can_go_up);
|
1684
|
+
|
1685
|
+
return {
|
1686
|
+
can_go_up: this.sort_by_row_asc(wgd_can_go_up),
|
1687
|
+
can_not_go_up: this.sort_by_row_desc(wgd_can_not_go_up)
|
1688
|
+
};
|
1689
|
+
};
|
1690
|
+
|
1691
|
+
|
1692
|
+
/**
|
1693
|
+
* Sorts an Array of grid coords objects (representing the grid coords of
|
1694
|
+
* each widget) in ascending way.
|
1695
|
+
*
|
1696
|
+
* @method sort_by_row_asc
|
1697
|
+
* @param {Array} widgets Array of grid coords objects
|
1698
|
+
* @return {Array} Returns the array sorted.
|
1699
|
+
*/
|
1700
|
+
fn.sort_by_row_asc = function(widgets) {
|
1701
|
+
widgets = widgets.sort(function(a, b) {
|
1702
|
+
if (!a.row) {
|
1703
|
+
a = $(a).coords().grid;
|
1704
|
+
b = $(b).coords().grid;
|
1705
|
+
}
|
1706
|
+
|
1707
|
+
if (a.row > b.row) {
|
1708
|
+
return 1;
|
1709
|
+
}
|
1710
|
+
return -1;
|
1711
|
+
});
|
1712
|
+
|
1713
|
+
return widgets;
|
1714
|
+
};
|
1715
|
+
|
1716
|
+
|
1717
|
+
/**
|
1718
|
+
* Sorts an Array of grid coords objects (representing the grid coords of
|
1719
|
+
* each widget) placing first the empty cells upper left.
|
1720
|
+
*
|
1721
|
+
* @method sort_by_row_and_col_asc
|
1722
|
+
* @param {Array} widgets Array of grid coords objects
|
1723
|
+
* @return {Array} Returns the array sorted.
|
1724
|
+
*/
|
1725
|
+
fn.sort_by_row_and_col_asc = function(widgets) {
|
1726
|
+
widgets = widgets.sort(function(a, b) {
|
1727
|
+
if (a.row > b.row || a.row === b.row && a.col > b.col) {
|
1728
|
+
return 1;
|
1729
|
+
}
|
1730
|
+
return -1;
|
1731
|
+
});
|
1732
|
+
|
1733
|
+
return widgets;
|
1734
|
+
};
|
1735
|
+
|
1736
|
+
|
1737
|
+
/**
|
1738
|
+
* Sorts an Array of grid coords objects by column (representing the grid
|
1739
|
+
* coords of each widget) in ascending way.
|
1740
|
+
*
|
1741
|
+
* @method sort_by_col_asc
|
1742
|
+
* @param {Array} widgets Array of grid coords objects
|
1743
|
+
* @return {Array} Returns the array sorted.
|
1744
|
+
*/
|
1745
|
+
fn.sort_by_col_asc = function(widgets) {
|
1746
|
+
widgets = widgets.sort(function(a, b) {
|
1747
|
+
if (a.col > b.col) {
|
1748
|
+
return 1;
|
1749
|
+
}
|
1750
|
+
return -1;
|
1751
|
+
});
|
1752
|
+
|
1753
|
+
return widgets;
|
1754
|
+
};
|
1755
|
+
|
1756
|
+
|
1757
|
+
/**
|
1758
|
+
* Sorts an Array of grid coords objects (representing the grid coords of
|
1759
|
+
* each widget) in descending way.
|
1760
|
+
*
|
1761
|
+
* @method sort_by_row_desc
|
1762
|
+
* @param {Array} widgets Array of grid coords objects
|
1763
|
+
* @return {Array} Returns the array sorted.
|
1764
|
+
*/
|
1765
|
+
fn.sort_by_row_desc = function(widgets) {
|
1766
|
+
widgets = widgets.sort(function(a, b) {
|
1767
|
+
if (a.row + a.size_y < b.row + b.size_y) {
|
1768
|
+
return 1;
|
1769
|
+
}
|
1770
|
+
return -1;
|
1771
|
+
});
|
1772
|
+
return widgets;
|
1773
|
+
};
|
1774
|
+
|
1775
|
+
|
1776
|
+
/**
|
1777
|
+
* Sorts an Array of grid coords objects (representing the grid coords of
|
1778
|
+
* each widget) in descending way.
|
1779
|
+
*
|
1780
|
+
* @method manage_movements
|
1781
|
+
* @param {jQuery} $widgets A jQuery collection of HTMLElements
|
1782
|
+
* representing the widgets you want to move.
|
1783
|
+
* @param {Number} to_col The column to which we want to move the widgets.
|
1784
|
+
* @param {Number} to_row The row to which we want to move the widgets.
|
1785
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
1786
|
+
*/
|
1787
|
+
fn.manage_movements = function($widgets, to_col, to_row) {
|
1788
|
+
$.each($widgets, $.proxy(function(i, w) {
|
1789
|
+
var wgd = w;
|
1790
|
+
var $w = wgd.el;
|
1791
|
+
|
1792
|
+
var can_go_widget_up = this.can_go_widget_up(wgd);
|
1793
|
+
|
1794
|
+
if (can_go_widget_up) {
|
1795
|
+
//target CAN go up
|
1796
|
+
//so move widget up
|
1797
|
+
this.move_widget_to($w, can_go_widget_up);
|
1798
|
+
this.set_placeholder(to_col, can_go_widget_up + wgd.size_y);
|
1799
|
+
|
1800
|
+
} else {
|
1801
|
+
//target can't go up
|
1802
|
+
var can_go_player_up = this.can_go_player_up(
|
1803
|
+
this.player_grid_data);
|
1804
|
+
|
1805
|
+
if (!can_go_player_up) {
|
1806
|
+
// target can't go up
|
1807
|
+
// player cant't go up
|
1808
|
+
// so we need to move widget down to a position that dont
|
1809
|
+
// overlaps player
|
1810
|
+
var y = (to_row + this.player_grid_data.size_y) - wgd.row;
|
1811
|
+
|
1812
|
+
this.move_widget_down($w, y);
|
1813
|
+
this.set_placeholder(to_col, to_row);
|
1814
|
+
}
|
1815
|
+
}
|
1816
|
+
}, this));
|
1817
|
+
|
1818
|
+
return this;
|
1819
|
+
};
|
1820
|
+
|
1821
|
+
/**
|
1822
|
+
* Determines if there is a widget in the row and col given. Or if the
|
1823
|
+
* HTMLElement passed as first argument is the player.
|
1824
|
+
*
|
1825
|
+
* @method is_player
|
1826
|
+
* @param {Number|HTMLElement} col_or_el A jQuery wrapped collection of
|
1827
|
+
* HTMLElements.
|
1828
|
+
* @param {Number} [row] The column to which we want to move the widgets.
|
1829
|
+
* @return {Boolean} Returns true or false.
|
1830
|
+
*/
|
1831
|
+
fn.is_player = function(col_or_el, row) {
|
1832
|
+
if (row && !this.gridmap[col_or_el]) { return false; }
|
1833
|
+
var $w = row ? this.gridmap[col_or_el][row] : col_or_el;
|
1834
|
+
return $w && ($w.is(this.$player) || $w.is(this.$helper));
|
1835
|
+
};
|
1836
|
+
|
1837
|
+
|
1838
|
+
/**
|
1839
|
+
* Determines if the widget that is being dragged is currently over the row
|
1840
|
+
* and col given.
|
1841
|
+
*
|
1842
|
+
* @method is_player_in
|
1843
|
+
* @param {Number} col The column to check.
|
1844
|
+
* @param {Number} row The row to check.
|
1845
|
+
* @return {Boolean} Returns true or false.
|
1846
|
+
*/
|
1847
|
+
fn.is_player_in = function(col, row) {
|
1848
|
+
var c = this.cells_occupied_by_player || {};
|
1849
|
+
return $.inArray(col, c.cols) >= 0 && $.inArray(row, c.rows) >= 0;
|
1850
|
+
};
|
1851
|
+
|
1852
|
+
|
1853
|
+
/**
|
1854
|
+
* Determines if the placeholder is currently over the row and col given.
|
1855
|
+
*
|
1856
|
+
* @method is_placeholder_in
|
1857
|
+
* @param {Number} col The column to check.
|
1858
|
+
* @param {Number} row The row to check.
|
1859
|
+
* @return {Boolean} Returns true or false.
|
1860
|
+
*/
|
1861
|
+
fn.is_placeholder_in = function(col, row) {
|
1862
|
+
var c = this.cells_occupied_by_placeholder || {};
|
1863
|
+
return this.is_placeholder_in_col(col) && $.inArray(row, c.rows) >= 0;
|
1864
|
+
};
|
1865
|
+
|
1866
|
+
|
1867
|
+
/**
|
1868
|
+
* Determines if the placeholder is currently over the column given.
|
1869
|
+
*
|
1870
|
+
* @method is_placeholder_in_col
|
1871
|
+
* @param {Number} col The column to check.
|
1872
|
+
* @return {Boolean} Returns true or false.
|
1873
|
+
*/
|
1874
|
+
fn.is_placeholder_in_col = function(col) {
|
1875
|
+
var c = this.cells_occupied_by_placeholder || [];
|
1876
|
+
return $.inArray(col, c.cols) >= 0;
|
1877
|
+
};
|
1878
|
+
|
1879
|
+
|
1880
|
+
/**
|
1881
|
+
* Determines if the cell represented by col and row params is empty.
|
1882
|
+
*
|
1883
|
+
* @method is_empty
|
1884
|
+
* @param {Number} col The column to check.
|
1885
|
+
* @param {Number} row The row to check.
|
1886
|
+
* @return {Boolean} Returns true or false.
|
1887
|
+
*/
|
1888
|
+
fn.is_empty = function(col, row) {
|
1889
|
+
if (typeof this.gridmap[col] !== 'undefined' &&
|
1890
|
+
typeof this.gridmap[col][row] !== 'undefined' &&
|
1891
|
+
this.gridmap[col][row] === false
|
1892
|
+
) {
|
1893
|
+
return true;
|
1894
|
+
}
|
1895
|
+
return false;
|
1896
|
+
};
|
1897
|
+
|
1898
|
+
|
1899
|
+
/**
|
1900
|
+
* Determines if the cell represented by col and row params is occupied.
|
1901
|
+
*
|
1902
|
+
* @method is_occupied
|
1903
|
+
* @param {Number} col The column to check.
|
1904
|
+
* @param {Number} row The row to check.
|
1905
|
+
* @return {Boolean} Returns true or false.
|
1906
|
+
*/
|
1907
|
+
fn.is_occupied = function(col, row) {
|
1908
|
+
if (!this.gridmap[col]) {
|
1909
|
+
return false;
|
1910
|
+
}
|
1911
|
+
|
1912
|
+
if (this.gridmap[col][row]) {
|
1913
|
+
return true;
|
1914
|
+
}
|
1915
|
+
return false;
|
1916
|
+
};
|
1917
|
+
|
1918
|
+
|
1919
|
+
/**
|
1920
|
+
* Determines if there is a widget in the cell represented by col/row params.
|
1921
|
+
*
|
1922
|
+
* @method is_widget
|
1923
|
+
* @param {Number} col The column to check.
|
1924
|
+
* @param {Number} row The row to check.
|
1925
|
+
* @return {Boolean|HTMLElement} Returns false if there is no widget,
|
1926
|
+
* else returns the jQuery HTMLElement
|
1927
|
+
*/
|
1928
|
+
fn.is_widget = function(col, row) {
|
1929
|
+
var cell = this.gridmap[col];
|
1930
|
+
if (!cell) {
|
1931
|
+
return false;
|
1932
|
+
}
|
1933
|
+
|
1934
|
+
cell = cell[row];
|
1935
|
+
|
1936
|
+
if (cell) {
|
1937
|
+
return cell;
|
1938
|
+
}
|
1939
|
+
|
1940
|
+
return false;
|
1941
|
+
};
|
1942
|
+
|
1943
|
+
|
1944
|
+
/**
|
1945
|
+
* Determines if there is a widget in the cell represented by col/row
|
1946
|
+
* params and if this is under the widget that is being dragged.
|
1947
|
+
*
|
1948
|
+
* @method is_widget_under_player
|
1949
|
+
* @param {Number} col The column to check.
|
1950
|
+
* @param {Number} row The row to check.
|
1951
|
+
* @return {Boolean} Returns true or false.
|
1952
|
+
*/
|
1953
|
+
fn.is_widget_under_player = function(col, row) {
|
1954
|
+
if (this.is_widget(col, row)) {
|
1955
|
+
return this.is_player_in(col, row);
|
1956
|
+
}
|
1957
|
+
return false;
|
1958
|
+
};
|
1959
|
+
|
1960
|
+
|
1961
|
+
/**
|
1962
|
+
* Get widgets overlapping with the player or with the object passed
|
1963
|
+
* representing the grid cells.
|
1964
|
+
*
|
1965
|
+
* @method get_widgets_under_player
|
1966
|
+
* @return {HTMLElement} Returns a jQuery collection of HTMLElements
|
1967
|
+
*/
|
1968
|
+
fn.get_widgets_under_player = function(cells) {
|
1969
|
+
cells || (cells = this.cells_occupied_by_player || {cols: [], rows: []});
|
1970
|
+
var $widgets = $([]);
|
1971
|
+
|
1972
|
+
$.each(cells.cols, $.proxy(function(i, col) {
|
1973
|
+
$.each(cells.rows, $.proxy(function(i, row) {
|
1974
|
+
if(this.is_widget(col, row)) {
|
1975
|
+
$widgets = $widgets.add(this.gridmap[col][row]);
|
1976
|
+
}
|
1977
|
+
}, this));
|
1978
|
+
}, this));
|
1979
|
+
|
1980
|
+
return $widgets;
|
1981
|
+
};
|
1982
|
+
|
1983
|
+
|
1984
|
+
/**
|
1985
|
+
* Put placeholder at the row and column specified.
|
1986
|
+
*
|
1987
|
+
* @method set_placeholder
|
1988
|
+
* @param {Number} col The column to which we want to move the
|
1989
|
+
* placeholder.
|
1990
|
+
* @param {Number} row The row to which we want to move the
|
1991
|
+
* placeholder.
|
1992
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
1993
|
+
*/
|
1994
|
+
fn.set_placeholder = function(col, row) {
|
1995
|
+
var phgd = $.extend({}, this.placeholder_grid_data);
|
1996
|
+
var $nexts = this.widgets_below({
|
1997
|
+
col: phgd.col,
|
1998
|
+
row: phgd.row,
|
1999
|
+
size_y: phgd.size_y,
|
2000
|
+
size_x: phgd.size_x
|
2001
|
+
});
|
2002
|
+
|
2003
|
+
// Prevents widgets go out of the grid
|
2004
|
+
var right_col = (col + phgd.size_x - 1);
|
2005
|
+
if (right_col > this.cols) {
|
2006
|
+
col = col - (right_col - col);
|
2007
|
+
}
|
2008
|
+
|
2009
|
+
var moved_down = this.placeholder_grid_data.row < row;
|
2010
|
+
var changed_column = this.placeholder_grid_data.col !== col;
|
2011
|
+
|
2012
|
+
this.placeholder_grid_data.col = col;
|
2013
|
+
this.placeholder_grid_data.row = row;
|
2014
|
+
|
2015
|
+
this.cells_occupied_by_placeholder = this.get_cells_occupied(
|
2016
|
+
this.placeholder_grid_data);
|
2017
|
+
|
2018
|
+
this.$preview_holder.attr({
|
2019
|
+
'data-row' : row,
|
2020
|
+
'data-col' : col
|
2021
|
+
});
|
2022
|
+
|
2023
|
+
if (moved_down || changed_column) {
|
2024
|
+
$nexts.each($.proxy(function(i, widget) {
|
2025
|
+
this.move_widget_up(
|
2026
|
+
$(widget), this.placeholder_grid_data.col - col + phgd.size_y);
|
2027
|
+
}, this));
|
2028
|
+
}
|
2029
|
+
|
2030
|
+
|
2031
|
+
var $widgets_under_ph = this.get_widgets_under_player(this.cells_occupied_by_placeholder);
|
2032
|
+
if ($widgets_under_ph.length) {
|
2033
|
+
$widgets_under_ph.each($.proxy(function(i, widget) {
|
2034
|
+
var $w = $(widget);
|
2035
|
+
this.move_widget_down(
|
2036
|
+
$w, row + phgd.size_y - $w.data('coords').grid.row);
|
2037
|
+
}, this));
|
2038
|
+
}
|
2039
|
+
|
2040
|
+
};
|
2041
|
+
|
2042
|
+
|
2043
|
+
/**
|
2044
|
+
* Determines whether the player can move to a position above.
|
2045
|
+
*
|
2046
|
+
* @method can_go_player_up
|
2047
|
+
* @param {Object} widget_grid_data The actual grid coords object of the
|
2048
|
+
* player.
|
2049
|
+
* @return {Number|Boolean} If the player can be moved to an upper row
|
2050
|
+
* returns the row number, else returns false.
|
2051
|
+
*/
|
2052
|
+
fn.can_go_player_up = function(widget_grid_data) {
|
2053
|
+
var p_bottom_row = widget_grid_data.row + widget_grid_data.size_y - 1;
|
2054
|
+
var result = true;
|
2055
|
+
var upper_rows = [];
|
2056
|
+
var min_row = 10000;
|
2057
|
+
var $widgets_under_player = this.get_widgets_under_player();
|
2058
|
+
|
2059
|
+
/* generate an array with columns as index and array with upper rows
|
2060
|
+
* empty as value */
|
2061
|
+
this.for_each_column_occupied(widget_grid_data, function(tcol) {
|
2062
|
+
var grid_col = this.gridmap[tcol];
|
2063
|
+
var r = p_bottom_row + 1;
|
2064
|
+
upper_rows[tcol] = [];
|
2065
|
+
|
2066
|
+
while (--r > 0) {
|
2067
|
+
if (this.is_empty(tcol, r) || this.is_player(tcol, r) ||
|
2068
|
+
this.is_widget(tcol, r) &&
|
2069
|
+
grid_col[r].is($widgets_under_player)
|
2070
|
+
) {
|
2071
|
+
upper_rows[tcol].push(r);
|
2072
|
+
min_row = r < min_row ? r : min_row;
|
2073
|
+
}else{
|
2074
|
+
break;
|
2075
|
+
}
|
2076
|
+
}
|
2077
|
+
|
2078
|
+
if (upper_rows[tcol].length === 0) {
|
2079
|
+
result = false;
|
2080
|
+
return true; //break
|
2081
|
+
}
|
2082
|
+
|
2083
|
+
upper_rows[tcol].sort();
|
2084
|
+
});
|
2085
|
+
|
2086
|
+
if (!result) { return false; }
|
2087
|
+
|
2088
|
+
return this.get_valid_rows(widget_grid_data, upper_rows, min_row);
|
2089
|
+
};
|
2090
|
+
|
2091
|
+
|
2092
|
+
/**
|
2093
|
+
* Determines whether a widget can move to a position above.
|
2094
|
+
*
|
2095
|
+
* @method can_go_widget_up
|
2096
|
+
* @param {Object} widget_grid_data The actual grid coords object of the
|
2097
|
+
* widget we want to check.
|
2098
|
+
* @return {Number|Boolean} If the widget can be moved to an upper row
|
2099
|
+
* returns the row number, else returns false.
|
2100
|
+
*/
|
2101
|
+
fn.can_go_widget_up = function(widget_grid_data) {
|
2102
|
+
var p_bottom_row = widget_grid_data.row + widget_grid_data.size_y - 1;
|
2103
|
+
var result = true;
|
2104
|
+
var upper_rows = [];
|
2105
|
+
var min_row = 10000;
|
2106
|
+
|
2107
|
+
/* generate an array with columns as index and array with topmost rows
|
2108
|
+
* empty as value */
|
2109
|
+
this.for_each_column_occupied(widget_grid_data, function(tcol) {
|
2110
|
+
var grid_col = this.gridmap[tcol];
|
2111
|
+
upper_rows[tcol] = [];
|
2112
|
+
|
2113
|
+
var r = p_bottom_row + 1;
|
2114
|
+
// iterate over each row
|
2115
|
+
while (--r > 0) {
|
2116
|
+
if (this.is_widget(tcol, r) && !this.is_player_in(tcol, r)) {
|
2117
|
+
if (!grid_col[r].is(widget_grid_data.el)) {
|
2118
|
+
break;
|
2119
|
+
}
|
2120
|
+
}
|
2121
|
+
|
2122
|
+
if (!this.is_player(tcol, r) &&
|
2123
|
+
!this.is_placeholder_in(tcol, r) &&
|
2124
|
+
!this.is_player_in(tcol, r)) {
|
2125
|
+
upper_rows[tcol].push(r);
|
2126
|
+
}
|
2127
|
+
|
2128
|
+
if (r < min_row) {
|
2129
|
+
min_row = r;
|
2130
|
+
}
|
2131
|
+
}
|
2132
|
+
|
2133
|
+
if (upper_rows[tcol].length === 0) {
|
2134
|
+
result = false;
|
2135
|
+
return true; //break
|
2136
|
+
}
|
2137
|
+
|
2138
|
+
upper_rows[tcol].sort();
|
2139
|
+
});
|
2140
|
+
|
2141
|
+
if (!result) { return false; }
|
2142
|
+
|
2143
|
+
return this.get_valid_rows(widget_grid_data, upper_rows, min_row);
|
2144
|
+
};
|
2145
|
+
|
2146
|
+
|
2147
|
+
/**
|
2148
|
+
* Search a valid row for the widget represented by `widget_grid_data' in
|
2149
|
+
* the `upper_rows` array. Iteration starts from row specified in `min_row`.
|
2150
|
+
*
|
2151
|
+
* @method get_valid_rows
|
2152
|
+
* @param {Object} widget_grid_data The actual grid coords object of the
|
2153
|
+
* player.
|
2154
|
+
* @param {Array} upper_rows An array with columns as index and arrays
|
2155
|
+
* of valid rows as values.
|
2156
|
+
* @param {Number} min_row The upper row from which the iteration will start.
|
2157
|
+
* @return {Number|Boolean} Returns the upper row valid from the `upper_rows`
|
2158
|
+
* for the widget in question.
|
2159
|
+
*/
|
2160
|
+
fn.get_valid_rows = function(widget_grid_data, upper_rows, min_row) {
|
2161
|
+
var p_top_row = widget_grid_data.row;
|
2162
|
+
var p_bottom_row = widget_grid_data.row + widget_grid_data.size_y - 1;
|
2163
|
+
var size_y = widget_grid_data.size_y;
|
2164
|
+
var r = min_row - 1;
|
2165
|
+
var valid_rows = [];
|
2166
|
+
|
2167
|
+
while (++r <= p_bottom_row ) {
|
2168
|
+
var common = true;
|
2169
|
+
$.each(upper_rows, function(col, rows) {
|
2170
|
+
if ($.isArray(rows) && $.inArray(r, rows) === -1) {
|
2171
|
+
common = false;
|
2172
|
+
}
|
2173
|
+
});
|
2174
|
+
|
2175
|
+
if (common === true) {
|
2176
|
+
valid_rows.push(r);
|
2177
|
+
if (valid_rows.length === size_y) {
|
2178
|
+
break;
|
2179
|
+
}
|
2180
|
+
}
|
2181
|
+
}
|
2182
|
+
|
2183
|
+
var new_row = false;
|
2184
|
+
if (size_y === 1) {
|
2185
|
+
if (valid_rows[0] !== p_top_row) {
|
2186
|
+
new_row = valid_rows[0] || false;
|
2187
|
+
}
|
2188
|
+
}else{
|
2189
|
+
if (valid_rows[0] !== p_top_row) {
|
2190
|
+
new_row = this.get_consecutive_numbers_index(
|
2191
|
+
valid_rows, size_y);
|
2192
|
+
}
|
2193
|
+
}
|
2194
|
+
|
2195
|
+
return new_row;
|
2196
|
+
};
|
2197
|
+
|
2198
|
+
|
2199
|
+
fn.get_consecutive_numbers_index = function(arr, size_y) {
|
2200
|
+
var max = arr.length;
|
2201
|
+
var result = [];
|
2202
|
+
var first = true;
|
2203
|
+
var prev = -1; // or null?
|
2204
|
+
|
2205
|
+
for (var i=0; i < max; i++) {
|
2206
|
+
if (first || arr[i] === prev + 1) {
|
2207
|
+
result.push(i);
|
2208
|
+
if (result.length === size_y) {
|
2209
|
+
break;
|
2210
|
+
}
|
2211
|
+
first = false;
|
2212
|
+
}else{
|
2213
|
+
result = [];
|
2214
|
+
first = true;
|
2215
|
+
}
|
2216
|
+
|
2217
|
+
prev = arr[i];
|
2218
|
+
}
|
2219
|
+
|
2220
|
+
return result.length >= size_y ? arr[result[0]] : false;
|
2221
|
+
};
|
2222
|
+
|
2223
|
+
|
2224
|
+
/**
|
2225
|
+
* Get widgets overlapping with the player.
|
2226
|
+
*
|
2227
|
+
* @method get_widgets_overlapped
|
2228
|
+
* @return {jQuery} Returns a jQuery collection of HTMLElements.
|
2229
|
+
*/
|
2230
|
+
fn.get_widgets_overlapped = function() {
|
2231
|
+
var $w;
|
2232
|
+
var $widgets = $([]);
|
2233
|
+
var used = [];
|
2234
|
+
var rows_from_bottom = this.cells_occupied_by_player.rows.slice(0);
|
2235
|
+
rows_from_bottom.reverse();
|
2236
|
+
|
2237
|
+
$.each(this.cells_occupied_by_player.cols, $.proxy(function(i, col) {
|
2238
|
+
$.each(rows_from_bottom, $.proxy(function(i, row) {
|
2239
|
+
// if there is a widget in the player position
|
2240
|
+
if (!this.gridmap[col]) { return true; } //next iteration
|
2241
|
+
var $w = this.gridmap[col][row];
|
2242
|
+
if (this.is_occupied(col, row) && !this.is_player($w) &&
|
2243
|
+
$.inArray($w, used) === -1
|
2244
|
+
) {
|
2245
|
+
$widgets = $widgets.add($w);
|
2246
|
+
used.push($w);
|
2247
|
+
}
|
2248
|
+
|
2249
|
+
}, this));
|
2250
|
+
}, this));
|
2251
|
+
|
2252
|
+
return $widgets;
|
2253
|
+
};
|
2254
|
+
|
2255
|
+
|
2256
|
+
/**
|
2257
|
+
* This callback is executed when the player begins to collide with a column.
|
2258
|
+
*
|
2259
|
+
* @method on_start_overlapping_column
|
2260
|
+
* @param {Number} col The collided column.
|
2261
|
+
* @return {jQuery} Returns a jQuery collection of HTMLElements.
|
2262
|
+
*/
|
2263
|
+
fn.on_start_overlapping_column = function(col) {
|
2264
|
+
this.set_player(col, false);
|
2265
|
+
};
|
2266
|
+
|
2267
|
+
|
2268
|
+
/**
|
2269
|
+
* A callback executed when the player begins to collide with a row.
|
2270
|
+
*
|
2271
|
+
* @method on_start_overlapping_row
|
2272
|
+
* @param {Number} row The collided row.
|
2273
|
+
* @return {jQuery} Returns a jQuery collection of HTMLElements.
|
2274
|
+
*/
|
2275
|
+
fn.on_start_overlapping_row = function(row) {
|
2276
|
+
this.set_player(false, row);
|
2277
|
+
};
|
2278
|
+
|
2279
|
+
|
2280
|
+
/**
|
2281
|
+
* A callback executed when the the player ends to collide with a column.
|
2282
|
+
*
|
2283
|
+
* @method on_stop_overlapping_column
|
2284
|
+
* @param {Number} col The collided row.
|
2285
|
+
* @return {jQuery} Returns a jQuery collection of HTMLElements.
|
2286
|
+
*/
|
2287
|
+
fn.on_stop_overlapping_column = function(col) {
|
2288
|
+
this.set_player(col, false);
|
2289
|
+
|
2290
|
+
var self = this;
|
2291
|
+
this.for_each_widget_below(col, this.cells_occupied_by_player.rows[0],
|
2292
|
+
function(tcol, trow) {
|
2293
|
+
self.move_widget_up(this, self.player_grid_data.size_y);
|
2294
|
+
});
|
2295
|
+
};
|
2296
|
+
|
2297
|
+
|
2298
|
+
/**
|
2299
|
+
* This callback is executed when the player ends to collide with a row.
|
2300
|
+
*
|
2301
|
+
* @method on_stop_overlapping_row
|
2302
|
+
* @param {Number} row The collided row.
|
2303
|
+
* @return {jQuery} Returns a jQuery collection of HTMLElements.
|
2304
|
+
*/
|
2305
|
+
fn.on_stop_overlapping_row = function(row) {
|
2306
|
+
this.set_player(false, row);
|
2307
|
+
|
2308
|
+
var self = this;
|
2309
|
+
var cols = this.cells_occupied_by_player.cols;
|
2310
|
+
for (var c = 0, cl = cols.length; c < cl; c++) {
|
2311
|
+
this.for_each_widget_below(cols[c], row, function(tcol, trow) {
|
2312
|
+
self.move_widget_up(this, self.player_grid_data.size_y);
|
2313
|
+
});
|
2314
|
+
}
|
2315
|
+
};
|
2316
|
+
|
2317
|
+
|
2318
|
+
/**
|
2319
|
+
* Move a widget to a specific row. The cell or cells must be empty.
|
2320
|
+
* If the widget has widgets below, all of these widgets will be moved also
|
2321
|
+
* if they can.
|
2322
|
+
*
|
2323
|
+
* @method move_widget_to
|
2324
|
+
* @param {HTMLElement} $widget The jQuery wrapped HTMLElement of the
|
2325
|
+
* widget is going to be moved.
|
2326
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
2327
|
+
*/
|
2328
|
+
fn.move_widget_to = function($widget, row) {
|
2329
|
+
var self = this;
|
2330
|
+
var widget_grid_data = $widget.coords().grid;
|
2331
|
+
var diff = row - widget_grid_data.row;
|
2332
|
+
var $next_widgets = this.widgets_below($widget);
|
2333
|
+
|
2334
|
+
var can_move_to_new_cell = this.can_move_to(
|
2335
|
+
widget_grid_data, widget_grid_data.col, row, $widget);
|
2336
|
+
|
2337
|
+
if (can_move_to_new_cell === false) {
|
2338
|
+
return false;
|
2339
|
+
}
|
2340
|
+
|
2341
|
+
this.remove_from_gridmap(widget_grid_data);
|
2342
|
+
widget_grid_data.row = row;
|
2343
|
+
this.add_to_gridmap(widget_grid_data);
|
2344
|
+
$widget.attr('data-row', row);
|
2345
|
+
this.$changed = this.$changed.add($widget);
|
2346
|
+
|
2347
|
+
|
2348
|
+
$next_widgets.each(function(i, widget) {
|
2349
|
+
var $w = $(widget);
|
2350
|
+
var wgd = $w.coords().grid;
|
2351
|
+
var can_go_up = self.can_go_widget_up(wgd);
|
2352
|
+
if (can_go_up && can_go_up !== wgd.row) {
|
2353
|
+
self.move_widget_to($w, can_go_up);
|
2354
|
+
}
|
2355
|
+
});
|
2356
|
+
|
2357
|
+
return this;
|
2358
|
+
};
|
2359
|
+
|
2360
|
+
|
2361
|
+
/**
|
2362
|
+
* Move up the specified widget and all below it.
|
2363
|
+
*
|
2364
|
+
* @method move_widget_up
|
2365
|
+
* @param {HTMLElement} $widget The widget you want to move.
|
2366
|
+
* @param {Number} [y_units] The number of cells that the widget has to move.
|
2367
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
2368
|
+
*/
|
2369
|
+
fn.move_widget_up = function($widget, y_units) {
|
2370
|
+
var el_grid_data = $widget.coords().grid;
|
2371
|
+
var actual_row = el_grid_data.row;
|
2372
|
+
var moved = [];
|
2373
|
+
var can_go_up = true;
|
2374
|
+
y_units || (y_units = 1);
|
2375
|
+
|
2376
|
+
if (!this.can_go_up($widget)) { return false; } //break;
|
2377
|
+
|
2378
|
+
this.for_each_column_occupied(el_grid_data, function(col) {
|
2379
|
+
// can_go_up
|
2380
|
+
if ($.inArray($widget, moved) === -1) {
|
2381
|
+
var widget_grid_data = $widget.coords().grid;
|
2382
|
+
var next_row = actual_row - y_units;
|
2383
|
+
next_row = this.can_go_up_to_row(
|
2384
|
+
widget_grid_data, col, next_row);
|
2385
|
+
|
2386
|
+
if (!next_row) {
|
2387
|
+
return true;
|
2388
|
+
}
|
2389
|
+
|
2390
|
+
var $next_widgets = this.widgets_below($widget);
|
2391
|
+
|
2392
|
+
this.remove_from_gridmap(widget_grid_data);
|
2393
|
+
widget_grid_data.row = next_row;
|
2394
|
+
this.add_to_gridmap(widget_grid_data);
|
2395
|
+
$widget.attr('data-row', widget_grid_data.row);
|
2396
|
+
this.$changed = this.$changed.add($widget);
|
2397
|
+
|
2398
|
+
moved.push($widget);
|
2399
|
+
|
2400
|
+
$next_widgets.each($.proxy(function(i, widget) {
|
2401
|
+
this.move_widget_up($(widget), y_units);
|
2402
|
+
}, this));
|
2403
|
+
}
|
2404
|
+
});
|
2405
|
+
|
2406
|
+
};
|
2407
|
+
|
2408
|
+
|
2409
|
+
/**
|
2410
|
+
* Move down the specified widget and all below it.
|
2411
|
+
*
|
2412
|
+
* @method move_widget_down
|
2413
|
+
* @param {jQuery} $widget The jQuery object representing the widget
|
2414
|
+
* you want to move.
|
2415
|
+
* @param {Number} y_units The number of cells that the widget has to move.
|
2416
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
2417
|
+
*/
|
2418
|
+
fn.move_widget_down = function($widget, y_units) {
|
2419
|
+
var el_grid_data = $widget.coords().grid;
|
2420
|
+
var actual_row = el_grid_data.row;
|
2421
|
+
var moved = [];
|
2422
|
+
var y_diff = y_units;
|
2423
|
+
|
2424
|
+
if (!$widget) { return false; }
|
2425
|
+
|
2426
|
+
if ($.inArray($widget, moved) === -1) {
|
2427
|
+
|
2428
|
+
var widget_grid_data = $widget.coords().grid;
|
2429
|
+
var next_row = actual_row + y_units;
|
2430
|
+
var $next_widgets = this.widgets_below($widget);
|
2431
|
+
|
2432
|
+
this.remove_from_gridmap(widget_grid_data);
|
2433
|
+
|
2434
|
+
$next_widgets.each($.proxy(function(i, widget) {
|
2435
|
+
var $w = $(widget);
|
2436
|
+
var wd = $w.coords().grid;
|
2437
|
+
var tmp_y = this.displacement_diff(
|
2438
|
+
wd, widget_grid_data, y_diff);
|
2439
|
+
|
2440
|
+
if (tmp_y > 0) {
|
2441
|
+
this.move_widget_down($w, tmp_y);
|
2442
|
+
}
|
2443
|
+
}, this));
|
2444
|
+
|
2445
|
+
widget_grid_data.row = next_row;
|
2446
|
+
this.update_widget_position(widget_grid_data, $widget);
|
2447
|
+
$widget.attr('data-row', widget_grid_data.row);
|
2448
|
+
this.$changed = this.$changed.add($widget);
|
2449
|
+
|
2450
|
+
moved.push($widget);
|
2451
|
+
}
|
2452
|
+
};
|
2453
|
+
|
2454
|
+
|
2455
|
+
/**
|
2456
|
+
* Check if the widget can move to the specified row, else returns the
|
2457
|
+
* upper row possible.
|
2458
|
+
*
|
2459
|
+
* @method can_go_up_to_row
|
2460
|
+
* @param {Number} widget_grid_data The current grid coords object of the
|
2461
|
+
* widget.
|
2462
|
+
* @param {Number} col The target column.
|
2463
|
+
* @param {Number} row The target row.
|
2464
|
+
* @return {Boolean|Number} Returns the row number if the widget can move
|
2465
|
+
* to the target position, else returns false.
|
2466
|
+
*/
|
2467
|
+
fn.can_go_up_to_row = function(widget_grid_data, col, row) {
|
2468
|
+
var ga = this.gridmap;
|
2469
|
+
var result = true;
|
2470
|
+
var urc = []; // upper_rows_in_columns
|
2471
|
+
var actual_row = widget_grid_data.row;
|
2472
|
+
var r;
|
2473
|
+
|
2474
|
+
/* generate an array with columns as index and array with
|
2475
|
+
* upper rows empty in the column */
|
2476
|
+
this.for_each_column_occupied(widget_grid_data, function(tcol) {
|
2477
|
+
var grid_col = ga[tcol];
|
2478
|
+
urc[tcol] = [];
|
2479
|
+
|
2480
|
+
r = actual_row;
|
2481
|
+
while (r--) {
|
2482
|
+
if (this.is_empty(tcol, r) &&
|
2483
|
+
!this.is_placeholder_in(tcol, r)
|
2484
|
+
) {
|
2485
|
+
urc[tcol].push(r);
|
2486
|
+
}else{
|
2487
|
+
break;
|
2488
|
+
}
|
2489
|
+
}
|
2490
|
+
|
2491
|
+
if (!urc[tcol].length) {
|
2492
|
+
result = false;
|
2493
|
+
return true;
|
2494
|
+
}
|
2495
|
+
|
2496
|
+
});
|
2497
|
+
|
2498
|
+
if (!result) { return false; }
|
2499
|
+
|
2500
|
+
/* get common rows starting from upper position in all the columns
|
2501
|
+
* that widget occupies */
|
2502
|
+
r = row;
|
2503
|
+
for (r = 1; r < actual_row; r++) {
|
2504
|
+
var common = true;
|
2505
|
+
|
2506
|
+
for (var uc = 0, ucl = urc.length; uc < ucl; uc++) {
|
2507
|
+
if (urc[uc] && $.inArray(r, urc[uc]) === -1) {
|
2508
|
+
common = false;
|
2509
|
+
}
|
2510
|
+
}
|
2511
|
+
|
2512
|
+
if (common === true) {
|
2513
|
+
result = r;
|
2514
|
+
break;
|
2515
|
+
}
|
2516
|
+
}
|
2517
|
+
|
2518
|
+
return result;
|
2519
|
+
};
|
2520
|
+
|
2521
|
+
|
2522
|
+
fn.displacement_diff = function(widget_grid_data, parent_bgd, y_units) {
|
2523
|
+
var actual_row = widget_grid_data.row;
|
2524
|
+
var diffs = [];
|
2525
|
+
var parent_max_y = parent_bgd.row + parent_bgd.size_y;
|
2526
|
+
|
2527
|
+
this.for_each_column_occupied(widget_grid_data, function(col) {
|
2528
|
+
var temp_y_units = 0;
|
2529
|
+
|
2530
|
+
for (var r = parent_max_y; r < actual_row; r++) {
|
2531
|
+
if (this.is_empty(col, r)) {
|
2532
|
+
temp_y_units = temp_y_units + 1;
|
2533
|
+
}
|
2534
|
+
}
|
2535
|
+
|
2536
|
+
diffs.push(temp_y_units);
|
2537
|
+
});
|
2538
|
+
|
2539
|
+
var max_diff = Math.max.apply(Math, diffs);
|
2540
|
+
y_units = (y_units - max_diff);
|
2541
|
+
|
2542
|
+
return y_units > 0 ? y_units : 0;
|
2543
|
+
};
|
2544
|
+
|
2545
|
+
|
2546
|
+
/**
|
2547
|
+
* Get widgets below a widget.
|
2548
|
+
*
|
2549
|
+
* @method widgets_below
|
2550
|
+
* @param {HTMLElement} $el The jQuery wrapped HTMLElement.
|
2551
|
+
* @return {jQuery} A jQuery collection of HTMLElements.
|
2552
|
+
*/
|
2553
|
+
fn.widgets_below = function($el) {
|
2554
|
+
var el_grid_data = $.isPlainObject($el) ? $el : $el.coords().grid;
|
2555
|
+
var self = this;
|
2556
|
+
var ga = this.gridmap;
|
2557
|
+
var next_row = el_grid_data.row + el_grid_data.size_y - 1;
|
2558
|
+
var $nexts = $([]);
|
2559
|
+
|
2560
|
+
this.for_each_column_occupied(el_grid_data, function(col) {
|
2561
|
+
self.for_each_widget_below(col, next_row, function(tcol, trow) {
|
2562
|
+
if (!self.is_player(this) && $.inArray(this, $nexts) === -1) {
|
2563
|
+
$nexts = $nexts.add(this);
|
2564
|
+
return true; // break
|
2565
|
+
}
|
2566
|
+
});
|
2567
|
+
});
|
2568
|
+
|
2569
|
+
return this.sort_by_row_asc($nexts);
|
2570
|
+
};
|
2571
|
+
|
2572
|
+
|
2573
|
+
/**
|
2574
|
+
* Update the array of mapped positions with the new player position.
|
2575
|
+
*
|
2576
|
+
* @method set_cells_player_occupies
|
2577
|
+
* @param {Number} col The new player col.
|
2578
|
+
* @param {Number} col The new player row.
|
2579
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
2580
|
+
*/
|
2581
|
+
fn.set_cells_player_occupies = function(col, row) {
|
2582
|
+
this.remove_from_gridmap(this.placeholder_grid_data);
|
2583
|
+
this.placeholder_grid_data.col = col;
|
2584
|
+
this.placeholder_grid_data.row = row;
|
2585
|
+
this.add_to_gridmap(this.placeholder_grid_data, this.$player);
|
2586
|
+
return this;
|
2587
|
+
};
|
2588
|
+
|
2589
|
+
|
2590
|
+
/**
|
2591
|
+
* Remove from the array of mapped positions the reference to the player.
|
2592
|
+
*
|
2593
|
+
* @method empty_cells_player_occupies
|
2594
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
2595
|
+
*/
|
2596
|
+
fn.empty_cells_player_occupies = function() {
|
2597
|
+
this.remove_from_gridmap(this.placeholder_grid_data);
|
2598
|
+
return this;
|
2599
|
+
};
|
2600
|
+
|
2601
|
+
|
2602
|
+
fn.can_go_up = function($el) {
|
2603
|
+
var el_grid_data = $el.coords().grid;
|
2604
|
+
var initial_row = el_grid_data.row;
|
2605
|
+
var prev_row = initial_row - 1;
|
2606
|
+
var ga = this.gridmap;
|
2607
|
+
var upper_rows_by_column = [];
|
2608
|
+
|
2609
|
+
var result = true;
|
2610
|
+
if (initial_row === 1) { return false; }
|
2611
|
+
|
2612
|
+
this.for_each_column_occupied(el_grid_data, function(col) {
|
2613
|
+
var $w = this.is_widget(col, prev_row);
|
2614
|
+
|
2615
|
+
if (this.is_occupied(col, prev_row) ||
|
2616
|
+
this.is_player(col, prev_row) ||
|
2617
|
+
this.is_placeholder_in(col, prev_row) ||
|
2618
|
+
this.is_player_in(col, prev_row)
|
2619
|
+
) {
|
2620
|
+
result = false;
|
2621
|
+
return true; //break
|
2622
|
+
}
|
2623
|
+
});
|
2624
|
+
|
2625
|
+
return result;
|
2626
|
+
};
|
2627
|
+
|
2628
|
+
|
2629
|
+
|
2630
|
+
/**
|
2631
|
+
* Check if it's possible to move a widget to a specific col/row. It takes
|
2632
|
+
* into account the dimensions (`size_y` and `size_x` attrs. of the grid
|
2633
|
+
* coords object) the widget occupies.
|
2634
|
+
*
|
2635
|
+
* @method can_move_to
|
2636
|
+
* @param {Object} widget_grid_data The grid coords object that represents
|
2637
|
+
* the widget.
|
2638
|
+
* @param {Object} col The col to check.
|
2639
|
+
* @param {Object} row The row to check.
|
2640
|
+
* @param {Number} [max_row] The max row allowed.
|
2641
|
+
* @return {Boolean} Returns true if all cells are empty, else return false.
|
2642
|
+
*/
|
2643
|
+
fn.can_move_to = function(widget_grid_data, col, row, max_row) {
|
2644
|
+
var ga = this.gridmap;
|
2645
|
+
var $w = widget_grid_data.el;
|
2646
|
+
var future_wd = {
|
2647
|
+
size_y: widget_grid_data.size_y,
|
2648
|
+
size_x: widget_grid_data.size_x,
|
2649
|
+
col: col,
|
2650
|
+
row: row
|
2651
|
+
};
|
2652
|
+
var result = true;
|
2653
|
+
|
2654
|
+
//Prevents widgets go out of the grid
|
2655
|
+
var right_col = col + widget_grid_data.size_x - 1;
|
2656
|
+
if (right_col > this.cols) {
|
2657
|
+
return false;
|
2658
|
+
}
|
2659
|
+
|
2660
|
+
if (max_row && max_row < row + widget_grid_data.size_y - 1) {
|
2661
|
+
return false;
|
2662
|
+
}
|
2663
|
+
|
2664
|
+
this.for_each_cell_occupied(future_wd, function(tcol, trow) {
|
2665
|
+
var $tw = this.is_widget(tcol, trow);
|
2666
|
+
if ($tw && (!widget_grid_data.el || $tw.is($w))) {
|
2667
|
+
result = false;
|
2668
|
+
}
|
2669
|
+
});
|
2670
|
+
|
2671
|
+
return result;
|
2672
|
+
};
|
2673
|
+
|
2674
|
+
|
2675
|
+
/**
|
2676
|
+
* Given the leftmost column returns all columns that are overlapping
|
2677
|
+
* with the player.
|
2678
|
+
*
|
2679
|
+
* @method get_targeted_columns
|
2680
|
+
* @param {Number} [from_col] The leftmost column.
|
2681
|
+
* @return {Array} Returns an array with column numbers.
|
2682
|
+
*/
|
2683
|
+
fn.get_targeted_columns = function(from_col) {
|
2684
|
+
var max = (from_col || this.player_grid_data.col) +
|
2685
|
+
(this.player_grid_data.size_x - 1);
|
2686
|
+
var cols = [];
|
2687
|
+
for (var col = from_col; col <= max; col++) {
|
2688
|
+
cols.push(col);
|
2689
|
+
}
|
2690
|
+
return cols;
|
2691
|
+
};
|
2692
|
+
|
2693
|
+
|
2694
|
+
/**
|
2695
|
+
* Given the upper row returns all rows that are overlapping with the player.
|
2696
|
+
*
|
2697
|
+
* @method get_targeted_rows
|
2698
|
+
* @param {Number} [from_row] The upper row.
|
2699
|
+
* @return {Array} Returns an array with row numbers.
|
2700
|
+
*/
|
2701
|
+
fn.get_targeted_rows = function(from_row) {
|
2702
|
+
var max = (from_row || this.player_grid_data.row) +
|
2703
|
+
(this.player_grid_data.size_y - 1);
|
2704
|
+
var rows = [];
|
2705
|
+
for (var row = from_row; row <= max; row++) {
|
2706
|
+
rows.push(row);
|
2707
|
+
}
|
2708
|
+
return rows;
|
2709
|
+
};
|
2710
|
+
|
2711
|
+
/**
|
2712
|
+
* Get all columns and rows that a widget occupies.
|
2713
|
+
*
|
2714
|
+
* @method get_cells_occupied
|
2715
|
+
* @param {Object} el_grid_data The grid coords object of the widget.
|
2716
|
+
* @return {Object} Returns an object like `{ cols: [], rows: []}`.
|
2717
|
+
*/
|
2718
|
+
fn.get_cells_occupied = function(el_grid_data) {
|
2719
|
+
var cells = { cols: [], rows: []};
|
2720
|
+
var i;
|
2721
|
+
if (arguments[1] instanceof jQuery) {
|
2722
|
+
el_grid_data = arguments[1].coords().grid;
|
2723
|
+
}
|
2724
|
+
|
2725
|
+
for (i = 0; i < el_grid_data.size_x; i++) {
|
2726
|
+
var col = el_grid_data.col + i;
|
2727
|
+
cells.cols.push(col);
|
2728
|
+
}
|
2729
|
+
|
2730
|
+
for (i = 0; i < el_grid_data.size_y; i++) {
|
2731
|
+
var row = el_grid_data.row + i;
|
2732
|
+
cells.rows.push(row);
|
2733
|
+
}
|
2734
|
+
|
2735
|
+
return cells;
|
2736
|
+
};
|
2737
|
+
|
2738
|
+
|
2739
|
+
/**
|
2740
|
+
* Iterate over the cells occupied by a widget executing a function for
|
2741
|
+
* each one.
|
2742
|
+
*
|
2743
|
+
* @method for_each_cell_occupied
|
2744
|
+
* @param {Object} el_grid_data The grid coords object that represents the
|
2745
|
+
* widget.
|
2746
|
+
* @param {Function} callback The function to execute on each column
|
2747
|
+
* iteration. Column and row are passed as arguments.
|
2748
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
2749
|
+
*/
|
2750
|
+
fn.for_each_cell_occupied = function(grid_data, callback) {
|
2751
|
+
this.for_each_column_occupied(grid_data, function(col) {
|
2752
|
+
this.for_each_row_occupied(grid_data, function(row) {
|
2753
|
+
callback.call(this, col, row);
|
2754
|
+
});
|
2755
|
+
});
|
2756
|
+
return this;
|
2757
|
+
};
|
2758
|
+
|
2759
|
+
|
2760
|
+
/**
|
2761
|
+
* Iterate over the columns occupied by a widget executing a function for
|
2762
|
+
* each one.
|
2763
|
+
*
|
2764
|
+
* @method for_each_column_occupied
|
2765
|
+
* @param {Object} el_grid_data The grid coords object that represents
|
2766
|
+
* the widget.
|
2767
|
+
* @param {Function} callback The function to execute on each column
|
2768
|
+
* iteration. The column number is passed as first argument.
|
2769
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
2770
|
+
*/
|
2771
|
+
fn.for_each_column_occupied = function(el_grid_data, callback) {
|
2772
|
+
for (var i = 0; i < el_grid_data.size_x; i++) {
|
2773
|
+
var col = el_grid_data.col + i;
|
2774
|
+
callback.call(this, col, el_grid_data);
|
2775
|
+
}
|
2776
|
+
};
|
2777
|
+
|
2778
|
+
|
2779
|
+
/**
|
2780
|
+
* Iterate over the rows occupied by a widget executing a function for
|
2781
|
+
* each one.
|
2782
|
+
*
|
2783
|
+
* @method for_each_row_occupied
|
2784
|
+
* @param {Object} el_grid_data The grid coords object that represents
|
2785
|
+
* the widget.
|
2786
|
+
* @param {Function} callback The function to execute on each column
|
2787
|
+
* iteration. The row number is passed as first argument.
|
2788
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
2789
|
+
*/
|
2790
|
+
fn.for_each_row_occupied = function(el_grid_data, callback) {
|
2791
|
+
for (var i = 0; i < el_grid_data.size_y; i++) {
|
2792
|
+
var row = el_grid_data.row + i;
|
2793
|
+
callback.call(this, row, el_grid_data);
|
2794
|
+
}
|
2795
|
+
};
|
2796
|
+
|
2797
|
+
|
2798
|
+
|
2799
|
+
fn._traversing_widgets = function(type, direction, col, row, callback) {
|
2800
|
+
var ga = this.gridmap;
|
2801
|
+
if (!ga[col]) { return; }
|
2802
|
+
|
2803
|
+
var cr, max;
|
2804
|
+
var action = type + '/' + direction;
|
2805
|
+
if (arguments[2] instanceof jQuery) {
|
2806
|
+
var el_grid_data = arguments[2].coords().grid;
|
2807
|
+
col = el_grid_data.col;
|
2808
|
+
row = el_grid_data.row;
|
2809
|
+
callback = arguments[3];
|
2810
|
+
}
|
2811
|
+
var matched = [];
|
2812
|
+
var trow = row;
|
2813
|
+
|
2814
|
+
|
2815
|
+
var methods = {
|
2816
|
+
'for_each/above': function() {
|
2817
|
+
while (trow--) {
|
2818
|
+
if (trow > 0 && this.is_widget(col, trow) &&
|
2819
|
+
$.inArray(ga[col][trow], matched) === -1
|
2820
|
+
) {
|
2821
|
+
cr = callback.call(ga[col][trow], col, trow);
|
2822
|
+
matched.push(ga[col][trow]);
|
2823
|
+
if (cr) { break; }
|
2824
|
+
}
|
2825
|
+
}
|
2826
|
+
},
|
2827
|
+
'for_each/below': function() {
|
2828
|
+
for (trow = row + 1, max = ga[col].length; trow < max; trow++) {
|
2829
|
+
if (this.is_widget(col, trow) &&
|
2830
|
+
$.inArray(ga[col][trow], matched) === -1
|
2831
|
+
) {
|
2832
|
+
cr = callback.call(ga[col][trow], col, trow);
|
2833
|
+
matched.push(ga[col][trow]);
|
2834
|
+
if (cr) { break; }
|
2835
|
+
}
|
2836
|
+
}
|
2837
|
+
}
|
2838
|
+
};
|
2839
|
+
|
2840
|
+
if (methods[action]) {
|
2841
|
+
methods[action].call(this);
|
2842
|
+
}
|
2843
|
+
};
|
2844
|
+
|
2845
|
+
|
2846
|
+
/**
|
2847
|
+
* Iterate over each widget above the column and row specified.
|
2848
|
+
*
|
2849
|
+
* @method for_each_widget_above
|
2850
|
+
* @param {Number} col The column to start iterating.
|
2851
|
+
* @param {Number} row The row to start iterating.
|
2852
|
+
* @param {Function} callback The function to execute on each widget
|
2853
|
+
* iteration. The value of `this` inside the function is the jQuery
|
2854
|
+
* wrapped HTMLElement.
|
2855
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
2856
|
+
*/
|
2857
|
+
fn.for_each_widget_above = function(col, row, callback) {
|
2858
|
+
this._traversing_widgets('for_each', 'above', col, row, callback);
|
2859
|
+
return this;
|
2860
|
+
};
|
2861
|
+
|
2862
|
+
|
2863
|
+
/**
|
2864
|
+
* Iterate over each widget below the column and row specified.
|
2865
|
+
*
|
2866
|
+
* @method for_each_widget_below
|
2867
|
+
* @param {Number} col The column to start iterating.
|
2868
|
+
* @param {Number} row The row to start iterating.
|
2869
|
+
* @param {Function} callback The function to execute on each widget
|
2870
|
+
* iteration. The value of `this` inside the function is the jQuery wrapped
|
2871
|
+
* HTMLElement.
|
2872
|
+
* @return {Class} Returns the instance of the Gridster Class.
|
2873
|
+
*/
|
2874
|
+
fn.for_each_widget_below = function(col, row, callback) {
|
2875
|
+
this._traversing_widgets('for_each', 'below', col, row, callback);
|
2876
|
+
return this;
|
2877
|
+
};
|
2878
|
+
|
2879
|
+
|
2880
|
+
/**
|
2881
|
+
* Returns the highest occupied cell in the grid.
|
2882
|
+
*
|
2883
|
+
* @method get_highest_occupied_cell
|
2884
|
+
* @return {Object} Returns an object with `col` and `row` numbers.
|
2885
|
+
*/
|
2886
|
+
fn.get_highest_occupied_cell = function() {
|
2887
|
+
var r;
|
2888
|
+
var gm = this.gridmap;
|
2889
|
+
var rows = [];
|
2890
|
+
var row_in_col = [];
|
2891
|
+
for (var c = gm.length - 1; c >= 1; c--) {
|
2892
|
+
for (r = gm[c].length - 1; r >= 1; r--) {
|
2893
|
+
if (this.is_widget(c, r)) {
|
2894
|
+
rows.push(r);
|
2895
|
+
row_in_col[r] = c;
|
2896
|
+
break;
|
2897
|
+
}
|
2898
|
+
}
|
2899
|
+
}
|
2900
|
+
|
2901
|
+
var highest_row = Math.max.apply(Math, rows);
|
2902
|
+
|
2903
|
+
this.highest_occupied_cell = {
|
2904
|
+
col: row_in_col[highest_row],
|
2905
|
+
row: highest_row
|
2906
|
+
};
|
2907
|
+
|
2908
|
+
return this.highest_occupied_cell;
|
2909
|
+
};
|
2910
|
+
|
2911
|
+
|
2912
|
+
fn.get_widgets_from = function(col, row) {
|
2913
|
+
var ga = this.gridmap;
|
2914
|
+
var $widgets = $();
|
2915
|
+
|
2916
|
+
if (col) {
|
2917
|
+
$widgets = $widgets.add(
|
2918
|
+
this.$widgets.filter(function() {
|
2919
|
+
var tcol = $(this).attr('data-col');
|
2920
|
+
return (tcol === col || tcol > col);
|
2921
|
+
})
|
2922
|
+
);
|
2923
|
+
}
|
2924
|
+
|
2925
|
+
if (row) {
|
2926
|
+
$widgets = $widgets.add(
|
2927
|
+
this.$widgets.filter(function() {
|
2928
|
+
var trow = $(this).attr('data-row');
|
2929
|
+
return (trow === row || trow > row);
|
2930
|
+
})
|
2931
|
+
);
|
2932
|
+
}
|
2933
|
+
|
2934
|
+
return $widgets;
|
2935
|
+
};
|
2936
|
+
|
2937
|
+
|
2938
|
+
/**
|
2939
|
+
* Set the current height of the parent grid.
|
2940
|
+
*
|
2941
|
+
* @method set_dom_grid_height
|
2942
|
+
* @return {Object} Returns the instance of the Gridster class.
|
2943
|
+
*/
|
2944
|
+
fn.set_dom_grid_height = function() {
|
2945
|
+
var r = this.get_highest_occupied_cell().row;
|
2946
|
+
this.$el.css('height', r * this.min_widget_height);
|
2947
|
+
return this;
|
2948
|
+
};
|
2949
|
+
|
2950
|
+
|
2951
|
+
/**
|
2952
|
+
* It generates the neccessary styles to position the widgets.
|
2953
|
+
*
|
2954
|
+
* @method generate_stylesheet
|
2955
|
+
* @param {Number} rows Number of columns.
|
2956
|
+
* @param {Number} cols Number of rows.
|
2957
|
+
* @return {Object} Returns the instance of the Gridster class.
|
2958
|
+
*/
|
2959
|
+
fn.generate_stylesheet = function(opts) {
|
2960
|
+
var styles = '';
|
2961
|
+
var max_size_x = this.options.max_size_x;
|
2962
|
+
var max_rows = 0;
|
2963
|
+
var max_cols = 0;
|
2964
|
+
var i;
|
2965
|
+
var rules;
|
2966
|
+
|
2967
|
+
opts || (opts = {});
|
2968
|
+
opts.cols || (opts.cols = this.cols);
|
2969
|
+
opts.rows || (opts.rows = this.rows);
|
2970
|
+
opts.namespace || (opts.namespace = this.options.namespace);
|
2971
|
+
opts.widget_base_dimensions ||
|
2972
|
+
(opts.widget_base_dimensions = this.options.widget_base_dimensions);
|
2973
|
+
opts.widget_margins ||
|
2974
|
+
(opts.widget_margins = this.options.widget_margins);
|
2975
|
+
opts.min_widget_width = (opts.widget_margins[0] * 2) +
|
2976
|
+
opts.widget_base_dimensions[0];
|
2977
|
+
opts.min_widget_height = (opts.widget_margins[1] * 2) +
|
2978
|
+
opts.widget_base_dimensions[1];
|
2979
|
+
|
2980
|
+
// don't duplicate stylesheets for the same configuration
|
2981
|
+
var serialized_opts = $.param(opts);
|
2982
|
+
if ($.inArray(serialized_opts, Gridster.generated_stylesheets) >= 0) {
|
2983
|
+
return false;
|
2984
|
+
}
|
2985
|
+
|
2986
|
+
Gridster.generated_stylesheets.push(serialized_opts);
|
2987
|
+
|
2988
|
+
/* generate CSS styles for cols */
|
2989
|
+
for (i = opts.cols; i >= 0; i--) {
|
2990
|
+
styles += (opts.namespace + ' [data-col="'+ (i + 1) + '"] { left:' +
|
2991
|
+
((i * opts.widget_base_dimensions[0]) +
|
2992
|
+
(i * opts.widget_margins[0]) +
|
2993
|
+
((i + 1) * opts.widget_margins[0])) + 'px;} ');
|
2994
|
+
}
|
2995
|
+
|
2996
|
+
/* generate CSS styles for rows */
|
2997
|
+
for (i = opts.rows; i >= 0; i--) {
|
2998
|
+
styles += (opts.namespace + ' [data-row="' + (i + 1) + '"] { top:' +
|
2999
|
+
((i * opts.widget_base_dimensions[1]) +
|
3000
|
+
(i * opts.widget_margins[1]) +
|
3001
|
+
((i + 1) * opts.widget_margins[1]) ) + 'px;} ');
|
3002
|
+
}
|
3003
|
+
|
3004
|
+
for (var y = 1; y <= opts.rows; y++) {
|
3005
|
+
styles += (opts.namespace + ' [data-sizey="' + y + '"] { height:' +
|
3006
|
+
(y * opts.widget_base_dimensions[1] +
|
3007
|
+
(y - 1) * (opts.widget_margins[1] * 2)) + 'px;}');
|
3008
|
+
}
|
3009
|
+
|
3010
|
+
for (var x = 1; x <= max_size_x; x++) {
|
3011
|
+
styles += (opts.namespace + ' [data-sizex="' + x + '"] { width:' +
|
3012
|
+
(x * opts.widget_base_dimensions[0] +
|
3013
|
+
(x - 1) * (opts.widget_margins[0] * 2)) + 'px;}');
|
3014
|
+
}
|
3015
|
+
|
3016
|
+
return this.add_style_tag(styles);
|
3017
|
+
};
|
3018
|
+
|
3019
|
+
|
3020
|
+
/**
|
3021
|
+
* Injects the given CSS as string to the head of the document.
|
3022
|
+
*
|
3023
|
+
* @method add_style_tag
|
3024
|
+
* @param {String} css The styles to apply.
|
3025
|
+
* @return {Object} Returns the instance of the Gridster class.
|
3026
|
+
*/
|
3027
|
+
fn.add_style_tag = function(css) {
|
3028
|
+
var d = document;
|
3029
|
+
var tag = d.createElement('style');
|
3030
|
+
|
3031
|
+
d.getElementsByTagName('head')[0].appendChild(tag);
|
3032
|
+
tag.setAttribute('type', 'text/css');
|
3033
|
+
|
3034
|
+
if (tag.styleSheet) {
|
3035
|
+
tag.styleSheet.cssText = css;
|
3036
|
+
}else{
|
3037
|
+
tag.appendChild(document.createTextNode(css));
|
3038
|
+
}
|
3039
|
+
return this;
|
3040
|
+
};
|
3041
|
+
|
3042
|
+
|
3043
|
+
/**
|
3044
|
+
* Generates a faux grid to collide with it when a widget is dragged and
|
3045
|
+
* detect row or column that we want to go.
|
3046
|
+
*
|
3047
|
+
* @method generate_faux_grid
|
3048
|
+
* @param {Number} rows Number of columns.
|
3049
|
+
* @param {Number} cols Number of rows.
|
3050
|
+
* @return {Object} Returns the instance of the Gridster class.
|
3051
|
+
*/
|
3052
|
+
fn.generate_faux_grid = function(rows, cols) {
|
3053
|
+
this.faux_grid = [];
|
3054
|
+
this.gridmap = [];
|
3055
|
+
var col;
|
3056
|
+
var row;
|
3057
|
+
for (col = cols; col > 0; col--) {
|
3058
|
+
this.gridmap[col] = [];
|
3059
|
+
for (row = rows; row > 0; row--) {
|
3060
|
+
this.add_faux_cell(row, col);
|
3061
|
+
}
|
3062
|
+
}
|
3063
|
+
return this;
|
3064
|
+
};
|
3065
|
+
|
3066
|
+
|
3067
|
+
/**
|
3068
|
+
* Add cell to the faux grid.
|
3069
|
+
*
|
3070
|
+
* @method add_faux_cell
|
3071
|
+
* @param {Number} row The row for the new faux cell.
|
3072
|
+
* @param {Number} col The col for the new faux cell.
|
3073
|
+
* @return {Object} Returns the instance of the Gridster class.
|
3074
|
+
*/
|
3075
|
+
fn.add_faux_cell = function(row, col) {
|
3076
|
+
var coords = $({
|
3077
|
+
left: this.baseX + ((col - 1) * this.min_widget_width),
|
3078
|
+
top: this.baseY + (row -1) * this.min_widget_height,
|
3079
|
+
width: this.min_widget_width,
|
3080
|
+
height: this.min_widget_height,
|
3081
|
+
col: col,
|
3082
|
+
row: row,
|
3083
|
+
original_col: col,
|
3084
|
+
original_row: row
|
3085
|
+
}).coords();
|
3086
|
+
|
3087
|
+
if (!$.isArray(this.gridmap[col])) {
|
3088
|
+
this.gridmap[col] = [];
|
3089
|
+
}
|
3090
|
+
|
3091
|
+
this.gridmap[col][row] = false;
|
3092
|
+
this.faux_grid.push(coords);
|
3093
|
+
|
3094
|
+
return this;
|
3095
|
+
};
|
3096
|
+
|
3097
|
+
|
3098
|
+
/**
|
3099
|
+
* Add rows to the faux grid.
|
3100
|
+
*
|
3101
|
+
* @method add_faux_rows
|
3102
|
+
* @param {Number} rows The number of rows you want to add to the faux grid.
|
3103
|
+
* @return {Object} Returns the instance of the Gridster class.
|
3104
|
+
*/
|
3105
|
+
fn.add_faux_rows = function(rows) {
|
3106
|
+
var actual_rows = this.rows;
|
3107
|
+
var max_rows = actual_rows + (rows || 1);
|
3108
|
+
|
3109
|
+
for (var r = max_rows; r > actual_rows; r--) {
|
3110
|
+
for (var c = this.cols; c >= 1; c--) {
|
3111
|
+
this.add_faux_cell(r, c);
|
3112
|
+
}
|
3113
|
+
}
|
3114
|
+
|
3115
|
+
this.rows = max_rows;
|
3116
|
+
|
3117
|
+
if (this.options.autogenerate_stylesheet) {
|
3118
|
+
this.generate_stylesheet();
|
3119
|
+
}
|
3120
|
+
|
3121
|
+
return this;
|
3122
|
+
};
|
3123
|
+
|
3124
|
+
/**
|
3125
|
+
* Add cols to the faux grid.
|
3126
|
+
*
|
3127
|
+
* @method add_faux_cols
|
3128
|
+
* @param {Number} cols The number of cols you want to add to the faux grid.
|
3129
|
+
* @return {Object} Returns the instance of the Gridster class.
|
3130
|
+
*/
|
3131
|
+
fn.add_faux_cols = function(cols) {
|
3132
|
+
var actual_cols = this.cols;
|
3133
|
+
var max_cols = actual_cols + (cols || 1);
|
3134
|
+
|
3135
|
+
for (var c = actual_cols; c < max_cols; c++) {
|
3136
|
+
for (var r = this.rows; r >= 1; r--) {
|
3137
|
+
this.add_faux_cell(r, c);
|
3138
|
+
}
|
3139
|
+
}
|
3140
|
+
|
3141
|
+
this.cols = max_cols;
|
3142
|
+
|
3143
|
+
if (this.options.autogenerate_stylesheet) {
|
3144
|
+
this.generate_stylesheet();
|
3145
|
+
}
|
3146
|
+
|
3147
|
+
return this;
|
3148
|
+
};
|
3149
|
+
|
3150
|
+
|
3151
|
+
/**
|
3152
|
+
* Recalculates the offsets for the faux grid. You need to use it when
|
3153
|
+
* the browser is resized.
|
3154
|
+
*
|
3155
|
+
* @method recalculate_faux_grid
|
3156
|
+
* @return {Object} Returns the instance of the Gridster class.
|
3157
|
+
*/
|
3158
|
+
fn.recalculate_faux_grid = function() {
|
3159
|
+
var aw = this.$wrapper.width();
|
3160
|
+
this.baseX = ($(window).width() - aw) / 2;
|
3161
|
+
this.baseY = this.$wrapper.offset().top;
|
3162
|
+
|
3163
|
+
$.each(this.faux_grid, $.proxy(function(i, coords) {
|
3164
|
+
this.faux_grid[i] = coords.update({
|
3165
|
+
left: this.baseX + (coords.data.col -1) * this.min_widget_width,
|
3166
|
+
top: this.baseY + (coords.data.row -1) * this.min_widget_height
|
3167
|
+
});
|
3168
|
+
|
3169
|
+
}, this));
|
3170
|
+
|
3171
|
+
return this;
|
3172
|
+
};
|
3173
|
+
|
3174
|
+
|
3175
|
+
/**
|
3176
|
+
* Get all widgets in the DOM and register them.
|
3177
|
+
*
|
3178
|
+
* @method get_widgets_from_DOM
|
3179
|
+
* @return {Object} Returns the instance of the Gridster class.
|
3180
|
+
*/
|
3181
|
+
fn.get_widgets_from_DOM = function() {
|
3182
|
+
this.$widgets.each($.proxy(function(i, widget) {
|
3183
|
+
this.register_widget($(widget));
|
3184
|
+
}, this));
|
3185
|
+
return this;
|
3186
|
+
};
|
3187
|
+
|
3188
|
+
|
3189
|
+
/**
|
3190
|
+
* Calculate columns and rows to be set based on the configuration
|
3191
|
+
* parameters, grid dimensions, etc ...
|
3192
|
+
*
|
3193
|
+
* @method generate_grid_and_stylesheet
|
3194
|
+
* @return {Object} Returns the instance of the Gridster class.
|
3195
|
+
*/
|
3196
|
+
fn.generate_grid_and_stylesheet = function() {
|
3197
|
+
var aw = this.$wrapper.width();
|
3198
|
+
var ah = this.$wrapper.height();
|
3199
|
+
|
3200
|
+
var cols = Math.floor(aw / this.min_widget_width) +
|
3201
|
+
this.options.extra_cols;
|
3202
|
+
|
3203
|
+
var actual_cols = this.$widgets.map(function() {
|
3204
|
+
return $(this).attr('data-col');
|
3205
|
+
});
|
3206
|
+
actual_cols = Array.prototype.slice.call(actual_cols, 0);
|
3207
|
+
//needed to pass tests with phantomjs
|
3208
|
+
actual_cols.length || (actual_cols = [0]);
|
3209
|
+
|
3210
|
+
var min_cols = Math.max.apply(Math, actual_cols);
|
3211
|
+
|
3212
|
+
// get all rows that could be occupied by the current widgets
|
3213
|
+
var max_rows = this.options.extra_rows;
|
3214
|
+
this.$widgets.each(function(i, w) {
|
3215
|
+
max_rows += (+$(w).attr('data-sizey'));
|
3216
|
+
});
|
3217
|
+
|
3218
|
+
this.cols = Math.max(min_cols, cols, this.options.min_cols);
|
3219
|
+
this.rows = Math.max(max_rows, this.options.min_rows);
|
3220
|
+
|
3221
|
+
this.baseX = ($(window).width() - aw) / 2;
|
3222
|
+
this.baseY = this.$wrapper.offset().top;
|
3223
|
+
|
3224
|
+
if (this.options.autogenerate_stylesheet) {
|
3225
|
+
this.generate_stylesheet();
|
3226
|
+
}
|
3227
|
+
|
3228
|
+
return this.generate_faux_grid(this.rows, this.cols);
|
3229
|
+
};
|
3230
|
+
|
3231
|
+
/**
|
3232
|
+
* Destroy this gridster by removing any sign of its presence, making it easy to avoid memory leaks
|
3233
|
+
*
|
3234
|
+
* @method destroy
|
3235
|
+
* @return {undefined}
|
3236
|
+
*/
|
3237
|
+
fn.destroy = function(){
|
3238
|
+
// remove bound callback on window resize
|
3239
|
+
$(window).unbind('resize', this.on_window_resize);
|
3240
|
+
|
3241
|
+
if(this.drag_api){
|
3242
|
+
this.drag_api.destroy();
|
3243
|
+
}
|
3244
|
+
|
3245
|
+
// lastly, remove gridster element
|
3246
|
+
// this will additionally cause any data associated to this element to be removed, including this
|
3247
|
+
// very gridster instance
|
3248
|
+
this.$el.remove();
|
3249
|
+
};
|
3250
|
+
|
3251
|
+
|
3252
|
+
//jQuery adapter
|
3253
|
+
$.fn.gridster = function(options) {
|
3254
|
+
return this.each(function() {
|
3255
|
+
if (!$(this).data('gridster')) {
|
3256
|
+
$(this).data('gridster', new Gridster( this, options ));
|
3257
|
+
}
|
3258
|
+
});
|
3259
|
+
};
|
3260
|
+
|
3261
|
+
$.Gridster = fn;
|
3262
|
+
|
3263
|
+
}(jQuery, window, document));
|