-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathng-clamp.js
137 lines (107 loc) · 3.33 KB
/
ng-clamp.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
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
(function() {
angular
.module('directives.clamp', [])
.directive('clamp', clampDirective);
function clampDirective($timeout, $rootScope) {
var directive = {
restrict: 'A',
scope: {
clampExpandable: '@',
disabledForSeo: '@',
clampAutoresize: '@'
},
link: linkDirective
};
return directive;
function linkDirective(scope, element, attrs) {
var originalHtml = '';
var reset = function reset() {
$timeout(function() {
element.css('display', 'block');
originalHtml = element.html();
doClamp(scope, element, attrs, originalHtml);
});
};
// ==========
if (scope.clampAutoresize === 'true') {
$rootScope.$on('window_resized', function() {
doClamp(scope, element, attrs, originalHtml);
});
}
// hide the element for a bit to prevent flicker
element.css('display', 'none');
reset();
// a way to refresh all clampings without data binding
scope.$on('clampReset', reset);
}
}
return;
function doClamp(scope, element, attrs, originalHtml) {
var lineCount = 1, lineMax = isNaN(attrs.clamp) ? 3 : +attrs.clamp;
var lineStart = 0, lineEnd = 0;
var expandable = scope.clampExpandable === 'true';
var shouldExpand = false;
// reset
element.html(originalHtml);
// 2 things:
// - we need only the text for clamping & remove excessive spaces (>1 to 1)
// - append a whitespace, so that the exact last word (rare case) will be calculated
var text = element.text().replace(/\s+/g, ' ') + ' ';
var maxWidth = element[0].offsetWidth ;
var estimateTag = createElement();
element.empty();
element.append(estimateTag);
text.replace(/ /g, function(m, pos) {
if (lineCount <= lineMax) {
estimateTag.html(text.slice(lineStart, pos));
if (estimateTag[0].offsetWidth > maxWidth) {
lineCount++;
if (lineCount > lineMax) {
shouldExpand = true;
return;
}
estimateTag.html(text.slice(lineStart, lineEnd));
resetElement(estimateTag);
lineStart = lineEnd + 1;
estimateTag = createElement();
element.append(estimateTag);
}
lineEnd = pos;
}
});
estimateTag.html(text.slice(lineStart));
resetElement(estimateTag, true);
if (expandable && shouldExpand) {
element.css('cursor', 'pointer');
// clean up
element.off('click');
element.one('click', function() {
element.html(originalHtml);
element.css('cursor', 'default');
});
var showMore = angular.element('<a style="display: block; text-align: center">Read more</a>');
element.append(showMore);
}
}
function createElement() {
var tagDiv = document.createElement('div');
(function(s) {
s.position = 'absolute';
s.whiteSpace = 'pre';
s.visibility = 'hidden';
s.display = 'inline-block';
})(tagDiv.style);
return angular.element(tagDiv);
}
function resetElement(element, type) {
element.css({
position: 'inherit',
overflow: 'hidden',
display: 'block',
textOverflow: (type ? 'ellipsis' : 'clip'),
visibility: 'inherit',
whiteSpace: 'nowrap',
width: '100%'
});
}
})();