All files / addon/components nucleus-modal.js

80.77% Statements 21/26
60% Branches 6/10
88.89% Functions 8/9
80.77% Lines 21/26

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 182 183 184 185 186 187 188 189 190 191                                                                                                14x                               12x                   12x                   12x                     9x                     12x                     2x 2x   2x                       1x 1x           2x 1x   1x                       1x     1x 1x                     3x 3x                   1x       1x            
import { layout as templateLayout } from '@ember-decorators/component';
import { observes } from '@ember-decorators/object';
import defaultProp from '@freshworks/core/utils/default-decorator';
import Component from "@ember/component";
import { set, setProperties, computed, action } from "@ember/object";
import layout from "../templates/components/nucleus-modal";
 
/**
  __Usage:__
  [Refer component page](/docs/components/nucleus-modal)
 
  The component yields references to the following contextual components, that you can use to further customize the output:
  * [modal.body](nucleus-modal/body)
  * [modal.header](nucleus-modal/header)
  * [modal.footer](nucleus-modal/footer)
 
  Furthermore references to the following actions are yielded:
  * `close`: triggers the `onClose` action and closes the modal
  * `submit`: triggers the `onSubmit` action
 
  @class Nucleus Modal
  @namespace Components
  @extends Ember.Component
  @public
*/
@templateLayout(layout)
class Modal extends Component {
  /**
  * Flag to open the modal
  *
  * @field open
  * @type boolean
  * @default false
  * @readonly
  * @public
  */
  @defaultProp
  open = true;
 
  /**
  * isOpen
  *
  * @field isOpen
  * @type function
  * @private
  */
  @computed("open", {
    get() {
      return this.get('open');
    },
    set(key, value) { // eslint-disable-line no-unused-vars
      return value;
    }
  })
  isOpen;
 
  /**
  * Private flag to prevent multiple show/hide calls
  *
  * @field _isOpen
  * @type boolean
  * @default false
  * @private
  */
  _isOpen = false;
 
  /**
  * Flag to show or hide the translucent backdrop.
  *
  * @field backdrop
  * @type boolean
  * @public
  */
  @defaultProp
  backdrop = true;
 
  /**
  * Flag to enable or disable dismiss option. `false` hides the close button and disables close on Escape.
  *
  * @field isDismissible
  * @type boolean
  * @public
  */
  @defaultProp
  isDismissible = true;
 
  /**
  * Modal sizes: `small`, `medium` & `large`
  *
  * @property size
  * @type string|null
  * @default null
  * @public
  */
  @defaultProp
  size = null;
 
  /**
  * Render the Modal markup in-place (true) or in wormhole (false)
  *
  * @field renderInPlace
  * @type boolean
  * @default false
  * @public
  */
  @defaultProp
  renderInPlace = false;
 
  /**
  * close
  *
  * @method close
  * @public
  *
  */
  @action
  close() {
    Eif (this.get('onClose')) {
      this.get('onClose')();
    }
    this._hide();
  }
 
  /**
  * submit
  *
  * @method submit
  * @public
  *
  */
  @action
  submit() {
    Eif (this.get('onSubmit')) {
      return this.get('onSubmit')();
    }
  }
 
  @observes('isOpen') // eslint-disable-line
  _observeOpen() {
    if (this.get('isOpen')) {
      this._initialize();
    } else {
      this._dismantle();
    }
  }
 
  /**
  * show
  *
  * @method show
  * @private
  *
  */
  _show() {
    Iif (this._isOpen) {
      return;
    }
    set(this, "_isOpen", true);
    document.body.classList.add("nucleus-modal--open");
  }
 
  /**
  * hide
  *
  * @method hide
  * @private
  *
  */
  _hide() {
    Eif (!this._isOpen) {
      return;
    }
    setProperties(this, {
      isOpen: false,
      _isOpen: false
    });
    document.body.classList.remove("nucleus-modal--open");
  }
 
  _initialize() {
    this._show();
  }
 
  _dismantle() {
    this._hide();
  }
 
}
 
export default Modal;