* Shows or hides modal view. * @param {boolean} visible Wether to make the modal view visible * @param {boolean} [cancelled=false] Wether modal view has been cancelled * @return {ModalView} Fluent interface
(visible = true, cancelled = false)
| 41 | * @return {ModalView} Fluent interface |
| 42 | */ |
| 43 | setVisible (visible = true, cancelled = false) { |
| 44 | if (this._visible !== visible) { |
| 45 | this._visible = visible |
| 46 | |
| 47 | // Make sure modal is rendered |
| 48 | const $element = this.getElement() |
| 49 | |
| 50 | if (visible) { |
| 51 | // Add modal view to dom |
| 52 | document.body.appendChild($element) |
| 53 | // Store the trigger element to move the focus back to it |
| 54 | // when being dismissed |
| 55 | this._$trigger = document.activeElement |
| 56 | } |
| 57 | |
| 58 | // Measure dialog height |
| 59 | const dialogHeight = this._$dialog.getBoundingClientRect().height |
| 60 | |
| 61 | // Set dialog height to transition from |
| 62 | if (visible) { |
| 63 | this._$dialog.style.height = '0px' |
| 64 | } else { |
| 65 | this._$dialog.style.height = `${dialogHeight}px` |
| 66 | |
| 67 | // Setting the outer height explicitly prevents it from scrolling to the |
| 68 | // top when the dialog height is set to zero |
| 69 | this._$outer.style.height = `${dialogHeight}px` |
| 70 | } |
| 71 | |
| 72 | // Trigger browser layout |
| 73 | $element.getBoundingClientRect() |
| 74 | |
| 75 | // Listen to the transition end event; add a short delay to let the |
| 76 | // transition finish without cutting off frames at the end |
| 77 | $element.addEventListener('transitionend', () => { |
| 78 | setTimeout(this.visibilityDidChange.bind(this, visible, cancelled), 100) |
| 79 | }, { once: true }) |
| 80 | |
| 81 | if (visible) { |
| 82 | // Trigger modal transition |
| 83 | document.body.classList.add('modal-visible') |
| 84 | $element.classList.add('modal--visible') |
| 85 | |
| 86 | // Set dialog height to transition to |
| 87 | this._$dialog.style.height = `${dialogHeight}px` |
| 88 | |
| 89 | // Listen to key press events to close the modal when hitting escape |
| 90 | this._keyUpHandler && |
| 91 | document.addEventListener('keyup', this._keyUpHandler) |
| 92 | } else { |
| 93 | // Trigger modal transition |
| 94 | document.body.classList.remove('modal-visible') |
| 95 | $element.classList.remove('modal--visible') |
| 96 | |
| 97 | // Set dialog height to transition to |
| 98 | this._$dialog.style.height = '0px' |
| 99 | |
| 100 | // Remove key press listener |
no test coverage detected