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

feature(amp-live-list): add update feature #3260

Merged
merged 1 commit into from
May 23, 2016
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
31 changes: 30 additions & 1 deletion build-system/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ var bodyParser = require('body-parser');
var clr = require('connect-livereload');
var finalhandler = require('finalhandler');
var fs = BBPromise.promisifyAll(require('fs'));
var jsdom = require('jsdom');
var path = require('path');
var url = require('url');
var request = require('request');
var serveIndex = require('serve-index');
var serveStatic = require('serve-static');
var url = require('url');

var args = Array.prototype.slice.call(process.argv, 2, 4);
var paths = args[0];
Expand Down Expand Up @@ -103,6 +104,34 @@ app.use('/examples.build/live-list.amp.max.html', function(req, res) {
});
});

var liveListUpdateFile = '/examples.build/live-list-update.amp.max.html';
var liveListUpdateFullPath = `${process.cwd()}${liveListUpdateFile}`;
var liveListFile = fs.readFileSync(liveListUpdateFullPath);
var ctr = 0;
var doc = jsdom.jsdom(liveListFile);
var win = doc.defaultView;
app.use(liveListUpdateFile, function(req, res) {
var doctype = '<!doctype html>\n';
res.setHeader('Content-Type', 'text/html');
res.statusCode = 200;
if (ctr != 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Should this be throttled a bit and sometimes return nothing?

Copy link
Member Author

Choose a reason for hiding this comment

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

good point, will add variation here and on upcoming full sample

Copy link
Member Author

Choose a reason for hiding this comment

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

done.

if (Math.random() < .7) {
var item1 = doc.querySelector('#list-item-1');
var item1Content = item1.querySelectorAll('.content');
item1.setAttribute('data-update-time', Date.now());
item1Content[0].textContent = Math.floor(Math.random() * 10);
item1Content[1].textContent = Math.floor(Math.random() * 10);
} else {
// Sometimes we want an empty response to simulate no changes.
res.end(`${doctype}<html></html>`);
return;
}
}
var outerHTML = doc.documentElement./*OK*/outerHTML;
res.end(`${doctype}${outerHTML}`);
ctr++;
});

// Proxy with unminified JS.
// Example:
// http://localhost:8000/max/s/www.washingtonpost.com/amphtml/news/post-politics/wp/2016/02/21/bernie-sanders-says-lower-turnout-contributed-to-his-nevada-loss-to-hillary-clinton/
Expand Down
Binary file added examples/img/ampicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
109 changes: 109 additions & 0 deletions examples/live-list-update.amp.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<!doctype html>
<html ⚡>
<head>
<meta charset="utf-8">
<title>Lorem Ipsum | PublisherName</title>
<link rel="canonical" href="amps.html" >
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<link href='https://fonts.googleapis.com/css?family=Georgia|Open+Sans|Roboto' rel='stylesheet' type='text/css'>
<style amp-custom>
amp-live-list > [items] {
display: flex;
flex-direction: column;
}

.amp-live-list-item {
height: 100px;
background-color: #f8f8f8;
border-bottom: 1px solid rgba(0, 0, 0, .12);
}

.card {
display: flex;
margin: 15px;
padding: 10px;
justify-content: center;
align-items: center;
background-color: #fff;
box-shadow: rgba(0, 0, 0, .3) 0 0 5px;
}

.card > div {
flex-basis: 50%;
}

.side {
display: flex;
align-items: center;
justify-content: space-around;
}

.content {
font-size: 1.5rem;
}

header {
font-size: 2rem;
}
</style>
<link href='https://fonts.googleapis.com/css?family=Georgia|Open+Sans|Roboto' rel='stylesheet' type='text/css'>
<script async custom-element="amp-live-list" src="https://cdn.ampproject.org/v0/amp-live-list-0.1.js"></script>
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<script async src="https://cdn.ampproject.org/v0.js"></script>
</head>
<body>
<header>
Scoreboard refresh takes 15 seconds.
</header>
<amp-live-list
layout="container"
data-poll-interval="15000"
data-max-items-per-page="5"
id="my-live-list">
<button id="btn" update on="tap:my-live-list.update">You have updates</button>
<div items>
<div id="list-item-2" data-sort-time="1462814963693">
<div class="card">
<div class="side side-left">
<div class="logo">
<amp-img src="/examples/img/ampicon.png"
layout="fixed" height="50" width="50">
</amp-img>
</div>
<div class="content">2</div>
</div>
<div class="side side-right">
<div class="content">2</div>
<div class="logo">
<amp-img src="/examples/img/ampicon.png"
layout="fixed" height="50" width="50">
</amp-img>
</div>
</div>
</div>
</div>
<div id="list-item-1" data-sort-time="1462814955597">
<div class="card">
<div class="side side-left">
<div class="logo">
<amp-img src="/examples/img/ampicon.png"
layout="fixed" height="50" width="50">
</amp-img>
</div>
<div class="content">2</div>
</div>
<div class="side side-right">
<div class="content">2</div>
<div class="logo">
<amp-img src="/examples/img/ampicon.png"
layout="fixed" height="50" width="50">
</amp-img>
</div>
</div>
</div>
</div>
</div>
</amp-live-list>
</body>
</html>

Loading