1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-03 23:34:09 +02:00

Make update notification work without jQuery & optimize slide animation

This commit is contained in:
chylex 2018-06-30 14:30:45 +02:00
parent cfedb7d6b1
commit 770619d948

View File

@ -1,17 +1,18 @@
(function($, $TDU){ (function($TDU){
// //
// Function: Creates the update notification element. Removes the old one if already exists. // Function: Creates the update notification element. Removes the old one if already exists.
// //
var displayNotification = function(version, changelog){ var displayNotification = function(version, changelog){
// styles // styles
var css = $("#tweetduck-update-css"); let css = document.getElementById("tweetduck-update-css");
if (!css.length){ if (!css){
css = $(` css = document.createElement("style");
<style id='tweetduck-update-css'> css.id = "tweetduck-update-css";
css.innerText = `
#tweetduck-update { #tweetduck-update {
position: absolute; position: fixed;
bottom: 0; bottom: 0;
width: 200px; width: 200px;
height: 178px; height: 178px;
@ -20,6 +21,11 @@
background-color: rgb(32, 94, 138); background-color: rgb(32, 94, 138);
text-align: center; text-align: center;
text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.5); text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.5);
transition: transform 400ms cubic-bezier(.02, .01, .47, 1);
}
#tweetduck-update.hidden-below {
transform: translateY(178px);
} }
#tweetduck-update .tdu-title { #tweetduck-update .tdu-title {
@ -130,86 +136,102 @@
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace; font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
color: #24292e; color: #24292e;
background-color: rgba(27, 31, 35, 0.05); background-color: rgba(27, 31, 35, 0.05);
} }`;
</style>
`).appendTo(document.head); document.head.appendChild(css);
} }
// changelog // changelog
var log = $("#tweetduck-changelog"); let log = document.getElementById("tweetduck-changelog");
if (!log.length){ if (!log){
var log = $(` log = document.createElement("div");
<div id='tweetduck-changelog'> log.id = "tweetduck-changelog";
<div id='tweetduck-changelog-box'> log.style.display = "none";
<h2>TweetDuck Update ${version}</h2> log.innerHTML = `
${markdown(atob(changelog))} <div id='tweetduck-changelog-box'>
</div> <h2>TweetDuck Update ${version}</h2>
</div> ${markdown(atob(changelog))}
`).appendTo(document.body).css("display", "none"); </div>`;
document.body.appendChild(log);
} }
// notification // notification
var ele = $("#tweetduck-update"); let ele = document.getElementById("tweetduck-update");
var existed = ele.length > 0; let existed = !!ele;
if (existed){ if (existed){
ele.remove(); ele.remove();
} }
ele = $(` ele = document.createElement("div");
<div id='tweetduck-update'> ele.id = "tweetduck-update";
<p class='tdu-title'>T&#8202;weetDuck Update ${version}</p> ele.innerHTML = `
<p class='tdu-info tdu-showlog'>View update information</p> <p class='tdu-title'>T&#8202;weetDuck Update ${version}</p>
<div class='tdu-buttons'> <p class='tdu-info tdu-showlog'>View update information</p>
<button class='tdu-btn-download'>Update now</button> <div class='tdu-buttons'>
<button class='tdu-btn-later'>Remind me later</button> <button class='tdu-btn-download'>Update now</button>
<!-- TODO <button class='tdu-btn-ignore'>Ignore this update</button>--> <button class='tdu-btn-later'>Remind me later</button>
</div> <button class='tdu-btn-ignore'>Ignore this update</button>
</div> </div>`;
`).appendTo(document.body).css("display", existed ? "block" : "none");
// ui logic if (!existed){
var hide = function(){ ele.classList.add("hidden-below");
}
document.body.appendChild(ele);
// ui functions
const exitNow = function(){
ele.remove(); ele.remove();
log.remove(); log.remove();
css.remove(); css.remove();
}; };
var slide = function(){ const exitSlide = function(){
log.hide(); log.style.display = "none";
ele.slideUp(hide);
ele.classList.add("hidden-below");
setTimeout(exitNow, 400);
}; };
ele.children(".tdu-showlog").click(function(){ const onClick = function(element, callback){
log.toggle(); element.addEventListener("click", callback);
};
// ui listeners
onClick(ele.querySelector(".tdu-showlog"), function(){
log.style.display = window.getComputedStyle(log).display === "none" ? "block" : "none";
}); });
log.click(function(){ onClick(log, function(){
log.hide(); log.style.display = "none";
}).children().first().click(function(e){ });
onClick(log.children[0], function(e){
e.stopPropagation(); e.stopPropagation();
}); });
var buttonDiv = ele.children(".tdu-buttons").first(); onClick(ele.querySelector(".tdu-btn-download"), function(){
exitNow();
buttonDiv.children(".tdu-btn-download").click(function(){
hide();
$TDU.onUpdateAccepted(); $TDU.onUpdateAccepted();
}); });
buttonDiv.children(".tdu-btn-later").click(function(){ onClick(ele.querySelector(".tdu-btn-later"), function(){
$TDU.onUpdateDelayed(); $TDU.onUpdateDelayed();
slide(); exitSlide();
});
buttonDiv.children(".tdu-btn-ignore").click(function(){
$TDU.onUpdateDismissed();
slide();
}); });
onClick(ele.querySelector(".tdu-btn-ignore"), function(){
$TDU.onUpdateDismissed();
exitSlide();
});
// finalize notification
if (!existed){ if (!existed){
ele.slideDown(); ele.getBoundingClientRect(); // reflow
ele.classList.remove("hidden-below");
} }
return ele; return ele;
@ -218,7 +240,7 @@
// //
// Function: Ghetto-converts markdown to HTML. // Function: Ghetto-converts markdown to HTML.
// //
var markdown = function(md){ const markdown = function(md){
return md.replace(/&/g, "&amp;") return md.replace(/&/g, "&amp;")
.replace(/</g, "&lt;") .replace(/</g, "&lt;")
.replace(/>/g, "&gt;") .replace(/>/g, "&gt;")
@ -237,12 +259,17 @@
// //
// Block: Check updates on startup. // Block: Check updates on startup.
// //
$(document).one("TD.ready", function(){ if ("$" in window && typeof $._data === "function" && "TD" in $._data(document, "events")){
$(document).one("TD.ready", function(){
$TDU.triggerUpdateCheck();
});
}
else{
$TDU.triggerUpdateCheck(); $TDU.triggerUpdateCheck();
}); }
// //
// Block: Setup global functions. // Block: Setup global functions.
// //
window.TDUF_displayNotification = displayNotification; window.TDUF_displayNotification = displayNotification;
})($, $TDU); })($TDU);