Skip to content

Commit

Permalink
Web integration button position can be overridden
Browse files Browse the repository at this point in the history
  • Loading branch information
YellowFish085 committed Apr 26, 2020
1 parent fe802a6 commit 9c91828
Showing 1 changed file with 49 additions and 6 deletions.
55 changes: 49 additions & 6 deletions src/content-scripts/web-integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import * as Enum from '@/utils/Enum';
import Button from '@/content-scripts/web-integration/Button';
import Settings from '@/utils/Settings';

interface Position {
inPage: boolean;
x: Enum.WebIntegrationX;
y: Enum.WebIntegrationY;
}

interface PositionnOverride {
inPage?: boolean;
x?: Enum.WebIntegrationX;
y?: Enum.WebIntegrationY;
}

interface OverlayParameters {
/**
* CSS selector used to find the search value for the content.
Expand Down Expand Up @@ -48,6 +60,14 @@ interface OverlayParameters {
* This method receives one parameter which is the button node.
*/
appendInPage: (node: HTMLElement) => void;

/**
* Overrides button position settings.
*
* If a page needs the button to be placed in a specific way because it can't be placed any other
* ways, use this object to override the user settings and force the button position.
*/
positionOverride?: PositionnOverride;
}

/**
Expand All @@ -73,6 +93,31 @@ function getTitle(node: HTMLElement): string {
return node.innerText;
}

/**
* Get button position.
*/
function getPosition(settings: ALSearch.Settings, positionOverride?: PositionnOverride): Position {
const p = {
inPage: settings.integration.overlay.inPage,
x: settings.integration.overlay.x,
y: settings.integration.overlay.y,
};

if (positionOverride && Object.prototype.hasOwnProperty.call(positionOverride, 'inPage')) {
p.inPage = positionOverride.inPage!;
}

if (positionOverride && Object.prototype.hasOwnProperty.call(positionOverride, 'x')) {
p.x = positionOverride.x!;
}

if (positionOverride && Object.prototype.hasOwnProperty.call(positionOverride, 'y')) {
p.y = positionOverride.y!;
}

return p;
}

/**
* Create and add a button in the page if the user has enabled web integration.
*
Expand Down Expand Up @@ -103,12 +148,10 @@ export default async function create(args: OverlayParameters): Promise<Button |
/**
* Create instance and the button node.
*/
const position = getPosition(settings, args.positionOverride);

const button = new Button(
!settings.integration.overlay.inPage,
{
x: settings.integration.overlay.x,
y: settings.integration.overlay.y,
},
position,
args.type || Enum.SearchType.ANIME,
value,
title,
Expand All @@ -119,7 +162,7 @@ export default async function create(args: OverlayParameters): Promise<Button |
/**
* Append node to document.
*/
if (!settings.integration.overlay.inPage) {
if (!position.inPage) {
document.body.appendChild(node);
}
else {
Expand Down

0 comments on commit 9c91828

Please sign in to comment.