github.com/jojicluka/consul-api@v1.4.5/ui-v2/lib/block-slots/addon/components/block-slot.js (about)

     1  import Component from '@ember/component';
     2  import { isPresent } from '@ember/utils';
     3  import { get, set, computed, defineProperty } from '@ember/object';
     4  import layout from '../templates/components/block-slot';
     5  import Slots from '../mixins/slots';
     6  import YieldSlot from './yield-slot';
     7  const BlockSlot = Component.extend({
     8    layout,
     9    tagName: '',
    10    didInsertElement: function() {
    11      const slottedComponent = this.nearestOfType(Slots);
    12      if (!slottedComponent._isRegistered(this._name)) {
    13        slottedComponent._activateSlot(this._name);
    14        set(this, 'slottedComponent', slottedComponent);
    15        return;
    16      }
    17      const yieldSlot = this.nearestOfType(YieldSlot);
    18      if (yieldSlot) {
    19        set(this, '_yieldSlot', yieldSlot);
    20        // The slotted component will yield multiple times - once to register
    21        // the activate slots and once more per active slot - only display this
    22        // block when its associated slot is the one yielding
    23        const isTargetSlotYielding = yieldSlot._name === this._name;
    24        set(this, 'isTargetSlotYielding', isTargetSlotYielding);
    25  
    26        // If the associated slot has block params, create a computed property
    27        // for each block param.  Technically this could be an unlimited, but
    28        // hbs lacks a spread operator so params are currently limited to 9
    29        // (see the yield in the block-slot template)
    30        //
    31        // Spread PR: https://github.com/wycats/handlebars.js/pull/1149
    32        if (isTargetSlotYielding && isPresent(yieldSlot._blockParams)) {
    33          // p0 p1 p2...
    34          yieldSlot._blockParams.forEach((param, index) => {
    35            defineProperty(this, `p${index}`, computed.readOnly(`_yieldSlot._blockParams.${index}`));
    36          });
    37        }
    38      }
    39    },
    40    willDestroyElement: function() {
    41      const slottedComponent = get(this, 'slottedComponent');
    42      if (slottedComponent) {
    43        // Deactivate the yield slot using the slots interface when the block
    44        // is destroyed to allow the yield slot default {{else}} to take effect
    45        // dynamically
    46        slottedComponent._deactivateSlot(this._name);
    47      }
    48    },
    49  });
    50  
    51  BlockSlot.reopenClass({
    52    positionalParams: ['_name'],
    53  });
    54  
    55  export default BlockSlot;