Skip to content

Commit

Permalink
Type more extensions.
Browse files Browse the repository at this point in the history
Who knew that we hadn't finished this? :)

Part of ampproject#9876
  • Loading branch information
cramforce committed Jun 30, 2017
1 parent 4b9718e commit d72e0a7
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 168 deletions.
3 changes: 3 additions & 0 deletions build-system/tasks/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ function compile(entryModuleFilenames, outputDir,
'extensions/amp-bind/**/*.js',
// Needed to access form impl from other extensions
'extensions/amp-form/**/*.js',
// Needed to access UserNotificationManager from other extensions
'extensions/amp-user-notification/**/*.js',
'src/*.js',
'src/!(inabox)*/**/*.js',
'!third_party/babel/custom-babel-helpers.js',
Expand Down Expand Up @@ -274,6 +276,7 @@ function compile(entryModuleFilenames, outputDir,
'third_party/caja/',
'third_party/closure-library/sha384-generated.js',
'third_party/d3/',
'third_party/mustache/',
'third_party/vega/',
'third_party/webcomponentsjs/',
'node_modules/',
Expand Down
15 changes: 9 additions & 6 deletions extensions/amp-3q-player/0.1/amp-3q-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

import {isLayoutSizeDefined} from '../../../src/layout';
import {tryParseJson} from '../../../src/json';
import {user} from '../../../src/log';
import {user, dev} from '../../../src/log';
import {removeElement} from '../../../src/dom';
import {
installVideoManagerForDoc,
} from '../../../src/service/video-manager-impl';
import {isObject} from '../../../src/types';
import {listen} from '../../../src/event-helper';
import {listen, getData} from '../../../src/event-helper';
import {VideoEvents} from '../../../src/video-interface';
import {videoManagerForDoc} from '../../../src/services';

Expand Down Expand Up @@ -89,8 +89,9 @@ class Amp3QPlayer extends AMP.BaseElement {
);

this.applyFillContent(iframe, true);
iframe.src = 'https://playout.3qsdn.com/' +
encodeURIComponent(this.dataId) + '?autoplay=false&amp=true';
iframe.src = 'https://playout.3qsdn.com/'
+ encodeURIComponent(dev().assertString(this.dataId))
+ '?autoplay=false&amp=true';
this.element.appendChild(iframe);

return this.loadPromise(this.iframe_).then(() =>
Expand Down Expand Up @@ -138,12 +139,14 @@ class Amp3QPlayer extends AMP.BaseElement {
}
}

const data = isObject(event.data) ? event.data : tryParseJson(event.data);
const data = isObject(getData(event))
? getData(event)
: tryParseJson(getData(event));
if (data === undefined) {
return;
}

switch (data.data) {
switch (data['data']) {
case 'ready':
this.element.dispatchCustomEvent(VideoEvents.LOAD);
this.playerReadyResolver_();
Expand Down
3 changes: 2 additions & 1 deletion extensions/amp-analytics/0.1/amp-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ export class AmpAnalytics extends AMP.BaseElement {

if (this.consentNotificationId_ != null) {
this.consentPromise_ = userNotificationManagerFor(this.win)
.then(service => service.get(this.consentNotificationId_));
.then(service => service.get(dev().assertString(
this.consentNotificationId_)));
}

if (this.element.getAttribute('trigger') == 'immediate') {
Expand Down
107 changes: 50 additions & 57 deletions extensions/amp-apester-media/0.1/amp-apester-media.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,48 @@ const TAG = 'amp-apester-media';
* AMP Apester-media
*/
class AmpApesterMedia extends AMP.BaseElement {
/** @override */

/** @param {!AmpElement} element */
constructor(element) {
super(element);
/**
* @const @private {string}
*/
this.rendererBaseUrl_ = 'https://renderer.qmerce.com';

/**
* @const @private {string}
*/
this.displayBaseUrl_ = 'https://display.apester.com';

/**
* @const @private {string}
*/
this.loaderUrl_ = 'https://images.apester.com/images%2Floader.gif';
/** @private {boolean} */
this.seen_ = false;
/** @private {?Element} */
this.iframe_ = null;
/** @private {?Promise} */
this.iframePromise_ = null;
/** @private {boolean} */
this.ready_ = false;
/** @private {?number|undefined} */
this.width_ = null;
/** @private {?number|undefined} */
this.height_ = null;
/** @private {boolean} */
this.random_ = false;
/**
* @private {?string}
*/
this.mediaAttribute_ = null;
}

/**
* @param {boolean=} onLayout
* @override
*/
preconnectCallback(onLayout) {
this.preconnect.url(this.displayBaseUrl_, onLayout);
this.preconnect.url(this.rendererBaseUrl_, onLayout);
Expand Down Expand Up @@ -57,67 +98,16 @@ class AmpApesterMedia extends AMP.BaseElement {
buildCallback() {
const width = this.element.getAttribute('width');
const height = this.element.getAttribute('height');

/**
* @private @const {number}
* */
this.width_ = getLengthNumeral(width);

/**
* @private @const {number}
* */
this.height_ = getLengthNumeral(height);

/**
* @const @private {string}
*/
this.rendererBaseUrl_ = 'https://renderer.qmerce.com';

/**
* @const @private {string}
*/
this.displayBaseUrl_ = 'https://display.apester.com';

/**
* @const @private {string}
*/
this.loaderUrl_ = 'https://images.apester.com/images%2Floader.gif';

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

/**
* @const @private {string}
*/
this.mediaAttribute_ = user().assert(
(this.element.getAttribute('data-apester-media-id') ||
(this.random_ =
this.element.getAttribute('data-apester-channel-token'))),
'Either the data-apester-media-id or the data-apester-channel-token ' +
'attributes must be specified for <amp-apester-media> %s',
this.element);

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

/**
* @private {?Promise}
*/
this.iframePromise_ = null;

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

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

/** @override */
Expand All @@ -131,7 +121,8 @@ class AmpApesterMedia extends AMP.BaseElement {
* @return {string}
**/
buildUrl_() {
const encodedMediaAttribute = encodeURIComponent(this.mediaAttribute_);
const encodedMediaAttribute = encodeURIComponent(
dev().assertString(this.mediaAttribute_));
const suffix = (this.random_) ?
`/tokens/${encodedMediaAttribute}/interactions/random` :
`/interactions/${encodedMediaAttribute}/display`;
Expand Down Expand Up @@ -241,9 +232,9 @@ class AmpApesterMedia extends AMP.BaseElement {
this.element.classList.add('amp-apester-container');
return this.queryMedia_()
.then(response => {
const media = response.payload;
const media = response['payload'];
const src = this.constructUrlFromMedia_(
media.interactionId);
media['interactionId']);
const iframe = this.constructIframe_(src);
const overflow = this.constructOverflow_();
const mutate = state => {
Expand All @@ -262,10 +253,12 @@ class AmpApesterMedia extends AMP.BaseElement {
}, error => {
dev().error(TAG, 'Display', error);
return undefined;
}).then(media => {
})
/** @param {!JsonObject} media */
.then(media => {
this.togglePlaceholder(false);
this.ready_ = true;
const height = 0 || media.data.size.height;
const height = 0 || media['data']['size']['height'];
if (height != this.height_) {
this.height_ = height;
if (this.random_) {
Expand Down
9 changes: 6 additions & 3 deletions extensions/amp-experiment/0.1/amp-experiment.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

import {user} from '../../../src/log';
import {dev, user} from '../../../src/log';
import {parseJson} from '../../../src/json';
import {Layout} from '../../../src/layout';
import {waitForBodyPromise} from '../../../src/dom';
import {allocateVariant} from './variant';
Expand Down Expand Up @@ -53,6 +54,7 @@ export class AmpExperiment extends AMP.BaseElement {
});
}

/** @return {!JsonObject} [description] */
getConfig_() {
const children = this.element.children;
user().assert(
Expand All @@ -62,7 +64,7 @@ export class AmpExperiment extends AMP.BaseElement {
'<amp-experiment> should contain exactly one ' +
'<script type="application/json"> child.');

return JSON.parse(children[0].textContent);
return dev().assert(parseJson(children[0].textContent));
}

/**
Expand All @@ -78,7 +80,8 @@ export class AmpExperiment extends AMP.BaseElement {
return waitForBodyPromise(doc).then(() => {
for (const name in experiments) {
if (experiments[name]) {
doc.body.setAttribute(ATTR_PREFIX + name, experiments[name]);
doc.body.setAttribute(ATTR_PREFIX + name,
dev().assertString(experiments[name]));
}
}
return experiments;
Expand Down
40 changes: 21 additions & 19 deletions extensions/amp-experiment/0.1/variant.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import {isObject} from '../../../src/types';
import {user} from '../../../src/log';
import {dev, user} from '../../../src/log';
import {cidForDoc} from '../../../src/services';
import {viewerForDoc} from '../../../src/services';
import {userNotificationManagerFor} from '../../../src/services';
Expand All @@ -29,7 +29,7 @@ const nameValidator = /^[\w-]+$/;
* experiment config.
* @param {!../../../src/service/ampdoc-impl.AmpDoc} ampdoc
* @param {string} experimentName
* @param {!Object} config
* @param {!JsonObject} config
* @return {!Promise<?string>}
*/
export function allocateVariant(ampdoc, experimentName, config) {
Expand All @@ -39,21 +39,22 @@ export function allocateVariant(ampdoc, experimentName, config) {
// Variant can be overridden from URL fragment.
const viewer = viewerForDoc(ampdoc);
const override = viewer.getParam(ATTR_PREFIX + experimentName);
if (override && config.variants.hasOwnProperty(override)) {
return Promise.resolve(override);
if (override && config['variants'].hasOwnProperty(override)) {
return Promise.resolve(/** @type {?string} */ (override));
}

const sticky = config.sticky !== false;
const cidScope = config.cidScope || 'amp-experiment';
const sticky = config['sticky'] !== false;
const cidScope = config['cidScope'] || 'amp-experiment';

let hasConsentPromise = Promise.resolve(true);

if (sticky && config.consentNotificationId) {
if (sticky && config['consentNotificationId']) {
hasConsentPromise = userNotificationManagerFor(ampdoc.win)
.then(manager => manager.getNotification(config.consentNotificationId))
.then(manager => manager.getNotification(
config['consentNotificationId']))
.then(userNotification => {
user().assert(userNotification,
`Notification not found: ${config.consentNotificationId}`);
`Notification not found: ${config['consentNotificationId']}`);
return userNotification.isDismissed();
});
}
Expand All @@ -62,16 +63,16 @@ export function allocateVariant(ampdoc, experimentName, config) {
if (!hasConsent) {
return null;
}
const group = config.group || experimentName;
const group = config['group'] || experimentName;
return getBucketTicket(ampdoc, group, sticky ? cidScope : null)
.then(ticket => {
let upperBound = 0;

// Loop through keys in a specific order since the default object key
// enumeration is implementation (browser) dependent.
const variantNames = Object.keys(config.variants).sort();
const variantNames = Object.keys(config['variants']).sort();
for (let i = 0; i < variantNames.length; i++) {
upperBound += config.variants[variantNames[i]];
upperBound += config['variants'][variantNames[i]];
if (ticket < upperBound) {
return variantNames[i];
}
Expand All @@ -83,15 +84,15 @@ export function allocateVariant(ampdoc, experimentName, config) {

/**
* Validates an experiment config.
* @param {!Object} config
* @param {!JsonObject} config
* @throws {!Error}
*/
function validateConfig(config) {
const variants = config.variants;
const variants = config['variants'];
user().assert(isObject(variants) && Object.keys(variants).length > 0,
'Missing experiment variants config.');
if (config.group) {
assertName(config.group);
if (config['group']) {
assertName(config['group']);
}
let totalPercentage = 0;
for (const variantName in variants) {
Expand Down Expand Up @@ -124,9 +125,10 @@ function getBucketTicket(ampdoc, group, opt_cidScope) {
return Promise.resolve(ampdoc.win.Math.random() * 100);
}

const cidPromise = cidForDoc(ampdoc).then(cidService => cidService.get(
{scope: opt_cidScope, createCookieIfNotPresent: true},
Promise.resolve()));
const cidPromise = cidForDoc(ampdoc).then(cidService => cidService.get({
scope: dev().assertString(opt_cidScope),
createCookieIfNotPresent: true,
}, Promise.resolve()));

return Promise.all([cidPromise, cryptoFor(ampdoc.win)])
.then(results => results[1].uniform(group + ':' + results[0]))
Expand Down
Loading

0 comments on commit d72e0a7

Please sign in to comment.