forked from ClickerMonkey/SemanticUI-Angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sm-comment.js
132 lines (108 loc) · 3.97 KB
/
sm-comment.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
(function(app)
{
app
.controller('SemanticCommentsController', ['$scope', SemanticCommentsController])
.directive('smComments', ['SemanticUI', SemanticComments])
;
function SemanticComments(SemanticUI)
{
return {
restrict: 'E',
replace: true,
// transclude: true,
scope: {
/* Required */
comments: '=',
content: '&',
/* Optional */
avatar: '&',
author: '&',
date: '&',
replies: '&',
reply: '=',
collapsible: '=',
onAuthor: '&',
onReply: '&',
onShowReplies: '&',
onHideReplies: '&'
},
template: [
'<div class="ui comments">',
' <div class="comment" ng-repeat="c in comments" ng-init="$ = {comment: c}; c.$isCollapsed = true;">',
' <a ng-if="avatar($)" class="avatar" ng-click="onAuthor({comment: c, $event: $event})">',
' <img ng-src="{{ avatar($) }}">',
' </a>',
' <div class="content">',
' <a class="author" ng-click="onAuthor({comment: c, $event: $event})" sm-html="author($)"></a>',
' <div class="metadata">',
' <span class="date" sm-time-ago="date($)"></span>',
' </div>',
' <div class="text" sm-html="content($)"></div>',
' <div class="actions">',
' <a class="reply" ng-click="onReply({comment: c, $event: $event})" ng-if="reply">Reply</a>',
' <a class="show-replies" ng-if="reply && collapsible && c.$isCollapsed" href ng-click="setCollapsed(c, $event, false)" sm-html="getShowRepliesText($)"></a>',
' <a class="hide-replies" ng-if="reply && collapsible && !c.$isCollapsed" href ng-click="setCollapsed(c, $event, true)" sm-html="getHideRepliesText($)"></a>',
' </div>',
' </div>',
' <sm-comments ng-if="hasReplies($)" ng-class="{collapsed: collapsible && c.$isCollapsed}" comments="replies($)" content="content({comment: comment})" avatar="avatar({comment: comment})" author="author({comment: comment})" date="date({comment: comment})" replies="replies({comment: comment})" reply="reply" collapsible="collapsible"',
' on-author="onAuthor({comment: comment, $event: $event})" on-reply="onReply({comment: comment, $event: $event})" on-show-replies="onShowReplies({comment: comment, $event: $event})" on-hide-replies="onHideReplies({comment: comment, $event: $event})"></sm-comments>',
' </div>',
'</div>'
].join('\n'),
controller: 'SemanticCommentsController',
compile: SemanticUI.RecursiveCompiler()
};
}
function SemanticCommentsController($scope)
{
$scope.setCollapsed = function(comment, $event, collapse)
{
var $ = {comment: comment, $event: $event};
if ( comment.$isCollapsed != collapse )
{
if ( comment.$isCollapsed )
{
if ( $scope.onShowReplies($) !== false )
{
comment.$isCollapsed = false;
}
}
else
{
if ( $scope.onHideReplies($) !== false )
{
comment.$isCollapsed = true;
}
}
}
};
$scope.hasReplies = function($)
{
if ( !$scope.reply )
{
return false;
}
var replies = $scope.replies($);
return replies && replies.length;
};
$scope.getReplyCount = function($)
{
if ( !$scope.reply )
{
return false;
}
var replies = $scope.replies($);
return replies ? replies.length : 0;
};
$scope.getShowRepliesText = function($)
{
var count = $scope.getReplyCount($);
return count === 0 ? '' : (count === 1 ? 'Show Reply' : 'Show Replies (' + count + ')');
};
$scope.getHideRepliesText = function($)
{
var count = $scope.getReplyCount($);
return count === 0 ? '' : (count === 1 ? 'Hide Reply' : 'Hide Replies (' + count + ')');
};
}
})( angular.module('semantic-ui-comment', ['semantic-ui-core', 'semantic-ui-timeago']) );