form.controller.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. (function(){
  2. 'use strict';
  3. app.controller('Form',['$scope', '$rootScope', 'comment.repository', function($scope, $rootScope, commentRepository,){
  4. // Mod FUNCTION
  5. $scope.editModToggle = function(id){
  6. for (let i = 0; i < $scope.mainComments.length; i++) {
  7. if($scope.mainComments[i].id === id){
  8. $scope.mainComments[i].isEditMod = !$scope.mainComments[i].isEditMod
  9. }
  10. }
  11. };
  12. $scope.editModSecondToggle = function(mainId, secondId){
  13. for (let i = 0; i < $scope.mainComments.length; i++) {
  14. for (let j = 0; j < $scope.mainComments[i].secondComments.length; j++) {
  15. if ($scope.mainComments[i].secondComments[j].mainId === mainId) {
  16. if ($scope.mainComments[i].secondComments[j].id === secondId) {
  17. $scope.mainComments[i].secondComments[j].isEditMod = !$scope.mainComments[i].secondComments[j].isEditMod
  18. }
  19. }
  20. }
  21. }
  22. };
  23. $scope.replyModToggle = function(id){
  24. for (let i = 0; i < $scope.mainComments.length; i++) {
  25. if($scope.mainComments[i].id === id){
  26. $scope.mainComments[i].isReplyMod = !$scope.mainComments[i].isReplyMod
  27. }
  28. }
  29. };
  30. // MAIN Comment
  31. commentRepository.getComments().then(function(response){
  32. $scope.mainComments = response.data
  33. }, function(error) {
  34. console.log(error)
  35. });
  36. $scope.newComment = {
  37. authorName: '',
  38. foto: '',
  39. commentText: '',
  40. isEditMod: false,
  41. isReplyMod: false,
  42. secondComments: []
  43. }
  44. $scope.addComment = function(){
  45. $scope.newComment.id = new Date().getTime();
  46. $scope.mainComments.unshift($scope.newComment)
  47. commentRepository.addComment($scope.newComment).then(function(response){
  48. console.log(response.data)
  49. }, function(error) {
  50. console.log(error)
  51. });
  52. $scope.newComment = {
  53. authorName: '',
  54. foto: '',
  55. commentText: '',
  56. isEditMod: false,
  57. isReplyMod: false,
  58. secondComments: []
  59. };
  60. };
  61. $scope.deleteMainComment = function(id){
  62. commentRepository.deleteComment(id).then(function(response){
  63. console.log(response.data)
  64. }, function(error) {
  65. console.log(error)
  66. });
  67. for (let i = 0; i < $scope.mainComments.length; i++) {
  68. if($scope.mainComments[i].id === id){
  69. $scope.mainComments.splice(i, 1);
  70. }
  71. }
  72. return $scope.mainComments
  73. };
  74. $scope.editMainComment = function(id){
  75. $scope.editModToggle(id)
  76. $rootScope.$broadcast('EditComment', $scope.mainComments, id);
  77. };
  78. $rootScope.$on('saveEdit', function(event, comment, id){
  79. $scope.editModToggle(id)
  80. commentRepository.editComment(comment).then(function(response){
  81. console.log(response.data)
  82. }, function(error) {
  83. console.log(error)
  84. });
  85. for (let i = 0; i < $scope.mainComments.length; i++) {
  86. if ($scope.mainComments[i].id === comment.id) {
  87. $scope.mainComments.splice(i,1,comment);
  88. }
  89. }
  90. });
  91. $rootScope.$on('cancelEdit', function(event,id){
  92. $scope.editModToggle(id)
  93. })
  94. // SECOND COMMENTS
  95. $scope.deleteSecondComment = function(mainId, secondId){
  96. commentRepository.deleteSecondComment(mainId, secondId).then(function(response){
  97. console.log(response.data)
  98. }, function(error) {
  99. console.log(error)
  100. });
  101. for (let i = 0; i < $scope.mainComments.length; i++) {
  102. for (let j = 0; j < $scope.mainComments[i].secondComments.length; j++) {
  103. if ($scope.mainComments[i].secondComments[j].mainId === mainId) {
  104. if ($scope.mainComments[i].secondComments[j].id === secondId) {
  105. $scope.mainComments[i].secondComments.splice(j,1);
  106. }
  107. }
  108. }
  109. }
  110. };
  111. $scope.editSecondComment = function(mainId, secondId){
  112. $scope.editModSecondToggle(mainId,secondId)
  113. $rootScope.$broadcast('EditSecondComment', $scope.mainComments, mainId, secondId);
  114. }
  115. $rootScope.$on('SaveSecondEdit', function(event,comment, mainId, secondId){
  116. $scope.editModSecondToggle(mainId,secondId)
  117. commentRepository.editSecondComment(comment).then(function(response){
  118. console.log(response.data)
  119. }, function(error) {
  120. console.log(error)
  121. });
  122. for (let i = 0; i < $scope.mainComments.length; i++) {
  123. for (let j = 0; j < $scope.mainComments[i].secondComments.length; j++) {
  124. if ($scope.mainComments[i].secondComments[j].mainId === comment.mainId) {
  125. if ($scope.mainComments[i].secondComments[j].id === comment.id) {
  126. $scope.mainComments[i].secondComments.splice(j,1,comment);
  127. }
  128. }
  129. }
  130. }
  131. });
  132. $rootScope.$on('CancelSecondEdit', function(event, mainId, secondId){
  133. $scope.editModSecondToggle(mainId,secondId)
  134. });
  135. // REPLY
  136. $scope.reply = function(id){
  137. $scope.replyModToggle(id)
  138. $rootScope.$broadcast('Reply', $scope.mainComments, id);
  139. };
  140. $rootScope.$on('commentReply', function(event,comment, id){
  141. $scope.replyModToggle(id)
  142. commentRepository.commentReply(comment).then(function(response){
  143. console.log(response.data)
  144. }, function(error) {
  145. console.log(error)
  146. });
  147. for (let i = 0; i < $scope.mainComments.length; i++) {
  148. if ($scope.mainComments[i].id === comment.mainId) {
  149. $scope.mainComments[i].secondComments.unshift(comment);
  150. }
  151. }
  152. });
  153. $rootScope.$on('cancelReplyMod', function(event, id){
  154. $scope.replyModToggle(id)
  155. })
  156. }])
  157. })()