-
Notifications
You must be signed in to change notification settings - Fork 97
/
google-codelab.html
552 lines (471 loc) · 16.8 KB
/
google-codelab.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
<!--
Copyright (c) 2015 Google Inc.
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.
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-a11y-keys-behavior/iron-a11y-keys-behavior.html">
<link rel="import" href="../iron-icons/iron-icons.html">
<link rel="import" href="../iron-localstorage/iron-localstorage.html">
<link rel="import" href="../neon-animation/neon-animated-pages.html">
<link rel="import" href="../neon-animation/animations/slide-from-right-animation.html">
<link rel="import" href="../neon-animation/animations/slide-from-left-animation.html">
<link rel="import" href="../neon-animation/animations/slide-right-animation.html">
<link rel="import" href="../neon-animation/animations/slide-left-animation.html">
<link rel="import" href="../app-layout/app-header-layout/app-header-layout.html">
<link rel="import" href="../app-layout/app-drawer/app-drawer.html">
<link rel="import" href="../app-layout/app-drawer-layout/app-drawer-layout.html">
<link rel="import" href="../paper-button/paper-button.html">
<link rel="import" href="../paper-dialog/paper-dialog.html">
<link rel="import" href="../paper-fab/paper-fab.html">
<link rel="import" href="../paper-icon-button/paper-icon-button.html">
<link rel="import" href="../paper-menu/paper-menu.html">
<link rel="import" href="../paper-item/paper-item.html">
<link rel="import" href="layout-style.html">
<link rel="import" href="codelab-style.html">
<link rel="import" href="analytics-behavior.html">
<!--
This is the top level component of google-codelab elements.
It expects to find one or more `google-codelab-step` elements in its content.
Example:
<google-codelab title="Awesome codelab" feedback-link="http://example.org"
last-updated="2015-01-28">
<google-codelab-step label="One" step="1" duration="10">
...
</google-codelab-step>
<google-codelab-step label="Two" step="2" duration="15">
...
</google-codelab-step>
</google-codelab>
The element can also be used in a minimalistic mode where less styling is
applied. Use the `theme="minimal"` attribute and combine with the
`noToolbar`, `noArrows`, and `noHighlight` properties to turn off the top nav and
prevent syntax highlighting code blocks, respectively:
<google-codelab theme="minimal" no-toolbar no-arrows no-highlight>
@demo demo/codelab.html
@demo demo/embed.html
-->
<dom-module id="google-codelab">
<template strip-whitespace>
<style include="layout-style"></style>
<style include="codelab-style"></style>
<iron-localstorage name="{{title}}" value="{{state}}" hidden
on-iron-localstorage-load-empty="_stateInit"
on-iron-localstorage-load="_stateLoaded">
</iron-localstorage>
<app-drawer-layout fullbleed narrow="{{_narrow}}">
<app-drawer id="drawer" swipe-open>
<div class="drawer-content-wrapper layout vertical" style="height:100%">
<div class="flex" style="overflow: auto;">
<div title="Time remaining" hidden$="{{!_isPositiveNum(duration)}}"
class="countdown layout horizontal">
<iron-icon icon="schedule"></iron-icon> <span>[[remaining]]</span>
</div>
<paper-menu id="toc" class="flex" selected="{{selected}}"
on-iron-select="_allowCodelabComplete">
<template is="dom-repeat" items="{{steps}}" strip-whitespace>
<paper-item class$="{{_tocItemClass(selected, index)}}">
<i>{{item.step}}</i><span>{{item.label}}</span>
</paper-item>
</template>
</paper-menu>
</div>
<div id="metadata">
<template is="dom-if" if="{{lastUpdated}}" strip-whitespace>
<div id="last-updated">Last updated on {{lastUpdated}}.</div>
</template>
<div id="feedback" hidden$="[[!feedbackLink]]">
Did you find a mistake?
<a target="_blank" href$="{{feedbackLink}}">Please file a bug</a>.
</div>
</div>
</div>
</app-drawer>
<app-header-layout fullbleed>
<div id="headerpanel" fixed narrow$="[[_narrow]]">
<div id="main-toolbar" hidden$="[[noToolbar]]">
<div>
<template is="dom-if" if="{{_narrow}}" strip-whitespace>
<paper-icon-button icon="menu" drawer-toggle></paper-icon-button>
</template>
<template is="dom-if" if="{{!_narrow}}" strip-whitespace>
<paper-icon-button icon="arrow-back" on-tap="_goToHome"></paper-icon-button>
</template>
</div>
<h3 class="title">[[title]]</h3>
<div title="Time remaining" hidden$="{{!_isPositiveNum(duration)}}"
class="countdown layout horizontal">
<iron-icon icon="schedule"></iron-icon> <span>{{remaining}}</span>
</div>
</div>
</div>
<div id="main-content">
<neon-animated-pages id="pages" selected="{{selected}}"
on-iron-deselect="_onStepLeave"
on-iron-items-changed="_onStepsChanged">
<content select="google-codelab-step"></content>
</neon-animated-pages>
<footer id="controls" hidden$="[[noArrows]]" narrow$="[[_narrow]]">
<div class="fabs layout horizontal justified">
<paper-button raised class="navbutton prevbutton"
on-tap="selectPrevious"
title="Previous step"
disabled$="{{_isFirstStep(selected)}}">Back</paper-button>
<div>
<template is="dom-if" if="{{_showNextFab(selected, steps)}}" strip-whitespace>
<paper-button raised class="navbutton nextbutton"
title="{{_nextFabTitle(selected)}}"
on-tap="selectNext">Next</paper-button>
</template>
<template is="dom-if" if="{{_showDoneFab(selected, steps)}}" strip-whitespace>
<paper-button raised class="navbutton donebutton"
on-tap="_goToHome" title="Complete codelab">Done</paper-button>
</template>
</div>
</div>
</footer>
</div>
</app-header-layout>
</app-drawer-layout>
<paper-dialog id="resumeDialog" modal>
<h2>Would you like to resume where you left off?</h2>
<p>Step <span>{{_storedStep.step}}</span>: <span>{{_storedStep.label}}</span></p>
<div class="buttons">
<paper-button dialog-dismiss on-click="_stateInit">Cancel</paper-button>
<paper-button dialog-confirm on-click="_resume" autofocus>Resume</paper-button>
</div>
</paper-dialog>
</template>
<script>
Polymer({
is: 'google-codelab',
/**
* Fired when the codelab steps have been fully initialized.
* @event google-codelab-ready
*/
/**
* Fired when user advances a codelab step or goes backwards.
* @event google-codelab-step
* detail {{step: Object, index: Number}}
*/
/**
* Fired when user reaches the last step of the codelab.
* @event google-codelab-complete
*/
behaviors: [
Polymer.IronA11yKeysBehavior,
GoogleCodelabs.AnalyticsBehavior
],
properties: {
/**
* Codelab title
*/
title: {
type: String,
value: ''
},
/**
* Main category, e.g. 'Android'
*/
category: {
type: String,
value: null
},
/**
* Codelab environment, e.g. 'web'
*/
environment: {
type: String,
value: null
},
/**
* Feedback URL for the codelab bug repors.
*/
feedbackLink: {
type: String,
value: ''
},
/**
* Current step index, 0-based.
* `location.hash` is synced with this number.
*/
selected: {
type: Number,
value: 0
},
/**
* Total duration of the codelab, in minutes.
*/
duration: {
type: Number,
value: 0,
readOnly: true
},
/**
* UI string for remaining tie to completion. Example: "5 min remaining".
*/
remaining: {
type: String,
value: '',
readOnly: true
},
/**
* Last date the codelab was updated, in YYYY-MM-DD format.
*/
lastUpdated: {
type: String,
value: ''
},
/**
* Step elements of the codelab. Currently, only `<google-codelab-step>`
* elements are supported.
* The steps are populated on 'attached': callback for distributed children
* changes is still being worked on https://github.com/Polymer/polymer/issues/1773
*/
steps: {
type: Array,
value: function() { return []; },
readOnly: true
},
/**
* Codelab state, synced with `localStorage`.
* See `_stateInit` for object properties.
*/
state: {
type: Object,
value: null
},
/**
* Hides the topnav toolbar.
*/
noToolbar: {
type: Boolean,
value: false
},
/**
* Hides the forward/background navigation arrows.
*/
noArrows: {
type: Boolean,
value: false
},
/**
* Property determines whether the toolbar is hidden.
*/
noHighlight: {
type: Boolean,
value: false,
reflectToAttribute: true
},
/**
* Last selected step before leaving the codelab.
* Used in the $.resumeDialog to prompt a user to resume the codelab
* where the left off.
*
* It is synced with the `state.stepIndex` one time only, when the storage
* is loaded.
*/
_storedStep: {
type: Object,
value: null
},
/**
* When true, indicates that the current step change was initiated
* from the $.resumeDialog by the user clicking on 'resume' button.
* This is mainly to prevent firing 'codelab-complete' event
* every time users return to a codelab they've actually already done.
*/
_resumed: {
type: Boolean,
value: false
},
/**
* True once the user has navigated to another page.
*/
_userHasNavigated: {
type: Boolean,
value: false,
readOnly: true
}
},
observers: [
'_selectedChanged(steps, selected)'
],
keyBindings: {
'left': 'selectPrevious',
'right': 'selectNext',
},
ready: function() {
// TODO: cannot be done on prototype.
// SEe https://github.com/PolymerElements/iron-a11y-keys-behavior/issues/19
this.keyEventTarget = /** @type {!HTMLBodyElement} */(document.body);
// select the step early, before localStorage is loaded
this._updateStepIndexFromUrl();
},
_onStepsChanged: function() {
var duration = 0;
var steps = this.$.pages.items.map(function(item, i) {
duration += item.duration;
item.step = i + 1;
return item;
}.bind(this));
this._setSteps(steps);
this._setDuration(duration);
this._updateRemaining();
this.fire('google-codelab-ready', {steps: this.steps});
},
/**
* Advance one step of the codelab.
*/
selectNext: function() {
this.$.pages.entryAnimation = 'slide-from-right-animation';
this.$.pages.exitAnimation = 'slide-left-animation';
this.select(this.selected + 1);
},
/**
* Go one step backwards.
*/
selectPrevious: function() {
this.$.pages.entryAnimation = 'slide-from-left-animation';
this.$.pages.exitAnimation = 'slide-right-animation';
this.select(this.selected - 1);
},
/**
* Select an arbitrary step of the codelab.
*/
select: function(stepIndex) {
if (stepIndex < 0 || stepIndex > this.steps.length - 1) {
return;
}
this._allowCodelabComplete();
this.selected = stepIndex;
},
_selectedChanged: function(steps, selected) {
this.set('state.stepIndex', this.selected);
this._updateRemaining();
if (!this.steps.length) {
return;
}
var step = this.steps[this.selected];
step.active = true;
location.hash = this.selected.toString();
this.fire('google-codelab-step', {index: this.selected, step: step});
if (this.selected === this.steps.length - 1 &&
!this._resumed && this._userHasNavigated) {
this.fire('google-codelab-complete');
}
},
_onStepLeave: function(e, detail) {
var prev = detail.item;
prev.active = false;
this._scrollToTopOfStep();
},
_stateInit: function() {
this.state = {stepIndex: this.selected};
},
_stateLoaded: function() {
// allow everything render and initialize,
// especially this.steps which runs at the same init level
// as iron-localstorage (on attached).
this.async(function() {
this._storedStep = this.steps[this.state.stepIndex];
// resume dialog is shown only when all 3 conditions hold:
// 1. user has already visited the codelab in the past
// 2. has made at least 1 step
// 3. user is landed on the first step
if (this.selected === 0 && this.state.stepIndex > 0) {
this.$.resumeDialog.open();
}
}, 500);
},
_updateStepIndexFromUrl: function() {
var step = parseInt(location.hash.slice(1), 10);
if (isNaN(step) || step < 0) {
step = 0;
}
if (this.steps && this.steps.length && step > this.steps.length - 1) {
step = this.steps.length - 1;
}
this.selected = step;
},
_allowCodelabComplete: function() {
this._set_userHasNavigated(true);
},
_updateRemaining: function() {
if (!this.steps) {
return;
}
var remain = 0;
this.steps.slice(this.selected).forEach(function(item) {
remain += (item.duration || 0);
});
var str = this._narrow ? '' : ' remaining';
this._setRemaining(remain + ' min' + str);
},
_resume: function() {
this._resumed = true;
this.select(this.state.stepIndex);
this._resumed = false;
},
_goToHome: function () {
// extract 'index' search param
var index;
var parts = location.search.substring(1).split('&');
for (var i = 0; i < parts.length; i++) {
var param = parts[i].split('=');
if (param[0] === 'index') {
index = param[1];
break;
}
}
// decode and extract index name from the search param
// default index is 'index'
index = index ? decodeURIComponent(index) : '';
index = index.replace(/[^a-z0-9\-]+/ig, '');
if (index === 'index') {
index = '';
}
// navigate away to the index page
window.location.href = '/' + index;
},
_tocItemClass: function(selected, i) {
var cls = ['toc-item'];
if (i < selected) {
cls.push('completed');
}
return cls.join(' ');
},
_scrollToTopOfStep: function() {
document.body.scrollTop = 0;
},
_isPositiveNum: function(duration) {
return duration > 0;
},
_isFirstStep: function(stepIndex) {
return isNaN(stepIndex) || stepIndex == 0;
},
_isLastStep: function(stepIndex) {
if (!this.steps.length) {
return false;
}
return stepIndex >= this.steps.length - 1;
},
_showDoneFab: function(stepIndex, steps) {
return this._isLastStep(stepIndex);
},
_showNextFab: function(stepIndex, steps) {
return !this._isLastStep(stepIndex);
},
_nextFabTitle: function(stepIndex) {
return stepIndex > 0 ? 'Next step' : 'Start codelab';
}
});
</script>
</dom-module>