Skip to content

Commit

Permalink
Version specific logic and bump to 4.4
Browse files Browse the repository at this point in the history
  • Loading branch information
aopell committed Dec 12, 2018
1 parent 7f08a11 commit 37a33d2
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 66 deletions.
2 changes: 1 addition & 1 deletion js/home.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @typedef {{id:number,title:string,message:string,shortMessage:string,timestamp?:Date,icon?:string}} Broadcast */
/** @typedef {{id:number,title:string,message:string,timestamp?:Date,icon?:string}} Broadcast */

let homeFeedContainer = document.getElementById("home-feed-container");
let feed = homeFeedContainer.querySelector(".feed .item-list .s-edge-feed");
Expand Down
165 changes: 101 additions & 64 deletions js/version-specific.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,42 +39,91 @@ function compareVersions(a, b) {
return 0;
}

/**
* Shows an iziToast with the given options
* @param {string} title Toast title
* @param {string} message Toast message
* @param {string} color Progress bar color
* @param {{theme:string,layout:number,buttons:[],timeout:number,position:string,options:{}}} options Options object (options object inside options is for additional options). Default values: `{ theme: "dark", layout: 1, timeout: 0, position: "topRight", iconUrl: chrome.runtime.getURL("/imgs/plus-icon.png") }`
*/
function showToast(title, message, color, { theme = "dark", layout = 1, buttons = [], timeout = 0, position = "topRight", options = {}, iconUrl = chrome.runtime.getURL("/imgs/plus-icon.png") } = { theme: "dark", layout: 1, timeout: 0, position: "topRight", iconUrl: chrome.runtime.getURL("/imgs/plus-icon.png") }) {
let toastOptions = {
theme,
iconUrl,
title,
message,
progressBarColor: color,
layout,
buttons,
timeout,
position
};
Object.assign(toastOptions, options);
iziToast.show(toastOptions);
}

/**
* Creates a button for an iziToast. Results of calls to this function should be passed to `showToast` as an array.
* @typedef {[string,(any,any)=>void]} ToastButton
* @param {string} text Text to display on the button
* @param {string} id The element ID of the button
* @param {(instance:any,toast:any,closedBy:any)=>void} onClick Function called when the button is clicked
* @param {"fadeOut"|"fadeOutUp"|"fadeOutDown"|"fadeOutLeft"|"fadeOutRight"|"flipOutX"} [transition="fadeOutRight"] The notification's exit transition
* @returns {ToastButton}
*/
function createToastButton(text, id, onClick, transition = "fadeOutRight") {
return [`<button>${text}</button>`, function (instance, toast) {
instance.hide({
transitionOut: transition,
onClosing: onClick
}, toast, id);
}];
}

/**
* @typedef {{id:number,title:string,message:string,timestamp?:Date,icon?:string}} Broadcast
* @param {Broadcast[]} broadcasts Broadcasts to save
* @param {()=>void} callback Function called after broadcasts are saved
*/
function createBroadcasts(broadcasts, callback = undefined) {
chrome.storage.sync.get(["unreadBroadcasts"], values => {
let b = values.unreadBroadcasts || [];
b.push(...broadcasts);
chrome.storage.sync.set({ unreadBroadcasts: b }, callback);
});
}

/**
* Creates a Broadcast. Result should be passed directly to `createBroadcasts`.
* @param {number} id Broadcast ID number, should be unique
* @param {string} title Short title for the broadcast
* @param {string} message HTML content to be displayed in the home feed
* @param {Date|number} timestamp Timestamp to show as the post time in the home feed
* @returns {Broadcast}
*/
function createBroadcast(id, title, message, timestamp = Date.now()) {
return { id, title, message, timestamp };
}

/*
* Migrations to a given version from any versions before it.
* Should be ordered in increasing version order.
*/
let migrationsTo = {
"4.2": function (currentVersion, previousVersion) {
iziToast.show({
theme: 'dark',
iconUrl: chrome.runtime.getURL("/imgs/plus-icon.png"),
title: "Take the Schoology Plus Fall 2018 Survey!",
message: "Complete for a chance to win an Amazon gift card!",
layout: 1,
position: 'topRight',
timeout: 0,
progressBarColor: 'yellow',
showToast("Take the Schoology Plus Fall 2018 Survey!", "Complete for a chance to win an Amazon gift card!", "yellow", {
buttons: [
['<button>Take the Survey!</button>', function (instance, toast) {
instance.hide({
transitionOut: 'fadeOutRight',
onClosing: function (instance, toast, closedBy) {
window.open("https://goo.gl/forms/EVi8cTaakVhLekiN2", "_blank");
}
}, toast, 'takeSurveyButton');
}]
createToastButton("Take the Survey!", "take-survey-button", (i, t, c) => window.open("https://goo.gl/forms/EVi8cTaakVhLekiN2", "_blank"))
]
});

chrome.storage.sync.get(["unreadBroadcasts", "broadcasts", "themes"], (values) => {
let broadcasts = values.unreadBroadcasts || [];
broadcasts.push({
id: 420,
title: '',
message: '<span style="font-size: 16px; font-weight: bold">Take the Schoology Plus Fall 2018 Survey!</span><br/><br/><span style="font-size: 14px">Let us know your thoughts about Schoology Plus and complete the survey for a chance to win <strong style="background-color: yellow">one of two $5 Amazon gift cards!</strong><br/><br/><strong style="background-color: yellow"><a href="https://goo.gl/forms/EVi8cTaakVhLekiN2" target="_blank">Click Here to Take The Survey!</a></strong></span><br/><br/>',
shortMessage: "Complete for a chance to win an Amazon gift card!",
timestamp: Date.now()
});
broadcasts.push(createBroadcast(
420,
'',
'<span style="font-size: 16px; font-weight: bold">Take the Schoology Plus Fall 2018 Survey!</span><br/><br/><span style="font-size: 14px">Let us know your thoughts about Schoology Plus and complete the survey for a chance to win <strong style="background-color: yellow">one of two $5 Amazon gift cards!</strong><br/><br/><strong style="background-color: yellow"><a href="https://goo.gl/forms/EVi8cTaakVhLekiN2" target="_blank">Click Here to Take The Survey!</a></strong></span><br/><br/>',
));

let oldFormatThemesExist = false;
for (let t of values.themes || []) {
Expand All @@ -99,51 +148,39 @@ let migrationsTo = {
});
});
},
"4.3.1": function (currentVersion, previousVersion) {
iziToast.show({
theme: 'dark',
iconUrl: chrome.runtime.getURL("/imgs/plus-icon.png"),
title: 'Important Message!',
message: 'Schoology Plus will temporarily stop working beginning December 15',
progressBarColor: 'red',
timeout: 0,
layout: 2,
position: 'topRight',
buttons: [
['<button>Click Here For More Info</button>', function (instance, toast) {
instance.hide({
transitionOut: 'fadeOutRight',
onClosing: function (instance, toast, closedBy) {
window.open("https://aopell.me/SchoologyPlus/notice", "_blank");
}
}, toast, 'moreInfoNoticeButton');
}]
]
});

chrome.storage.sync.get(["unreadBroadcasts", "broadcasts", "themes"], (storage) => {
let broadcasts = storage.unreadBroadcasts || [];
broadcasts.push({
id: 432,
title: '',
message: '<div style="border: 3px solid red; background-color: black; color: white;"><span style="font-size: 16px; font-weight: bold">Important Message!</span><br/><br/><span style="font-size: 14px">LAUSD is updating the Schoology interface on December 15, which will cause Schoology Plus to function incorrectly until the next version.</span><br/><br/><a href="https://aopell.me/SchoologyPlus/notice" style="font-size: 16px; color: white; text-decoration: underline;">Click Here For More Info</a></div>',
shortMessage: "Schoology Plus will temporarily stop working beginning December 15",
timestamp: Date.now()
});

chrome.storage.sync.set({ unreadBroadcasts: broadcasts });
});
"4.4": function (currentVersion, previousVersion) {
showToast(
"Important Message!",
"Schoology Plus will temporarily stop working beginning December 15",
"red",
{
layout: 2,
buttons: [
createToastButton(
"Click Here for More Info",
"more-info-notice-button",
(i, t, c) => window.open("https://aopell.me/SchoologyPlus/new-interface-notice", "_blank")
)
]
}
);
createBroadcasts([
createBroadcast(
440,
"Last Chance!",
'The Schoology Plus Fall 2018 survey <strong>will close on December 20</strong>. This is the last chance for you to complete the survey for your chance to win <strong>one of two $5 Amazon gift cards!</strong> Winners will be announced after January 1.<br><br><a style="font-size: 14px; font-weight: bold" href="https://goo.gl/forms/EVi8cTaakVhLekiN2" target="_blank">Click Here to Take The Survey!</a>'
),
createBroadcast(
432,
'',
'<div style="border: 3px solid red; background-color: black; color: white;"><span style="font-size: 16px; font-weight: bold">Important Message!</span><br/><br/><span style="font-size: 14px">LAUSD is updating the Schoology interface on December 15, which will cause Schoology Plus to function incorrectly until the next version.</span><br/><br/><a href="https://aopell.me/SchoologyPlus/new-interface-notice" style="font-size: 16px; color: white; text-decoration: underline;">Click Here For More Info</a></div>'
)
]);
}
};

function versionSpecificFirstLaunch(currentVersion, previousVersion) {
Logger.log("[Updater] Version specific first launch: ", currentVersion, " from ", previousVersion);
/*
switch (currentVersion) {
default:
break;
}
*/

// TODO add special handling if any migrations return a Promise such that we run in order
for (let migrateTo in migrationsTo) {
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"update_url": "https://aopell.me/SchoologyPlus/update.json"
}
},
"version": "4.3.2",
"version": "4.4",
"icons": {
"128": "imgs/[email protected]",
"64": "imgs/[email protected]",
Expand Down

0 comments on commit 37a33d2

Please sign in to comment.