(function(){ 'use strict'; app.controller('Form',['$scope', '$rootScope', 'comment.repository', function($scope, $rootScope, commentRepository,){ // Mod FUNCTION $scope.editModToggle = function(id){ for (let i = 0; i < $scope.mainComments.length; i++) { if($scope.mainComments[i].id === id){ $scope.mainComments[i].isEditMod = !$scope.mainComments[i].isEditMod } } }; $scope.editModSecondToggle = function(mainId, secondId){ for (let i = 0; i < $scope.mainComments.length; i++) { for (let j = 0; j < $scope.mainComments[i].secondComments.length; j++) { if ($scope.mainComments[i].secondComments[j].mainId === mainId) { if ($scope.mainComments[i].secondComments[j].id === secondId) { $scope.mainComments[i].secondComments[j].isEditMod = !$scope.mainComments[i].secondComments[j].isEditMod } } } } }; $scope.replyModToggle = function(id){ for (let i = 0; i < $scope.mainComments.length; i++) { if($scope.mainComments[i].id === id){ $scope.mainComments[i].isReplyMod = !$scope.mainComments[i].isReplyMod } } }; // MAIN Comment commentRepository.getComments().then(function(response){ $scope.mainComments = response.data }, function(error) { console.log(error) }); $scope.newComment = { authorName: '', foto: '', commentText: '', isEditMod: false, isReplyMod: false, secondComments: [] } $scope.addComment = function(){ $scope.newComment.id = new Date().getTime(); $scope.mainComments.unshift($scope.newComment) commentRepository.addComment($scope.newComment).then(function(response){ console.log(response.data) }, function(error) { console.log(error) }); $scope.newComment = { authorName: '', foto: '', commentText: '', isEditMod: false, isReplyMod: false, secondComments: [] }; }; $scope.deleteMainComment = function(id){ commentRepository.deleteComment(id).then(function(response){ console.log(response.data) }, function(error) { console.log(error) }); for (let i = 0; i < $scope.mainComments.length; i++) { if($scope.mainComments[i].id === id){ $scope.mainComments.splice(i, 1); } } return $scope.mainComments }; $scope.editMainComment = function(id){ $scope.editModToggle(id) $rootScope.$broadcast('EditComment', $scope.mainComments, id); }; $rootScope.$on('saveEdit', function(event, comment, id){ $scope.editModToggle(id) commentRepository.editComment(comment).then(function(response){ console.log(response.data) }, function(error) { console.log(error) }); for (let i = 0; i < $scope.mainComments.length; i++) { if ($scope.mainComments[i].id === comment.id) { $scope.mainComments.splice(i,1,comment); } } }); $rootScope.$on('cancelEdit', function(event,id){ $scope.editModToggle(id) }) // SECOND COMMENTS $scope.deleteSecondComment = function(mainId, secondId){ commentRepository.deleteSecondComment(mainId, secondId).then(function(response){ console.log(response.data) }, function(error) { console.log(error) }); for (let i = 0; i < $scope.mainComments.length; i++) { for (let j = 0; j < $scope.mainComments[i].secondComments.length; j++) { if ($scope.mainComments[i].secondComments[j].mainId === mainId) { if ($scope.mainComments[i].secondComments[j].id === secondId) { $scope.mainComments[i].secondComments.splice(j,1); } } } } }; $scope.editSecondComment = function(mainId, secondId){ $scope.editModSecondToggle(mainId,secondId) $rootScope.$broadcast('EditSecondComment', $scope.mainComments, mainId, secondId); } $rootScope.$on('SaveSecondEdit', function(event,comment, mainId, secondId){ $scope.editModSecondToggle(mainId,secondId) commentRepository.editSecondComment(comment).then(function(response){ console.log(response.data) }, function(error) { console.log(error) }); for (let i = 0; i < $scope.mainComments.length; i++) { for (let j = 0; j < $scope.mainComments[i].secondComments.length; j++) { if ($scope.mainComments[i].secondComments[j].mainId === comment.mainId) { if ($scope.mainComments[i].secondComments[j].id === comment.id) { $scope.mainComments[i].secondComments.splice(j,1,comment); } } } } }); $rootScope.$on('CancelSecondEdit', function(event, mainId, secondId){ $scope.editModSecondToggle(mainId,secondId) }); // REPLY $scope.reply = function(id){ $scope.replyModToggle(id) $rootScope.$broadcast('Reply', $scope.mainComments, id); }; $rootScope.$on('commentReply', function(event,comment, id){ $scope.replyModToggle(id) commentRepository.commentReply(comment).then(function(response){ console.log(response.data) }, function(error) { console.log(error) }); for (let i = 0; i < $scope.mainComments.length; i++) { if ($scope.mainComments[i].id === comment.mainId) { $scope.mainComments[i].secondComments.unshift(comment); } } }); $rootScope.$on('cancelReplyMod', function(event, id){ $scope.replyModToggle(id) }) }]) })()