Skip to content

Commit

Permalink
Support mobile browser
Browse files Browse the repository at this point in the history
closes #79
closes #67
  • Loading branch information
MurhafSousli committed Mar 20, 2017
1 parent 67c465d commit 733b13e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/helpers/share-buttons.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class ShareArgs {
public description?: string,
public image?: string,
public tags?: string,
public via?: string) {
public via?: string,
public mobile?: boolean) {
}
}
62 changes: 52 additions & 10 deletions src/helpers/share-links.functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import { ShareArgs } from './share-buttons.class';
export module ShareLinks {

export function fbShare(args: ShareArgs) {
let shareUrl = `https://www.facebook.com/sharer/sharer.php?u=${args.url}`;
let shareUrl;
if (args.mobile) {
shareUrl = 'fb://';
} else {
shareUrl = 'https://www.facebook.com/sharer/sharer.php';
}
shareUrl += `?u=${args.url}`;

if (args.title) {
shareUrl += `&title=${args.title}`;
}
Expand All @@ -22,9 +29,16 @@ export module ShareLinks {

// TWITTER DOCS: https://dev.twitter.com/web/tweet-button/web-intent
export function twitterShare(args: ShareArgs) {
let shareUrl = `https://twitter.com/intent/tweet?url=${args.url}`;
if (args.description) {
shareUrl += `&text=${args.description}`;
let shareUrl;
if (args.mobile) {
shareUrl = 'twitter://';
} else {
shareUrl = 'https://twitter.com/intent/tweet';
}
shareUrl += `?url=${args.url}`;

if (args.title) {
shareUrl += `&text=${args.title}`;
}
if (args.via) {
shareUrl += `&via=${args.via}`;
Expand All @@ -37,7 +51,14 @@ export module ShareLinks {

// LINKEDIN DOCS https://developer.linkedin.com/docs/share-on-linkedin#!
export function linkedInShare(args: ShareArgs) {
let shareUrl = `http://www.linkedin.com/shareArticle?url=${args.url}`;
let shareUrl;
if (args.mobile) {
shareUrl = 'linkedin://';
} else {
shareUrl = 'http://www.linkedin.com/shareArticle';
}
shareUrl += `?url=${args.url}`;

if (args.title) {
shareUrl += `&title=${args.title}`;
}
Expand All @@ -49,7 +70,14 @@ export module ShareLinks {

// REDDIT DOCS: http://stackoverflow.com/questions/24823114/post-to-reddit-via-url
export function redditShare(args: ShareArgs) {
let shareUrl = `http://www.reddit.com/submit?url=${args.url}`;
let shareUrl;
if (args.mobile) {
shareUrl = 'reddit://';
} else {
shareUrl = 'http://www.reddit.com/submit';
}
shareUrl += `?url=${args.url}`;

if (args.title) {
shareUrl += `&title=${args.title}`;
}
Expand All @@ -58,7 +86,14 @@ export module ShareLinks {

// TUMBLR DOCS: https://www.tumblr.com/docs/en/share_button
export function tumblrShare(args: ShareArgs) {
let shareUrl = `http://tumblr.com/widgets/share/tool?canonicalUrl=${args.url}`;
let shareUrl;
if (args.mobile) {
shareUrl = 'tumblr://';
} else {
shareUrl = 'http://tumblr.com/widgets/share/tool';
}
shareUrl += `?canonicalUrl=${args.url}`;

if (args.description) {
shareUrl += `&caption=${args.description}`;
}
Expand All @@ -70,7 +105,14 @@ export module ShareLinks {

// STUMBLE DOCS: http://stackoverflow.com/questions/10591424/how-can-i-create-a-custom-stumbleupon-button
export function stumbleShare(args: ShareArgs) {
return `http://www.stumbleupon.com/submit?url=${args.url}`;
let shareUrl;
if (args.mobile) {
shareUrl = 'stumbleupon://';
} else {
shareUrl = 'http://www.stumbleupon.com/submit';
}
shareUrl += `?url=${args.url}`;
return shareUrl;
}

// GPLUS DOCS: https://developers.google.com/+/web/share/#sharelink
Expand Down Expand Up @@ -101,11 +143,11 @@ export module ShareLinks {
}

export function whatsappShare(args: ShareArgs) {
let shareUrl = 'whatsapp://send?text=';
let shareUrl = `${(args.mobile) ? 'whatsapp://' : 'https://web.whatsapp.com/'}send?text=`;

// Title will add a new line
if (args.title) {
shareUrl += `*${args.title}* %0A`;
shareUrl += `${args.title} %0A`;
}

// Description adds a pipe to separate the url
Expand Down
7 changes: 6 additions & 1 deletion src/services/share-buttons.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/empty';

import { WindowService } from './window.service';
import { ShareArgs, ShareProvider, Helper } from '../helpers/index';
import { ShareArgs, ShareProvider, Helper } from '../helpers';

declare const global: any; // To make AoT compiler (ngc) happy

Expand Down Expand Up @@ -49,6 +49,11 @@ export class ShareButtonsService {
args = Object.assign({}, args, { via: this.twitterAccount });
}

/** check for mobile browser (this won't detect tablets browser) */
if (this.window.innerWidth <= 480) {
args = Object.assign({}, args, { mobile: true });
}

let popUp = this.window.open(Helper.shareFactory(type, args), 'newwindow', this.windowAttr());

/** Emit clicked button */
Expand Down

0 comments on commit 733b13e

Please sign in to comment.