iron-doc-viewer.html
7.57 KB
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
<!--
@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="../marked-element/marked-element.html">
<link rel="import" href="../paper-styles/paper-styles.html">
<link rel="import" href="../paper-button/paper-button.html">
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../prism-element/prism-highlighter.html">
<link rel="import" href="iron-doc-property.html">
<!--
Renders documentation describing an element's API.
`iron-doc-viewer` renders element and behavior descriptions as extracted by
[Hydrolysis](https://github.com/PolymerLabs/hydrolysis). You can provide them
either via binding...
<iron-doc-viewer descriptor="{{elementDescriptor}}"></iron-doc-viewer>
...or by placing the element descriptor in JSON as the text content of an
`iron-doc-viewer`:
<iron-doc-viewer>
{
"is": "awesome-sauce",
"properties": [
{"name": "isAwesome", "type": "boolean", "desc": "Is it awesome?"},
]
}
</iron-doc-viewer>
However, be aware that due to current limitations in Polymer 0.8, _changes_ to
the text content will not be respected, only the initial value will be loaded.
If you wish to update the documented element, please set it via the `descriptor`
property.
@demo demo/index.html Basic Demo
-->
<dom-module id="iron-doc-viewer">
<link rel="import" type="css" href="iron-doc-viewer.css">
<template>
<prism-highlighter></prism-highlighter>
<section id="summary" class="card" hidden$="[[!descriptor.desc]]">
<header>Documentation</header>
<marked-element markdown="{{descriptor.desc}}"></marked-element>
</section>
<nav id="api">
<header>API Reference</header>
<paper-button id="togglePrivate"
on-tap="_togglePrivate">{{_privateToggleLabel}}</paper-button>
</nav>
<section id="properties" class="card" hidden$="{{_noneToShow(_showPrivate,_properties)}}">
<header>Properties</header>
<template is="dom-repeat" items="{{_properties}}" hidden$="{{!_properties.length}}">
<iron-doc-property descriptor="{{item}}"></iron-doc-property>
</template>
</section>
<section id="methods" class="card" hidden$="{{_noneToShow(_showPrivate,_methods)}}">
<header>Methods</header>
<template is="dom-repeat" items="{{_methods}}">
<iron-doc-property descriptor="{{item}}"></iron-doc-property>
</template>
</section>
<section id="events" class="card" hidden$="{{_noneToShow(_showPrivate,_events)}}">
<header>Events</header>
<template is="dom-repeat" items="{{_events}}">
<iron-doc-property descriptor="{{item}}"></iron-doc-property>
</template>
</section>
<section id="behaviors" class="card" hidden$="{{_hideBehaviors(_behaviors)}}">
<header>Behaviors</header>
<template is="dom-repeat" items="{{_behaviors}}">
<p on-click="_broadcastBehavior">{{item}}</p>
</template>
</section>
</template>
</dom-module>
<script>
(function() {
Polymer({
is: 'iron-doc-viewer',
properties: {
/**
* The [Hydrolysis](https://github.com/PolymerLabs/hydrolysis)-generated
* element descriptor to display details for.
*
* Alternatively, the element descriptor can be provided as JSON via the text content
* of this element.
*
* @type {hydrolysis.ElementDescriptor}
*/
descriptor: {
type: Object,
observer: '_descriptorChanged',
},
/** Whether private properties should be hidden or shown. */
_showPrivate: {
type: Boolean,
value: false,
observer: '_showPrivateChanged',
},
/** The label to show for the Private API toggle. */
_privateToggleLabel: String,
/**
* Broadcast when another component is clicked on
* @param {String} detail name of the component
* iron-doc-viewer container should load component if possible
* @event iron-doc-viewer-component-selected
*/
},
ready: function() {
var jsonDescriptor = this._loadJson();
// Note that this is only an error during element creation. You are free
// to stomp over the descriptor after it is ready.
if (jsonDescriptor && this.descriptor) {
console.error(
this,
'received both a bound descriptor:', this.descriptor,
'and JSON descriptor:', this._jsonDescriptor,
'Please provide only one');
throw new Error(
'<iron-doc-viewer> accepts either a bound or JSON descriptor; not both');
}
if (jsonDescriptor) {
this.descriptor = jsonDescriptor;
}
},
/**
* Loads a hydrolysis element descriptor (as JSON) from the text content of
* this element, if present.
*
* @return {hydrolysis.ElementDescriptor} The parsed descriptor, 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 {
return JSON.parse(textContent);
} catch(error) {
console.error('Failure when parsing JSON:', textContent, error);
throw error;
}
},
/** Converts `descriptor` into our template-friendly `_model`. */
_descriptorChanged: function() {
if (!this.descriptor) return;
// Split the documented properties between functions and other types.
var properties = [];
var methods = [];
for (var i = 0, property; property = this.descriptor.properties[i]; i++) {
(property.type === 'Function' ? methods : properties).push(property);
}
this._properties = properties;
this._methods = methods;
this._events = this.descriptor.events || [];
this._behaviors = this.descriptor.behaviors || [];
this.toggleAttribute('abstract', this.descriptor.abstract);
},
_collapsedChanged: function() {
this._collapseToggleLabel = this._collapsed ? 'expand' : 'collapse';
// Bound values aren't exposed to dom-repeat's scope.
var properties = this.querySelectorAll('iron-doc-property');
for (var i = 0, property; property = properties[i]; i++) {
property.collapsed = this._collapsed;
}
},
_toggleCollapsed: function() {
this._collapsed = !this._collapsed;
},
_showPrivateChanged: function() {
this._privateToggleLabel = (this._showPrivate ? 'hide' : 'show') + ' private API';
this.toggleClass('show-private', this._showPrivate);
},
_togglePrivate: function() {
this._showPrivate = !this._showPrivate;
},
_noneToShow: function(showPrivate, items) {
for (var i = 0; i < items.length; i++) {
if (showPrivate || !items[i].private) return false;
}
return true;
},
_hideBehaviors: function(behaviors) {
return behaviors === null || behaviors.length === 0;
},
_broadcastBehavior: function(ev) {
this.fire('iron-doc-viewer-component-selected', ev.target._templateInstance.item);
}
});
})();
</script>