Skip to content

Commit

Permalink
fix: navigation url externe
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoblanc committed Aug 28, 2019
1 parent dbdc516 commit 98eb58f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { InAppBrowser } from '@ionic-native/in-app-browser/ngx';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { Platform } from '@ionic/angular';

import { ListMetaMedias } from './models/meta-media/list-meta-medias';
import { LinkService } from './provider/helper/link.service';
import { StorageService } from './provider/helper/storage.service';
import { MetaMediaService } from './provider/meta-media/meta-media.service';
import { NotificationService } from './provider/notification.service';


/**
* *~~~~~~~~~~~~~~~~~~~
* Author: HugoBlanc |
Expand All @@ -34,7 +34,7 @@ export class AppComponent implements OnInit {
private notificationService: NotificationService,
private storage: StorageService,
private router: Router,
private iab: InAppBrowser
private linkService: LinkService
) { }

// La liste des différent médias que l'on veut afficher dans le menu
Expand Down Expand Up @@ -93,6 +93,6 @@ export class AppComponent implements OnInit {
}

openPrivacy() {
const browser = this.iab.create('https://athena-api.caprover.athena-app.fr/privacy', '_blank');
this.linkService.launchInAppBrowser('https://athena-api.caprover.athena-app.fr/privacy');
}
}
14 changes: 9 additions & 5 deletions src/app/content-details/content-details.page.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Component, OnInit, NgZone, OnDestroy } from '@angular/core';
import { Component, OnInit, NgZone, OnDestroy, ElementRef } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { MetaMedia } from '../models/meta-media/meta-media';
import { MetaMediaService } from '../provider/meta-media/meta-media.service';
import { IContent } from '../models/content/icontent';
import { contentServiceProvider } from '../provider/content/content.service.provider';
import { ContentService } from '../provider/content/content.service';
import { StyleService } from '../provider/style.service';
import { LinkService } from '../provider/helper/link.service';

/**
* *~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -34,6 +35,8 @@ export class ContentDetailsPage implements OnInit, OnDestroy {
public contentService: ContentService<IContent>,
public metaMediaService: MetaMediaService,
public styleService: StyleService,
public linkService: LinkService,
public element: ElementRef,
private zone: NgZone) { }

ionViewWillEnter() {
Expand All @@ -48,11 +51,11 @@ export class ContentDetailsPage implements OnInit, OnDestroy {
// Si on fait pas ça bug a l'affichage
// On cherche en local puis si rien en locq on cherche coté serveur
this.contentService.getContentById(this.id)
.subscribe((content) => {
this.zone.run(() => {
this.content = content;
.subscribe((content) => {
this.zone.run(() => {
this.content = content;
});
});
});
}

/**
Expand All @@ -61,6 +64,7 @@ export class ContentDetailsPage implements OnInit, OnDestroy {
*/
ngOnInit() {
this.styleService.initPage();
this.linkService._enableDynamicHyperlinks(this.element);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/app/provider/helper/link.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';

import { LinkService } from './link.service';

describe('LinkService', () => {
beforeEach(() => TestBed.configureTestingModule({}));

it('should be created', () => {
const service: LinkService = TestBed.get(LinkService);
expect(service).toBeTruthy();
});
});
52 changes: 52 additions & 0 deletions src/app/provider/helper/link.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Injectable, ElementRef } from '@angular/core';
import { InAppBrowser } from '@ionic-native/in-app-browser/ngx';

@Injectable({
providedIn: 'root'
})
export class LinkService {

constructor(private iab: InAppBrowser) { }


/**
* Enable hyperlinks that are embedded within a HTML string
*/
public _enableDynamicHyperlinks(element: ElementRef): void {
// Provide a minor delay to allow the HTML to be rendered and 'found'
// within the view template
setTimeout(() => {
// Query the DOM to find ALL occurrences of the <a> hyperlink tag
const urls: any = element.nativeElement.querySelectorAll('a');

// Iterate through these
urls.forEach((url) => {
// Listen for a click event on each hyperlink found
url.addEventListener('click', (event) => {
// Retrieve the href value from the selected hyperlink
event.preventDefault();
const link = event.target.href;

// Log values to the console and open the link within the InAppBrowser plugin
console.log('Name is: ' + event.target.innerText);
console.log('Link is: ' + link);
this.launchInAppBrowser(link);
}, false);
});
}, 2000);
}



/**
* Creates/launches an Ionic Native InAppBrowser window to display hyperlink locations within
* @param string link The URL to visit within the InAppBrowser window
*/
public launchInAppBrowser(link: string): void {
const opts = 'location=yes,clearcache=yes,hidespinner=no,beforeload=yes';
this.iab.create(link, '_system', opts);
}



}

0 comments on commit 98eb58f

Please sign in to comment.