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

[amp-story-player] Entry point skeleton code and bundle #28999

Merged
merged 5 commits into from
Jun 26, 2020
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
10 changes: 10 additions & 0 deletions build-system/compile/bundles.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ exports.jsBundles = {
includePolyfills: false,
},
},
'amp-story-entry-point.js': {
srcDir: './src/amp-story-player/amp-story-entry-point/',
srcFilename: 'amp-story-entry-point.js',
destDir: './dist',
minifiedDestDir: './dist',
options: {
minifiedName: 'amp-story-entry-point-v0.js',
includePolyfills: false,
},
},
'amp-story-player.js': {
srcDir: './src/amp-story-player/',
srcFilename: 'amp-story-player.js',
Expand Down
5 changes: 5 additions & 0 deletions build-system/tasks/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ const cssEntryPoints = [
// than the JS file to avoid loading CSS as JS
outCss: 'video-autoplay-out.css',
},
{
path: 'amp-story-entry-point.css',
outJs: 'amp-story-entry-point.css.js',
outCss: 'amp-story-entry-point-v0.css',
},
{
// Publisher imported CSS for `src/amp-story-player/amp-story-player.js`.
path: 'amp-story-player.css',
Expand Down
1 change: 1 addition & 0 deletions build-system/tasks/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ async function compileAllJs(options) {
doBuildJs(jsBundles, 'recaptcha.js', options),
doBuildJs(jsBundles, 'amp-viewer-host.max.js', options),
doBuildJs(jsBundles, 'video-iframe-integration.js', options),
doBuildJs(jsBundles, 'amp-story-entry-point.js', options),
doBuildJs(jsBundles, 'amp-story-player.js', options),
doBuildJs(jsBundles, 'amp-inabox-host.js', options),
doBuildJs(jsBundles, 'amp-shadow.js', options),
Expand Down
2 changes: 1 addition & 1 deletion build-system/tasks/presubmit-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ const forbiddenTerms = {
'dist.3p/current/integration.js',
'extensions/amp-access/0.1/amp-login-done.js',
'extensions/amp-viewer-integration/0.1/examples/amp-viewer-host.js',
'src/amp-story-player/amp-story-player-manager.js',
'src/amp-story-player/amp-story-component-manager.js',
'src/runtime.js',
'src/log.js',
'src/web-worker/web-worker.js',
Expand Down
20 changes: 20 additions & 0 deletions css/amp-story-entry-point.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

amp-story-entry-point {
position: relative;
display: block;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
* limitations under the License.
*/

import {AmpStoryEntryPoint} from './amp-story-entry-point/amp-story-entry-point-impl';
import {AmpStoryPlayer} from './amp-story-player-impl';
import {initLogConstructor} from '../log';
import {throttle} from '../utils/rate-limit';

/** @const {string} */
const SCROLL_THROTTLE_MS = 500;

export class AmpStoryPlayerManager {
export class AmpStoryComponentManager {
/**
* @param {!Window} win
* @constructor
Expand All @@ -32,13 +33,13 @@ export class AmpStoryPlayerManager {
}

/**
* Calls layoutCallback on the player when it is close to the viewport.
* @param {!AmpStoryPlayer} playerImpl
* Calls layoutCallback on the element when it is close to the viewport.
* @param {!AmpStoryPlayer|!AmpStoryEntryPoint} elImpl
* @private
*/
layoutPlayer_(playerImpl) {
layoutEl_(elImpl) {
if (!this.win_.IntersectionObserver || this.win_ !== this.win_.parent) {
this.layoutFallback_(playerImpl);
this.layoutFallback_(elImpl);
return;
}

Expand All @@ -47,48 +48,47 @@ export class AmpStoryPlayerManager {
if (!entry.isIntersecting) {
return;
}
playerImpl.layoutCallback();
elImpl.layoutCallback();
});
};

const observer = new IntersectionObserver(intersectingCallback, {
rootMargin: '100%',
});
observer.observe(playerImpl.getElement());
observer.observe(elImpl.getElement());
}

/**
* Fallback for when IntersectionObserver is not supported. Calls
* layoutCallback on the player when it is close to the viewport.
* @param {!AmpStoryPlayer} playerImpl
* layoutCallback on the element when it is close to the viewport.
* @param {!AmpStoryPlayer|!AmpStoryEntryPoint} elImpl
* @private
*/
layoutFallback_(playerImpl) {
// TODO(Enriqe): pause players when scrolling away from viewport.
layoutFallback_(elImpl) {
// TODO(Enriqe): pause elements when scrolling away from viewport.
this.win_.addEventListener(
'scroll',
throttle(
this.win_,
this.layoutIfVisible_.bind(this, playerImpl),
this.layoutIfVisible_.bind(this, elImpl),
SCROLL_THROTTLE_MS
)
);

// Calls it once it in case scroll event never fires.
this.layoutIfVisible_(playerImpl);
this.layoutIfVisible_(elImpl);
}

/**
* Checks if player is close to the viewport and calls layoutCallback when it
* Checks if element is close to the viewport and calls layoutCallback when it
* is.
* @param {!AmpStoryPlayer} playerImpl
* @param {!AmpStoryPlayer|!AmpStoryEntryPoint} elImpl
* @private
*/
layoutIfVisible_(playerImpl) {
const playerTop = playerImpl.getElement()./*OK*/ getBoundingClientRect()
.top;
if (this.win_./*OK*/ innerHeight * 2 > playerTop) {
playerImpl.layoutCallback();
layoutIfVisible_(elImpl) {
const elTop = elImpl.getElement()./*OK*/ getBoundingClientRect().top;
if (this.win_./*OK*/ innerHeight * 2 > elTop) {
elImpl.layoutCallback();
}
}

Expand All @@ -104,7 +104,24 @@ export class AmpStoryPlayerManager {
const playerEl = players[i];
const player = new AmpStoryPlayer(this.win_, playerEl);
player.buildCallback();
this.layoutPlayer_(player);
this.layoutEl_(player);
}
}

/**
* Builds and layouts entry points.
* @public
*/
loadEntryPoints() {
const doc = this.win_.document;
const entryPoints = doc.getElementsByTagName('amp-story-entry-point');
initLogConstructor();

for (let i = 0; i < entryPoints.length; i++) {
const entryPointEl = entryPoints[i];
const entryPoint = new AmpStoryEntryPoint(this.win_, entryPointEl);
entryPoint.buildCallback();
this.layoutEl_(entryPoint);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Source for this constant is css/amp-story-entry-point.css
import {cssText} from '../../../build/amp-story-entry-point.css';

/**
* <amp-story-entry-point> component for embedding stories and launching them in
* the <amp-story-player>.
*
* Note that this is a vanilla JavaScript class and should not depend on AMP
* services, as v0.js is not expected to be loaded in this context.
*/
export class AmpStoryEntryPoint {
/**
* @param {!Window} win
* @param {!Element} element
* @constructor
*/
constructor(win, element) {
/** @private {!Window} */
this.win_ = win;

/** @private {!Element} */
this.element_ = element;

/** @private {!Document} */
this.doc_ = this.win_.document;

/** @private {boolean} */
this.isBuilt_ = false;

/** @private {boolean} */
this.isLaidOut_ = false;

/** @private {?Element} */
this.rootEl_ = null;
}

/** @public */
buildCallback() {
if (this.isBuilt_) {
return;
}

this.initializeShadowRoot_();

this.isBuilt_ = true;
}

/** @public */
layoutCallback() {
if (this.isLaidOut_) {
return;
}

this.isLaidOut_ = true;
}

/** @private */
initializeShadowRoot_() {
this.rootEl_ = this.doc_.createElement('main');

// Create shadow root
const shadowRoot = this.element_.attachShadow({mode: 'open'});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail on many browsers, we should have a fallback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll address as a follow-up since this is something that we also do for the player.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, is is tracked somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// Inject default styles
const styleEl = this.doc_.createElement('style');
styleEl.textContent = cssText;
shadowRoot.appendChild(styleEl);
shadowRoot.appendChild(this.rootEl_);
}

/**
* @public
* @return {!Element}
*/
getElement() {
return this.element_;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {AmpStoryComponentManager} from '../amp-story-component-manager';

self.onload = () => {
const manager = new AmpStoryComponentManager(self);
manager.loadEntryPoints();
};
4 changes: 2 additions & 2 deletions src/amp-story-player/amp-story-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* limitations under the License.
*/

import {AmpStoryComponentManager} from './amp-story-component-manager';
import {AmpStoryPlayer} from './amp-story-player-impl';
import {AmpStoryPlayerManager} from './amp-story-player-manager';

self.onload = () => {
const manager = new AmpStoryPlayerManager(self);
const manager = new AmpStoryComponentManager(self);
manager.loadPlayers();
};

Expand Down
4 changes: 2 additions & 2 deletions test/unit/test-amp-story-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* limitations under the License.
*/

import {AmpStoryComponentManager} from '../../src/amp-story-player/amp-story-component-manager';
import {
AmpStoryPlayer,
IFRAME_IDX,
} from '../../src/amp-story-player/amp-story-player-impl';
import {AmpStoryPlayerManager} from '../../src/amp-story-player/amp-story-player-manager';
import {Messaging} from '@ampproject/viewer-messaging';
import {toArray} from '../../src/types';

Expand Down Expand Up @@ -53,7 +53,7 @@ describes.realWin('AmpStoryPlayer', {amp: false}, (env) => {
playerEl.appendChild(storyAnchor);
}
win.document.body.appendChild(playerEl);
manager = new AmpStoryPlayerManager(win);
manager = new AmpStoryComponentManager(win);

env.sandbox
.stub(Messaging, 'waitForHandshakeFromDocument')
Expand Down