ReadyMenuSecondController.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. app.controller('ReadyMenuSecondController',['$scope','$location','$timeout','$window','$interval',function($scope,$location,$timeout,$window,$interval){
  2. $scope.currentPath = $location.path();
  3. if($scope.currentPath.indexOf('loss') > -1){
  4. $scope.filterBy = 'loss';
  5. } else if($scope.currentPath.indexOf('gain') > -1){
  6. $scope.filterBy = 'gain';
  7. } else if($scope.currentPath.indexOf('balance') > -1){
  8. $scope.filterBy = 'balance';
  9. }
  10. var firebaseDiets = firebase.database().ref().child('diets');
  11. $interval(function(){
  12. if(firebase.auth().currentUser){
  13. $scope.firebaseShoppingCart = firebase.database().ref().child('users').child(firebase.auth().currentUser.uid).child('shoppingCart');
  14. }
  15. },1000)
  16. $scope.diets = {};
  17. firebaseDiets.on('value', snap => {
  18. $timeout(function(){
  19. $scope.diets = snap.val();
  20. })
  21. })
  22. $scope.weekCount = "1";
  23. // $scope.selectArr = [
  24. // {val: '1', name: 'week 1', isSelected:'true'},
  25. // {val: '2', name: 'week 2', isSelected:'false'},
  26. // {val: '3', name: 'week 3', isSelected:'false'},
  27. // {val: '4', name: 'week 4', isSelected:'false'},
  28. // ]
  29. // scroll
  30. var JQWindow = angular.element($window);
  31. JQWindow.bind('scroll', function(){
  32. // if(window.pageYOffset >= '10'){
  33. // var item = document.querySelector('.ready-menu-second .item:nth-child(1)');
  34. // var JQItem = angular.element(item);
  35. // JQItem.addClass('ready-menu-second-scroll-class');
  36. // } else{
  37. // var item = document.querySelector('.ready-menu-second .item:nth-child(1)');
  38. // var JQItem = angular.element(item);
  39. // JQItem.removeClass('ready-menu-second-scroll-class');
  40. // }
  41. if(window.pageYOffset >= '350' || $window.innerWidth < 1180){
  42. var item = document.querySelector('.ready-menu-second .item:nth-child(2)');
  43. var JQItem = angular.element(item);
  44. JQItem.addClass('ready-menu-second-scroll-class');
  45. }
  46. else{
  47. var item = document.querySelector('.ready-menu-second .item:nth-child(2)');
  48. var JQItem = angular.element(item);
  49. JQItem.removeClass('ready-menu-second-scroll-class');
  50. }
  51. if(window.pageYOffset >= '600' || $window.innerWidth < 1180){
  52. var item = document.querySelector('.ready-menu-second .item:nth-child(3)');
  53. var JQItem = angular.element(item);
  54. JQItem.addClass('ready-menu-second-scroll-class');
  55. }
  56. else{
  57. var item = document.querySelector('.ready-menu-second .item:nth-child(3)');
  58. var JQItem = angular.element(item);
  59. JQItem.removeClass('ready-menu-second-scroll-class');
  60. }
  61. })
  62. $scope.onInit = function(){
  63. $timeout(function(){
  64. var item = document.querySelector('.ready-menu-second .item:nth-child(1)');
  65. var JQItem = angular.element(item);
  66. JQItem.addClass('ready-menu-second-scroll-class');
  67. },500)
  68. }
  69. // functions
  70. $scope.addToCart = function(diet, dietPrice, weekCount){
  71. if(firebase.auth().currentUser){
  72. var readyDiet = diet;
  73. readyDiet.overallPrice = dietPrice * weekCount;
  74. readyDiet.weekCount = weekCount;
  75. $scope.firebaseShoppingCart.push().set(readyDiet);
  76. var shoppingCartIconJQ = angular.element(document.querySelector('.shopping-cart-icon span'));
  77. shoppingCartIconJQ.addClass('animate');
  78. $timeout(function(){
  79. shoppingCartIconJQ.removeClass('animate');
  80. },500)
  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.deleteDietItem = function(i){
  90. var newDiets = Object.assign($scope.diets);
  91. var dietsKeys = Object.keys(newDiets);
  92. delete newDiets[dietsKeys[i]];
  93. firebase.database().ref().child('diets').set(newDiets);
  94. }
  95. }])