├── .gitignore
├── .travis.yml
├── LICENSE
├── design
├── homeicon.svg
├── icon_67.svg
├── refresh-opt.svg
└── refresh.svg
├── gulpfile.js
├── package.json
├── readme.md
├── src
├── base.html
├── css
│ ├── _components.scss
│ ├── _global.scss
│ ├── _utils.scss
│ └── all.scss
├── imgs
│ ├── .gitignore
│ ├── icon.png
│ └── logo.svg
├── index.html
└── js
│ ├── page
│ ├── flickr.js
│ ├── index.js
│ ├── utils.js
│ └── views
│ │ └── photos.hbs
│ └── sw
│ └── index.js
└── url-src.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | dist
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | # For more information about the configurations used
2 | # in this file, please see the Travis CI documentation:
3 | # http://docs.travis-ci.com
4 |
5 | after_success:
6 | - curl -sSL "https://raw.githubusercontent.com/h5bp-bot/scripts/0.6.1/update_site_branch.sh" |
7 | bash -s -- --commands "npm install && npm run build"
8 | --commit-message "Hey GitHub, this content is for you! [skip ci]"
9 | --directory "dist"
10 | --distribution-branch "gh-pages"
11 | --source-branch "master"
12 |
13 | env:
14 | global:
15 | - secure: "SRgsqz2HuSWbUbYkO6kqXMwSXNTUj0SX+FumE364cE4QnmrpajNEYJpE56ot7BlVzimSi3p75FoodY4Xj3P/0HmcSgLXqEb9ize7P2aG2YfIEDwoCtgAeW1Qphyn+I9Dy+LgoFoEUC2DxOhA5pcuwpm0oDKgUQN667DqzB6zasM="
16 |
17 | git:
18 | depth: 10
19 |
20 | language: node_js
21 |
22 | node_js:
23 | - "0.10"
24 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright 2014 Google Inc.
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
203 |
--------------------------------------------------------------------------------
/design/homeicon.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
100 |
--------------------------------------------------------------------------------
/design/icon_67.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/design/refresh-opt.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/design/refresh.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/gulpfile.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2015 Google Inc. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | var gulp = require('gulp');
19 | var plugins = require('gulp-load-plugins')();
20 | var runSequence = require('run-sequence');
21 | var through = require('through2');
22 | var browserSync = require('browser-sync');
23 | var watchify = require('watchify');
24 | var browserify = require('browserify');
25 | var uglifyify = require('uglifyify');
26 | var mergeStream = require('merge-stream');
27 | var source = require('vinyl-source-stream');
28 | var buffer = require('vinyl-buffer');
29 | var hbsfy = require("hbsfy");
30 |
31 | var reload = browserSync.reload;
32 |
33 | gulp.task('clean', function (done) {
34 | require('del')(['dist'], done);
35 | });
36 |
37 | gulp.task('browser-sync', function() {
38 | browserSync({
39 | notify: false,
40 | port: 8000,
41 | server: "dist",
42 | open: false
43 | });
44 | });
45 |
46 | gulp.task('html', function () {
47 | return gulp.src([
48 | 'src/index.html',
49 | ])
50 | .pipe(plugins.swig({
51 | defaults: { cache: false }
52 | }))
53 | .pipe(plugins.htmlmin({
54 | collapseBooleanAttributes: true,
55 | collapseWhitespace: true,
56 | minifyJS: true,
57 | removeAttributeQuotes: true,
58 | removeComments: true,
59 | removeEmptyAttributes: true,
60 | removeOptionalTags: true,
61 | removeRedundantAttributes: true,
62 | })).pipe(gulp.dest('dist'))
63 | .pipe(reload({stream: true}));
64 | });
65 |
66 | gulp.task('css', function () {
67 | return gulp.src('src/css/*.scss')
68 | .pipe(plugins.sourcemaps.init())
69 | .pipe(plugins.sass({ outputStyle: 'compressed' }))
70 | .pipe(plugins.sourcemaps.write('./'))
71 | .pipe(gulp.dest('dist/css'))
72 | .pipe(plugins.filter('**/*.css'))
73 | .pipe(reload({stream: true}));
74 | });
75 |
76 | gulp.task('misc', function () {
77 | return gulp.src([
78 | // Copy all files
79 | 'src/**',
80 | // Exclude the following files
81 | // (other tasks will handle the copying of these files)
82 | '!src/*.html',
83 | '!src/{css,css/**}',
84 | '!src/{js,js/**}'
85 | ]).pipe(gulp.dest('dist'));
86 | });
87 |
88 | function createBundler(src) {
89 | var b;
90 |
91 | if (plugins.util.env.production) {
92 | b = browserify();
93 | }
94 | else {
95 | b = browserify({
96 | cache: {}, packageCache: {}, fullPaths: true,
97 | debug: true
98 | });
99 | }
100 |
101 | b.transform(hbsfy);
102 |
103 | if (plugins.util.env.production) {
104 | b.transform({
105 | global: true
106 | }, 'uglifyify');
107 | }
108 |
109 | b.add(src);
110 | return b;
111 | }
112 |
113 | var bundlers = {
114 | 'js/page.js': createBundler('./src/js/page/index.js'),
115 | 'sw.js': createBundler('./src/js/sw/index.js')
116 | };
117 |
118 | function bundle(bundler, outputPath) {
119 | var splitPath = outputPath.split('/');
120 | var outputFile = splitPath[splitPath.length - 1];
121 | var outputDir = splitPath.slice(0, -1).join('/');
122 |
123 | return bundler.bundle()
124 | // log errors if they happen
125 | .on('error', plugins.util.log.bind(plugins.util, 'Browserify Error'))
126 | .pipe(source(outputFile))
127 | .pipe(buffer())
128 | .pipe(plugins.sourcemaps.init({ loadMaps: true })) // loads map from browserify file
129 | .pipe(plugins.sourcemaps.write('./')) // writes .map file
130 | .pipe(plugins.size({ gzip: true, title: outputFile }))
131 | .pipe(gulp.dest('dist/' + outputDir))
132 | .pipe(reload({ stream: true }));
133 | }
134 |
135 | gulp.task('js', function () {
136 | return mergeStream.apply(null,
137 | Object.keys(bundlers).map(function(key) {
138 | return bundle(bundlers[key], key);
139 | })
140 | );
141 | });
142 |
143 | gulp.task('watch', ['build'], function () {
144 | gulp.watch(['src/*.html'], ['html']);
145 | gulp.watch(['src/**/*.scss'], ['css']);
146 |
147 | Object.keys(bundlers).forEach(function(key) {
148 | var watchifyBundler = watchify(bundlers[key]);
149 | watchifyBundler.on('update', function() {
150 | return bundle(watchifyBundler, key);
151 | });
152 | bundle(watchifyBundler, key);
153 | });
154 | });
155 |
156 | gulp.task('build', function() {
157 | return runSequence('clean', ['css', 'misc', 'html', 'js']);
158 | });
159 |
160 | gulp.task('serve', ['browser-sync', 'watch']);
161 | gulp.task('default', ['build']);
162 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "trained-to-thrill",
3 | "scripts": {
4 | "test": "gulp build --production",
5 | "build": "gulp build --production",
6 | "serve": "gulp serve"
7 | },
8 | "private": true,
9 | "devDependencies": {
10 | "browser-sync": "^2.6.1",
11 | "browserify": "^9.0.8",
12 | "del": "^1.1.1",
13 | "es6-promise": "^2.0.1",
14 | "gulp": "^3.8.11",
15 | "gulp-filter": "^2.0.2",
16 | "gulp-htmlmin": "^1.1.1",
17 | "gulp-load-plugins": "^0.10.0",
18 | "gulp-sass": "^2.2.0",
19 | "gulp-size": "^1.2.1",
20 | "gulp-sourcemaps": "^1.5.1",
21 | "gulp-swig": "^0.7.4",
22 | "gulp-util": "^3.0.4",
23 | "handlebars": "^3.0.1",
24 | "hbsfy": "^2.2.1",
25 | "merge-stream": "^0.1.7",
26 | "run-sequence": "^1.0.2",
27 | "serviceworker-cache-polyfill": "^3.0.0",
28 | "through2": "^0.6.5",
29 | "uglifyify": "^3.0.1",
30 | "vinyl-buffer": "^1.0.0",
31 | "vinyl-source-stream": "^1.1.0",
32 | "watchify": "^3.1.0",
33 | "whatwg-fetch": "^0.7.0"
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | ## [View the Trained to Thrill demo](https://jakearchibald.github.io/trained-to-thrill/)
2 |
3 | ## Instructions:
4 |
5 | 1. Open Chrome
6 | 1. Click the above link
7 | 1. Turn off your network connection via wifi or Chrome DevTools [Device Mode](https://developer.chrome.com/devtools/docs/device-mode#network-conditions)
8 | 1. Reload the page! OMG STILL WORKS!!!1
9 | 1. Read the [service worker source js](https://github.com/jakearchibald/trained-to-thrill/blob/master/src/js/sw/index.js)
10 |
11 | ## Hack on the code
12 |
13 | ### Prerequisites
14 |
15 | * [Node](http://nodejs.org/)
16 |
17 | Then clone this repo & run:
18 |
19 | ```sh
20 | npm install
21 | ```
22 |
23 | ### Running
24 |
25 | ```sh
26 | npm run serve
27 | ```
28 |
29 | The server will be running at
30 | [http://localhost:8000/](http://localhost:8000/
31 | )
32 |
--------------------------------------------------------------------------------
/src/base.html:
--------------------------------------------------------------------------------
1 |
18 |
19 |
20 |
21 | {% block title %}{% endblock %}
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 | {% block head %}{% endblock %}
30 |
31 |
32 | {% block body %}{% endblock %}
33 |
34 | {% block bodyEnd %}{% endblock %}
35 |
36 |
37 |
--------------------------------------------------------------------------------
/src/css/_components.scss:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2015 Google Inc. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | // overall structure
19 | body {
20 | @include display-flex;
21 | @include flex-flow(column);
22 | }
23 |
24 | // site header
25 | @include keyframes (spin) {
26 | from {
27 | stroke-dashoffset: -29;
28 | }
29 | to {
30 | stroke-dashoffset: 222.33;
31 | }
32 | }
33 |
34 | .site-header {
35 | top: 0;
36 | left: 0;
37 | width: 100%;
38 | background: #DA50A8;
39 | overflow: hidden;
40 | position: relative;
41 | z-index: 10;
42 |
43 | & .logo {
44 | margin: 5px auto;
45 | display: block;
46 | width: 50px;
47 | height: 27px;
48 | }
49 | & .container {
50 | position: absolute;
51 | top: 0;
52 | left: 0;
53 | bottom: 0;
54 | right: 0;
55 | padding: 0 1rem;
56 | }
57 | & .inner {
58 | overflow: hidden;
59 | margin: 0 auto;
60 | max-width: 800px;
61 | height: 100%;
62 | }
63 | & .refresh {
64 | display: block;
65 | margin: 0 0 0 auto;
66 | position: relative;
67 | width: 20px;
68 | height: 100%;
69 |
70 | &:focus {
71 | outline: none;
72 | opacity: 0.7;
73 | }
74 |
75 | & svg {
76 | position: absolute;
77 | top: 0;
78 | left: 0;
79 | width: 100%;
80 | height: 100%;
81 | }
82 | &.loading {
83 | pointer-events: none;
84 |
85 | & circle {
86 | @include animation(spin 1s infinite linear);
87 | stroke-dasharray: 79.33 172;
88 | }
89 | & path {
90 | display: none;
91 | }
92 | }
93 | & circle {
94 | stroke-width: 20px;
95 | fill: none;
96 | stroke: #fff;
97 | stroke-linecap: round;
98 | stroke-dasharray: 201.33 50;
99 | stroke-dashoffset: -29;
100 | }
101 | & path {
102 | fill: #fff;
103 | }
104 | }
105 | }
106 |
107 | // photo
108 | .photos {
109 | overflow-y: scroll;
110 | -webkit-overflow-scrolling: touch;
111 | margin: 0;
112 | padding: 0 1rem;
113 | @include flex(1);
114 | }
115 | .photo {
116 | border-radius: 5px;
117 | background: #fff;
118 | padding: 1rem;
119 | margin: 1rem auto;
120 | max-width: 800px;
121 | box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
122 |
123 | & h1 {
124 | margin: 0;
125 |
126 | & a:link,
127 | & a:visited {
128 | color: #333;
129 | }
130 | & a:hover {
131 | color: #CE46C8;
132 | text-decoration: none;
133 | }
134 | & a:active {
135 | color: #FF00EB;
136 | }
137 | }
138 | & .title {
139 | display: block;
140 | margin: 1rem 0 0;
141 | font-size: 1rem;
142 | max-width: 570px;
143 | overflow: hidden;
144 | text-overflow: ellipsis;
145 | }
146 | & .description {
147 | margin: 1rem 0 0;
148 | font-size: 0.9rem;
149 | max-width: 570px;
150 | overflow: hidden;
151 | }
152 | }
153 | .main-photo {
154 | display: block;
155 | position: relative;
156 | padding-top: 69.15%;
157 | background: #E9E9E9;
158 | overflow: hidden;
159 |
160 |
161 | & .photo-fader {
162 | opacity: 0;
163 | @include transition(opacity 0.2s ease-out);
164 |
165 | &.loaded {
166 | opacity: 1;
167 | }
168 | }
169 |
170 |
171 | & img {
172 | position: absolute;
173 | top: 0;
174 | left: 0;
175 | display: block;
176 | width: 100%;
177 | }
178 | }
179 |
180 | @supports (object-fit: cover) {
181 | .main-photo {
182 | overflow: visible; // this saves us a lot of layer creation
183 | // and we don't overflow if the images fit
184 |
185 | }
186 | .main-photo img {
187 | height: 100%;
188 | object-fit: cover;
189 | }
190 | }
191 |
192 | // error
193 | .msg-container {
194 | position: fixed;
195 | top: 53px;
196 | left: 0;
197 | right: 0;
198 | z-index: 10;
199 | padding: 0 1rem;
200 | pointer-events: none;
201 | display: none;
202 | opacity: 0;
203 | @include transition(opacity 0.5s ease-in-out);
204 |
205 | &.show {
206 | opacity: 1;
207 | display: block;
208 | }
209 | }
210 | .msg {
211 | margin: 0 auto;
212 | color: #FFF;
213 | background: rgba(0, 0, 0, 0.7);
214 | @include max-width-max-content;
215 | padding: 1rem 2rem;
216 | border-radius: 100px;
217 | box-shadow: 0 2px 7px rgba(0, 0, 0, 0.7);
218 | }
219 |
--------------------------------------------------------------------------------
/src/css/_global.scss:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2015 Google Inc. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | html, body {
19 | height: 100%;
20 | }
21 |
22 | body {
23 | margin: 0;
24 | font-family: sans-serif;
25 | line-height: 1.4;
26 | background: #ECECEC;
27 | color: #333;
28 | -webkit-text-size-adjust: 100%;
29 | }
30 |
31 | * {
32 | -webkit-tap-highlight-color: rgba(0,0,0,0);
33 | -webkit-tap-highlight-color: transparent;
34 | }
35 |
36 | a:link,
37 | a:visited {
38 | text-decoration: none;
39 | }
40 |
41 | a:hover {
42 | text-decoration: underline;
43 | }
44 |
45 | button.custom {
46 | padding: 0;
47 | margin: 0;
48 | border: 0;
49 | background: none;
50 | cursor: pointer;
51 | overflow: visible;
52 | vertical-align: middle;
53 | font-size: inherit;
54 | width: auto;
55 | }
56 |
57 | button.custom::-moz-focus-inner {
58 | border: 0;
59 | padding: 0;
60 | }
61 |
--------------------------------------------------------------------------------
/src/css/_utils.scss:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2015 Google Inc. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | @mixin transition($spec...) {
19 | @each $prefix in -webkit-, '' {
20 | #{$prefix}transition: $spec;
21 | }
22 | }
23 |
24 | @mixin animation($spec...) {
25 | @each $prefix in -webkit-, '' {
26 | #{$prefix}animation: $spec;
27 | }
28 | }
29 |
30 | @mixin transform($spec...) {
31 | @each $prefix in -webkit-, '' {
32 | #{$prefix}transform: $spec;
33 | }
34 | }
35 |
36 | @mixin keyframes($name) {
37 | @-webkit-keyframes #{$name} { @content; }
38 | @keyframes #{$name} { @content; }
39 | }
40 |
41 | @mixin max-width-max-content {
42 | @each $prefix in -webkit-, -moz-, '' {
43 | max-width: #{$prefix}max-content;
44 | }
45 | }
46 |
47 | /**
48 | * Flexbox
49 | */
50 |
51 | @mixin display-flex {
52 | @each $prefix in -webkit-, '' {
53 | display: #{$prefix}flex;
54 | }
55 | }
56 | @mixin flex-direction($spec...) {
57 | @each $prefix in -webkit-, '' {
58 | #{$prefix}flex-direction: $spec;
59 | }
60 | }
61 | @mixin justify-content($spec...) {
62 | @each $prefix in -webkit-, '' {
63 | #{$prefix}justify-content: $spec;
64 | }
65 | }
66 | @mixin flex($spec...) {
67 | @each $prefix in -webkit-, '' {
68 | #{$prefix}flex: $spec;
69 | }
70 | }
71 | @mixin flex-flow($spec...) {
72 | @each $prefix in -webkit-, '' {
73 | #{$prefix}flex-flow: $spec;
74 | }
75 | }
76 | @mixin align-items($spec...) {
77 | @each $prefix in -webkit-, '' {
78 | #{$prefix}align-items: $spec;
79 | }
80 | }
81 | @mixin max-height-max-content {
82 | @each $prefix in -webkit-, -moz-, '' {
83 | max-height: #{$prefix}max-content;
84 | }
85 | }
86 | @mixin max-height-min-content {
87 | @each $prefix in -webkit-, -moz-, '' {
88 | max-height: #{$prefix}max-content;
89 | }
90 | }
91 | @mixin min-height-min-content {
92 | @each $prefix in -webkit-, -moz-, '' {
93 | min-height: #{$prefix}min-content;
94 | }
95 | }
96 | @mixin min-width-min-content {
97 | @each $prefix in -webkit-, -moz-, '' {
98 | min-width: #{$prefix}min-content;
99 | }
100 | }
101 |
--------------------------------------------------------------------------------
/src/css/all.scss:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2015 Google Inc. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | @import 'utils';
19 | @import 'global';
20 | @import 'components';
21 |
--------------------------------------------------------------------------------
/src/imgs/.gitignore:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jakearchibald/trained-to-thrill/4a1776232a91e62c9126562ce1820cd229cd6154/src/imgs/.gitignore
--------------------------------------------------------------------------------
/src/imgs/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jakearchibald/trained-to-thrill/4a1776232a91e62c9126562ce1820cd229cd6154/src/imgs/icon.png
--------------------------------------------------------------------------------
/src/imgs/logo.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/index.html:
--------------------------------------------------------------------------------
1 |
18 |
19 | {% extends 'base.html' %}
20 | {% block title %}Trained To Thrill{% endblock %}
21 |
22 | {% block body %}
23 |
24 |
25 |
26 |
27 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 | {% endblock %}
41 |
--------------------------------------------------------------------------------
/src/js/page/flickr.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2015 Google Inc. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | var utils = require('./utils');
19 |
20 | var apiKey = 'f2cca7d09b75c6cdea6864aca72e9895';
21 | var apiUrl = 'https://api.flickr.com/services/rest/';
22 |
23 | function search(text, opts) {
24 | var params = {
25 | method: 'flickr.photos.search',
26 | extras: 'description',
27 | format: 'json',
28 | api_key: apiKey,
29 | text: text,
30 | license: '4,5,6,7',
31 | content_type: 1,
32 | nojsoncallback: 1,
33 | per_page: 10
34 | };
35 |
36 | return fetch(apiUrl + '?' + utils.toQuerystring(params), opts).then(function(response) {
37 | return response.json();
38 | }).then(function(response) {
39 | if (response.stat == 'fail') {
40 | throw Error(response.err.msg);
41 | }
42 |
43 | return response.photos.photo.sort(function(a, b) {
44 | return b.id - a.id;
45 | }).map(function(photo) {
46 | return {
47 | id: photo.id,
48 | title: photo.title,
49 | flickrUrl: 'https://www.flickr.com/photos/' + photo.owner + '/' + photo.id + '/',
50 | imgUrl: 'https://farm' + photo.farm + '.staticflickr.com/' + photo.server + '/' + photo.id + '_' + photo.secret + '_c.jpg',
51 | description: photo.description._content.trim()
52 | };
53 | });
54 | });
55 | }
56 |
57 | module.exports.search = search;
58 |
--------------------------------------------------------------------------------
/src/js/page/index.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2015 Google Inc. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | window.Promise = window.Promise || require('es6-promise').Promise;
19 | require('whatwg-fetch');
20 |
21 | var flickr = require('./flickr');
22 | var photosTemplate = require('./views/photos.hbs');
23 | var utils = require('./utils');
24 | var searchTerm = 'train station';
25 |
26 | // force https
27 | if ((!location.port || location.port == "80") && location.protocol != 'https:') {
28 | location.protocol = 'https:';
29 | }
30 |
31 | var photosEl = document.querySelector('.photos');
32 | var refreshButton = document.querySelector('button.refresh');
33 | var msgEl = document.querySelector('.msg-container');
34 | var msgContentEl = document.querySelector('.msg');
35 | var photoIDsDisplayed = null;
36 |
37 | if ('serviceWorker' in navigator) {
38 | navigator.serviceWorker.register('./sw.js');
39 |
40 | // Warm up the cache on that very first use
41 | if (!navigator.serviceWorker.controller) {
42 | navigator.serviceWorker.addEventListener('controllerchange', function changeListener() {
43 | // New worker has claimed, warm up the caches
44 | flickr.search(searchTerm, {
45 | headers: {'x-cache-warmup': '1'}
46 | });
47 |
48 | // We only care about this once.
49 | navigator.serviceWorker.removeEventListener('controllerchange', changeListener);
50 | });
51 | }
52 | }
53 |
54 | function showSpinner(data) {
55 | refreshButton.classList.add('loading');
56 | }
57 |
58 | function hideSpinner(data) {
59 | refreshButton.classList.remove('loading');
60 | }
61 |
62 | function updatePage(data) {
63 | var scrollHeight;
64 |
65 |
66 | if (photoIDsDisplayed) {
67 | scrollHeight = photosEl.scrollHeight;
68 |
69 | data = data.filter(function(photo) {
70 | if (photoIDsDisplayed.indexOf(photo.id) == -1) {
71 | photoIDsDisplayed.push(photo.id);
72 | return true;
73 | }
74 | return false;
75 | });
76 |
77 | photosEl.insertBefore(utils.strToEls(photosTemplate(data)), photosEl.firstChild);
78 | photosEl.scrollTop += photosEl.scrollHeight - scrollHeight;
79 | }
80 | else {
81 | photoIDsDisplayed = data.map(function(p) { return p.id; });
82 | photosEl.insertBefore(utils.strToEls(photosTemplate(data)), photosEl.firstChild);
83 | }
84 | }
85 |
86 | function getTrainPhotoData() {
87 | return flickr.search(searchTerm, {
88 | headers: {}
89 | }).catch(function() {
90 | return null;
91 | });
92 | }
93 |
94 | function getCachedTrainPhotoData() {
95 | if ('serviceWorker' in navigator && navigator.serviceWorker.controller) {
96 | return flickr.search(searchTerm, {
97 | headers: {'x-use-cache-only': '1'}
98 | }).catch(function() {
99 | return null;
100 | });
101 | }
102 | else {
103 | return Promise.resolve(null);
104 | }
105 | }
106 |
107 | function showMessage(msg, duration) {
108 | msgContentEl.textContent = msg;
109 | msgEl.style.display = 'block';
110 | msgEl.offsetWidth;
111 | msgEl.classList.add('show');
112 | setTimeout(function() {
113 | msgEl.classList.remove('show');
114 | }, duration);
115 | }
116 |
117 | function showConnectionError() {
118 | showMessage("Connectivity derailed!", 5000);
119 | }
120 |
121 | // Refresh button
122 | refreshButton.addEventListener('click', function(event) {
123 | this.blur();
124 | event.preventDefault();
125 | showSpinner();
126 | getTrainPhotoData().then(function(data) {
127 | var oldLen = photoIDsDisplayed && photoIDsDisplayed.length;
128 | updatePage(data);
129 | if (oldLen != photoIDsDisplayed.length) {
130 | photosEl.scrollTop = 0;
131 | }
132 | }).catch(showConnectionError).then(hideSpinner);
133 | });
134 |
135 | // Initial load
136 |
137 | var liveDataFetched = getTrainPhotoData().then(function(data) {
138 | if (!data) return false;
139 |
140 | var alreadyRendered = !!photoIDsDisplayed;
141 | var oldLen = photoIDsDisplayed && photoIDsDisplayed.length;
142 | updatePage(data);
143 | if (alreadyRendered && oldLen != photoIDsDisplayed.length) {
144 | showMessage("▲ New trains ▲", 3000);
145 | }
146 | return true;
147 | });
148 |
149 | var cachedDataFetched = getCachedTrainPhotoData().then(function(data) {
150 | if (!data) return false;
151 | if (!photoIDsDisplayed) {
152 | updatePage(data);
153 | }
154 | return true;
155 | });
156 |
157 | liveDataFetched.then(function(fetched) {
158 | return fetched || cachedDataFetched;
159 | }).then(function(dataFetched) {
160 | if (!dataFetched) {
161 | showConnectionError();
162 | }
163 | hideSpinner();
164 | });
165 |
166 | // Add classes to fade-in images
167 | document.addEventListener('load', function(event) {
168 | if (event.target.classList.contains('main-photo-img')) {
169 | event.target.parentNode.classList.add('loaded');
170 | }
171 | }, true);
172 |
--------------------------------------------------------------------------------
/src/js/page/utils.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2015 Google Inc. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | */
17 |
18 | function defaults(opts, defaultOpts) {
19 | var r = Object.create(defaultOpts);
20 |
21 | if (!opts) { return r; }
22 |
23 | for (var key in opts) if (opts.hasOwnProperty(key)) {
24 | r[key] = opts[key];
25 | }
26 |
27 | return r;
28 | }
29 |
30 | function toQuerystring(obj) {
31 | var str = [];
32 | for (var p in obj) {
33 | if (obj.hasOwnProperty(p)) {
34 | str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
35 | }
36 | }
37 | return str.join("&");
38 | }
39 |
40 | var strToEls = (function () {
41 | var tmpEl = document.createElement('div');
42 | return function (str) {
43 | var r = document.createDocumentFragment();
44 | tmpEl.innerHTML = str;
45 | while (tmpEl.childNodes[0]) {
46 | r.appendChild(tmpEl.childNodes[0]);
47 | }
48 | return r;
49 | };
50 | }());
51 |
52 |
53 | module.exports = {
54 | defaults: defaults,
55 | toQuerystring: toQuerystring,
56 | strToEls: strToEls
57 | };
58 |
--------------------------------------------------------------------------------
/src/js/page/views/photos.hbs:
--------------------------------------------------------------------------------
1 | {{#each this}}
2 |
3 |