-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathangular-gridster.js
More file actions
executable file
·2189 lines (1881 loc) · 62.2 KB
/
angular-gridster.js
File metadata and controls
executable file
·2189 lines (1881 loc) · 62.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*global define:true*/
(function(root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD
define(['angular'], factory);
} else if (typeof exports === 'object') {
// CommonJS
module.exports = factory(require('angular'));
} else {
// Browser, nothing "exported". Only registered as a module with angular.
factory(root.angular);
}
}(this, function(angular) {
'use strict';
// This returned angular module 'gridster' is what is exported.
return angular.module('gridster', [])
.constant('gridsterConfig', {
columns: 6, // number of columns in the grid
pushing: true, // whether to push other items out of the way
floating: true, // whether to automatically float items up so they stack
swapping: false, // whether or not to have items switch places instead of push down if they are the same size
width: 'auto', // width of the grid. "auto" will expand the grid to its parent container
colWidth: 'auto', // width of grid columns. "auto" will divide the width of the grid evenly among the columns
rowHeight: 'match', // height of grid rows. 'match' will make it the same as the column width, a numeric value will be interpreted as pixels, '/2' is half the column width, '*5' is five times the column width, etc.
margins: [10, 10], // margins in between grid items
outerMargin: true,
isMobile: false, // toggle mobile view
mobileBreakPoint: 600, // width threshold to toggle mobile mode
mobileModeEnabled: true, // whether or not to toggle mobile mode when screen width is less than mobileBreakPoint
minColumns: 1, // minimum amount of columns the grid can scale down to
minRows: 1, // minimum amount of rows to show if the grid is empty
maxRows: 100, // maximum amount of rows in the grid
defaultSizeX: 2, // default width of an item in columns
defaultSizeY: 1, // default height of an item in rows
minSizeX: 1, // minimum column width of an item
maxSizeX: null, // maximum column width of an item
minSizeY: 1, // minumum row height of an item
maxSizeY: null, // maximum row height of an item
saveGridItemCalculatedHeightInMobile: false, // grid item height in mobile display. true- to use the calculated height by sizeY given
resizable: { // options to pass to resizable handler
enabled: true,
handles: ['s', 'e', 'n', 'w', 'se', 'ne', 'sw', 'nw']
},
draggable: { // options to pass to draggable handler
enabled: true,
scrollSensitivity: 20, // Distance in pixels from the edge of the viewport after which the viewport should scroll, relative to pointer
scrollSpeed: 15 // Speed at which the window should scroll once the mouse pointer gets within scrollSensitivity distance
}
})
.controller('GridsterCtrl', ['gridsterConfig', '$timeout',
function(gridsterConfig, $timeout) {
var gridster = this;
/**
* Create options from gridsterConfig constant
*/
angular.extend(this, gridsterConfig);
this.resizable = angular.extend({}, gridsterConfig.resizable || {});
this.draggable = angular.extend({}, gridsterConfig.draggable || {});
var flag = false;
this.layoutChanged = function() {
if (flag) {
return;
}
flag = true;
$timeout(function() {
flag = false;
if (gridster.loaded) {
gridster.floatItemsUp();
}
gridster.updateHeight(gridster.movingItem ? gridster.movingItem.sizeY : 0);
}, 30);
};
/**
* A positional array of the items in the grid
*/
this.grid = [];
/**
* Clean up after yourself
*/
this.destroy = function() {
// empty the grid to cut back on the possibility
// of circular references
if (this.grid) {
this.grid = [];
}
this.$element = null;
};
/**
* Overrides default options
*
* @param {Object} options The options to override
*/
this.setOptions = function(options) {
if (!options) {
return;
}
options = angular.extend({}, options);
// all this to avoid using jQuery...
if (options.draggable) {
angular.extend(this.draggable, options.draggable);
delete(options.draggable);
}
if (options.resizable) {
angular.extend(this.resizable, options.resizable);
delete(options.resizable);
}
angular.extend(this, options);
if (!this.margins || this.margins.length !== 2) {
this.margins = [0, 0];
} else {
for (var x = 0, l = this.margins.length; x < l; ++x) {
this.margins[x] = parseInt(this.margins[x], 10);
if (isNaN(this.margins[x])) {
this.margins[x] = 0;
}
}
}
};
/**
* Check if item can occupy a specified position in the grid
*
* @param {Object} item The item in question
* @param {Number} row The row index
* @param {Number} column The column index
* @returns {Boolean} True if if item fits
*/
this.canItemOccupy = function(item, row, column) {
return row > -1 && column > -1 && item.sizeX + column <= this.columns && item.sizeY + row <= this.maxRows;
};
/**
* Set the item in the first suitable position
*
* @param {Object} item The item to insert
*/
this.autoSetItemPosition = function(item) {
// walk through each row and column looking for a place it will fit
for (var rowIndex = 0; rowIndex < this.maxRows; ++rowIndex) {
for (var colIndex = 0; colIndex < this.columns; ++colIndex) {
// only insert if position is not already taken and it can fit
var items = this.getItems(rowIndex, colIndex, item.sizeX, item.sizeY, item);
if (items.length === 0 && this.canItemOccupy(item, rowIndex, colIndex)) {
this.putItem(item, rowIndex, colIndex);
return;
}
}
}
throw new Error('Unable to place item!');
};
/**
* Gets items at a specific coordinate
*
* @param {Number} row
* @param {Number} column
* @param {Number} sizeX
* @param {Number} sizeY
* @param {Array} excludeItems An array of items to exclude from selection
* @returns {Array} Items that match the criteria
*/
this.getItems = function(row, column, sizeX, sizeY, excludeItems) {
var items = [];
if (!sizeX || !sizeY) {
sizeX = sizeY = 1;
}
if (excludeItems && !(excludeItems instanceof Array)) {
excludeItems = [excludeItems];
}
for (var h = 0; h < sizeY; ++h) {
for (var w = 0; w < sizeX; ++w) {
var item = this.getItem(row + h, column + w, excludeItems);
if (item && (!excludeItems || excludeItems.indexOf(item) === -1) && items.indexOf(item) === -1) {
items.push(item);
}
}
}
return items;
};
/**
* @param {Array} items
* @returns {Object} An item that represents the bounding box of the items
*/
this.getBoundingBox = function(items) {
if (items.length === 0) {
return null;
}
if (items.length === 1) {
return {
row: items[0].row,
col: items[0].col,
sizeY: items[0].sizeY,
sizeX: items[0].sizeX
};
}
var maxRow = 0;
var maxCol = 0;
var minRow = 9999;
var minCol = 9999;
for (var i = 0, l = items.length; i < l; ++i) {
var item = items[i];
minRow = Math.min(item.row, minRow);
minCol = Math.min(item.col, minCol);
maxRow = Math.max(item.row + item.sizeY, maxRow);
maxCol = Math.max(item.col + item.sizeX, maxCol);
}
return {
row: minRow,
col: minCol,
sizeY: maxRow - minRow,
sizeX: maxCol - minCol
};
};
/**
* Removes an item from the grid
*
* @param {Object} item
*/
this.removeItem = function(item) {
for (var rowIndex = 0, l = this.grid.length; rowIndex < l; ++rowIndex) {
var columns = this.grid[rowIndex];
if (!columns) {
continue;
}
var index = columns.indexOf(item);
if (index !== -1) {
columns[index] = null;
break;
}
}
this.layoutChanged();
};
/**
* Returns the item at a specified coordinate
*
* @param {Number} row
* @param {Number} column
* @param {Array} excludeItems Items to exclude from selection
* @returns {Object} The matched item or null
*/
this.getItem = function(row, column, excludeItems) {
if (excludeItems && !(excludeItems instanceof Array)) {
excludeItems = [excludeItems];
}
var sizeY = 1;
while (row > -1) {
var sizeX = 1,
col = column;
while (col > -1) {
var items = this.grid[row];
if (items) {
var item = items[col];
if (item && (!excludeItems || excludeItems.indexOf(item) === -1) && item.sizeX >= sizeX && item.sizeY >= sizeY) {
return item;
}
}
++sizeX;
--col;
}
--row;
++sizeY;
}
return null;
};
/**
* Insert an array of items into the grid
*
* @param {Array} items An array of items to insert
*/
this.putItems = function(items) {
for (var i = 0, l = items.length; i < l; ++i) {
this.putItem(items[i]);
}
};
/**
* Insert a single item into the grid
*
* @param {Object} item The item to insert
* @param {Number} row (Optional) Specifies the items row index
* @param {Number} column (Optional) Specifies the items column index
* @param {Array} ignoreItems
*/
this.putItem = function(item, row, column, ignoreItems) {
// auto place item if no row specified
if (typeof row === 'undefined' || row === null) {
row = item.row;
column = item.col;
if (typeof row === 'undefined' || row === null) {
this.autoSetItemPosition(item);
return;
}
}
// keep item within allowed bounds
if (!this.canItemOccupy(item, row, column)) {
column = Math.min(this.columns - item.sizeX, Math.max(0, column));
row = Math.min(this.maxRows - item.sizeY, Math.max(0, row));
}
// check if item is already in grid
if (item.oldRow !== null && typeof item.oldRow !== 'undefined') {
var samePosition = item.oldRow === row && item.oldColumn === column;
var inGrid = this.grid[row] && this.grid[row][column] === item;
if (samePosition && inGrid) {
item.row = row;
item.col = column;
return;
} else {
// remove from old position
var oldRow = this.grid[item.oldRow];
if (oldRow && oldRow[item.oldColumn] === item) {
delete oldRow[item.oldColumn];
}
}
}
item.oldRow = item.row = row;
item.oldColumn = item.col = column;
this.moveOverlappingItems(item, ignoreItems);
if (!this.grid[row]) {
this.grid[row] = [];
}
this.grid[row][column] = item;
if (this.movingItem === item) {
this.floatItemUp(item);
}
this.layoutChanged();
};
/**
* Trade row and column if item1 with item2
*
* @param {Object} item1
* @param {Object} item2
*/
this.swapItems = function(item1, item2) {
this.grid[item1.row][item1.col] = item2;
this.grid[item2.row][item2.col] = item1;
var item1Row = item1.row;
var item1Col = item1.col;
item1.row = item2.row;
item1.col = item2.col;
item2.row = item1Row;
item2.col = item1Col;
};
/**
* Prevents items from being overlapped
*
* @param {Object} item The item that should remain
* @param {Array} ignoreItems
*/
this.moveOverlappingItems = function(item, ignoreItems) {
// don't move item, so ignore it
if (!ignoreItems) {
ignoreItems = [item];
} else if (ignoreItems.indexOf(item) === -1) {
ignoreItems = ignoreItems.slice(0);
ignoreItems.push(item);
}
// get the items in the space occupied by the item's coordinates
var overlappingItems = this.getItems(
item.row,
item.col,
item.sizeX,
item.sizeY,
ignoreItems
);
this.moveItemsDown(overlappingItems, item.row + item.sizeY, ignoreItems);
};
/**
* Moves an array of items to a specified row
*
* @param {Array} items The items to move
* @param {Number} newRow The target row
* @param {Array} ignoreItems
*/
this.moveItemsDown = function(items, newRow, ignoreItems) {
if (!items || items.length === 0) {
return;
}
items.sort(function(a, b) {
return a.row - b.row;
});
ignoreItems = ignoreItems ? ignoreItems.slice(0) : [];
var topRows = {},
item, i, l;
// calculate the top rows in each column
for (i = 0, l = items.length; i < l; ++i) {
item = items[i];
var topRow = topRows[item.col];
if (typeof topRow === 'undefined' || item.row < topRow) {
topRows[item.col] = item.row;
}
}
// move each item down from the top row in its column to the row
for (i = 0, l = items.length; i < l; ++i) {
item = items[i];
var rowsToMove = newRow - topRows[item.col];
this.moveItemDown(item, item.row + rowsToMove, ignoreItems);
ignoreItems.push(item);
}
};
/**
* Moves an item down to a specified row
*
* @param {Object} item The item to move
* @param {Number} newRow The target row
* @param {Array} ignoreItems
*/
this.moveItemDown = function(item, newRow, ignoreItems) {
if (item.row >= newRow) {
return;
}
while (item.row < newRow) {
++item.row;
this.moveOverlappingItems(item, ignoreItems);
}
this.putItem(item, item.row, item.col, ignoreItems);
};
/**
* Moves all items up as much as possible
*/
this.floatItemsUp = function() {
if (this.floating === false) {
return;
}
for (var rowIndex = 0, l = this.grid.length; rowIndex < l; ++rowIndex) {
var columns = this.grid[rowIndex];
if (!columns) {
continue;
}
for (var colIndex = 0, len = columns.length; colIndex < len; ++colIndex) {
var item = columns[colIndex];
if (item) {
this.floatItemUp(item);
}
}
}
};
/**
* Float an item up to the most suitable row
*
* @param {Object} item The item to move
*/
this.floatItemUp = function(item) {
if (this.floating === false) {
return;
}
var colIndex = item.col,
sizeY = item.sizeY,
sizeX = item.sizeX,
bestRow = null,
bestColumn = null,
rowIndex = item.row - 1;
while (rowIndex > -1) {
var items = this.getItems(rowIndex, colIndex, sizeX, sizeY, item);
if (items.length !== 0) {
break;
}
bestRow = rowIndex;
bestColumn = colIndex;
--rowIndex;
}
if (bestRow !== null) {
this.putItem(item, bestRow, bestColumn);
}
};
/**
* Update gridsters height
*
* @param {Number} plus (Optional) Additional height to add
*/
this.updateHeight = function(plus) {
var maxHeight = this.minRows;
plus = plus || 0;
for (var rowIndex = this.grid.length; rowIndex >= 0; --rowIndex) {
var columns = this.grid[rowIndex];
if (!columns) {
continue;
}
for (var colIndex = 0, len = columns.length; colIndex < len; ++colIndex) {
if (columns[colIndex]) {
maxHeight = Math.max(maxHeight, rowIndex + plus + columns[colIndex].sizeY);
}
}
}
this.gridHeight = this.maxRows - maxHeight > 0 ? Math.min(this.maxRows, maxHeight) : Math.max(this.maxRows, maxHeight);
};
/**
* Returns the number of rows that will fit in given amount of pixels
*
* @param {Number} pixels
* @param {Boolean} ceilOrFloor (Optional) Determines rounding method
*/
this.pixelsToRows = function(pixels, ceilOrFloor) {
if (!this.outerMargin) {
pixels += this.margins[0] / 2;
}
if (ceilOrFloor === true) {
return Math.ceil(pixels / this.curRowHeight);
} else if (ceilOrFloor === false) {
return Math.floor(pixels / this.curRowHeight);
}
return Math.round(pixels / this.curRowHeight);
};
/**
* Returns the number of columns that will fit in a given amount of pixels
*
* @param {Number} pixels
* @param {Boolean} ceilOrFloor (Optional) Determines rounding method
* @returns {Number} The number of columns
*/
this.pixelsToColumns = function(pixels, ceilOrFloor) {
if (!this.outerMargin) {
pixels += this.margins[1] / 2;
}
if (ceilOrFloor === true) {
return Math.ceil(pixels / this.curColWidth);
} else if (ceilOrFloor === false) {
return Math.floor(pixels / this.curColWidth);
}
return Math.round(pixels / this.curColWidth);
};
}
])
.directive('gridsterPreview', function() {
return {
replace: true,
scope: true,
require: '^gridster',
template: '<div ng-style="previewStyle()" class="gridster-item gridster-preview-holder"></div>',
link: function(scope, $el, attrs, gridster) {
/**
* @returns {Object} style object for preview element
*/
scope.previewStyle = function() {
if (!gridster.movingItem) {
return {
display: 'none'
};
}
return {
display: 'block',
height: (gridster.movingItem.sizeY * gridster.curRowHeight - gridster.margins[0]) + 'px',
width: (gridster.movingItem.sizeX * gridster.curColWidth - gridster.margins[1]) + 'px',
top: (gridster.movingItem.row * gridster.curRowHeight + (gridster.outerMargin ? gridster.margins[0] : 0)) + 'px',
left: (gridster.movingItem.col * gridster.curColWidth + (gridster.outerMargin ? gridster.margins[1] : 0)) + 'px'
};
};
}
};
})
/**
* The gridster directive
*
* @param {Function} $timeout
* @param {Object} $window
* @param {Object} $rootScope
* @param {Function} gridsterDebounce
*/
.directive('gridster', ['$timeout', '$window', '$rootScope', 'gridsterDebounce',
function($timeout, $window, $rootScope, gridsterDebounce) {
return {
scope: true,
restrict: 'EAC',
controller: 'GridsterCtrl',
controllerAs: 'gridster',
compile: function($tplElem) {
$tplElem.prepend('<div ng-if="gridster.movingItem" gridster-preview></div>');
return function(scope, $elem, attrs, gridster) {
gridster.loaded = false;
gridster.$element = $elem;
scope.gridster = gridster;
$elem.addClass('gridster');
var isVisible = function(ele) {
return ele.style.visibility !== 'hidden' && ele.style.display !== 'none';
};
function refresh(config) {
gridster.setOptions(config);
if (!isVisible($elem[0])) {
return;
}
// resolve "auto" & "match" values
if (gridster.width === 'auto') {
gridster.curWidth = $elem[0].offsetWidth || parseInt($elem.css('width'), 10);
} else {
gridster.curWidth = gridster.width;
}
if (gridster.colWidth === 'auto') {
gridster.curColWidth = (gridster.curWidth + (gridster.outerMargin ? -gridster.margins[1] : gridster.margins[1])) / gridster.columns;
} else {
gridster.curColWidth = gridster.colWidth;
}
gridster.curRowHeight = gridster.rowHeight;
if (typeof gridster.rowHeight === 'string') {
if (gridster.rowHeight === 'match') {
gridster.curRowHeight = Math.round(gridster.curColWidth);
} else if (gridster.rowHeight.indexOf('*') !== -1) {
gridster.curRowHeight = Math.round(gridster.curColWidth * gridster.rowHeight.replace('*', '').replace(' ', ''));
} else if (gridster.rowHeight.indexOf('/') !== -1) {
gridster.curRowHeight = Math.round(gridster.curColWidth / gridster.rowHeight.replace('/', '').replace(' ', ''));
}
}
gridster.isMobile = gridster.mobileModeEnabled && gridster.curWidth <= gridster.mobileBreakPoint;
// loop through all items and reset their CSS
for (var rowIndex = 0, l = gridster.grid.length; rowIndex < l; ++rowIndex) {
var columns = gridster.grid[rowIndex];
if (!columns) {
continue;
}
for (var colIndex = 0, len = columns.length; colIndex < len; ++colIndex) {
if (columns[colIndex]) {
var item = columns[colIndex];
item.setElementPosition();
item.setElementSizeY();
item.setElementSizeX();
}
}
}
updateHeight();
}
var optionsKey = attrs.gridster;
if (optionsKey) {
scope.$parent.$watch(optionsKey, function(newConfig) {
refresh(newConfig);
}, true);
} else {
refresh({});
}
scope.$watch(function() {
return gridster.loaded;
}, function() {
if (gridster.loaded) {
$elem.addClass('gridster-loaded');
} else {
$elem.removeClass('gridster-loaded');
}
});
scope.$watch(function() {
return gridster.isMobile;
}, function() {
if (gridster.isMobile) {
$elem.addClass('gridster-mobile').removeClass('gridster-desktop');
} else {
$elem.removeClass('gridster-mobile').addClass('gridster-desktop');
}
$rootScope.$broadcast('gridster-mobile-changed', gridster);
});
scope.$watch(function() {
return gridster.draggable;
}, function() {
$rootScope.$broadcast('gridster-draggable-changed', gridster);
}, true);
scope.$watch(function() {
return gridster.resizable;
}, function() {
$rootScope.$broadcast('gridster-resizable-changed', gridster);
}, true);
function updateHeight() {
$elem.css('height', (gridster.gridHeight * gridster.curRowHeight) + (gridster.outerMargin ? gridster.margins[0] : -gridster.margins[0]) + 'px');
}
scope.$watch(function() {
return gridster.gridHeight;
}, updateHeight);
scope.$watch(function() {
return gridster.movingItem;
}, function() {
gridster.updateHeight(gridster.movingItem ? gridster.movingItem.sizeY : 0);
});
var prevWidth = $elem[0].offsetWidth || parseInt($elem.css('width'), 10);
var resize = function() {
var width = $elem[0].offsetWidth || parseInt($elem.css('width'), 10);
if (!width || width === prevWidth || gridster.movingItem) {
return;
}
prevWidth = width;
if (gridster.loaded) {
$elem.removeClass('gridster-loaded');
}
refresh();
if (gridster.loaded) {
$elem.addClass('gridster-loaded');
}
$rootScope.$broadcast('gridster-resized', [width, $elem[0].offsetHeight], gridster);
};
// track element width changes any way we can
var onResize = gridsterDebounce(function onResize() {
resize();
$timeout(function() {
scope.$apply();
});
}, 100);
scope.$watch(function() {
return isVisible($elem[0]);
}, onResize);
// see https://github.com/sdecima/javascript-detect-element-resize
if (typeof window.addResizeListener === 'function') {
window.addResizeListener($elem[0], onResize);
} else {
scope.$watch(function() {
return $elem[0].offsetWidth || parseInt($elem.css('width'), 10);
}, resize);
}
var $win = angular.element($window);
$win.on('resize', onResize);
// be sure to cleanup
scope.$on('$destroy', function() {
gridster.destroy();
$win.off('resize', onResize);
if (typeof window.removeResizeListener === 'function') {
window.removeResizeListener($elem[0], onResize);
}
});
// allow a little time to place items before floating up
$timeout(function() {
scope.$watch('gridster.floating', function() {
gridster.floatItemsUp();
});
gridster.loaded = true;
}, 100);
};
}
};
}
])
.controller('GridsterItemCtrl', function() {
this.$element = null;
this.gridster = null;
this.row = null;
this.col = null;
this.sizeX = null;
this.sizeY = null;
this.minSizeX = 0;
this.minSizeY = 0;
this.maxSizeX = null;
this.maxSizeY = null;
this.init = function($element, gridster) {
this.$element = $element;
this.gridster = gridster;
this.sizeX = gridster.defaultSizeX;
this.sizeY = gridster.defaultSizeY;
};
this.destroy = function() {
// set these to null to avoid the possibility of circular references
this.gridster = null;
this.$element = null;
};
/**
* Returns the items most important attributes
*/
this.toJSON = function() {
return {
row: this.row,
col: this.col,
sizeY: this.sizeY,
sizeX: this.sizeX
};
};
this.isMoving = function() {
return this.gridster.movingItem === this;
};
/**
* Set the items position
*
* @param {Number} row
* @param {Number} column
*/
this.setPosition = function(row, column) {
this.gridster.putItem(this, row, column);
if (!this.isMoving()) {
this.setElementPosition();
}
};
/**
* Sets a specified size property
*
* @param {String} key Can be either "x" or "y"
* @param {Number} value The size amount
* @param {Boolean} preventMove
*/
this.setSize = function(key, value, preventMove) {
key = key.toUpperCase();
var camelCase = 'size' + key,
titleCase = 'Size' + key;
if (value === '') {
return;
}
value = parseInt(value, 10);
if (isNaN(value) || value === 0) {
value = this.gridster['default' + titleCase];
}
var max = key === 'X' ? this.gridster.columns : this.gridster.maxRows;
if (this['max' + titleCase]) {
max = Math.min(this['max' + titleCase], max);
}
if (this.gridster['max' + titleCase]) {
max = Math.min(this.gridster['max' + titleCase], max);
}
if (key === 'X' && this.cols) {
max -= this.cols;
} else if (key === 'Y' && this.rows) {
max -= this.rows;
}
var min = 0;
if (this['min' + titleCase]) {
min = Math.max(this['min' + titleCase], min);
}
if (this.gridster['min' + titleCase]) {
min = Math.max(this.gridster['min' + titleCase], min);
}
value = Math.max(Math.min(value, max), min);
var changed = (this[camelCase] !== value || (this['old' + titleCase] && this['old' + titleCase] !== value));
this['old' + titleCase] = this[camelCase] = value;
if (!this.isMoving()) {
this['setElement' + titleCase]();
}
if (!preventMove && changed) {
this.gridster.moveOverlappingItems(this);
this.gridster.layoutChanged();
}
return changed;
};
/**
* Sets the items sizeY property
*
* @param {Number} rows
* @param {Boolean} preventMove
*/
this.setSizeY = function(rows, preventMove) {
return this.setSize('Y', rows, preventMove);
};
/**
* Sets the items sizeX property
*
* @param {Number} columns
* @param {Boolean} preventMove
*/
this.setSizeX = function(columns, preventMove) {
return this.setSize('X', columns, preventMove);
};
/**
* Sets an elements position on the page
*/
this.setElementPosition = function() {
if (this.gridster.isMobile) {
this.$element.css({
marginLeft: this.gridster.margins[0] + 'px',
marginRight: this.gridster.margins[0] + 'px',
marginTop: this.gridster.margins[1] + 'px',
marginBottom: this.gridster.margins[1] + 'px',
top: '',
left: ''
});
} else {
this.$element.css({
margin: 0,
top: (this.row * this.gridster.curRowHeight + (this.gridster.outerMargin ? this.gridster.margins[0] : 0)) + 'px',
left: (this.col * this.gridster.curColWidth + (this.gridster.outerMargin ? this.gridster.margins[1] : 0)) + 'px'
});
}
};
/**
* Sets an elements height
*/
this.setElementSizeY = function() {
if (this.gridster.isMobile && !this.gridster.saveGridItemCalculatedHeightInMobile) {
this.$element.css('height', '');
} else {
this.$element.css('height', (this.sizeY * this.gridster.curRowHeight - this.gridster.margins[0]) + 'px');
}
};
/**
* Sets an elements width
*/
this.setElementSizeX = function() {
if (this.gridster.isMobile) {
this.$element.css('width', '');
} else {
this.$element.css('width', (this.sizeX * this.gridster.curColWidth - this.gridster.margins[1]) + 'px');
}
};
/**
* Gets an element's width
*/
this.getElementSizeX = function() {
return (this.sizeX * this.gridster.curColWidth - this.gridster.margins[1]);
};
/**
* Gets an element's height
*/
this.getElementSizeY = function() {
return (this.sizeY * this.gridster.curRowHeight - this.gridster.margins[0]);