<commit_before><!--
Lets a component collective takes as its content the children of the innermost
aspect.

@element basic-children-content
-->

<link rel="import" href="../basic-aspect/basic-aspect.html">
<script src="../basic-shared/ContentChanged.js"></script>

<!--
TODO: Don't respond to changes in attributes, or at least offer that as an
option.
-->

<script>
Polymer({

  behaviors: [Basic.Aspect],

  contribute: {

    // TODO: Wait to observe changes until we have a shadow DOM host. Right
    // now, the initial collectiveChanged call can happen too early.
    collectiveChanged: function() {
      // console.log(this.localName + " collectiveChanged: " + this.collective.aspects.length);
      var innermost = this.collective.innermost;

      // Optimize for the case where the innermost item didn't change.
      // REVIEW: In Polymer 0.8, the call to collectiveChanged could come too
      // early, and we would never correctly begin observing changes on the
      // component host. In 0.8rc7, this appears(?) to no longer be the case.
      // Need to apply more rigor to convince ourselves that this code is now
      // correct.

      if (innermost === this._previousInnermostAspect) {
        // We should already be observing changes on the innermost aspect.
        // Even though the content hasn't actually changed, we want to give the
        // new aspects a chance to respond to contentChanged.
        this.contentChanged();
        return;
      }

      // A new aspect is now innermost.
      if (this._previousInnermostAspect && this._previousInnermostAspect._contentChangeObserver) {
        // Stop observing changes on the old innermost aspect.
        // console.log("stopping observation of changes on old innermost aspect");
        Basic.ContentHelpers.observeContentChanges(this._previousInnermostAspect, false);
      }
      // Start observing changes on the new innermost aspect.
      if (innermost._contentChangeObserver) {
        // console.log("already being observed");
      }
      Basic.ContentHelpers.observeContentChanges(innermost);
      this._previousInnermostAspect = innermost;
    },

    contentChanged: function() {
      // Reset memoized content.
      this._content = null;

      var event = new CustomEvent('content-changed', {
        bubbles: true
      });
      this.dispatchEvent(event);
    },

    /**
     * The flattened content of this collective.
     *
     * The content is the collective of nodes which are children of the
     * collective's innermost aspect. If any of those nodes are `<content>`
     * elements, those are recursively expanded.
     *
     * @property content
     * @type [Object]
     */
    get content() {
      if (!this._content) {
        this._content = Basic.ContentHelpers.flattenChildren(this.collective.innermost);
      }
      return this._content;
    },

  },

  is: 'basic-children-content',

  ready: function() {
    // HACK: Ensure targetChanged (also) happens after ready.
    this.targetChanged(this.target);
  },

  // Memoized content
  _content: null,

  _previousInnermostAspect: null

});
</script>
<commit_msg>Use basic-shared.html import<commit_after><!--
Lets a component collective takes as its content the children of the innermost
aspect.

@element basic-children-content
-->

<link rel="import" href="../basic-aspect/basic-aspect.html">
<link rel="import" href="../basic-shared/basic-shared.html">

<!--
TODO: Don't respond to changes in attributes, or at least offer that as an
option.
-->

<script>
Polymer({

  behaviors: [Basic.Aspect],

  contribute: {

    // TODO: Wait to observe changes until we have a shadow DOM host. Right
    // now, the initial collectiveChanged call can happen too early.
    collectiveChanged: function() {
      // console.log(this.localName + " collectiveChanged: " + this.collective.aspects.length);
      var innermost = this.collective.innermost;

      // Optimize for the case where the innermost item didn't change.
      // REVIEW: In Polymer 0.8, the call to collectiveChanged could come too
      // early, and we would never correctly begin observing changes on the
      // component host. In 0.8rc7, this appears(?) to no longer be the case.
      // Need to apply more rigor to convince ourselves that this code is now
      // correct.

      if (innermost === this._previousInnermostAspect) {
        // We should already be observing changes on the innermost aspect.
        // Even though the content hasn't actually changed, we want to give the
        // new aspects a chance to respond to contentChanged.
        this.contentChanged();
        return;
      }

      // A new aspect is now innermost.
      if (this._previousInnermostAspect && this._previousInnermostAspect._contentChangeObserver) {
        // Stop observing changes on the old innermost aspect.
        // console.log("stopping observation of changes on old innermost aspect");
        Basic.ContentHelpers.observeContentChanges(this._previousInnermostAspect, false);
      }
      // Start observing changes on the new innermost aspect.
      if (innermost._contentChangeObserver) {
        // console.log("already being observed");
      }
      Basic.ContentHelpers.observeContentChanges(innermost);
      this._previousInnermostAspect = innermost;
    },

    contentChanged: function() {
      // Reset memoized content.
      this._content = null;

      var event = new CustomEvent('content-changed', {
        bubbles: true
      });
      this.dispatchEvent(event);
    },

    /**
     * The flattened content of this collective.
     *
     * The content is the collective of nodes which are children of the
     * collective's innermost aspect. If any of those nodes are `<content>`
     * elements, those are recursively expanded.
     *
     * @property content
     * @type [Object]
     */
    get content() {
      if (!this._content) {
        this._content = Basic.ContentHelpers.flattenChildren(this.collective.innermost);
      }
      return this._content;
    },

  },

  is: 'basic-children-content',

  ready: function() {
    // HACK: Ensure targetChanged (also) happens after ready.
    this.targetChanged(this.target);
  },

  // Memoized content
  _content: null,

  _previousInnermostAspect: null

});
</script>

{'fraction_non_alphanumeric': 0.09946065989847716, 'fraction_numerical': 0.0015862944162436548, 'mean_word_length': 2.7219598583234945, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4197953', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1683, 'n_words': 575}