From 3e7df3c50be3c983070e5b1ac1783084fc2082f5 Mon Sep 17 00:00:00 2001 From: feildmaster Date: Sat, 3 Mar 2018 23:00:19 -0600 Subject: [PATCH 01/29] Classes are joined with spaces. I'm a fool. --- simpletoast.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/simpletoast.js b/simpletoast.js index dbac3a3..2c1e40a 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -130,7 +130,7 @@ const el = document.createElement('div'); if (className) { const clazz = className.toast || className; - el.className = Array.isArray(clazz) ? clazz.join(',') : (typeof clazz === 'string' ? clazz : undefined); + el.className = Array.isArray(clazz) ? clazz.join(' ') : (typeof clazz === 'string' ? clazz : undefined); } applyCSS(el, style.toast); applyCSS(el, css.toast || css); @@ -165,7 +165,7 @@ const elb = document.createElement('button'); if (button.className || className && className.button) { const clazz = button.className || className.button; - elb.className = Array.isArray(clazz) ? clazz.join(',') : clazz; + elb.className = Array.isArray(clazz) ? clazz.join(' ') : clazz; } elb.innerHTML = button.text; applyCSS(elb, style.button); From ace54c7dfaee64b83c58f97c57d5722191064f65 Mon Sep 17 00:00:00 2001 From: feildmaster Date: Thu, 8 Mar 2018 19:25:36 -0600 Subject: [PATCH 02/29] Add function to check toast existence --- simpletoast.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/simpletoast.js b/simpletoast.js index 2c1e40a..86ed6b9 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -3,7 +3,7 @@ */ (() => { if (window !== window.top) return; - const version = buildVersion(1, 4, 1); + const version = buildVersion(1, 5); if (window.SimpleToast) { if (SimpleToast.version) { if (SimpleToast.version >= version.number) return; @@ -147,6 +147,7 @@ body.textContent = text; el.appendChild(body); const toast = { + exists: () => toasts.has(id), close: () => { root.removeChild(el); toasts.delete(id); From 64f642c4612a4f96c72a71a8574f57be16fb9724 Mon Sep 17 00:00:00 2001 From: feildmaster Date: Mon, 27 Aug 2018 18:07:48 -0500 Subject: [PATCH 03/29] fix spacing --- simpletoast.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simpletoast.js b/simpletoast.js index 86ed6b9..099ae81 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -208,7 +208,7 @@ function buildVersion(major, minor = 0, patch = 0) { return { string: `${major}.${minor}${patch ? `.${patch}` : ''}`, - number: major * 1000000000 + minor * 1000 + patch, + number: major * 1000000000 + minor * 1000 + patch, }; } })(); From c9ab7ae2548644ec55a86bad12b6e1ed47057792 Mon Sep 17 00:00:00 2001 From: feildmaster Date: Mon, 27 Aug 2018 18:14:22 -0500 Subject: [PATCH 04/29] SimpleToast.count() to retrieve amount of toasts --- simpletoast.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/simpletoast.js b/simpletoast.js index 099ae81..44dd30d 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -3,7 +3,7 @@ */ (() => { if (window !== window.top) return; - const version = buildVersion(1, 5); + const version = buildVersion(1, 6); if (window.SimpleToast) { if (SimpleToast.version) { if (SimpleToast.version >= version.number) return; @@ -204,6 +204,7 @@ Toast.version = version.number; Toast.versionString = version.string; + Toast.count = () => toasts.size; window.SimpleToast = Toast; function buildVersion(major, minor = 0, patch = 0) { return { From 5059ba4e7e5f62c09ceeaf77a57dd4d7faa94588 Mon Sep 17 00:00:00 2001 From: feildmaster Date: Mon, 27 Aug 2018 20:06:44 -0500 Subject: [PATCH 05/29] Return a blank "toast" --- simpletoast.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/simpletoast.js b/simpletoast.js index 44dd30d..d604e2f 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -3,7 +3,7 @@ */ (() => { if (window !== window.top) return; - const version = buildVersion(1, 6); + const version = buildVersion(1, 6, 1); if (window.SimpleToast) { if (SimpleToast.version) { if (SimpleToast.version >= version.number) return; @@ -121,11 +121,15 @@ }, 1000); } + const blankToast = { + exists: () => false, + close: () => {}, + }; function Toast({title, text, className, css = {}, buttons, timeout}) { if (typeof arguments[0] === 'string') { text = arguments[0]; } - if (!text) return; + if (!text) return blankToast; const id = count++; const el = document.createElement('div'); if (className) { From 73ef0f517114c9cbcd64e7cd7c672602dd2cde77 Mon Sep 17 00:00:00 2001 From: feildmaster Date: Mon, 27 Aug 2018 20:49:05 -0500 Subject: [PATCH 06/29] Allow changing the body text of toasts --- simpletoast.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/simpletoast.js b/simpletoast.js index d604e2f..9efb68c 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -151,6 +151,10 @@ body.textContent = text; el.appendChild(body); const toast = { + setText: (newText) => { + if (!newText || !toast.exists()) return; + body.textContent = newText; + }, exists: () => toasts.has(id), close: () => { root.removeChild(el); From 0698da55cb54a08351f8d23de3a4d7a3040dec85 Mon Sep 17 00:00:00 2001 From: feildmaster Date: Thu, 18 Oct 2018 06:50:31 -0500 Subject: [PATCH 07/29] Closing toasts can now trigger a function Pass "onClose" to be called when a toast closes --- simpletoast.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/simpletoast.js b/simpletoast.js index 9efb68c..944ce47 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -3,7 +3,7 @@ */ (() => { if (window !== window.top) return; - const version = buildVersion(1, 6, 1); + const version = buildVersion(1, 7, 0); if (window.SimpleToast) { if (SimpleToast.version) { if (SimpleToast.version >= version.number) return; @@ -125,7 +125,7 @@ exists: () => false, close: () => {}, }; - function Toast({title, text, className, css = {}, buttons, timeout}) { + function Toast({title, text, className, css = {}, buttons, timeout, onClose}) { if (typeof arguments[0] === 'string') { text = arguments[0]; } @@ -157,8 +157,12 @@ }, exists: () => toasts.has(id), close: () => { + if (!toast.exists()) return; root.removeChild(el); toasts.delete(id); + if (typeof onClose === 'function') { + onClose(toast); + } }, }; if (timeout) { From 4cdc514c3a727561f52dfe2b7e9ec3e8200d5d8f Mon Sep 17 00:00:00 2001 From: feildmaster Date: Thu, 18 Oct 2018 06:57:38 -0500 Subject: [PATCH 08/29] Fix missing "setText" on blank toasts --- simpletoast.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/simpletoast.js b/simpletoast.js index 944ce47..d938611 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -3,7 +3,7 @@ */ (() => { if (window !== window.top) return; - const version = buildVersion(1, 7, 0); + const version = buildVersion(1, 7, 1); if (window.SimpleToast) { if (SimpleToast.version) { if (SimpleToast.version >= version.number) return; @@ -121,9 +121,11 @@ }, 1000); } + function noop() {} const blankToast = { + setText: noop, exists: () => false, - close: () => {}, + close: noop, }; function Toast({title, text, className, css = {}, buttons, timeout, onClose}) { if (typeof arguments[0] === 'string') { From 181ef696a1efa29cd80a42002317ac1310ead407 Mon Sep 17 00:00:00 2001 From: feildmaster Date: Thu, 18 Oct 2018 07:08:28 -0500 Subject: [PATCH 09/29] Stop exposing (potentially) unsafe internals --- simpletoast.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/simpletoast.js b/simpletoast.js index d938611..515a862 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -3,7 +3,7 @@ */ (() => { if (window !== window.top) return; - const version = buildVersion(1, 7, 1); + const version = buildVersion(1, 7, 2); if (window.SimpleToast) { if (SimpleToast.version) { if (SimpleToast.version >= version.number) return; @@ -131,7 +131,8 @@ if (typeof arguments[0] === 'string') { text = arguments[0]; } - if (!text) return blankToast; + const safeToast = Object.assign({}, blankToast); + if (!text) return safeToast; const id = count++; const el = document.createElement('div'); if (className) { @@ -213,7 +214,8 @@ if (timeout) { startTimeout(); } - return toast; + Object.keys(safeToast).forEach((key) => safeToast[key] = toast[key]); + return safeToast; } Toast.version = version.number; From df855cc46353cdd03f7d0be232314e2a0ff8b9e1 Mon Sep 17 00:00:00 2001 From: feildmaster Date: Thu, 18 Oct 2018 08:07:59 -0500 Subject: [PATCH 10/29] Fix new lines not rendering --- simpletoast.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/simpletoast.js b/simpletoast.js index 515a862..18ee8a4 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -3,7 +3,7 @@ */ (() => { if (window !== window.top) return; - const version = buildVersion(1, 7, 2); + const version = buildVersion(1, 7, 3); if (window.SimpleToast) { if (SimpleToast.version) { if (SimpleToast.version >= version.number) return; @@ -18,6 +18,7 @@ 'flex-direction': 'column-reverse', 'align-items': 'flex-end', position: 'fixed', + 'white-space': 'pre-wrap', bottom: 0, right: 0, zIndex: 1000, From db017d8ddb1bb6b10ebf84f0f3a48423d1bb218d Mon Sep 17 00:00:00 2001 From: feildmaster Date: Thu, 18 Oct 2018 08:48:26 -0500 Subject: [PATCH 11/29] Support empty constructors --- simpletoast.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/simpletoast.js b/simpletoast.js index 18ee8a4..58ec57f 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -3,7 +3,7 @@ */ (() => { if (window !== window.top) return; - const version = buildVersion(1, 7, 3); + const version = buildVersion(1, 8, 0); if (window.SimpleToast) { if (SimpleToast.version) { if (SimpleToast.version >= version.number) return; @@ -128,7 +128,7 @@ exists: () => false, close: noop, }; - function Toast({title, text, className, css = {}, buttons, timeout, onClose}) { + function Toast({title, text, className, css = {}, buttons, timeout, onClose} = {}) { if (typeof arguments[0] === 'string') { text = arguments[0]; } From eb93af90468a4eb2e902de800e641343d5a1f481 Mon Sep 17 00:00:00 2001 From: feildmaster Date: Thu, 18 Oct 2018 09:30:10 -0500 Subject: [PATCH 12/29] Added "closeType" to callback Why did I not use a wrapper object? I hate this style of function --- simpletoast.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/simpletoast.js b/simpletoast.js index 58ec57f..dd35712 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -3,7 +3,7 @@ */ (() => { if (window !== window.top) return; - const version = buildVersion(1, 8, 0); + const version = buildVersion(1, 9, 0); if (window.SimpleToast) { if (SimpleToast.version) { if (SimpleToast.version >= version.number) return; @@ -114,7 +114,7 @@ pending += 1; return; } - toast.close(); + toast.timedout(); }); if (pending) { startTimeout(); @@ -154,6 +154,7 @@ const body = document.createElement('span'); body.textContent = text; el.appendChild(body); + let closeType = 'unknown'; const toast = { setText: (newText) => { if (!newText || !toast.exists()) return; @@ -165,9 +166,17 @@ root.removeChild(el); toasts.delete(id); if (typeof onClose === 'function') { - onClose(toast); + onClose(toast, closeType); } }, + timedout: () => { + closeType = 'timeout'; + toast.close(); + }, + closed: () => { + closeType = 'dismissed'; + toast.close(); + }, }; if (timeout) { toast.timeout = Date.now() + timeout; @@ -208,7 +217,7 @@ el.appendChild(elb); }); } - el.addEventListener('click', toast.close); + el.addEventListener('click', toast.closed); root.appendChild(el); toasts.set(id, toast); From 41078a570be5592492a080fd6641eb0dfbf33cbb Mon Sep 17 00:00:00 2001 From: feildmaster Date: Fri, 19 Oct 2018 06:19:17 -0500 Subject: [PATCH 13/29] Add footer support How can you walk without feet? --- simpletoast.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/simpletoast.js b/simpletoast.js index dd35712..3e16edd 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -3,7 +3,7 @@ */ (() => { if (window !== window.top) return; - const version = buildVersion(1, 9, 0); + const version = buildVersion(1, 10, 0); if (window.SimpleToast) { if (SimpleToast.version) { if (SimpleToast.version >= version.number) return; @@ -40,6 +40,10 @@ textShadow: '#3498db 1px 2px 1px', background: '#2980b9', }, + footer: { + display: 'block', + fontSize: '10px', + }, button: { height: '20px', margin: '-3px 0 0 3px', @@ -128,7 +132,7 @@ exists: () => false, close: noop, }; - function Toast({title, text, className, css = {}, buttons, timeout, onClose} = {}) { + function Toast({title, text, footer, className, css = {}, buttons, timeout, onClose} = {}) { if (typeof arguments[0] === 'string') { text = arguments[0]; } @@ -154,6 +158,13 @@ const body = document.createElement('span'); body.textContent = text; el.appendChild(body); + if (footer) { + const fel = el.appendChild(document.createElement('span')); + applyCSS(fel, style.footer); + applyCSS(fel, css.footer); + fel.textContent = footer; + } + let closeType = 'unknown'; const toast = { setText: (newText) => { From 92e8651facbb255db24517b84715bacf5e2933bb Mon Sep 17 00:00:00 2001 From: feildmaster Date: Fri, 19 Oct 2018 06:22:55 -0500 Subject: [PATCH 14/29] Singletons are great memory savers --- simpletoast.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/simpletoast.js b/simpletoast.js index 3e16edd..ddd3c17 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -3,7 +3,7 @@ */ (() => { if (window !== window.top) return; - const version = buildVersion(1, 10, 0); + const version = buildVersion(1, 10, 1); if (window.SimpleToast) { if (SimpleToast.version) { if (SimpleToast.version >= version.number) return; @@ -127,17 +127,16 @@ } function noop() {} - const blankToast = { + const blankToast = Object.freeze({ setText: noop, exists: () => false, close: noop, - }; + }); function Toast({title, text, footer, className, css = {}, buttons, timeout, onClose} = {}) { if (typeof arguments[0] === 'string') { text = arguments[0]; } - const safeToast = Object.assign({}, blankToast); - if (!text) return safeToast; + if (!text) return blankToast; const id = count++; const el = document.createElement('div'); if (className) { @@ -235,7 +234,8 @@ if (timeout) { startTimeout(); } - Object.keys(safeToast).forEach((key) => safeToast[key] = toast[key]); + const safeToast = {}; + Object.keys(blankToast).forEach((key) => safeToast[key] = toast[key]); return safeToast; } From 8bf81935257ffba49b0372363fa3c87c6d7949cd Mon Sep 17 00:00:00 2001 From: feildmaster Date: Fri, 19 Oct 2018 07:51:47 -0500 Subject: [PATCH 15/29] Fix buttons being under footers --- simpletoast.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/simpletoast.js b/simpletoast.js index ddd3c17..4c0924a 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -3,7 +3,7 @@ */ (() => { if (window !== window.top) return; - const version = buildVersion(1, 10, 1); + const version = buildVersion(1, 10, 2); if (window.SimpleToast) { if (SimpleToast.version) { if (SimpleToast.version >= version.number) return; @@ -139,6 +139,9 @@ if (!text) return blankToast; const id = count++; const el = document.createElement('div'); + const tel = el.appendChild(document.createElement('span')); + const body = el.appendChild(document.createElement('span')); + const fel = el.appendChild(document.createElement('span')); if (className) { const clazz = className.toast || className; el.className = Array.isArray(clazz) ? clazz.join(' ') : (typeof clazz === 'string' ? clazz : undefined); @@ -148,17 +151,12 @@ // Add title, body if (title) { - const tel = document.createElement('span'); applyCSS(tel, style.title); applyCSS(tel, css.title); tel.textContent = title; - el.appendChild(tel); } - const body = document.createElement('span'); body.textContent = text; - el.appendChild(body); if (footer) { - const fel = el.appendChild(document.createElement('span')); applyCSS(fel, style.footer); applyCSS(fel, css.footer); fel.textContent = footer; @@ -224,7 +222,7 @@ applyCSS(elb, prev); prev = {}; }; - el.appendChild(elb); + el.insertBefore(elb, fel); }); } el.addEventListener('click', toast.closed); From d921026b8ab568a383ebd617d8b29c052ebeb9d0 Mon Sep 17 00:00:00 2001 From: feildmaster Date: Wed, 31 Oct 2018 03:55:44 -0500 Subject: [PATCH 16/29] Bind to sandboxes, where possible This allows multiple instances of SimpleToast to run in different environments, with each environment using the version intended! This has some downsides too, such as some environments not getting bug fixes they would have gotten from using the "best" version available --- simpletoast.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/simpletoast.js b/simpletoast.js index 4c0924a..0510dc3 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -1,17 +1,20 @@ /* * SimpleToast - A small library for toasts */ -(() => { +((root, factory) => { + // Do we care about frames? Until I get some tests in... no if (window !== window.top) return; - const version = buildVersion(1, 10, 2); - if (window.SimpleToast) { - if (SimpleToast.version) { - if (SimpleToast.version >= version.number) return; - } - console.log(`SimpleToast(v${version.string}): Overriding SimpleToast(v${SimpleToast.versionString || '[unknown]'})`); - } else { - console.log(`SimpleToast(v${version.string}): Loading`); + const boundToast = window.SimpleToast; + const localToast = factory(); + root.SimpleToast = localToast; + console.log(`SimpleToast(v${localToast.versionString}): Loaded`); + // Apply to window if SimpleToast doesn't currently exist + if (root !== window && !(boundToast instanceof localToast)) { + window.SimpleToast = localToast; + console.log(`SimpleToast(v${localToast.versionString}): Publicized`); } +})(this, () => { + const version = buildVersion(1, 11, 0); const style = { root: { display: 'flex', @@ -240,11 +243,11 @@ Toast.version = version.number; Toast.versionString = version.string; Toast.count = () => toasts.size; - window.SimpleToast = Toast; function buildVersion(major, minor = 0, patch = 0) { return { string: `${major}.${minor}${patch ? `.${patch}` : ''}`, number: major * 1000000000 + minor * 1000 + patch, }; } -})(); + return Object.freeze(Toast); +}); From b64e67f94ff84db701133ab4061bd3a182dd90b1 Mon Sep 17 00:00:00 2001 From: feildmaster Date: Wed, 14 Nov 2018 14:24:03 -0600 Subject: [PATCH 17/29] feat: Use innerHTML instead of text --- simpletoast.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/simpletoast.js b/simpletoast.js index 0510dc3..b7dd815 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -14,7 +14,7 @@ console.log(`SimpleToast(v${localToast.versionString}): Publicized`); } })(this, () => { - const version = buildVersion(1, 11, 0); + const version = buildVersion(1, 12, 0); const style = { root: { display: 'flex', @@ -156,20 +156,20 @@ if (title) { applyCSS(tel, style.title); applyCSS(tel, css.title); - tel.textContent = title; + tel.innerHTML = title; } - body.textContent = text; + body.innerHTML = text; if (footer) { applyCSS(fel, style.footer); applyCSS(fel, css.footer); - fel.textContent = footer; + fel.innerHTML = footer; } let closeType = 'unknown'; const toast = { setText: (newText) => { if (!newText || !toast.exists()) return; - body.textContent = newText; + body.innerHTML = newText; }, exists: () => toasts.has(id), close: () => { From 7c2004b91125b683e88b7b1a48e309de27755bea Mon Sep 17 00:00:00 2001 From: feildmaster Date: Sat, 15 Dec 2018 04:52:20 -0600 Subject: [PATCH 18/29] fix: Don't expose internal toast Also use "call" instead of simply running the function (this is breaking but no one uses my script anyway but me, yeah? :P) --- simpletoast.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/simpletoast.js b/simpletoast.js index b7dd815..57e54aa 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -14,7 +14,7 @@ console.log(`SimpleToast(v${localToast.versionString}): Publicized`); } })(this, () => { - const version = buildVersion(1, 12, 0); + const version = buildVersion(1, 13, 0); const style = { root: { display: 'flex', @@ -166,6 +166,7 @@ } let closeType = 'unknown'; + const safeToast = {}; const toast = { setText: (newText) => { if (!newText || !toast.exists()) return; @@ -177,7 +178,7 @@ root.removeChild(el); toasts.delete(id); if (typeof onClose === 'function') { - onClose(toast, closeType); + onClose.call(safeToast, closeType, safeToast); } }, timedout: () => { @@ -235,7 +236,6 @@ if (timeout) { startTimeout(); } - const safeToast = {}; Object.keys(blankToast).forEach((key) => safeToast[key] = toast[key]); return safeToast; } From f2fbaae1cf2c23cab9dfc58f529ffaeee9041ce4 Mon Sep 17 00:00:00 2001 From: feildmaster Date: Sat, 15 Dec 2018 05:00:16 -0600 Subject: [PATCH 19/29] feat: Specify close reason Bump to 2.0 because the last thing was breaking --- simpletoast.js | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/simpletoast.js b/simpletoast.js index 57e54aa..b047bf9 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -14,7 +14,7 @@ console.log(`SimpleToast(v${localToast.versionString}): Publicized`); } })(this, () => { - const version = buildVersion(1, 13, 0); + const version = buildVersion(2, 0, 0); const style = { root: { display: 'flex', @@ -121,7 +121,7 @@ pending += 1; return; } - toast.timedout(); + toast.close('timeout'); }); if (pending) { startTimeout(); @@ -164,8 +164,7 @@ applyCSS(fel, css.footer); fel.innerHTML = footer; } - - let closeType = 'unknown'; + const safeToast = {}; const toast = { setText: (newText) => { @@ -173,22 +172,14 @@ body.innerHTML = newText; }, exists: () => toasts.has(id), - close: () => { + close: (closeType) => { if (!toast.exists()) return; root.removeChild(el); toasts.delete(id); if (typeof onClose === 'function') { - onClose.call(safeToast, closeType, safeToast); + onClose.call(safeToast, closeType || 'unknown', safeToast); } }, - timedout: () => { - closeType = 'timeout'; - toast.close(); - }, - closed: () => { - closeType = 'dismissed'; - toast.close(); - }, }; if (timeout) { toast.timeout = Date.now() + timeout; @@ -229,7 +220,7 @@ el.insertBefore(elb, fel); }); } - el.addEventListener('click', toast.closed); + el.addEventListener('click', toast.close.bind(null, 'dismissed')); root.appendChild(el); toasts.set(id, toast); From 1025a12264ccb3477bce59f00c0af5f8fbc8f570 Mon Sep 17 00:00:00 2001 From: Alan H <879790+feildmaster@users.noreply.github.com> Date: Wed, 28 Dec 2022 18:40:51 -0600 Subject: [PATCH 20/29] fix: window binding being greedy --- simpletoast.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/simpletoast.js b/simpletoast.js index b047bf9..4dff326 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -8,13 +8,13 @@ const localToast = factory(); root.SimpleToast = localToast; console.log(`SimpleToast(v${localToast.versionString}): Loaded`); - // Apply to window if SimpleToast doesn't currently exist - if (root !== window && !(boundToast instanceof localToast)) { + // Apply to window if SimpleToast doesn't exist or is outdated + if (root !== window && (!boundToast?.version || boundToast.version < localToast.version)) { window.SimpleToast = localToast; console.log(`SimpleToast(v${localToast.versionString}): Publicized`); } })(this, () => { - const version = buildVersion(2, 0, 0); + const version = buildVersion(2, 0, 1); const style = { root: { display: 'flex', From dc31d59aaab597c95825fea45003157f6befdf04 Mon Sep 17 00:00:00 2001 From: Alan H <879790+feildmaster@users.noreply.github.com> Date: Wed, 28 Dec 2022 18:55:15 -0600 Subject: [PATCH 21/29] chore: package.json --- package.json | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 package.json diff --git a/package.json b/package.json new file mode 100644 index 0000000..e68aa4a --- /dev/null +++ b/package.json @@ -0,0 +1,6 @@ +{ + "name": "simpletoast", + "main": "simpletoast.js", + "browser": "simpletoast.js", + "private": true, +} From c1804d084acebe002117b64972e17dd4d766548c Mon Sep 17 00:00:00 2001 From: Alan H <879790+feildmaster@users.noreply.github.com> Date: Mon, 9 Jan 2023 00:53:17 -0600 Subject: [PATCH 22/29] fix: package.json I hate json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e68aa4a..dfb9736 100644 --- a/package.json +++ b/package.json @@ -2,5 +2,5 @@ "name": "simpletoast", "main": "simpletoast.js", "browser": "simpletoast.js", - "private": true, + "private": true } From bbcbb7fa7464db517b17d3b36221c21d75000cb4 Mon Sep 17 00:00:00 2001 From: Alan H <879790+feildmaster@users.noreply.github.com> Date: Mon, 17 Mar 2025 15:06:19 -0500 Subject: [PATCH 23/29] Update readme.md --- readme.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/readme.md b/readme.md index bb37b10..fe1ab23 100644 --- a/readme.md +++ b/readme.md @@ -80,3 +80,30 @@ You can provide a single button within an object, or an array of buttons. ], }); ``` + +### All Options +```javascript +new SimpleToast({ + title: '', + text: '', + buttons: [...button] || { + text: '', + className: '', + css: {}, + onclick() {}, + }, + footer: '', + className: '', + css: { + toast: {}, + title: {}, + button: {}, + }, + timeout: 0, + onClose(reason, toast) {}, +}); + +SimpleToast.version; // Version in number form +SimpleToast.versionString; // Readable string of version +SimpleToast.count(); // Numer of toasts open +``` From 75c0e2fe44352585320edf9162416fb99b1ff2c9 Mon Sep 17 00:00:00 2001 From: Alan H <879790+feildmaster@users.noreply.github.com> Date: Mon, 17 Mar 2025 15:07:55 -0500 Subject: [PATCH 24/29] Update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index fe1ab23..d2de33f 100644 --- a/readme.md +++ b/readme.md @@ -99,7 +99,7 @@ new SimpleToast({ title: {}, button: {}, }, - timeout: 0, + timeout: 0, // Close toast after # milliseconds onClose(reason, toast) {}, }); From 4139f3d03c9bfc43d3fb4ef6527b3397c1a7fc54 Mon Sep 17 00:00:00 2001 From: Alan H <879790+feildmaster@users.noreply.github.com> Date: Mon, 17 Mar 2025 15:19:39 -0500 Subject: [PATCH 25/29] Update readme.md --- readme.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index d2de33f..b0b9696 100644 --- a/readme.md +++ b/readme.md @@ -83,14 +83,16 @@ You can provide a single button within an object, or an array of buttons. ### All Options ```javascript -new SimpleToast({ +const toast = new SimpleToast({ title: '', text: '', buttons: [...button] || { text: '', className: '', css: {}, - onclick() {}, + onclick() { + // this; // toast reference + }, }, footer: '', className: '', @@ -103,6 +105,11 @@ new SimpleToast({ onClose(reason, toast) {}, }); +// Methods on toast +toast.setText(newText); // Change text to newText +toast.exists(); // Does toast still exist? +toast.close(reason); // Close toast for optional reason + SimpleToast.version; // Version in number form SimpleToast.versionString; // Readable string of version SimpleToast.count(); // Numer of toasts open From f21bb8c96ece502cc31038010b6209a63fc64468 Mon Sep 17 00:00:00 2001 From: Alan H <879790+feildmaster@users.noreply.github.com> Date: Mon, 17 Mar 2025 15:24:29 -0500 Subject: [PATCH 26/29] Update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index b0b9696..00a23cc 100644 --- a/readme.md +++ b/readme.md @@ -112,5 +112,5 @@ toast.close(reason); // Close toast for optional reason SimpleToast.version; // Version in number form SimpleToast.versionString; // Readable string of version -SimpleToast.count(); // Numer of toasts open +SimpleToast.count(); // Number of toasts open ``` From fba1fb39b476a387e078329ec3a03dc73817d9b6 Mon Sep 17 00:00:00 2001 From: Alan H <879790+feildmaster@users.noreply.github.com> Date: Mon, 17 Mar 2025 16:27:55 -0500 Subject: [PATCH 27/29] Update readme.md --- readme.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index 00a23cc..f7ba6c2 100644 --- a/readme.md +++ b/readme.md @@ -90,19 +90,24 @@ const toast = new SimpleToast({ text: '', className: '', css: {}, - onclick() { + onclick(event, toast) { // this; // toast reference }, }, footer: '', - className: '', + className: '' || [''] || { + toast: '' || [''], + button: '' || [''], + }, css: { toast: {}, title: {}, button: {}, }, timeout: 0, // Close toast after # milliseconds - onClose(reason, toast) {}, + onClose(reason, toast) { + // this; // toast reference + }, }); // Methods on toast From cbf9900da335523edc3ea9e33450eea122f7ee85 Mon Sep 17 00:00:00 2001 From: Alan H <879790+feildmaster@users.noreply.github.com> Date: Mon, 17 Mar 2025 16:46:39 -0500 Subject: [PATCH 28/29] fix: binding of toast on button click --- simpletoast.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/simpletoast.js b/simpletoast.js index 4dff326..5d29425 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -14,7 +14,7 @@ console.log(`SimpleToast(v${localToast.versionString}): Publicized`); } })(this, () => { - const version = buildVersion(2, 0, 1); + const version = buildVersion(2, 0, 2); const style = { root: { display: 'flex', @@ -76,6 +76,16 @@ return old; } + function getClassName(clazz) { + if (Array.isArray(clazz)) { + return clazz.join(' '); + } + if (typeof clazz === 'string') { + return clazz; + } + return undefined; + } + const toasts = new Map(); let root = (() => { function create() { @@ -146,8 +156,7 @@ const body = el.appendChild(document.createElement('span')); const fel = el.appendChild(document.createElement('span')); if (className) { - const clazz = className.toast || className; - el.className = Array.isArray(clazz) ? clazz.join(' ') : (typeof clazz === 'string' ? clazz : undefined); + el.className = getClassName(className.toast || className); } applyCSS(el, style.toast); applyCSS(el, css.toast || css); @@ -172,12 +181,12 @@ body.innerHTML = newText; }, exists: () => toasts.has(id), - close: (closeType) => { + close: (closeType = 'unknown') => { if (!toast.exists()) return; root.removeChild(el); toasts.delete(id); if (typeof onClose === 'function') { - onClose.call(safeToast, closeType || 'unknown', safeToast); + onClose.call(safeToast, closeType, safeToast); } }, }; @@ -192,24 +201,23 @@ buttons.forEach((button) => { if (!button.text) return; const elb = document.createElement('button'); - if (button.className || className && className.button) { - const clazz = button.className || className.button; - elb.className = Array.isArray(clazz) ? clazz.join(' ') : clazz; + if (button.className || className?.button) { + elb.className = getClassName(button.className || className.button); } elb.innerHTML = button.text; applyCSS(elb, style.button); applyCSS(elb, css.button); applyCSS(elb, button.css); if (typeof button.onclick === 'function') { - elb.onclick = button.onclick; + elb.onclick = (e) => button.onclick.call(safeToast, e, safeToast); } let prev = {}; elb.onmouseover = () => { const hoverStyle = Object.assign( {}, style.button.mouseOver, - css.button && css.button.mouseOver, - button.css && button.css.mouseOver + css.button?.mouseOver, + button.css?.mouseOver ); prev = applyCSS(hoverStyle); }; From 1b1019ad8ad010a41677a0d0792f83cbf315c0d4 Mon Sep 17 00:00:00 2001 From: Alan H <879790+feildmaster@users.noreply.github.com> Date: Mon, 17 Mar 2025 16:53:00 -0500 Subject: [PATCH 29/29] fix: broken button hover style --- simpletoast.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/simpletoast.js b/simpletoast.js index 5d29425..a7bc134 100644 --- a/simpletoast.js +++ b/simpletoast.js @@ -14,7 +14,7 @@ console.log(`SimpleToast(v${localToast.versionString}): Publicized`); } })(this, () => { - const version = buildVersion(2, 0, 2); + const version = buildVersion(2, 0, 3); const style = { root: { display: 'flex', @@ -219,7 +219,7 @@ css.button?.mouseOver, button.css?.mouseOver ); - prev = applyCSS(hoverStyle); + prev = applyCSS(elb, hoverStyle); }; elb.onmouseout = () => { applyCSS(elb, prev);