appRootController.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. app.controller('AppRootController',['$scope','$window','$timeout','$location','$firebaseAuth','$interval','$firebaseObject',function($scope,$window,$timeout,$location,$firebaseAuth,$interval,$firebaseObject){
  2. var window = angular.element($window);
  3. $scope.load = function(){
  4. $timeout(function(){
  5. var preloader = angular.element(document.querySelector('.preload-screen'));
  6. preloader.css('opacity','0');
  7. },1700)
  8. $timeout(function(){
  9. var preloader = angular.element(document.querySelector('.preload-screen'));
  10. preloader.css('display','none');
  11. },2700)
  12. }
  13. $scope.isOpenMenu = false;
  14. $scope.toggleMenu = function(){
  15. if($scope.isOpenMenu){
  16. $scope.isOpenMenu = false;
  17. }else {
  18. $scope.isOpenMenu = true;
  19. }
  20. }
  21. $scope.selectedNumber = 0;
  22. $scope.meals = {};
  23. if(firebase.auth().currentUser){
  24. $scope.ref = firebase.database().ref().child('users').child(firebase.auth().currentUser.uid).child('meals');
  25. } else{
  26. $scope.ref = firebase.database().ref().child('meals');
  27. }
  28. $interval(function(){
  29. if(firebase.auth().currentUser){
  30. $scope.ref = firebase.database().ref().child('users').child(firebase.auth().currentUser.uid).child('meals');
  31. $scope.isUserSigned = true;
  32. if(firebase.auth().currentUser.uid == 'uRkqD9GmNxUsdgF1XtmPicDJwAz1'){
  33. $scope.isModeratorSigned = true;
  34. } else{
  35. $scope.isModeratorSigned = false;
  36. }
  37. } else{
  38. $scope.ref = firebase.database().ref().child('meals');
  39. $scope.isUserSigned = false;
  40. $scope.isModeratorSigned = false;
  41. }
  42. $scope.ref.on('value', snap => {
  43. $timeout(function(){
  44. var selectedNumber = 0;
  45. angular.forEach(snap.val(),function(el){
  46. if(el.isSelected === true){
  47. selectedNumber++;
  48. }
  49. })
  50. $scope.selectedNumber = selectedNumber;
  51. })
  52. $scope.meals = snap.val();
  53. })
  54. },1000)
  55. // var ref = firebase.database().ref().child('meals');
  56. $scope.selectedNumber = 0;
  57. $scope.ref.on('value', snap => {
  58. $timeout(function(){
  59. var selectedNumber = 0;
  60. angular.forEach(snap.val(),function(el){
  61. if(el.isSelected === true){
  62. selectedNumber++;
  63. }
  64. })
  65. $scope.selectedNumber = selectedNumber;
  66. })
  67. $scope.meals = snap.val();
  68. })
  69. $scope.openSelectedMeals = function(){
  70. if(firebase.auth().currentUser){
  71. var selectedMeals = angular.element(document.querySelector('.selected-meals'));
  72. var appRoot = angular.element(document.querySelector('.app-root'));
  73. var overlay2 = angular.element(document.querySelector('.overlay2'));
  74. // selectedMeals.css({'transform': 'translateX(0)'})
  75. appRoot.css({'transform': 'translateX(-281px)'});
  76. overlay2.css({'pointer-events': 'auto','opacity':'1'});
  77. overlay2.on('click',function(){
  78. overlay2.css({'pointer-events': 'none','opacity':'0'});
  79. appRoot.css({'transform': 'translateX(0px)'});
  80. })
  81. } else{
  82. var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
  83. loginPopupJQ.css('display','flex');
  84. $timeout(function(){
  85. loginPopupJQ.css('opacity','1');
  86. },50)
  87. }
  88. }
  89. $scope.isShowMealsBtn = function(){
  90. if($location.path() == '/select-menu'){
  91. return true;
  92. } else{
  93. return false;
  94. }
  95. }
  96. if(firebase.auth().currentUser){
  97. $scope.isUserSigned = true;
  98. } else{
  99. $scope.isUserSigned = false;
  100. }
  101. $scope.signOut = function(){
  102. firebase.auth().signOut().then(function(success){
  103. console.log('Sign out seccessfully: ',success);
  104. if($location.path().indexOf('shopping-cart') > -1 || $location.path().indexOf('add-item') > -1 || $location.path().indexOf('user-page') > -1 || $location.path().indexOf('messages') > -1){
  105. $window.location.assign('/');
  106. }
  107. }).catch(function(error){
  108. console.log('Sign out error: ',error);
  109. })
  110. }
  111. // login-popup
  112. $scope.isLoginActive = true;
  113. $scope.confirmPasswordIncorrect = false;
  114. $scope.newUserObj = {};
  115. $scope.newUserDataObj = {};
  116. $scope.registerObject = {};
  117. $scope.createNewUser = function(){
  118. $scope.confirmPasswordIncorrect = false;
  119. firebase.auth().createUserWithEmailAndPassword($scope.newUserObj.email, $scope.newUserObj.password).then(function(success){
  120. firebase.database().ref().child('users').child(success.uid).child('meals').set($scope.meals);
  121. firebase.database().ref().child('users').child(success.uid).child('userObject').set($scope.newUserDataObj);
  122. var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
  123. loginPopupJQ.css('opacity','0');
  124. $timeout(function(){
  125. loginPopupJQ.css('display','none');
  126. },1000)
  127. }).catch(function(error){
  128. // $scope.emailErrorMessage = error.code.replace(/auth\//g,'');
  129. $scope.passwordErrorMessage = error.message;
  130. });
  131. }
  132. $scope.signInUserObj = {};
  133. $scope.signInUser = function(){
  134. firebase.auth().signInWithEmailAndPassword($scope.signInUserObj.email,$scope.signInUserObj.password).then(function(success){
  135. console.log('Signed in successfully: ',success);
  136. var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
  137. loginPopupJQ.css('opacity','0');
  138. $timeout(function(){
  139. loginPopupJQ.css('display','none');
  140. },1000)
  141. }).catch(function(error){
  142. console.log('Sign in error: ',error.message);
  143. })
  144. }
  145. $scope.onPasswordInput = function(){
  146. if($scope.newUserObj.password){
  147. $scope.isShortPassword = $scope.newUserObj.password.length < 6 ? true : false;
  148. }
  149. }
  150. $scope.onConfirmPasswordInput = function(){
  151. if($scope.registerObject.confirmPassword){
  152. $scope.confirmPasswordIncorrect = $scope.registerObject.confirmPassword == $scope.newUserObj.password ? false : true;
  153. }
  154. }
  155. $scope.cancelPopup = function(){
  156. var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
  157. loginPopupJQ.css('opacity','0');
  158. $timeout(function(){
  159. loginPopupJQ.css('display','none');
  160. },1000)
  161. }
  162. $scope.openPopup = function(){
  163. var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
  164. loginPopupJQ.css('display','flex');
  165. $timeout(function(){
  166. loginPopupJQ.css('opacity','1');
  167. },50)
  168. }
  169. console.log('current user: ',firebase.auth().currentUser ? firebase.auth().currentUser.uid : null)
  170. $scope.goToShoppingCart = function(){
  171. if(firebase.auth().currentUser){
  172. $window.location.assign('#!/shopping-cart');
  173. } else{
  174. var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
  175. loginPopupJQ.css('display','flex');
  176. $timeout(function(){
  177. loginPopupJQ.css('opacity','1');
  178. },50)
  179. }
  180. }
  181. $scope.isDeleteIconsShow = false;
  182. $scope.deleteItemModeToggle = function(){
  183. var selectMenuItemJQ = angular.element(document.querySelectorAll('.select-menu-page .meal-item-delete-btn'));
  184. var selectReadyMenuItemJQ = angular.element(document.querySelectorAll('.diet-item-delete-btn'));
  185. if($scope.isDeleteIconsShow){
  186. selectMenuItemJQ.css('opacity','0');
  187. selectReadyMenuItemJQ.css('opacity','0');
  188. $scope.isDeleteIconsShow = false;
  189. } else{
  190. selectMenuItemJQ.css('opacity','1');
  191. selectReadyMenuItemJQ.css('opacity','1');
  192. $scope.isDeleteIconsShow = true;
  193. }
  194. }
  195. $scope.isShowDeleteItemModeToggle = function(){
  196. if($scope.isUserSigned && (firebase.auth().currentUser.uid == 'uRkqD9GmNxUsdgF1XtmPicDJwAz1') && (($location.path().indexOf('select-menu') > -1)||($location.path().indexOf('ready-menu-') > -1))){
  197. return true;
  198. } else{
  199. return false;
  200. }
  201. }
  202. }])