forked from PavelKonon/ng-text-truncate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ng-text-truncate-patched.js
147 lines (118 loc) · 6.59 KB
/
ng-text-truncate-patched.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
138
139
140
141
142
143
144
145
146
147
(function() {
'use strict';
angular.module( 'ngTextTruncate', [] )
.directive( "ngTextTruncate", [ "$compile", "ValidationServices", "CharBasedTruncation", "WordBasedTruncation",
function( $compile, ValidationServices, CharBasedTruncation, WordBasedTruncation ) {
return {
restrict: "A",
scope: {
text: "=ngTextTruncate",
charsThreshould: "@ngTtCharsThreshold",
wordsThreshould: "@ngTtWordsThreshold",
customMoreLabel: "@ngTtMoreLabel",
customLessLabel: "@ngTtLessLabel"
},
controller: [ "$scope", "$element", "$attrs",
function( $scope, $element, $attrs ) {
$scope.toggleShow = function() {
$scope.open = !$scope.open;
};
$scope.useToggling = $attrs.ngTtNoToggling === undefined;
}],
link: function( $scope, $element, $attrs ) {
$scope.open = false;
ValidationServices.failIfWrongThreshouldConfig( $scope.charsThreshould, $scope.wordsThreshould );
var CHARS_THRESHOLD = parseInt( $scope.charsThreshould );
var WORDS_THRESHOLD = parseInt( $scope.wordsThreshould );
$scope.$watch( "text", function() {
$element.empty();
if( CHARS_THRESHOLD ) {
if( $scope.text && CharBasedTruncation.truncationApplies( $scope, CHARS_THRESHOLD ) ) {
CharBasedTruncation.applyTruncation( CHARS_THRESHOLD, $scope, $element );
} else {
$element.append( $scope.text );
}
} else {
if( $scope.text && WordBasedTruncation.truncationApplies( $scope, WORDS_THRESHOLD ) ) {
WordBasedTruncation.applyTruncation( WORDS_THRESHOLD, $scope, $element );
} else {
$element.append( $scope.text );
}
}
} );
}
};
}] )
.factory( "ValidationServices", function() {
return {
failIfWrongThreshouldConfig: function( firstThreshould, secondThreshould ) {
if( (! firstThreshould && ! secondThreshould) || (firstThreshould && secondThreshould) ) {
throw "You must specify one, and only one, type of threshould (chars or words)";
}
}
};
})
.factory( "CharBasedTruncation", [ "$compile", function( $compile ) {
return {
truncationApplies: function( $scope, threshould ) {
return $scope.text.length > threshould;
},
applyTruncation: function( threshould, $scope, $element ) {
if( $scope.useToggling ) {
var el = angular.element( "<span>" +
$scope.text.substr( 0, threshould ) +
"<span ng-show='!open'>...</span>" +
"<span class='btn-link ngTruncateToggleText' " +
"ng-click='toggleShow()'" +
"ng-show='!open'>" +
" " + ($scope.customMoreLabel ? $scope.customMoreLabel : "More") +
"</span>" +
"<span ng-show='open'>" +
$scope.text.substring( threshould ) +
"<span class='btn-link ngTruncateToggleText'" +
"ng-click='toggleShow()'>" +
" " + ($scope.customLessLabel ? $scope.customLessLabel : "Less") +
"</span>" +
"</span>" +
"</span>" );
$compile( el )( $scope );
$element.append( el );
} else {
$element.append( $scope.text.substr( 0, threshould ) + "..." );
}
}
};
}])
.factory( "WordBasedTruncation", [ "$compile", function( $compile ) {
return {
truncationApplies: function( $scope, threshould ) {
return $scope.text.split( " " ).length > threshould;
},
applyTruncation: function( threshould, $scope, $element ) {
var splitText = $scope.text.split( " " );
if( $scope.useToggling ) {
var el = angular.element( "<span>" +
splitText.slice( 0, threshould ).join( " " ) + " " +
"<span ng-show='!open'>...</span>" +
"<span class='btn-link ngTruncateToggleText' " +
"ng-click='toggleShow()'" +
"ng-show='!open'>" +
" " + ($scope.customMoreLabel ? $scope.customMoreLabel : "More") +
"</span>" +
"<span ng-show='open'>" +
splitText.slice( threshould, splitText.length ).join( " " ) +
"<span class='btn-link ngTruncateToggleText'" +
"ng-click='toggleShow()'>" +
" " + ($scope.customLessLabel ? $scope.customLessLabel : "Less") +
"</span>" +
"</span>" +
"</span>" );
$compile( el )( $scope );
$element.append( el );
} else {
$element.append( splitText.slice( 0, threshould ).join( " " ) + "..." );
}
}
};
}]);
}());