/* global define */ ;(function(define){'use strict';define((require,exports,module) => { /** * Dependencies */ var component = require('gaia-component'); /** * Simple logger (toggle 0) * * @type {Function} */ var debug = 0 ? console.log.bind(console) : () => {}; /** * Use the dom-scheduler if it's around, * else fallback to fake shim. * * @type {Object} */ var schedule = (window.scheduler && typeof window.scheduler.mutation === "function" ? window.scheduler : null) || { mutation: block => Promise.resolve(block()), transition: (block, el, event, timeout) => { block(); return after(el, event, timeout || 500); } }; /** * Exports */ module.exports = component.register('gaia-dialog', { created() { this.setupShadowRoot(); this.els = { inner: this.shadowRoot.querySelector('.dialog-inner'), background: this.shadowRoot.querySelector('.background'), window: this.shadowRoot.querySelector('.window') }; this.shadowRoot.addEventListener('click', e => this.onClick(e)); setTimeout(() => this.makeAccessible()); }, makeAccessible() { this.setAttribute('role', 'dialog'); }, onClick(e) { var el = e.target.closest('[on-click]'); if (!el) { return; } debug('onClick'); var method = el.getAttribute('on-click'); if (typeof this[method] == 'function') { this[method](); } }, open(options) { if (this.isOpen) { return; } debug('open dialog'); this.isOpen = true; return this.show() .then(() => this.animateBackgroundIn(options)) .then(() => this.animateWindowIn()) .then(() => this.dispatch('opened')); }, close(options) { if (!this.isOpen) { return; } debug('close dialog'); this.isOpen = false; return this.animateWindowOut() .then(() => this.animateBackgroundOut()) .then(() => this.hide()) .then(() => this.dispatch('closed')); }, animateBackgroundIn(options) { if (options) { return this.animateBackgroundInFrom(options); } var el = this.els.background; return schedule.transition(() => { debug('animate background in'); el.classList.remove('animate-out'); el.classList.add('animate-in'); }, el, 'animationend'); }, animateBackgroundOut() { var el = this.els.background; return schedule.transition(() => { debug('animate background out'); el.classList.add('animate-out'); el.classList.remove('animate-in'); }, el, 'animationend') .then(() => el.style = ''); }, animateBackgroundInFrom(pos) { var el = this.els.background; var scale = Math.sqrt(window.innerWidth * window.innerHeight) / 15; var duration = scale * 9; return schedule.mutation(() => { el.classList.add('circular'); el.classList.remove('animate-out'); el.style.transform = `translate(${pos.clientX}px, ${pos.clientY}px)`; el.style.transitionDuration = duration + 'ms'; el.offsetTop; // Hack, any ideas? }) .then(() => { return schedule.transition(() => { debug('animate background in from', pos); el.style.transform += ` scale(${scale})`; el.style.opacity = 1; }, el, 'transitionend', duration * 1.5); }); }, show() { return schedule.mutation(() => { debug('show'); this.style.display = 'block'; }); }, hide() { return schedule.mutation(() => { debug('hide'); this.style.display = 'none'; }); }, animateWindowIn() { var el = this.els.window; return schedule.transition(() => { debug('animate window in'); el.classList.add('animate-in'); el.classList.remove('animate-out'); }, el, 'animationend'); }, animateWindowOut() { var el = this.els.window; return schedule.transition(() => { debug('animate window out'); el.classList.add('animate-out'); el.classList.remove('animate-in'); }, el, 'animationend'); }, dispatch(name) { this.dispatchEvent(new CustomEvent(name)); }, attrs: { opened: { get: function() { return !!this.isOpen; }, set: function(value) { value = value === '' || value; if (!value) { this.close(); } else { this.open(); } } } }, template: `
`, globalCss: ` @keyframes gaia-dialog-entrance { 0% { transform: translateY(100px); } 100% { transform: translateY(0px); } } @keyframes gaia-dialog-fade-in { 0% { opacity: 0 } 100% { opacity: 1 } } @keyframes gaia-dialog-fade-out { 0% { opacity: 1 } 100% { opacity: 0 } }` }); /** * Utils */ function after(target, event, timeout) { return new Promise(resolve => { var timer = timeout && setTimeout(cb, timeout); target.addEventListener(event, cb); function cb() { target.removeEventListener(event, cb); clearTimeout(timer); resolve(); } }); } });})(typeof define=='function'&&define.amd?define :(function(n,w){'use strict';return typeof module=='object'?function(c){ c(require,exports,module);}:function(c){var m={exports:{}};c(function(n){ return w[n];},m.exports,m);w[n]=m.exports;};})('gaia-dialog',this));