Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Reusable Util for Opening OAuth Popup #83

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
);
}
}
Loading