diff --git a/functions/index.js b/functions/index.js index bf2d718d..bee6fb0b 100644 --- a/functions/index.js +++ b/functions/index.js @@ -79,7 +79,8 @@ function scoreClass(score) { function error(res, code) { res.status(code); res.locals = { - sidebarDisabled: true + sidebarDisabled: true, + embed: res.locals.embed } return res.render('error', {code}); } @@ -123,14 +124,16 @@ app.use((req, res, next) => { res.locals.firebaseClientConfig = firebaseClientConfig; res.locals.isBeta = isBeta; - if (req.user && req.user.showWelcomeMsg && !req.query.latest && !req.path.includes('/api')) { + if (req.user && req.user.showWelcomeMsg && !req.query.latest && !('embed' in req.query) &&!req.path.includes('/api')) { userDB.doc(req.user.uid) // async .update({showWelcomeMsg: false}) .catch(() => console.error('Failed to update welcome msg', req.user.uid)); } - if (req.query.iframe) { - res.locals.insideIframe = true; + if ('embed' in req.query) { + res.locals.embed = true; + res.locals.privacyModalDisabled = true; + res.set('Content-Security-Policy', "frame-ancestors 'self' http://localhost:*"); } for (let l of LEVELS) { @@ -470,7 +473,7 @@ for (let p of ['about', 'introduction', 'parents', 'teachers', 'privacy-notice', const content = fs.readFileSync(path.join(__dirname, `build/${p}.html`)); app.get('/' + p, (_, res) => { res.locals.sidebarDisabled = pagesWithoutSidebar.includes(p); - res.locals.privacyModalDisabled = pagesWithoutPrivacyModal.includes(p); + res.locals.privacyModalDisabled = res.locals.embed || pagesWithoutPrivacyModal.includes(p); res.render('_layout', {content}) }); } diff --git a/functions/views/_layout.pug b/functions/views/_layout.pug index 3d8d330f..e23c3c8e 100644 --- a/functions/views/_layout.pug +++ b/functions/views/_layout.pug @@ -1,5 +1,5 @@ doctype html -html +html(class=embed ? 'embed' : undefined) head meta(charset="utf-8") meta(http-equiv="X-UA-Compatible" content="IE=edge") @@ -26,7 +26,7 @@ html //- Stylesheets link(href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600,300,400italic" rel="stylesheet") - link(href="/parallel.css?v=34" rel="stylesheet") + link(href="/parallel.css?v=37" rel="stylesheet") if isProduction include scripts/newrelic.html @@ -38,39 +38,40 @@ html body #vue - if isBeta + if isBeta && !embed .beta div You are on our Beta Testing Site! div The information & events on this page are for testing purposes and should not be followed. a(href="https://parallel.org.uk") Click here to return to the production site. - .headerWrapper - .header - if !sidebarDisabled - #menu(v-on:click="showSidebar=true") - svg(width=24 height=24) - rect(y=2 width=24 height=3 fill="white") - rect(y=10 width=24 height=3 fill="white") - rect(y=18 width=24 height=3 fill="white") - - if path && !noNavigation - div(class="homeButtonsLeft") - a#home(href="/" title="Home" class="homeButton homeButtonWithSvg") - include svgs/home.svg - | Home - - if user - div(class="homeButtonsRight") - a#home(href="/account" title="Home" class="homeButton") - | Settings - a#home(href="/" title="Home" class="homeButton" @click="user.logout") - | Sign out - - if noNavigation - div#logo(href="/" title="Home" class=(sidebarDisabled ? 'sidebarDisabled' : '')) - else - a#logo(href="/" title="Home" class=(sidebarDisabled ? 'sidebarDisabled' : '')) - - #shadow(v-on:click="showSidebar=false" v-bind:class="{visible: showSidebar}") + if !embed + .headerWrapper + .header + if !sidebarDisabled + #menu(v-on:click="showSidebar=true") + svg(width=24 height=24) + rect(y=2 width=24 height=3 fill="white") + rect(y=10 width=24 height=3 fill="white") + rect(y=18 width=24 height=3 fill="white") + + if path && !noNavigation + div(class="homeButtonsLeft") + a#home(href="/" title="Home" class="homeButton homeButtonWithSvg") + include svgs/home.svg + | Home + + if user + div(class="homeButtonsRight") + a#home(href="/account" title="Home" class="homeButton") + | Settings + a#home(href="/" title="Home" class="homeButton" @click="user.logout") + | Sign out + + if noNavigation + div#logo(href="/" title="Home" class=(sidebarDisabled ? 'sidebarDisabled' : '')) + else + a#logo(href="/" title="Home" class=(sidebarDisabled ? 'sidebarDisabled' : '')) + + #shadow(v-on:click="showSidebar=false" v-bind:class="{visible: showSidebar}") #container if !sidebarDisabled @@ -138,7 +139,7 @@ html for l in user ? user.sidebarLevels : levels nav(v-show=`user.level == '${l}'`) for p in pages[l] - a(href=("/" + p.url) class=('/' + p.url === path ? 'active' : '')) + a(href=(embed ? "/secret/" : "/") + p.url class=('/' + p.url === path ? 'active' : '')) .title-meta PG #{p.index} #[span.spaced •] #{p.date} .title= p.title if user && user.answers[p.url] && user.answers[p.url].submitted @@ -161,7 +162,7 @@ html //- label(for="terms-modal") We will email you when a new Parallelogram challenge is released, when the next Parallel Circles session is scheduled, and when there are special competitions, prizes and opportunities. By checking this box you consent to receiving these emails, in accordance with our #[a(href="/privacy-notice" target="_blank") Data Privacy policy]. //- button(type="submit") Continue - if user && !user.code && user.showWelcomeMsg + if user && !user.code && user.showWelcomeMsg && !embed .modal(v-if="showWelcomeMsg") .modal-background .modal-body @@ -169,17 +170,18 @@ html p.text-center We’ve updated your level to #[strong #{levelNamesWithAges[user.level]}]. You can always change it on your #[a(href="/account") account settings] page. The first Parallelogram is now available, but officially it is launched in early September. We will be releasing new Parallelograms every week, from mid-September. button(type="submit" v-on:click="showWelcomeMsg = false") Continue - footer - a(href="/about") About - | • - a(href="/contact") Contact - | • - a(href="/privacy-notice") Privacy Notice - br - | Created by - a(href="http://goodthinkingsociety.org" target="_blank") #[img(src="/images/good-thinking.png" width=10 height=16)] Good Thinking Society - | and - a(href="https://mathigon.org" target="_blank") #[img(src="/images/mathigon.png" width=16 height=16)] Mathigon + if !embed + footer + a(href="/about") About + | • + a(href="/contact") Contact + | • + a(href="/privacy-notice") Privacy Notice + br + | Created by + a(href="http://goodthinkingsociety.org" target="_blank") #[img(src="/images/good-thinking.png" width=10 height=16)] Good Thinking Society + | and + a(href="https://mathigon.org" target="_blank") #[img(src="/images/mathigon.png" width=16 height=16)] Mathigon if user && user.privacy && user.privacy.visible && !privacyModalDisabled iframe(src="/privacy-modal" id="privacy-iframe" style="height: 100%;width: 100%;position: absolute;z-index: 10000000000;top: 0;left: 0;border: none;") @@ -188,7 +190,7 @@ html script(defer src="https://www.gstatic.com/firebasejs/8.10.0/firebase-auth.js") script(defer src="https://www.gstatic.com/firebasejs/8.10.0/firebase-firestore.js") script(defer src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.9/vue.min.js") - script(defer src="/parallel.js?v=39") + script(defer src="/parallel.js?v=40") script. if (typeof window !== 'undefined' && window.newrelic && window.newrelic.setCustomAttribute) { diff --git a/static/scripts/challenge.js b/static/scripts/challenge.js index 164ed8ec..6461a6d5 100644 --- a/static/scripts/challenge.js +++ b/static/scripts/challenge.js @@ -54,7 +54,9 @@ export default function() { testPasswordSubmit(e) { e.preventDefault(); - window.location.search = `?p=${challenge.testPassword}`; + const params = new URLSearchParams(window.location.search); + params.set('p', challenge.testPassword); + window.location.search = params.toString(); }, // Only used by teachers diff --git a/static/scripts/embed.js b/static/scripts/embed.js new file mode 100644 index 00000000..b0209a2f --- /dev/null +++ b/static/scripts/embed.js @@ -0,0 +1,58 @@ +// ============================================================================= +// Embed height reporting (used when ?embed is set and inside an iframe) +// ============================================================================= + + +const MESSAGE_TYPE = 'parallel:embed:height'; + +export default function initEmbed() { + const root = document.documentElement; + if (!root.classList.contains('embed')) return; + + // Embed mode hides the privacy modal, so only allow it inside a frame. + if (window.parent === window) { + const url = new URL(window.location.href); + if (!url.searchParams.has('embed')) return; + url.searchParams.delete('embed'); + return window.location.replace(url.toString()); + } + + // Links to other pages open in the top window; in-page anchors stay in the frame. + document.addEventListener('click', (e) => { + const link = e.target.closest && e.target.closest('a[href]'); + if (!link || link.target) return; + const samePage = link.href.split('#')[0] === window.location.href.split('#')[0]; + if (!samePage) link.target = '_top'; + }, true); + + let lastHeight = 0; + let frame = 0; + + const post = () => { + frame = 0; + const height = Math.ceil(root.getBoundingClientRect().height); + // Body is hidden (height 0) while reloading after a submission. + if (!height || height === lastHeight) return; + lastHeight = height; + window.parent.postMessage({type: MESSAGE_TYPE, height}, '*'); + }; + + const schedule = () => { + if (!frame) frame = window.requestAnimationFrame(post); + }; + + if (window.ResizeObserver) { + const observer = new ResizeObserver(schedule); + observer.observe(root); + observer.observe(document.body); + } else { + window.setInterval(schedule, 500); + } + + window.addEventListener('resize', schedule); + window.addEventListener('load', schedule); + document.addEventListener('load', schedule, true); + if (document.fonts && document.fonts.ready) document.fonts.ready.then(schedule); + + schedule(); +} diff --git a/static/scripts/main.js b/static/scripts/main.js index 9533bbdb..6385bb4e 100755 --- a/static/scripts/main.js +++ b/static/scripts/main.js @@ -7,6 +7,7 @@ import '../../node_modules/babel-polyfill/dist/polyfill'; import getUser from './user'; import getChallenge from './challenge'; +import initEmbed from './embed'; import '../../node_modules/@johmun/vue-tags-input/dist/vue-tags-input'; function removeStudent(id, index, level) { @@ -128,7 +129,7 @@ document.addEventListener('DOMContentLoaded', function() { }); if (window.self !== window.top) { - document.body.className = 'insideIframe' + document.body.classList.add('insideIframe'); } window.app = new Vue({ @@ -137,4 +138,6 @@ document.addEventListener('DOMContentLoaded', function() { data: {showSidebar: false, showWelcomeMsg: true, user, c: challenge, removeStudent, sortTable} }); + + initEmbed(); }); diff --git a/static/styles/forms.less b/static/styles/forms.less index 37bd201d..80a274a8 100644 --- a/static/styles/forms.less +++ b/static/styles/forms.less @@ -224,7 +224,7 @@ form.loading button[type="submit"] { .toggle { display: flex; flex-direction: row; - margin: 6px 0 4px;; + margin: 0px 0 6px 4px; justify-content: center; padding: 0 2px; } diff --git a/static/styles/iframe.less b/static/styles/iframe.less index 4e0c91d7..d6047fde 100644 --- a/static/styles/iframe.less +++ b/static/styles/iframe.less @@ -30,3 +30,46 @@ max-width: 100%; } } + + +// ----------------------------------------------------------------------------- +// Embedded within the Parallel Squared site (?embed) + +html.embed { + background: transparent; + overflow: hidden; + + &, body { height: auto; min-height: 0; } + + #container { + width: 100%; + max-width: 100%; + } + + .intros, + .sidebar-circles-item, + .sidebar-field { + display: none; + } + + .sidebar { margin: 0 6px 0 0; } + + .sidebar-wrap { + position: static; + max-height: none; + overflow: visible; + border-radius: 4px; + } + + article, article.sidebarDisabled { + box-shadow: none; + min-height: 0; + } + + @media screen and (max-width: 720px) { + #container { display: flex; flex-direction: column; } + article, article.sidebarDisabled { width: auto; order: -1; padding: 24px; border-radius: 4px; } + .sidebar { margin: 24px 0 0; width: 100%; } + .sidebar-wrap { transform: none; box-shadow: none; width: auto; } + } +}