/
LessonCommentDrv.js
146 lines (139 loc) · 7.37 KB
/
LessonCommentDrv.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
angular.module('Lesson')
.directive('lessonComment', [
'$rootScope',
'LabelService',
'LessonService',
'AuthService',
'CommentDTO',
'$timeout',
function ($rootScope, LabelService, LessonService, AuthService, CommentDTO, $timeout) {
return {
restrict: 'E',
templateUrl: 'modules/lesson/LessonComment.html',
replace: true,
transclude: false,
scope: {
comment: '=?',
lessonId: '@',
addComment: '&',
deleteComment: '&'
},
link: function (scope, element, attrs) {
//-------- private methods-------
// call Label Service to get dynamic labels
var _getLabel = function (label) {
return LabelService.get('LessonCtrl', label);
}
//-------- private variables-------
var form = element.find('form');
//-------- public properties-------
scope.local = {
commentText: null,
commentError: null,
UserCommentForm: form.controller('form'),
base: angular.isUndefined(scope.comment),
isLogged: AuthService.user.isLogged,
sameUser: scope.comment ? (scope.comment.author.username == AuthService.user.username) : false,
answer: false,
edit: false,
showDeleteCommentErr: false
}
scope.labels = {
comments: _getLabel('comments'),
commentPlaceholder: _getLabel('commentPlaceholder'),
commentHelp: _getLabel('commentHelp'),
commentAnswer: _getLabel('commentAnswer'),
commentEdit: _getLabel('commentEdit'),
commentPreview: _getLabel('commentPreview'),
commentSave: _getLabel('commentSave'),
commentRequired: _getLabel('commentRequired'),
commentNotDelete: _getLabel('commentNotDelete')
};
//-------- public methods-------
scope.actions = {
// call Sign Modal Dialog to login
openSignIn: function () {
$rootScope.$broadcast('disc.login', scope.actions)
},
// save User Comment
saveComment: function () {
// retrieve current form
var localForm = scope.local.UserCommentForm;
var localTxtArea = localForm.CommentTXT;
// check for validation error
if (localTxtArea.$valid) {
var _comment = new CommentDTO();
_comment.lessonId = scope.lessonId;
_comment.content = localTxtArea.$modelValue;
//_comment.date = new Date();
_comment.parentId = scope.comment ? scope.comment.id : null;
_comment.level = scope.comment ? scope.comment.level + 1 : 0;
_comment.author.userid = AuthService.user.userid;
LessonService.saveComment(_comment)
.then(function (savedComment) {
// Parent controll method to add new comment into local lesson's comment array
scope.addComment({ comment: savedComment });
// Reset Aswer textarea
if (!scope.local.base) {
scope.local.answer = false;
}
scope.local.commentText = "";
localForm.$setPristine();
})
}
},
// Edit User Comment
editComment: function () {
// retrieve current form
var localForm = scope.local.UserEditCommentForm;
var localTxtArea = localForm.CommentTXT;
// check for validation error
if (localTxtArea.$valid) {
var _comment = new CommentDTO();
angular.extend(_comment, scope.comment);
_comment.content = localTxtArea.$modelValue
LessonService.editComment(_comment)
.then(function (modifiedComment) {
// Parent controll method to add new comment into local lesson's comment array
scope.comment = modifiedComment;
scope.local.edit = false;
localForm.$setPristine();
})
}
},
// Delete User Comment
deleteComment: function () {
LessonService.deleteComment(scope.comment)
.then(
function (deletedComment) {
// Parent controll method to add new comment into local lesson's comment array
scope.deleteComment({ comment: deletedComment });
},
function (errorData) {
scope.local.showDeleteCommentErr = true;
$timeout(function () { scope.local.showDeleteCommentErr = false }, 5000);
}
)
},
// check for authentication and open/close user comment textarea
openUserComment: function () {
if (!scope.local.isLogged) {
!scope.actions.openSignIn();
}
scope.local.answer = !scope.local.answer;
}
}
//-------- Initialization -------
// Watcher for authentication depending behaviours
scope.$watch(function () {
return AuthService.user.isLogged;
},
function () {
scope.local.isLogged = AuthService.user.isLogged;
scope.local.sameUser = scope.comment ? (scope.comment.author.username == AuthService.user.username) : false;
}
);
}
}
}
])