From f25469f9eece8d1a7aaf754269ebd2e7809ad705 Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 1 Apr 2019 16:12:54 +0200 Subject: [PATCH] fix toaster behavior upon setFile --- src/app/files/fileManager.js | 6 ++- src/app/ui/styles/tooltip-styles.js | 32 +++++++++----- src/app/ui/tooltip.js | 67 +++++++++++++++++++++++------ 3 files changed, 79 insertions(+), 26 deletions(-) diff --git a/src/app/files/fileManager.js b/src/app/files/fileManager.js index a12507fa1c..1142eff426 100644 --- a/src/app/files/fileManager.js +++ b/src/app/files/fileManager.js @@ -136,8 +136,9 @@ class FileManager extends ApiFactory { if (this.currentRequest) { let reject = false let savedAsAnotherFile = false + let warnToaster const actions = yo`
- +
` - await toaster(yo`
${this.currentRequest.from} is modyfing to ${path}
`, actions, { time: 4000 }) + warnToaster = await toaster(yo`
${this.currentRequest.from} is modyfing to ${path}
`, actions, { time: 4000 }) if (reject) throw new Error(`set file operation on ${path} aborted by user.`) if (savedAsAnotherFile) return } diff --git a/src/app/ui/styles/tooltip-styles.js b/src/app/ui/styles/tooltip-styles.js index 36af565a8c..fc669e4d07 100644 --- a/src/app/ui/styles/tooltip-styles.js +++ b/src/app/ui/styles/tooltip-styles.js @@ -9,30 +9,42 @@ var css = csjs` position: fixed; color: var(--primary) min-height: 50px; - min-width: 290px; padding: 16px 24px 12px; border-radius: 3px; bottom: -300; left: 40%; font-size: 12px; text-align: center; - -webkit-animation-name: animatebottom; - -webkit-animation-duration: 6s; - animation-name: animatebottom; - animation-duration: 6s + bottom: 0; } @-webkit-keyframes animatebottom { 0% {bottom: -300px} - 20% {bottom: 0} - 50% {bottom: 0} - 100% {bottom: -300px} + 100% {bottom: 0} } @keyframes animatebottom { 0% {bottom: -300px} - 20% {bottom: 0} - 50% {bottom: 0} + 100% {bottom: 0} + } + @-webkit-keyframes animatetop { + 0% {bottom: 0} + 100% {bottom: -300px} + } + @keyframes animatetop { + 0% {bottom: 0} 100% {bottom: -300px} } + .animateTop { + -webkit-animation-name: animatetop; + -webkit-animation-duration: 2s; + animation-name: animatetop; + animation-duration: 2s; + } + .animateBottom { + -webkit-animation-name: animatebottom; + -webkit-animation-duration: 2s; + animation-name: animatebottom; + animation-duration: 2s; + } ` module.exports = css diff --git a/src/app/ui/tooltip.js b/src/app/ui/tooltip.js index 3634650023..bf8b3f1a87 100644 --- a/src/app/ui/tooltip.js +++ b/src/app/ui/tooltip.js @@ -7,24 +7,63 @@ var css = require('./styles/tooltip-styles') * @param {HTMLElement} [action] An HTMLElement to display for action */ module.exports = function addTooltip (tooltipText, action, opts) { - opts = defaultOptions(opts) - var tooltip = yo` -
- ${tooltipText} - ${action} -
` - return new Promise((resolve, reject) => { - document.body.appendChild(tooltip) - setTimeout(function () { - document.body.removeChild(tooltip) - resolve() - }, opts.time) - }) + let t = new Toaster() + t.render(tooltipText, action, opts) + return t +} + +class Toaster { + hide () { + if (this.id) clearTimeout(this.id) + setTimeout(() => { + // remove from body after the animation is finished + if (this.tooltip.parentElement) this.tooltip.parentElement.removeChild(this.tooltip) + }, 2000) + animation(this.tooltip, css.animateTop.className) + } + render (tooltipText, action, opts) { + opts = defaultOptions(opts) + + return new Promise((resolve, reject) => { + this.tooltip = yo` +
{ over() }} onmouseleave=${() => { out() }}> + ${tooltipText} + ${action} +
` + let timeOut = () => { + return setTimeout(() => { + if (this.id) { + this.hide() + resolve() + } + }, opts.time) + } + let over = () => { + if (this.id) { + clearTimeout(this.id) + this.id = null + } + } + let out = () => { + if (!this.id) this.id = timeOut() + } + this.id = timeOut() + document.body.appendChild(this.tooltip) + animation(this.tooltip, css.animateBottom.className) + }) + } } let defaultOptions = (opts) => { opts = opts || {} return { - time: opts.time || 70000 + time: opts.time || 7000 } } + +let animation = (tooltip, anim) => { + tooltip.classList.remove(css.animateTop.className) + tooltip.classList.remove(css.animateBottom.className) + void tooltip.offsetWidth // trick for restarting the animation + tooltip.classList.add(anim) +}