All files / addon/components nucleus-banner.js

62.5% Statements 10/16
60% Branches 6/10
50% Functions 4/8
62.5% Lines 10/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                          6x                   5x                   5x                   1x                     6x 6x                                           6x 6x                     6x                       1x                                                                                                              
import { layout as templateLayout } from '@ember-decorators/component';
import defaultProp from '@freshworks/core/utils/default-decorator';
import { observes } from '@ember-decorators/object';
import { inject } from '@ember/service';
import { gt } from '@ember/object/computed';
import Component from '@ember/component';
import layout from "../templates/components/nucleus-banner";
import { action, computed } from '@ember/object';
 
/**
  __Usage:__
  [Refer component page](/docs/components/nucleus-banner)
 
  @class Nucleus Banner
  @namespace Components
  @extends Ember.Component
  @public
*/
@templateLayout(layout)
class NucleusBanner extends Component {
  /**
  * Service which holds the array of banner items
  *
  * @field nucleusBanner
  * @type function
  * @private
  */
  @inject('nucleus-banner')
  bannerService;
 
  /**
  * Show/hide the number of stacked notifications
  *
  * @field _isShowMore
  * @type boolean
  * @private
  */
  _isShowMore = false;
 
  /**
  * Flag to toggle the position of the banner between `absolute` & `fixed`.
  *
  * @field isFixed
  * @type boolean
  * @public
  */
  @defaultProp
  isFixed = false;
  
  /**
  * Button Label that describes the presence of multiple banners
  *
  * @field showMoreLabel
  * @type string
  * @public
  */
 @defaultProp
 showMoreLabel = 'more';
 
 /**
  * Title text that describes the stack of multiple banners
  *
  * @field stackTitle
  * @type string
  * @public
  */
 @defaultProp
 stackTitle = 'System Alerts';
 
  /**
  * List of banner items to be rendered.
  *
  * @field bannerItems
  * @type array
  * @public
  */
  @computed('bannerService.items')
  get bannerItems() {
    let bannerService = this.get('bannerService');
    return bannerService ? bannerService.get('items') : null;
  }
 
  /**
  * _isMultiple
  *
  * @field _isMultiple
  * @type function
  * @private
  */
  @gt('bannerItems.length', 1)
  _isMultiple;
 
  /**
  * Banner item that gets displayed.
  *
  * @field displayedItem
  * @type object
  * @public
  */
  @computed('bannerItems.[]')
  get displayedItem() {
    let items = this.get('bannerItems');
    return items && items.length > 0 ? items[0] : null;
  }
 
  /**
  * Stacked banner items.
  *
  * @computed stackedItems
  * @private
  */
  @computed('_isMultiple', 'bannerItems.[]')
  get stackedItems() {
    return this.get('_isMultiple') ? this.get('bannerItems').slice(1) : null;
  }
 
  /**
  * Toggle action to show or hide the stacked notifications.
  *
  * @method toggleShowMore
  * @public
  *
  */
  @action
  toggleShowMore() {
    this.toggleProperty('_isShowMore');
  }
 
  /**
  * Action that gets invoked on clicking the close button.
  *
  * @method deleteItem
  * @public
  * @param {object} bannerItem
  */
  @action
  deleteItem(item) {
    this.get('bannerService').remove(item);
  }
 
  /**
  * _observeOpen
  *
  * @field _observeOpen
  * @type function
  * @private
  */
  @observes('bannerItems.[]') // eslint-disable-line
  _observeOpen() {
    if (this.get('bannerItems').length > 0) {
      this._injectBodyClass();
    } else {
      this._removeBodyClass();
    }
  }
 
  /**
  * _injectBodyClass
  *
  * @method _injectBodyClass
  * @private
  *
  */
  _injectBodyClass() {
    document.body.classList.add("nucleus-banner--active");
  }
 
  /**
  * _removeBodyClass
  *
  * @method _removeBodyClass
  * @private
  *
  */
  _removeBodyClass() {
    document.body.classList.remove("nucleus-banner--active");
  }
}
 
export default NucleusBanner;