Blame view

bower_components/iron-component-page/iron-component-page.html 14.9 KB
eb240478   Luigi Serra   public room cards...
1
2
3
4
5
6
7
8
9
  <!--
  @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
  -->
eb240478   Luigi Serra   public room cards...
10
11
  <link rel="import" href="../polymer/polymer.html">
  <link rel="import" href="../hydrolysis/hydrolysis-analyzer.html">
eb240478   Luigi Serra   public room cards...
12
  <link rel="import" href="../iron-doc-viewer/iron-doc-viewer.html">
eb240478   Luigi Serra   public room cards...
13
  <link rel="import" href="../iron-icons/iron-icons.html">
c5169e0e   Renato De Donato   a new hope
14
  <link rel="import" href="../iron-ajax/iron-ajax.html">
eb240478   Luigi Serra   public room cards...
15
16
  <link rel="import" href="../iron-selector/iron-selector.html">
  <link rel="import" href="../paper-header-panel/paper-header-panel.html">
eb240478   Luigi Serra   public room cards...
17
  <link rel="import" href="../paper-toolbar/paper-toolbar.html">
c5169e0e   Renato De Donato   a new hope
18
  <link rel="import" href="../paper-styles/paper-styles.html">
eb240478   Luigi Serra   public room cards...
19
20
21
22
23
24
  
  <!--
  Loads Polymer element and behavior documentation using
  [Hydrolysis](https://github.com/PolymerLabs/hydrolysis) and renders a complete
  documentation page including demos (if available).
  -->
eb240478   Luigi Serra   public room cards...
25
  <dom-module id="iron-component-page">
c5169e0e   Renato De Donato   a new hope
26
    <link rel="import" type="css" href="iron-component-page.css">
eb240478   Luigi Serra   public room cards...
27
    <template>
eb240478   Luigi Serra   public room cards...
28
      <hydrolysis-analyzer id="analyzer" src="[[_srcUrl]]" transitive="[[transitive]]" clean analyzer="{{_hydroDesc}}" loading="{{_hydroLoading}}"></hydrolysis-analyzer>
c5169e0e   Renato De Donato   a new hope
29
      <iron-ajax id="ajax" url="[[docSrc]]" handle-as="json" on-response="_handleAjaxResponse"></iron-ajax>
eb240478   Luigi Serra   public room cards...
30
  
c5169e0e   Renato De Donato   a new hope
31
      <paper-header-panel id="headerPanel" mode="waterfall">
eb240478   Luigi Serra   public room cards...
32
        <paper-toolbar catalog-hidden>
c5169e0e   Renato De Donato   a new hope
33
          <div class="flex">
eb240478   Luigi Serra   public room cards...
34
            <!-- TODO: Replace with paper-dropdown-menu when available -->
c5169e0e   Renato De Donato   a new hope
35
            <select id="active" value="{{active::change}}">
eb240478   Luigi Serra   public room cards...
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
              <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>
c5169e0e   Renato De Donato   a new hope
55
              <iron-doc-viewer descriptor="{{_activeDescriptor}}"
eb240478   Luigi Serra   public room cards...
56
                on-iron-doc-viewer-component-selected="_handleComponentSelectedEvent"></iron-doc-viewer>
c5169e0e   Renato De Donato   a new hope
57
              <div id="nodocs" hidden$="[[_activeDescriptor]]" class="layout fit horizontal center-center">
eb240478   Luigi Serra   public room cards...
58
59
60
61
62
63
64
65
                No documentation found.
              </div>
            </div>
            <div id="demo"></div>
          </iron-selector>
        </div>
      </paper-header-panel>
    </template>
c5169e0e   Renato De Donato   a new hope
66
  </dom-module>
eb240478   Luigi Serra   public room cards...
67
  
c5169e0e   Renato De Donato   a new hope
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
  <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',
        },
eb240478   Luigi Serra   public room cards...
107
108
  
        /**
c5169e0e   Renato De Donato   a new hope
109
110
111
112
113
114
         * 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.
eb240478   Luigi Serra   public room cards...
115
         */
c5169e0e   Renato De Donato   a new hope
116
117
118
119
        docSrc: {
          type: String,
          observer: '_srcChanged',
        },
eb240478   Luigi Serra   public room cards...
120
  
c5169e0e   Renato De Donato   a new hope
121
122
123
124
125
126
127
128
129
130
        /**
         * The relative root for determining paths to demos and default source
         * detection.
         */
        base: {
          type: String,
          value: function() {
            return this.ownerDocument.baseURI;
          }
        },
eb240478   Luigi Serra   public room cards...
131
  
c5169e0e   Renato De Donato   a new hope
132
133
134
135
136
137
138
139
140
        /**
         * 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,
          notify: true,
          value: ''
        },
eb240478   Luigi Serra   public room cards...
141
  
c5169e0e   Renato De Donato   a new hope
142
143
144
145
146
147
148
149
        /**
         * The current view. Can be `docs` or `demo`.
         */
        view: {
          type: String,
          value: 'docs',
          notify: true
        },
eb240478   Luigi Serra   public room cards...
150
  
c5169e0e   Renato De Donato   a new hope
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
        /**
         * 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,
          notify: true,
          readOnly: true,
          value: function() {
            return [];
          }
        },
  
        /** The Hydrolysis behavior descriptors that have been loaded. */
        docBehaviors: {
          type:     Array,
          notify: true,
          readOnly: true,
          value: function() {
            return [];
          }
        },
eb240478   Luigi Serra   public room cards...
180
  
c5169e0e   Renato De Donato   a new hope
181
182
183
184
185
186
187
188
        /**
         * Demos for the currently selected element.
         */
        docDemos: {
          type: Array,
          notify: true,
          readOnly: true
        },
eb240478   Luigi Serra   public room cards...
189
  
c5169e0e   Renato De Donato   a new hope
190
191
192
193
194
195
        /**
         * The currently displayed element.
         *
         * @type {!hydrolysis.ElementDescriptor}
         */
        _activeDescriptor: Object,
eb240478   Luigi Serra   public room cards...
196
  
c5169e0e   Renato De Donato   a new hope
197
198
199
200
201
202
203
204
205
206
207
208
209
        /**
         * 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,
eb240478   Luigi Serra   public room cards...
210
  
c5169e0e   Renato De Donato   a new hope
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
        /**
         * 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,
      },
  
      observers: [
        '_updateFrameSrc(view, base)',
        '_activeChanged(active, _analyzer)'
      ],
  
      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();
        }
eb240478   Luigi Serra   public room cards...
312
  
c5169e0e   Renato De Donato   a new hope
313
314
315
316
317
        // 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];
        }
eb240478   Luigi Serra   public room cards...
318
  
c5169e0e   Renato De Donato   a new hope
319
320
321
322
        this._baseUrl = _baseUrl(srcUrl);
        this._srcUrl  = srcUrl;
        if (!this._hydroLoading) this.$.analyzer.analyze();
      },
eb240478   Luigi Serra   public room cards...
323
  
c5169e0e   Renato De Donato   a new hope
324
325
      _updateFrameSrc: function(view) {
        if (!view || view.indexOf("demo:") !== 0) return "about:blank";
eb240478   Luigi Serra   public room cards...
326
  
c5169e0e   Renato De Donato   a new hope
327
328
        var src = view.split(':')[1];
        var demoSrc = new URL(src, this.base).toString();
eb240478   Luigi Serra   public room cards...
329
  
c5169e0e   Renato De Donato   a new hope
330
331
        // If you use history.pushState with iframe.src = url, you will create 2 history entries,
        // but setting the `src` attribute imperatively will not.
eb240478   Luigi Serra   public room cards...
332
  
c5169e0e   Renato De Donato   a new hope
333
334
335
        if (this._iframe) {
          Polymer.dom(this.$.demo).removeChild(this._iframe);
        }
eb240478   Luigi Serra   public room cards...
336
  
c5169e0e   Renato De Donato   a new hope
337
338
339
340
        this._iframe = document.createElement('iframe');
        this._iframe.src = demoSrc;
        Polymer.dom(this.$.demo).appendChild(this._iframe);
      },
eb240478   Luigi Serra   public room cards...
341
  
c5169e0e   Renato De Donato   a new hope
342
343
344
345
      _getDefaultActive: function() {
        var matchedPage;
        var url = this._srcUrl || this.base;
        var mainFile = url.replace(_baseUrl(this.base), '');
eb240478   Luigi Serra   public room cards...
346
  
c5169e0e   Renato De Donato   a new hope
347
348
349
350
351
352
353
354
355
356
357
        function findMatch(list) {
          for (var item, i = 0; i < list.length; i++) {
            item = list[i];
            if (item && item.contentHref && item.contentHref.indexOf(mainFile) > 0) {
              return item;
            }
          }
          return null;
        }
        
        matchedPage = findMatch(this.docElements) || findMatch(this.docBehaviors);
eb240478   Luigi Serra   public room cards...
358
  
c5169e0e   Renato De Donato   a new hope
359
360
361
362
363
364
365
366
367
        if (matchedPage) {
          return matchedPage.is;
        } else if (this.docElements.length > 0) {
          return this.docElements[0].is;
        } else if (this.docBehaviors.length > 0) {
          return this.docBehaviors[0].is;
        }
        return null;
      },
eb240478   Luigi Serra   public room cards...
368
  
c5169e0e   Renato De Donato   a new hope
369
370
      _findDescriptor: function(name) {
        if (!this._analyzer) return null;
eb240478   Luigi Serra   public room cards...
371
  
c5169e0e   Renato De Donato   a new hope
372
373
        var descriptor = this._analyzer.elementsByTagName[name];
        if (descriptor) return descriptor;
eb240478   Luigi Serra   public room cards...
374
  
c5169e0e   Renato De Donato   a new hope
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
        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(active, analyzer) {
        if (active === '') {
          this.active = this._getDefaultActive();
          return;
        }
        this.async(function() { this.$.active.value = active; });
        if (analyzer && analyzer.elementsByTagName) {
          this.$.headerPanel.scroller.scrollTop = 0;
          this._activeDescriptor = this._findDescriptor(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;
                }
              }
eb240478   Luigi Serra   public room cards...
410
            }
c5169e0e   Renato De Donato   a new hope
411
412
            if (!hasDemo == undefined) {
              this.view = 'docs';
eb240478   Luigi Serra   public room cards...
413
            }
c5169e0e   Renato De Donato   a new hope
414
415
            if (this._activeDescriptor.is && !document.title) {
              document.title = this._activeDescriptor.is + " documentation";
eb240478   Luigi Serra   public room cards...
416
            }
eb240478   Luigi Serra   public room cards...
417
          }
c5169e0e   Renato De Donato   a new hope
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
          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() {
        var analyzer = this._analyzer;
        this._setDocElements(analyzer && analyzer.elements ? analyzer.elements : []);
        this._setDocBehaviors(analyzer && analyzer.behaviors ? analyzer.behaviors : []);
  
        if (!this._findDescriptor(this.active)) {
          this.active = this._getDefaultActive();
        }
      },
      _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, '&lt;').replace(/>/g, '&gt;') + '\n' +
               '</' + this.is + '>';
      },
  
      _demoView: function(path) {
        return "demo:" + path;
      },
      _viewType: function(view) {
        return view ? view.split(":")[0] : null;
      }
    });
  })();
  </script>