Giter Club home page Giter Club logo

ionic2-external-link-directive's Introduction

ionic2-external-link-directive

A directive to mark a link as an external link and open using the InAppBrowser plugin (will fallback to open in a new tab on browsers).

This directive requires Ionic Native and the InAppBrowser plugin.

How to install

Using Ionic CLI, run ionic g directive ExternalLink on your terminal. Make sure you are on your project's folder.

After it finished generating, add the directive on your app's @NgModule inside src/app/app.module.ts. It should look like this:

import ... //your other imports
import { ExternalLink } from '../components/external-link/external-link';
 
@NgModule({
  declarations: [
    ..., //your other declarations
    ElasticHeader
  ],
  ... //others
})
export class AppModule {}

Open src/components/external-link/external-link.ts and modify the code to:

import { Directive, ElementRef } from '@angular/core';
import { InAppBrowser } from '@ionic-native/in-app-browser';

@Directive({
	selector: '[external-link]' // Attribute selector
})
export class ExternalLink {

	constructor( private element: ElementRef, private iab: InAppBrowser ){
		//add click/tap listener
		['click', 'tap'].forEach(e => {
			this.element.nativeElement.addEventListener(e, (event) => {
				event.preventDefault();

				let target = event.target;
				let link = target.href;

				//do a validation if the clicked element has a valid link
				if( ! link ){
					//most probably that a child element has been clicked, find the parent element with a valid href
					let i = 1; //stop looking after 20 jumps which a high chance that it wont reach
					while( ! link && i < 20 ){
						target = target.parentNode;
						link = target.href;
						i++;
					}
				}
				
				if( link ){
					this.iab.create( link ); //opens the link with InAppBrowser if available, will fall back to window.open if not
				}
			});
		});
	}

}

How to use

Just add the [external-link] attribute to your <a> tag.

<a href="http://www.google.com" external-link>External link</a>

Thats it!

ionic2-external-link-directive's People

Contributors

ejfrias avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.