hydrolysis-analyzer.html 3.07 KB
<!--
@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.html">

<script>
(function() {
var hydrolysis = require('hydrolysis');

  Polymer({

    is: 'hydrolysis-analyzer',

    properties: {

      /**
       * The URL to an import that declares (or transitively imports) the
       * elements that you wish to see analyzed.
       *
       * If the URL is relative, it will be resolved relative to the master
       * document.
       *
       * If you change this value after the `<hydrolysis-analyzer>` has been
       * instantiated, you must call `analyze()`.
       */
      src: {
        type: String,
      },

      /**
       * Whether _all_ dependencies should be loaded and analyzed.
       *
       * Turning this on will probably slow down the load process dramatically.
       */
      transitive: {
        type: Boolean,
      },

      /**
       * Whether the hydrolysis descriptors should be cleaned of redundant
       * properties.
       */
      clean: {
        type: Boolean,
      },

      /** The resultant `Analyzer` object from Hydrolysis. */
      analyzer: {
        type:     Object,
        readOnly: true,
        notify:   true,
      },

      /** Whether the analyzer is loading/analyzing resources. */
      loading: {
        type:     Boolean,
        readOnly: true,
        notify:   true,
      },

    },

    ready: function() {
      this.analyze();
    },

    /**
     * Begins loading the imports referenced by `src`.
     *
     * If you make changes to this element's configuration, you must call this
     * function to kick off another analysis pass.
     */
    analyze: function() {
      if (!this.src) {
        return;
      }
      // We can implement request cancellation when someone needs it.
      if (this.loading) {
        console.error('Analyzer is already loading a document:', this);
        return;
      }
      this._setLoading(true);

      var options = {
        clean:  this.clean,
        filter: this.transitive ? function() { return false; } : null,
        attachAst: true,
      };

      var baseUri = this.ownerDocument.baseURI;
      var srcUrl  = new URL(this.src, baseUri).toString();
      hydrolysis.Analyzer.analyze(srcUrl, options).then(function(analyzer) {
        this._setLoading(false);
        this._setAnalyzer(analyzer);
      }.bind(this))
      .catch(function(error) {
        console.error('Failed to load source at:', this.src, error);
        console.error(error.stack);
        this._setLoading(false);
        this._setAnalyzer(null);
      }.bind(this));
    },

  });

})();
</script>