123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- (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)
- })
- }])
- })()
|