Skip to content

Commit

Permalink
Create Reusable Util for Opening OAuth Popup (#83)
Browse files Browse the repository at this point in the history
* refactor: create reusable util for opening popup

* chore: export util

* chore: typing

* chore: typing
  • Loading branch information
DavideIadeluca authored Oct 29, 2024
1 parent bf1005e commit 75f95f5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 28 deletions.
32 changes: 4 additions & 28 deletions js/src/forum/extenders/extendLoginSignup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,13 @@ import Tooltip from 'flarum/common/components/Tooltip';
import LogInModal from 'flarum/forum/components/LogInModal';
import SignUpModal from 'flarum/forum/components/SignUpModal';
import ForumApplication from 'flarum/forum/ForumApplication';
import { openOAuthPopup } from '../utils/popupUtils';

export default function () {
extend(LogInButton, 'initAttrs', function (returnedValue, attrs) {
const fullscreen = app.forum.attribute('fof-oauth.fullscreenPopup');

if (fullscreen) {
attrs.onclick = function () {
window.open(app.forum.attribute('baseUrl') + attrs.path, 'logInPopup', 'fullscreen=yes');
};
} else {
// Default values
const defaultWidth = 580;
const defaultHeight = 400;

const width = app.forum.attribute('fof-oauth.popupWidth') || defaultWidth;
const height = app.forum.attribute('fof-oauth.popupHeight') || defaultHeight;

const $window = $(window);

attrs.onclick = function () {
window.open(
app.forum.attribute('baseUrl') + attrs.path,
'logInPopup',
`width=${width},` +
`height=${height},` +
`top=${$window.height() / 2 - height / 2},` +
`left=${$window.width() / 2 - width / 2},` +
'status=no,scrollbars=yes,resizable=no'
);
};
}
attrs.onclick = function () {
openOAuthPopup(app, attrs);
};
});

extend(LogInButtons.prototype, 'items', function (items) {
Expand Down
1 change: 1 addition & 0 deletions js/src/forum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import extendLoginSignup from './extenders/extendLoginSignup';
export { default as extend } from './extend';

export * from './components';
export * from './utils';

app.initializers.add('fof/oauth', () => {
extendLoginSignup();
Expand Down
5 changes: 5 additions & 0 deletions js/src/forum/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { openOAuthPopup } from './popupUtils';

export const utils = {
openOAuthPopup,
};
26 changes: 26 additions & 0 deletions js/src/forum/utils/popupUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type ForumApplication from 'flarum/forum/ForumApplication';

export function openOAuthPopup(app: ForumApplication, attrs: Record<string, any>) {
const fullscreen = app.forum.attribute('fof-oauth.fullscreenPopup');
if (fullscreen) {
window.open(app.forum.attribute<string>('baseUrl') + attrs.path, 'logInPopup', 'fullscreen=yes');
} else {
const defaultWidth = 580;
const defaultHeight = 400;

const width = app.forum.attribute<number>('fof-oauth.popupWidth') || defaultWidth;
const height = app.forum.attribute<number>('fof-oauth.popupHeight') || defaultHeight;

const windowHeight = $(window).height() ?? 0;
const windowWidth = $(window).width() ?? 0;

const top = windowHeight / 2 - height / 2;
const left = windowWidth / 2 - width / 2;

window.open(
app.forum.attribute<string>('baseUrl') + attrs.path,
'logInPopup',
`width=${width},` + `height=${height},` + `top=${top},` + `left=${left},` + 'status=no,scrollbars=yes,resizable=no'
);
}
}

0 comments on commit 75f95f5

Please sign in to comment.