appRootController.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.signInUserError = '';
  134. $scope.signInUser = function(){
  135. firebase.auth().signInWithEmailAndPassword($scope.signInUserObj.email,$scope.signInUserObj.password).then(function(success){
  136. console.log('Signed in successfully: ',success);
  137. var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
  138. loginPopupJQ.css('opacity','0');
  139. $timeout(function(){
  140. loginPopupJQ.css('display','none');
  141. },1000)
  142. }).catch(function(error){
  143. console.log('Sign in error: ',error);
  144. $scope.signInUserError = error.message;
  145. })
  146. }
  147. $scope.onPasswordInput = function(){
  148. if($scope.newUserObj.password){
  149. $scope.isShortPassword = $scope.newUserObj.password.length < 6 ? true : false;
  150. }
  151. }
  152. $scope.onConfirmPasswordInput = function(){
  153. if($scope.registerObject.confirmPassword){
  154. $scope.confirmPasswordIncorrect = $scope.registerObject.confirmPassword == $scope.newUserObj.password ? false : true;
  155. }
  156. }
  157. $scope.cancelPopup = function(){
  158. var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
  159. loginPopupJQ.css('opacity','0');
  160. $timeout(function(){
  161. loginPopupJQ.css('display','none');
  162. },1000)
  163. }
  164. $scope.openPopup = function(){
  165. var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
  166. loginPopupJQ.css('display','flex');
  167. $timeout(function(){
  168. loginPopupJQ.css('opacity','1');
  169. },50)
  170. }
  171. console.log('current user: ',firebase.auth().currentUser ? firebase.auth().currentUser.uid : null)
  172. $scope.goToShoppingCart = function(){
  173. if(firebase.auth().currentUser){
  174. $window.location.assign('#!/shopping-cart');
  175. } else{
  176. var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
  177. loginPopupJQ.css('display','flex');
  178. $timeout(function(){
  179. loginPopupJQ.css('opacity','1');
  180. },50)
  181. }
  182. }
  183. $scope.isDeleteIconsShow = false;
  184. $scope.deleteItemModeToggle = function(){
  185. var selectMenuItemJQ = angular.element(document.querySelectorAll('.select-menu-page .meal-item-delete-btn'));
  186. var selectReadyMenuItemJQ = angular.element(document.querySelectorAll('.diet-item-delete-btn'));
  187. if($scope.isDeleteIconsShow){
  188. selectMenuItemJQ.css('opacity','0');
  189. selectReadyMenuItemJQ.css('opacity','0');
  190. $scope.isDeleteIconsShow = false;
  191. } else{
  192. selectMenuItemJQ.css('opacity','1');
  193. selectReadyMenuItemJQ.css('opacity','1');
  194. $scope.isDeleteIconsShow = true;
  195. }
  196. }
  197. $scope.isShowDeleteItemModeToggle = function(){
  198. if($scope.isUserSigned && (firebase.auth().currentUser.uid == 'uRkqD9GmNxUsdgF1XtmPicDJwAz1') && (($location.path().indexOf('select-menu') > -1)||($location.path().indexOf('ready-menu-') > -1))){
  199. return true;
  200. } else{
  201. return false;
  202. }
  203. }
  204. }])