appRootController.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. } 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 || $location.path().indexOf('add-item') > -1 || $location.path().indexOf('user-page') > -1 || $location.path().indexOf('messages') > -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.confirmPasswordIncorrect = false;
  108. $scope.newUserObj = {};
  109. $scope.newUserDataObj = {};
  110. $scope.registerObject = {};
  111. $scope.createNewUser = function(){
  112. $scope.confirmPasswordIncorrect = false;
  113. firebase.auth().createUserWithEmailAndPassword($scope.newUserObj.email, $scope.newUserObj.password).then(function(success){
  114. firebase.database().ref().child('users').child(success.uid).child('meals').set($scope.meals);
  115. firebase.database().ref().child('users').child(success.uid).child('userObject').set($scope.newUserDataObj);
  116. var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
  117. loginPopupJQ.css('opacity','0');
  118. $timeout(function(){
  119. loginPopupJQ.css('display','none');
  120. },1000)
  121. }).catch(function(error){
  122. // $scope.emailErrorMessage = error.code.replace(/auth\//g,'');
  123. $scope.passwordErrorMessage = error.message;
  124. });
  125. }
  126. $scope.signInUserObj = {};
  127. $scope.signInUser = function(){
  128. firebase.auth().signInWithEmailAndPassword($scope.signInUserObj.email,$scope.signInUserObj.password).then(function(success){
  129. console.log('Signed in successfully: ',success);
  130. var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
  131. loginPopupJQ.css('opacity','0');
  132. $timeout(function(){
  133. loginPopupJQ.css('display','none');
  134. },1000)
  135. }).catch(function(error){
  136. console.log('Sign in error: ',error.message);
  137. })
  138. }
  139. $scope.onPasswordInput = function(){
  140. if($scope.newUserObj.password){
  141. $scope.isShortPassword = $scope.newUserObj.password.length < 6 ? true : false;
  142. }
  143. }
  144. $scope.onConfirmPasswordInput = function(){
  145. if($scope.registerObject.confirmPassword){
  146. $scope.confirmPasswordIncorrect = $scope.registerObject.confirmPassword == $scope.newUserObj.password ? false : true;
  147. }
  148. }
  149. $scope.cancelPopup = function(){
  150. var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
  151. loginPopupJQ.css('opacity','0');
  152. $timeout(function(){
  153. loginPopupJQ.css('display','none');
  154. },1000)
  155. }
  156. $scope.openPopup = function(){
  157. var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
  158. loginPopupJQ.css('display','flex');
  159. $timeout(function(){
  160. loginPopupJQ.css('opacity','1');
  161. },50)
  162. }
  163. console.log('current user: ',firebase.auth().currentUser ? firebase.auth().currentUser.uid : null)
  164. $scope.goToShoppingCart = function(){
  165. if(firebase.auth().currentUser){
  166. $window.location.assign('#!/shopping-cart');
  167. } else{
  168. var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
  169. loginPopupJQ.css('display','flex');
  170. $timeout(function(){
  171. loginPopupJQ.css('opacity','1');
  172. },50)
  173. }
  174. }
  175. $scope.isDeleteIconsShow = false;
  176. $scope.deleteItemModeToggle = function(){
  177. var selectMenuItemJQ = angular.element(document.querySelectorAll('.select-menu-page .meal-item-delete-btn'));
  178. var selectReadyMenuItemJQ = angular.element(document.querySelectorAll('.diet-item-delete-btn'));
  179. if($scope.isDeleteIconsShow){
  180. selectMenuItemJQ.css('opacity','0');
  181. selectReadyMenuItemJQ.css('opacity','0');
  182. $scope.isDeleteIconsShow = false;
  183. } else{
  184. selectMenuItemJQ.css('opacity','1');
  185. selectReadyMenuItemJQ.css('opacity','1');
  186. $scope.isDeleteIconsShow = true;
  187. }
  188. }
  189. $scope.isShowDeleteItemModeToggle = function(){
  190. if($scope.isUserSigned && (firebase.auth().currentUser.uid == 'uRkqD9GmNxUsdgF1XtmPicDJwAz1') && (($location.path().indexOf('select-menu') > -1)||($location.path().indexOf('ready-menu-') > -1))){
  191. return true;
  192. } else{
  193. return false;
  194. }
  195. }
  196. }])