SelectMenuPageController.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. app.controller('SelectMenuPageController',['$scope','$firebaseObject','$timeout','$interval','$window',function($scope,$firebaseObject,$timeout,$interval,$window){
  2. if(firebase.auth().currentUser){
  3. $scope.ref = firebase.database().ref().child('users').child(firebase.auth().currentUser.uid).child('meals');
  4. } else{
  5. $scope.ref = firebase.database().ref().child('meals');
  6. }
  7. $interval(function(){
  8. if(firebase.auth().currentUser){
  9. $scope.ref = firebase.database().ref().child('users').child(firebase.auth().currentUser.uid).child('meals');
  10. } else{
  11. $scope.ref = firebase.database().ref().child('meals');
  12. }
  13. $scope.ref.on('value', snap => {
  14. $timeout(function(){
  15. $scope.meals = snap.val();
  16. })
  17. })
  18. },1000)
  19. // var ref = firebase.database().ref().child('meals');
  20. $scope.mealType = 'breakfast';
  21. $scope.meals = [];
  22. $scope.selectedNumber = 0;
  23. $scope.selectMeal = function(i){
  24. if(firebase.auth().currentUser){
  25. var keys = Object.keys($scope.meals);
  26. //animating btn
  27. if(!$scope.meals[keys[i]].isSelected){
  28. var selectedMealsBtn = angular.element(document.querySelector('.selected-meals-btn'));
  29. selectedMealsBtn.addClass('selected-meals-btn-animate');
  30. $timeout(function(){
  31. selectedMealsBtn.removeClass('selected-meals-btn-animate');
  32. },500)
  33. }
  34. //animating btn
  35. $scope.meals[keys[i]].isSelected = true;
  36. $scope.ref.child(keys[i]).set($scope.meals[keys[i]]);
  37. } else{
  38. var loginPopupJQ = angular.element(document.querySelector('.login-popup'));
  39. loginPopupJQ.css('display','flex');
  40. $timeout(function(){
  41. loginPopupJQ.css('opacity','1');
  42. },50)
  43. }
  44. }
  45. $scope.ref.on('value', snap => {
  46. $timeout(function(){
  47. $scope.meals = snap.val();
  48. })
  49. })
  50. var firebaseMainMeals = firebase.database().ref().child('meals');
  51. var firebaseUsers = firebase.database().ref().child('users');
  52. firebaseUsers.on('value', function(snap){
  53. $scope.testUsers = snap.val();
  54. })
  55. firebaseMainMeals.on('value',function(snap){
  56. $scope.mainObjDelete = snap.val();
  57. })
  58. $scope.deleteMealItem = function(key){
  59. var userKeys = Object.keys($scope.testUsers);
  60. for (let i = 0; i < userKeys.length; i++) {
  61. // var user = users[usersKeys[i]];
  62. var newObject = $scope.testUsers[userKeys[i]].meals;
  63. var userMealsKeys = Object.keys($scope.testUsers[userKeys[i]].meals);
  64. delete newObject[userMealsKeys[key]]
  65. firebaseUsers.child(userKeys[i]).child('meals').set(Object.assign(newObject));
  66. }
  67. var mainObjKeys = Object.keys($scope.mainObjDelete);
  68. delete $scope.mainObjDelete[mainObjKeys[key]];
  69. firebaseMainMeals.set(Object.assign($scope.mainObjDelete));
  70. // $timeout(function(){
  71. // $window.location.reload();
  72. // },1000)
  73. }
  74. }])