73bcce88
luigser
COMPONENTS
|
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
|
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../hydrolysis/hydrolysis-analyzer.html">
<link rel="import" href="../iron-doc-viewer/iron-doc-viewer.html">
<link rel="import" href="../iron-icons/iron-icons.html">
<link rel="import" href="../iron-ajax/iron-ajax.html">
<link rel="import" href="../iron-selector/iron-selector.html">
<link rel="import" href="../paper-header-panel/paper-header-panel.html">
<link rel="import" href="../paper-toolbar/paper-toolbar.html">
<link rel="import" href="../paper-styles/paper-styles.html">
<!--
Loads Polymer element and behavior documentation using
[Hydrolysis](https://github.com/PolymerLabs/hydrolysis) and renders a complete
documentation page including demos (if available).
-->
<dom-module id="iron-component-page">
<link rel="import" type="css" href="iron-component-page.css">
<template>
<hydrolysis-analyzer id="analyzer" src="[[_srcUrl]]" transitive="[[transitive]]" clean analyzer="{{_hydroDesc}}" loading="{{_hydroLoading}}"></hydrolysis-analyzer>
<iron-ajax id="ajax" url="[[docSrc]]" handle-as="json" on-response="_handleAjaxResponse"></iron-ajax>
<paper-header-panel id="headerPanel" mode="waterfall">
<paper-toolbar catalog-hidden>
<div class="flex">
<!-- TODO: Replace with paper-dropdown-menu when available -->
<select id="active" value="{{active::change}}">
<template is="dom-repeat" items="[[docElements]]">
<option value="[[item.is]]">[[item.is]]</option>
</template>
<template is="dom-repeat" items="[[docBehaviors]]">
<option value="[[item.is]]">[[item.is]]</option>
</template>
</select>
</div>
<iron-selector attr-for-selected="view" selected="{{view}}" id="links" hidden$="[[!docDemos.length]]">
<a view="docs"><iron-icon icon="description"></iron-icon> Docs</a>
<a view="[[_demoView(docDemos.0.path)]]"><iron-icon icon="visibility"></iron-icon> <span>Demo</span></a>
</iron-selector>
</paper-toolbar>
<div id="content">
<iron-selector id="view" selected="[[_viewType(view)]]" attr-for-selected="id">
<div id="docs">
<div id="catalog-heading" catalog-only>
<h2><span>[[active]]</span> <span class="version" hidden$="[[!version]]">[[version]]</span></h2>
</div>
<iron-doc-viewer descriptor="{{_activeDescriptor}}"
on-iron-doc-viewer-component-selected="_handleComponentSelectedEvent"></iron-doc-viewer>
<div id="nodocs" hidden$="[[_activeDescriptor]]" class="layout fit horizontal center-center">
No documentation found.
</div>
</div>
<iframe id="demo" src="[[_frameSrc(view, base)]]"></iframe>
</iron-selector>
</div>
</paper-header-panel>
</template>
</dom-module>
<script>
(function() {
// var hydrolysis = require('hydrolysis');
/**
* @param {string} url
* @return {string} `url` stripped of a file name, if one is present. This
* considers URLs like "example.com/foo" to already be a base (no `.` is)
* present in the final path part).
*/
function _baseUrl(url) {
return url.match(/^(.*?)\/?([^\/]+\.[^\/]+)?$/)[1] + '/';
}
Polymer({
is: 'iron-component-page',
enableCustomStyleProperties: true,
properties: {
/**
* The URL to an import that declares (or transitively imports) the
* elements that you wish to see documented.
*
* If the URL is relative, it will be resolved relative to the master
* document.
*
* If a `src` URL is not specified, it will resolve the name of the
* directory containing this element, followed by `dirname.html`. For
* example:
*
* `awesome-sauce/index.html`:
*
* <iron-doc-viewer></iron-doc-viewer>
*
* Would implicitly have `src="awesome-sauce.html"`.
*/
src: {
type: String,
observer: '_srcChanged',
},
/**
* The URL to a precompiled JSON descriptor. If you have precompiled
* and stored a documentation set using Hydrolysis, you can load the
* analyzer directly via AJAX by specifying this attribute.
*
* If a `doc-src` is not specified, it is ignored and the default
* rules according to the `src` attribute are used.
*/
docSrc: {
type: String,
observer: '_srcChanged',
},
/**
* The relative root for determining paths to demos and default source
* detection.
*/
base: {
type: String,
value: function() {
return this.ownerDocument.baseURI;
}
},
/**
* The element or behavior that will be displayed on the page. Defaults
* to the element matching the name of the source file.
*/
active: {
type: String,
observer: '_activeChanged',
notify: true
},
/**
* The current view. Can be `docs` or `demo`.
*/
view: {
type: String,
value: 'docs',
notify: true
},
/**
* Whether _all_ dependencies should be loaded and documented.
*
* Turning this on will probably slow down the load process dramatically.
*/
transitive: {
type: Boolean,
value: false
},
/** The Hydrolysis element descriptors that have been loaded. */
docElements: {
type: Array,
observer: '_descriptorsChanged',
notify: true,
readOnly: true
},
/** The Hydrolysis behavior descriptors that have been loaded. */
docBehaviors: {
type: Array,
observer: '_descriptorsChanged',
notify: true,
readOnly: true
},
/**
* Demos for the currently selected element.
*/
docDemos: {
type: Array,
notify: true,
readOnly: true
},
/**
* The currently displayed element.
*
* @type {!hydrolysis.ElementDescriptor}
*/
_activeDescriptor: Object,
/**
* Toggle flag to be used when this element is being displayed in the
* Polymer Elements catalog.
*/
_catalog: {
type: Boolean,
value: false,
reflectToAttribute: true
},
/**
* An optional version string.
*/
version: String,
/**
* The hydrolysis analyzer.
*
* @type {!hydrolysis.Analyzer}
*/
_analyzer: {
type: Object,
observer: '_analyzerChanged',
},
_hydroDesc: {
type: Object,
observer: '_detectAnalyzer'
},
_ajaxDesc: {
type: Object,
observer: '_detectAnalyzer'
},
/** Whether the analyzer is loading source. */
_loading: {
type: Boolean,
observer: '_loadingChanged',
},
_hydroLoading: {
type: Boolean,
observer: '_detectLoading'
},
_ajaxLoading: {
type: Boolean,
observer: '_detectLoading'
},
/** The complete URL to this component's demo. */
_demoUrl: {
type: String,
value: '',
},
/** The complete URL to this component's source. */
_srcUrl: String,
},
ready: function() {
var elements = this._loadJson();
if (elements) {
this.docElements = elements;
this._loading = false;
} else {
// Make sure our change handlers trigger in all cases.
if (!this.src && !this._catalog) {
this._srcChanged();
}
}
},
/**
* Loads an array of hydrolysis element descriptors (as JSON) from the text
* content of this element, if present.
*
* @return {Array<hydrolysis.ElementDescriptor>} The descriptors, or `null`.
*/
_loadJson: function() {
var textContent = '';
Array.prototype.forEach.call(Polymer.dom(this).childNodes, function(node) {
textContent = textContent + node.textContent;
});
textContent = textContent.trim();
if (textContent === '') return null;
try {
var json = JSON.parse(textContent);
if (!Array.isArray(json)) return [];
return json;
} catch(error) {
console.error('Failure when parsing JSON:', textContent, error);
throw error;
}
},
_srcChanged: function() {
var srcUrl;
if (this.docSrc) {
if (!this.$.ajax.lastRequest || (this.docSrc !== this.$.ajax.lastRequest.url && this.docSrc !== this._lastDocSrc)) {
this._ajaxLoading = true;
this._ajaxDesc = null;
this._activeDescriptor = null;
this.$.ajax.generateRequest();
}
this._lastDocSrc = this.docSrc;
return;
} else if (this.src) {
srcUrl = new URL(this.src, this.base).toString();
} else {
var base = _baseUrl(this.base);
srcUrl = new URL(base.match(/([^\/]*)\/$/)[1] + ".html", base).toString();
}
// Rewrite gh-pages URLs to https://rawgit.com/
var match = srcUrl.match(/([^\/\.]+)\.github\.io\/([^\/]+)\/?([^\/]*)$/);
if (match) {
srcUrl = "https://cdn.rawgit.com/" + match[1] + "/" + match[2] + "/master/" + match[3];
}
this._baseUrl = _baseUrl(srcUrl);
this._srcUrl = srcUrl;
if (!this._hydroLoading) this.$.analyzer.analyze();
},
_frameSrc: function(view) {
if (!view || view.indexOf("demo:") !== 0) return "about:blank";
var src = view.split(':')[1];
return new URL(src, this.base).toString();
},
_descriptorsChanged: function() {
if (this._findDescriptor(this.active)) {
this._activeChanged();
return;
}
if (this.docElements && this.docElements[0]) {
this.active = this.docElements[0].is;
} else if (this.docBehaviors && this.docBehaviors[0]) {
this.active = this.docBehaviors[0].is;
} else {
this.active = null;
}
},
_findDescriptor: function(name) {
if (!this._analyzer) return null;
var descriptor = this._analyzer.elementsByTagName[name];
if (descriptor) return descriptor;
for (var i = 0; i < this._analyzer.behaviors.length; i++) {
if (this._analyzer.behaviors[i].is === name) {
return this._analyzer.behaviors[i];
}
}
return null;
},
_activeChanged: function() {
this.async(function() { this.$.active.value = this.active; });
if (this._analyzer && this._analyzer.elementsByTagName) {
this.$.headerPanel.scroller.scrollTop = 0;
this._activeDescriptor = this._findDescriptor(this.active);
if (this._activeDescriptor) {
var hasDemo;
var demos = this._activeDescriptor.demos;
if (this.view && demos && demos.length) {
var parts = this.view.split(':');
if (parts[0] == 'demo') {
if (parts[1]) {
hasDemo = demos.some(function(d, i) {
if (d.path == parts[1]) {
return true;
}
});
}
if (!hasDemo) {
this.view = 'demo:' + demos[0].path;
hasDemo = true;
}
}
}
if (!hasDemo == undefined) {
this.view = 'docs';
}
if (this._activeDescriptor.is && !document.title) {
document.title = this._activeDescriptor.is + " documentation";
}
}
this._setDocDemos(this._activeDescriptor ? this._activeDescriptor.demos : []);
}
},
_loadingChanged: function() {
this.toggleClass('loaded', !this._loading);
},
_detectLoading: function() {
this._loading = this.docSrc ? this._ajaxLoading : this._hydroLoading;
},
_analyzerChanged: function() {
this._setDocElements(this._analyzer ? this._analyzer.elements : []);
this._setDocBehaviors(this._analyzer ? this._analyzer.behaviors : []);
},
_detectAnalyzer: function() {
this._analyzer = this.docSrc ? this._ajaxDesc : this._hydroDesc;
},
_handleAjaxResponse: function(e, req) {
this._ajaxLoading = false;
this._ajaxLastUrl = req.url;
this._ajaxDesc = req.response;
},
_handleComponentSelectedEvent: function(ev) {
var descriptor = this._findDescriptor(ev.detail);
if (!descriptor) {
console.warn("Could not navigate to ", ev.detail);
}
else {
this.active = ev.detail;
}
},
/**
* Renders this element into static HTML for offline use.
*
* This is mostly useful for debugging and one-off documentation generation.
* If you want to integrate doc generation into your build process, you
* probably want to be calling `hydrolysis.Analyzer.analyze()` directly.
*
* @return {string} The HTML for this element with all state baked in.
*/
marshal: function() {
var jsonText = JSON.stringify(this.docElements || [], null, ' ');
return '<' + this.is + '>\n' +
jsonText.replace(/</g, '<').replace(/>/g, '>') + '\n' +
'</' + this.is + '>';
},
_demoView: function(path) {
return "demo:" + path;
},
_viewType: function(view) {
return view ? view.split(":")[0] : null;
}
});
})();
</script>
|