appRootController.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. app.controller('AppRootController',['$scope','$window','$timeout','$location','$firebaseAuth','$interval',function($scope,$window,$timeout,$location,$firebaseAuth,$interval){
  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. } else{
  33. $scope.ref = firebase.database().ref().child('meals');
  34. $scope.isUserSigned = false;
  35. }
  36. $scope.ref.on('value', snap => {
  37. $timeout(function(){
  38. var selectedNumber = 0;
  39. angular.forEach(snap.val(),function(el){
  40. if(el.isSelected === true){
  41. selectedNumber++;
  42. }
  43. })
  44. $scope.selectedNumber = selectedNumber;
  45. })
  46. $scope.meals = snap.val();
  47. })
  48. },1000)
  49. // var ref = firebase.database().ref().child('meals');
  50. $scope.selectedNumber = 0;
  51. $scope.ref.on('value', snap => {
  52. $timeout(function(){
  53. var selectedNumber = 0;
  54. angular.forEach(snap.val(),function(el){
  55. if(el.isSelected === true){
  56. selectedNumber++;
  57. }
  58. })
  59. $scope.selectedNumber = selectedNumber;
  60. })
  61. $scope.meals = snap.val();
  62. })
  63. $scope.openSelectedMeals = function(){
  64. if(firebase.auth().currentUser){
  65. var selectedMeals = angular.element(document.querySelector('.selected-meals'));
  66. var appRoot = angular.element(document.querySelector('.app-root'));
  67. var overlay2 = angular.element(document.querySelector('.overlay2'));
  68. // selectedMeals.css({'transform': 'translateX(0)'})
  69. appRoot.css({'transform': 'translateX(-281px)'});
  70. overlay2.css({'pointer-events': 'auto','opacity':'1'});
  71. overlay2.on('click',function(){
  72. overlay2.css({'pointer-events': 'none','opacity':'0'});
  73. appRoot.css({'transform': 'translateX(0px)'});
  74. })
  75. } else{
  76. var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
  77. loginPopupJQ.css('display','flex');
  78. $timeout(function(){
  79. loginPopupJQ.css('opacity','1');
  80. },50)
  81. }
  82. }
  83. $scope.isShowMealsBtn = function(){
  84. if($location.path() == '/select-menu'){
  85. return true;
  86. } else{
  87. return false;
  88. }
  89. }
  90. if(firebase.auth().currentUser){
  91. $scope.isUserSigned = true;
  92. } else{
  93. $scope.isUserSigned = false;
  94. }
  95. $scope.signOut = function(){
  96. firebase.auth().signOut().then(function(success){
  97. console.log('Sign out seccessfully: ',success);
  98. if($location.path().indexOf('shopping-cart') > -1){
  99. $window.location.assign('/');
  100. }
  101. }).catch(function(error){
  102. console.log('Sign out error: ',error);
  103. })
  104. }
  105. // login-popup
  106. $scope.isLoginActive = true;
  107. $scope.newUserObj = {};
  108. $scope.createNewUser = function(){
  109. firebase.auth().createUserWithEmailAndPassword($scope.newUserObj.email, $scope.newUserObj.password).then(function(success){
  110. firebase.database().ref().child('users').child(success.uid).child('meals').set($scope.meals);
  111. var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
  112. loginPopupJQ.css('opacity','0');
  113. $timeout(function(){
  114. loginPopupJQ.css('display','none');
  115. },1000)
  116. }).catch(function(error){
  117. // $scope.emailErrorMessage = error.code.replace(/auth\//g,'');
  118. $scope.passwordErrorMessage = error.message;
  119. });
  120. }
  121. $scope.signInUserObj = {};
  122. $scope.signInUser = function(){
  123. firebase.auth().signInWithEmailAndPassword($scope.signInUserObj.email,$scope.signInUserObj.password).then(function(success){
  124. console.log('Signed in successfully: ',success);
  125. var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
  126. loginPopupJQ.css('opacity','0');
  127. $timeout(function(){
  128. loginPopupJQ.css('display','none');
  129. },1000)
  130. // refresh user meals object
  131. var mealsObject = $scope.meals;
  132. var mealsObjectKeys = Object.keys(mealsObject);
  133. $timeout(function(){
  134. var userObjectKeys = Object.keys($scope.meals);
  135. var newUserMealsObject = Object.assign(mealsObject);
  136. for (var i = 0; i < mealsObjectKeys.length; i++) {
  137. if(userObjectKeys.indexOf(mealsObjectKeys[i]) > -1){
  138. if($scope.meals[mealsObjectKeys[i]].isSelected){
  139. newUserMealsObject[mealsObjectKeys[i]].isSelected = true;
  140. }
  141. }
  142. }
  143. firebase.database().ref().child('users').child(firebase.auth().currentUser.uid).child('meals').set(newUserMealsObject);
  144. },1100)
  145. // refresh user meals object
  146. }).catch(function(error){
  147. console.log('Sign in error: ',error.message);
  148. })
  149. }
  150. $scope.onPasswordInput = function(){
  151. if($scope.newUserObj.password){
  152. $scope.isShortPassword = $scope.newUserObj.password.length < 6 ? true : false;
  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. }])