-
Notifications
You must be signed in to change notification settings - Fork 41
/
angular-vertilize.js
102 lines (90 loc) · 2.77 KB
/
angular-vertilize.js
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
/*!
* angular-vertilize 1.0.1
* Christopher Collins
* https://github.com/Sixthdim/angular-vertilize.git
* License: MIT
*/
(function () {
"use strict";
var module = angular.module('angular.vertilize', []);
// Vertilize Container
module.directive('vertilizeContainer', [
function(){
return {
restrict: 'EA',
controller: [
'$scope', '$window',
function($scope, $window){
// Alias this
var _this = this;
// Array of children heights
_this.childrenHeights = [];
// API: Allocate child, return index for tracking.
_this.allocateMe = function(){
_this.childrenHeights.push(0);
return (_this.childrenHeights.length - 1);
};
// API: Update a child's height
_this.updateMyHeight = function(index, height){
_this.childrenHeights[index] = height;
};
// API: Get tallest height
_this.getTallestHeight = function(){
var height = 0;
for (var i=0; i < _this.childrenHeights.length; i=i+1){
height = Math.max(height, _this.childrenHeights[i]);
}
return height;
};
// Add window resize to digest cycle
angular.element($window).bind('resize', function(){
return $scope.$apply();
});
}
]
};
}
]);
// Vertilize Item
module.directive('vertilize', [
function(){
return {
restrict: 'EA',
require: '^vertilizeContainer',
link: function(scope, element, attrs, parent){
// My index allocation
var myIndex = parent.allocateMe();
// Get my real height by cloning so my height is not affected.
var getMyRealHeight = function(){
var clone = element.clone()
.removeAttr('vertilize')
.css({
height: '',
width: element.outerWidth(),
position: 'fixed',
top: 0,
left: 0,
visibility: 'hidden'
});
element.after(clone);
var realHeight = clone.height();
clone['remove']();
return realHeight;
};
// Watch my height
scope.$watch(getMyRealHeight, function(myNewHeight){
if (myNewHeight){
parent.updateMyHeight(myIndex, myNewHeight);
}
});
// Watch for tallest height change
scope.$watch(parent.getTallestHeight, function(tallestHeight){
if (tallestHeight){
element.css('height', tallestHeight);
}
});
}
};
}
]);
}());