1 line
2.7 KiB
JavaScript
1 line
2.7 KiB
JavaScript
|
|
import layoutManager from"./../layoutmanager.js";import dom from"./../dom.js";import"./../dialoghelper/dialoghelper.js";require(["css!modules/toast/toast.css"]);let fillFromTop=layoutManager.tv,sectionElement=createSectionElement();function createSectionElement(){var section=document.createElement("section");return section.classList.add("toast-group"),fillFromTop&§ion.classList.add("toast-group-top"),document.body.appendChild(section),section}function createToastElement(options,position){var toast=document.createElement("output");let html="";if(options.icon){let iconClass="toast-icon flex-shrink-zero";"center"===position&&(iconClass+=" toast-icon-center"),options.iconStrikeThrough&&(iconClass+=" icon_circle_strike"),html=(html+='<i class="md-icon autortl '+iconClass+'">')+options.icon+"</i>"}return html+='<div class="flex flex-direction-column">',html="center"===position?(html+='<h3 class="toast-primarytext">')+options.text+"</h3>":(html+='<div class="toast-primarytext">')+options.text+"</div>",options.secondaryText&&(html=(html+='<div class="secondaryText toast-secondaryText">')+options.secondaryText+"</div>"),html+="</div>",toast.classList.add("toast"),toast.classList.add("dialog"),dom.allowBackdropFilter()&&toast.classList.add("dialog-blur"),position&&toast.classList.add("toast-"+position),"center"===position&&toast.classList.add("toast-large"),toast.setAttribute("role","status"),toast.innerHTML=html,toast}function animateAndAppend(element,position){if("center"===position)sectionElement.appendChild(element);else{var t=sectionElement.offsetHeight;sectionElement.appendChild(element);let s=sectionElement.offsetHeight-t;"top"===position&&(s*=-1);element=sectionElement.animate([{transform:`translateY(${s}px)`},{transform:"translateY(0)"}],{duration:150,easing:"ease-out"}),t=document.timeline;t&&(element.startTime=t.currentTime)}}let nativeAnimationSupported=document.documentElement.animate;function appendToastElement(element,position){nativeAnimationSupported&§ionElement.children.length&&window.matchMedia("(prefers-reduced-motion: no-preference)").matches?animateAndAppend(element,position):sectionElement.appendChild(element)}function setTimeoutPromise(delay){return new Promise(function(resolve,reject){setTimeout(resolve,delay)})}function getAnimationPromise(elem){return elem.getAnimations?Promise.allSettled(elem.getAnimations().map(o=>o.finished)):setTimeoutPromise(3300)}function sendToast(options){return"string"==typeof options&&(options={text:options}),require(["css!modules/toast/toast.css"]).then(function(){var position=fillFromTop?"top":null;let toast=createToastElement(options,position);return appendToastElement(toast,position),getAnimationPromise(toast).then(function(){sectionElement.removeChild(toast)})})}export default sendToast;
|