Project: Mall App Client: Maiora
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

malls.page.ts 4.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { Component, OnInit, ViewChild,ElementRef } from '@angular/core';
  2. import { MallService } from '../services/mall.service';
  3. import { ToastService } from '../services/toast.service';
  4. import { Router } from '@angular/router';
  5. @Component({
  6. selector: 'app-malls',
  7. templateUrl: './malls.page.html',
  8. styleUrls: ['./malls.page.scss'],
  9. })
  10. export class MallsPage implements OnInit {
  11. @ViewChild('searchbar', null) searchElement: ElementRef;
  12. selected_tab: string = 'you';
  13. tempMalls: any = [];
  14. allMalls: any = [];
  15. show_sort_popup: boolean = false;
  16. showSearchBar: boolean = false;
  17. selected_sort: string = null;
  18. searchTerm: string = '';
  19. constructor(
  20. private mallService: MallService,
  21. private router: Router,
  22. private toastService: ToastService
  23. ) { }
  24. ngOnInit() {
  25. if (localStorage.allMalls) {
  26. this.allMalls = JSON.parse(localStorage.allMalls);
  27. this.tempMalls = JSON.parse(localStorage.allMalls);
  28. } else {
  29. this.getMallsByLocation()
  30. }
  31. }
  32. doRefresh(e: any) {
  33. this.getMallsByLocation();
  34. e.target.complete();
  35. }
  36. toggleSearchBar() {
  37. if (this.searchTerm.length > 0) {
  38. this.searchTerm = '';
  39. this.tempMalls = JSON.parse(JSON.stringify(this.allMalls));
  40. } else {
  41. this.showSearchBar = !this.showSearchBar;
  42. setTimeout(()=>{ // this will make the execution after the above boolean has changed
  43. this.searchElement.nativeElement.focus();
  44. }, 1);
  45. }
  46. }
  47. searchAllMalls() {
  48. if (this.searchTerm.trim().length > 0) {
  49. this.tempMalls = this.allMalls.filter((mallData: any) => {
  50. return mallData.mall.mall_name.toLowerCase().includes(this.searchTerm.toLowerCase());
  51. });
  52. } else {
  53. this.tempMalls = JSON.parse(JSON.stringify(this.allMalls));
  54. }
  55. }
  56. shareMallDetails(mallData) {
  57. if (window.navigator && window.navigator['share']) {
  58. const shareData = {
  59. title: mallData.mall.mall_name,
  60. text: mallData.mall.description,
  61. url: 'https://maps.google.com/?q=' + mallData.latitude + ',' + mallData.longitude
  62. }
  63. window.navigator['share'](shareData);
  64. }
  65. }
  66. getMallsByLocation() {
  67. if (navigator.geolocation) {
  68. this.toastService.presentToast("Getting malls based on your location...", "dark");
  69. navigator.geolocation.getCurrentPosition((position) => {
  70. this.mallService.mallsByLocation(position.coords.latitude, position.coords.longitude).then((response: any) => {
  71. if (response.length > 0) {
  72. localStorage.allMalls = JSON.stringify(response);
  73. this.allMalls = JSON.parse(localStorage.allMalls);
  74. this.tempMalls = JSON.parse(localStorage.allMalls);
  75. } else {
  76. this.toastService.presentToast("No malls near you your location, Getting all the malls...", "warning");
  77. this.getAllMalls();
  78. }
  79. }, () => {
  80. this.toastService.presentToast("Failed to fetch malls for your location", "danger");
  81. });
  82. });
  83. } else {
  84. this.getAllMalls();
  85. }
  86. }
  87. getAllMalls() {
  88. this.mallService.allMalls().then((response) => {
  89. localStorage.allMalls = JSON.stringify(response);
  90. this.allMalls = JSON.parse(localStorage.allMalls);
  91. this.tempMalls = JSON.parse(localStorage.allMalls);
  92. }, (error) => {
  93. console.log(error);
  94. });
  95. }
  96. showMallDetails(mall: any) {
  97. this.router.navigate(['/mall-details', { mall: JSON.stringify(mall) }]);
  98. }
  99. sortBy(type: string) {
  100. this.selected_sort = type;
  101. switch(this.selected_sort) {
  102. case 'name': this.tempMalls.sort(function(a: any, b: any){
  103. if(a.mall.mall_name < b.mall.mall_name) { return -1; }
  104. if(a.mall.mall_name > b.mall.mall_name) { return 1; }
  105. return 0;
  106. });
  107. break;
  108. case 'rating': this.tempMalls.sort(function(a: any, b: any){
  109. if(a.mall.rating < b.mall.rating) { return -1; }
  110. if(a.mall.rating > b.mall.rating) { return 1; }
  111. return 0;
  112. }).reverse();
  113. break;
  114. }
  115. }
  116. }