Source: components/sl-modal.js

  1. import Ember from 'ember';
  2. import StreamEnabled from 'ember-stream/mixins/stream-enabled';
  3. import layout from '../templates/components/sl-modal';
  4. /**
  5. * @module
  6. * @augments ember/Component
  7. * @augments ember-stream/mixins/stream-enabled
  8. */
  9. export default Ember.Component.extend( StreamEnabled, {
  10. // -------------------------------------------------------------------------
  11. // Dependencies
  12. // -------------------------------------------------------------------------
  13. // Attributes
  14. /** @type {String[]} */
  15. attributeBindings: [
  16. 'ariaDescribedBy:aria-describedby',
  17. 'ariaHidden:aria-hidden',
  18. 'ariaLabelledBy:aria-labelledby',
  19. 'tabindex'
  20. ],
  21. /** @type {String[]} */
  22. classNames: [
  23. 'modal'
  24. ],
  25. /** @type {String[]} */
  26. classNameBindings: [
  27. 'animated:fade'
  28. ],
  29. /** @type {String} */
  30. tagName: 'div',
  31. /** @type {Object} */
  32. layout: layout,
  33. // -------------------------------------------------------------------------
  34. // Actions
  35. /** @type {Object} */
  36. actions: {
  37. /**
  38. * Trigger hiding the model
  39. *
  40. * @function actions:hide
  41. * @returns {undefined}
  42. */
  43. hide() {
  44. this.hide();
  45. },
  46. /**
  47. * Trigger showing the model
  48. *
  49. * @function actions:show
  50. * @returns {undefined}
  51. */
  52. show() {
  53. this.show();
  54. }
  55. },
  56. // -------------------------------------------------------------------------
  57. // Events
  58. // -------------------------------------------------------------------------
  59. // Properties
  60. /**
  61. * Whether the modal is animated with transition or not
  62. *
  63. * @type {Boolean}
  64. */
  65. animated: true,
  66. /**
  67. * ariaDescribedBy property, the value of this will be set as the value to
  68. * the aria-describedby attribute
  69. *
  70. * @type {?String}
  71. */
  72. ariaDescribedBy: null,
  73. /**
  74. * ariaHidden property, the value of this will be set as the value to the
  75. * aria-hidden attribute
  76. *
  77. * @type {String}
  78. */
  79. ariaHidden: 'true',
  80. /**
  81. * ariaLabelledBy property, the value of this will be set as the value to
  82. * the aria-labelledby attribute
  83. *
  84. * @function
  85. * @returns {String}
  86. */
  87. ariaLabelledBy: null,
  88. /**
  89. * The ariaRole property, the value of this will be set as the value to the
  90. * aria-role attribute
  91. *
  92. * @type {String}
  93. */
  94. ariaRole: 'dialog',
  95. /*
  96. * Whether to show Bootstrap's backdrop
  97. *
  98. * @type {Boolean}
  99. */
  100. backdrop: true,
  101. /*
  102. * Whether to modal is open or not
  103. *
  104. * @type {Boolean}
  105. */
  106. isOpen: false,
  107. /**
  108. * tabindex attribute value
  109. *
  110. * @type {String}
  111. */
  112. tabindex: '-1',
  113. // -------------------------------------------------------------------------
  114. // Observers
  115. /**
  116. * Get ariaLabelledBy target element id
  117. *
  118. * @function
  119. * @returns {undefined}
  120. */
  121. getLabelledby: Ember.on(
  122. 'willInsertElement',
  123. function() {
  124. this.set( 'ariaLabelledBy', this.$( '[id^="modalTitle"]' ).attr( 'id' ) );
  125. }
  126. ),
  127. /**
  128. * Setup stream actions bindings
  129. *
  130. * @function
  131. * @returns {undefined}
  132. */
  133. setupStreamActions: Ember.on(
  134. 'init',
  135. function() {
  136. const stream = this.get( 'stream' );
  137. if ( !stream ) {
  138. return;
  139. }
  140. stream.on( 'hide', () => {
  141. this.hide();
  142. });
  143. stream.on( 'show', () => {
  144. this.show();
  145. });
  146. }
  147. ),
  148. /**
  149. * Set up the component as a Bootstrap Modal and listen for events
  150. *
  151. * @function
  152. * @returns {undefined}
  153. */
  154. setupModal: Ember.on(
  155. 'didInsertElement',
  156. function() {
  157. const modal = this.$().modal({
  158. keyboard: true,
  159. show: false,
  160. backdrop: this.get( 'backdrop' )
  161. });
  162. modal.on( 'show.bs.modal', () => {
  163. this.sendAction( 'beforeShow' );
  164. });
  165. modal.on( 'shown.bs.modal', () => {
  166. this.set( 'isOpen', true );
  167. this.sendAction( 'afterShow' );
  168. });
  169. modal.on( 'hide.bs.modal', () => {
  170. this.sendAction( 'beforeHide' );
  171. });
  172. modal.on( 'hidden.bs.modal', () => {
  173. this.set( 'isOpen', false );
  174. this.sendAction( 'afterHide' );
  175. });
  176. }
  177. ),
  178. /**
  179. * Unbind bootstrap event handlers
  180. *
  181. * @function
  182. * @returns {undefined}
  183. */
  184. unbindHandlers: Ember.on(
  185. 'willClearRender',
  186. function() {
  187. this.$().off( 'show.bs.modal' );
  188. this.$().off( 'shown.bs.modal' );
  189. this.$().off( 'hide.bs.modal' );
  190. this.$().off( 'hidden.bs.modal' );
  191. }
  192. ),
  193. // -------------------------------------------------------------------------
  194. // Methods
  195. /**
  196. * Hide the modal by triggering Bootstrap's modal( hide )
  197. *
  198. * @function
  199. * @returns {undefined}
  200. */
  201. hide() {
  202. this.$().modal( 'hide' );
  203. },
  204. /**
  205. * Show the modal by triggering Bootstrap's modal( show )
  206. *
  207. * @function
  208. * @returns {undefined}
  209. */
  210. show() {
  211. this.$().modal( 'show' );
  212. }
  213. });