(function () { 'use strict'; var app = angular.module('comments'); var templatePath = modulesSharedResourcesUrl + 'Modules/Comments/Views/'; //a directive that renders the list of marking schemes app.directive('comments', ['commentsDataContext', 'commentsService', 'user', 'common', '$location', '$modal', 'myUsersDataContext', function (commentsDataContext, commentsService, user, common, $location, $modal, myUsersDataContext) { return { restrict: 'EA', templateUrl: templatePath + 'comments.html', scope: { externalId: '=', userId: '=', allowPrivate: '=', commentCount: '=', admin: '=' }, link: link }; function link(scope, $scope, elem, attrs) { var getLogFn = common.logger.getLogFn; var logSuccess = getLogFn("comments", "success"); var logError = getLogFn("comments", "error"); scope.comments = []; scope.$watch('externalId', function (newval, oldval) { if (newval) { scope.comments = []; getComments(); } }, true); function getComments() { scope.commentCount = 0; commentsDataContext.getCommentByExternalId(scope.externalId, scope.userId).then(function (data) { for (var i = 0; i < data.length; i++) { scope.commentCount = scope.commentCount + 1; scope.commentCount = scope.commentCount + data[i].childCommentsCount; if (data[i].childComments) { countChildComments(data[i].childComments) } } scope.comments = data; scope.commentsLoaded = true; }); } scope.newComment = commentsService.createBlankComment(scope.externalId); scope.createComment = function (comment, reply, form) { comment.originator = scope.userId; if (!reply) { comment.externalId = scope.externalId; } commentsDataContext.createComment(comment).then(function (data) { scope.commentCount = scope.commentCount + 1; comment.content = ''; comment.addReply = false; comment.isPrivate = false; form.$setPristine(); // We don't want to have to call the service to get comments again so we either push the comment to the root array or we find it's parent and push to it's child comments if (!reply) { scope.comments.push(data); } else { for (var i = 0; i < scope.comments.length; i++) { if (scope.comments[i].id == comment.parentId) { scope.comments[i].childComments.push(data); return; } if (scope.comments[i].childComments) { findParentComment(scope.comments[i].childComments, comment.parentId, data); } } } }); } function countChildComments(comments) { for (var i = 0; i < comments.length; i++) { scope.commentCount = scope.commentCount + comments[i].childCommentsCount; if (comments[i].childComments) { countChildComments(comments[i].childComments); } } } function findParentComment(comments, parentId, comment) { for (var i = 0; i < comments.length; i++) { if (comments[i].id == parentId) { comments[i].childComments.push(comment); return comments; } if (comments[i].childComments) { findParentComment(comments[i].childComments, parentId, comment); } // return comments; } } scope.getCommentUser = function (comment) { if (!comment.user) { user.getUserProfile(comment.originator).then(function (data) { comment.user = data; }); } } scope.deleteComment = function (comment) { commentsDataContext.deleteComment(comment).then(function (data) { logSuccess("Comment deleted"); getComments(); }); } } }]); //a directive that renders the list of marking schemes app.directive('getComment', ['commentsDataContext', 'commentsService', 'user', 'common', '$location', '$modal', 'myUsersDataContext', function (commentsDataContext, commentsService, user, common, $location, $modal, myUsersDataContext) { return { restrict: 'EA', templateUrl: templatePath + 'comment.html', scope: { commentId: '=', }, link: link }; function link(scope, $scope, elem, attrs) { var getLogFn = common.logger.getLogFn; var logSuccess = getLogFn("comments", "success"); var logError = getLogFn("comments", "error"); scope.$watch('commentId', function (newval, oldval) { if (newval) { scope.comment = {}; getComment(); } }, true); function getComment() { commentsDataContext.getCommentById(scope.commentId).then(function (data) { scope.comment = data; }); } } }]); })();