diff --git a/CHANGELOG.md b/CHANGELOG.md index 7315edb..280b1d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## 1.0.4 - 2021-09-27 +### Added +- Added `README.md`. +- Added the option to set the closing and opening days via html for debugging purposes. + +### Fixed +- Fixed an issue where you really couldn't see what time it would close on the upcoming closing day when simulating the time. + ## 1.0.3 - 2021-09-26 ### Fixed - Fixed an issue where the `html` tag was not being hidden once the script had loaded. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ba0f6f7 --- /dev/null +++ b/README.md @@ -0,0 +1,271 @@ +# Sunset to Sunset +This script was developed to help with closing down of a website from sunset on Friday to sunset on Saturday. It displays a banner leading up to the Sabbath notifying visitors of the site when it will not be available. + +## Features +- Automatically show a banner on the top of your site notifying visitors when your site will close. +- Show a message during the Sabbath hours letting visitors know why you are closed and when your site will be back online. + +## Dependencies +The Sunset to Sunset script is dependent on a date time library called [Luxon](https://moment.github.io/luxon/#/) to calculate closing and opening times based off of the sunset times at the location that is entered. A goal is to eventually include this in the Sunset to Sunset script to avoid the need to load two separate scripts. + +## Installation +Whichever method you choose, either **Download** or **CDN**, it’s best to include all these files in the head so that they load right away. It’s not ideal to have render-blocking scripts in the head but the reason we recommend putting them in the head is to avoid the flash of the rendered page before the Sabbath message gets rendered. Feel free to submit a PR for improved installation methods. + +### Download +- **CSS** + - [sunset-to-sunset.css](https://unpkg.com/sunset-to-sunset@1.0.4/dist/assets/sunset-to-sunset.css), minified +- **Javascript** + - [luxon.min.js](https://cdn.jsdelivr.net/npm/luxon@2.0.2/build/global/luxon.min.js), minified library for working with dates and times in JavaScript. + - [polyfills-legacy.53eda98a.js](https://unpkg.com/sunset-to-sunset@1.0.4/dist/assets/polyfills-legacy.53eda98a.js), legacy bundle for outdated browser support. + - [sunset-to-sunset.min.js](https://unpkg.com/sunset-to-sunset@1.0.4/dist/assets/sunset-to-sunset.min.js), minified + - [sunset-to-sunset-legacy.min.js](https://unpkg.com/sunset-to-sunset@1.0.4/dist/assets/sunset-to-sunset-legacy.min.js), minified for legacy browsers + +``` html + + + +
+ + + + + + + + + + + + + + + + + + + + + + + +``` + +### CDN +Link directly to the Sunset to Sunset files on [unpkg](https://unpkg.com/). +``` html + + + + + + + + + + + + + + + + + + + + + + + + + + + +``` + + +## Usage +Sunset to sunset needs some initial configuration needed for it to work correctly for your location. It will work out of the box without configuration but it won't be for your location. + +## Custom Templates +Sunset to sunset has some built-in banner and message templates that will be shown at the appropriate times but sometimes you need to define your own wording. You can do that with the html `template` elements. + +### Special Template Tags +You can add the following two tags to your custom templates to render the closing time and the opening times. + +#### Closing Time +``` html + +``` +#### Opening Time +``` html + +``` + +### Formatting Times +There are three methods for setting the format on the calculated times in your custom templates: + +1. **[Token](#token):** Allows you to set what order each part of the date and time appear so it looks the same for everyone. +2. **[Locale](#locale):** Using whatever the user's browser's date/time format is set to. +3. **[Default](#default):** Basically let the script decide for you. + +#### Token +This is the most flexible formatting option as it allows you to define the order of the date/time parts and the formatting. To use the `token` format include the [special closing and opening time tags](#special-template-tags) with a `data-format-token` like the following: +``` html + + + +``` +> Note that you can add words in the token format by surrounding them with single quotes like in the example above. + +**Advanced formatting:** The [Luxon documentation has a full list of formatting tokens](https://moment.github.io/luxon/#/formatting?id=table-of-tokens) so that you can fine-tune your dates and times. + +#### Locale +With this option you can't arrange the parts of the date and time but you can decide how they are formatted. To use the `locale` format include the [special closing and opening time tags](#special-template-tags) with a `data-format-locale` like the following: +``` html + + + +``` + +#### Default +Include the [special closing and opening time tags](#special-template-tags) with no `data-format-locale` or `data-format-token` attributes and it will output the times with the token format of `cccc, LLLL d 'at' h:mm a ZZZZ`: +``` html + + + +``` + +### Banner Template +To define your custom banner template add the following snippet to your page, preferrably in the `` tag. + +``` html + ++ Your message here +
+ +``` + +### Full Message Template +If you want to have full control over the appearance and layout of the message then you'll want to use the full message template. Everything inside of this template will be output into the `Message here
++ We will re-open on . +
+\n\t\t\t\tIn a world that seems to be spinning faster every day, we choose to stop \n\t\t\t\tand rest every Sabbath (Saturday). It’s a day for us to relax, refresh, \n\t\t\t\trefocus and worship; worship a God who loved us so much that He built a \n\t\t\t\tday of rest into Creation week and then commanded us to keep it \n\t\t\t\t(knowing we probably wouldn’t do it otherwise—even though it is for \n\t\t\t\tour best good).\n\t\t\t
\n\t\t",i=t}let a=document.createElement("template");a.innerHTML='\n\t\t\n\t\t\t\t\t\tWe will re-open on .\n\t\t\t\t\t
\n\t\t\t\t=p&&o<=b&&o.weekday>=r(),l=o>b&&o>=c()),t){const t=y(p).diff(o,"milliseconds").toObject();t.milliseconds>=0&&setTimeout((()=>{location.reload()}),t.milliseconds)}if(s){((t,n)=>{const s=document.querySelector("template#sts-banner-template");let o;if(s)o=s;else{let t=document.createElement("template");t.innerHTML='\n\t\t\t
\n\t\t\t\tIn a world that seems to be spinning faster every day, we choose to stop \n\t\t\t\tand rest every Sabbath (Saturday). It’s a day for us to relax, refresh, \n\t\t\t\trefocus and worship; worship a God who loved us so much that He built a \n\t\t\t\tday of rest into Creation week and then commanded us to keep it \n\t\t\t\t(knowing we probably wouldn’t do it otherwise—even though it is for \n\t\t\t\tour best good).\n\t\t\t
\n\t\t",a=t}let i=document.createElement("template");i.innerHTML='\n\t\t\n\t\t\t\t\t\tWe will re-open on .\n\t\t\t\t\t
\n\t\t\t\t\n\t\t\t\tIn a world that seems to be spinning faster every day, we choose to stop \n\t\t\t\tand rest every Sabbath (Saturday). It’s a day for us to relax, refresh, \n\t\t\t\trefocus and worship; worship a God who loved us so much that He built a \n\t\t\t\tday of rest into Creation week and then commanded us to keep it \n\t\t\t\t(knowing we probably wouldn’t do it otherwise—even though it is for \n\t\t\t\tour best good).\n\t\t\t
\n\t\t",a=t}let i=document.createElement("template");i.innerHTML='\n\t\t\n\t\t\t\t\t\tWe will re-open on .\n\t\t\t\t\t
\n\t\t\t\t=p&&o<=S&&o.weekday>=r(),l=o>S&&o>=c()),t){const t=b(p).diff(o,"milliseconds").toObject();t.milliseconds>=0&&setTimeout((()=>{location.reload()}),t.milliseconds)}if(s){((t,n)=>{const s=document.querySelector("template#sts-banner-template");let o;if(s)o=s;else{let t=document.createElement("template");t.innerHTML='\n\t\t\t
\n\t\t\t\tIn a world that seems to be spinning faster every day, we choose to stop \n\t\t\t\tand rest every Sabbath (Saturday). It’s a day for us to relax, refresh, \n\t\t\t\trefocus and worship; worship a God who loved us so much that He built a \n\t\t\t\tday of rest into Creation week and then commanded us to keep it \n\t\t\t\t(knowing we probably wouldn’t do it otherwise—even though it is for \n\t\t\t\tour best good).\n\t\t\t
\n\t\t",a=t}let i=document.createElement("template");i.innerHTML='\n\t\t\n\t\t\t\t\t\tWe will re-open on .\n\t\t\t\t\t
\n\t\t\t\tIn a world that seems to be spinning faster every day, we choose to stop and rest every Sabbath (Saturday). It’s a day for us to relax, refresh, diff --git a/package.json b/package.json index c331e65..82e29d6 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "sunset-to-sunset", - "version": "1.0.3", + "version": "1.0.4", "description": "", - "main": "src/js/main.js", + "main": "src/js/sunset-to-sunset.js", "author": "Cavell Blood", "scripts": { "dev": "vite", diff --git a/src/js/main.js b/src/js/sunset-to-sunset.js similarity index 85% rename from src/js/main.js rename to src/js/sunset-to-sunset.js index 0dc2d73..2270556 100644 --- a/src/js/main.js +++ b/src/js/sunset-to-sunset.js @@ -147,10 +147,6 @@ const SunsetToSunset = (() => { console.log(`Intializing Sunset to Sunset...`); - // Set day of week: zero-based index - const closingDayNumber = 5 - const openingDayNumber = 6 - const DateTime = luxon.DateTime const Duration = luxon.Duration @@ -164,17 +160,44 @@ const SunsetToSunset = (() => { 'Time': now.toLocaleString(DateTime.TIME_WITH_SHORT_OFFSET), } - const activateSunsetWatch = now.weekday == closingDayNumber || now.weekday == openingDayNumber + // Get options set in HTML + const stsContainer = document.querySelector('template#sts-settings') + let days = {} - const html = document.getElementsByTagName('html')[0] + // Mainly for debugging purposes you can set the closing and opening day number + // so the plugin activates on the day you are testing it for example instead of + // needing to wait until Friday to test it. + if (stsContainer.dataset.days) { + days = JSON.parse(stsContainer.dataset.days) + } + + // Set day of week: zero-based index + const dayDefaults = { + closing: 5, + opening: 6 + } + + // Merge days with dayDefaults + days = extend(extend({}, dayDefaults), days) + const getClosingDayNumber = () => { + return days.closing + } + + const getOpeningDayNumber = () => { + return days.opening + } + + const activateSunsetWatch = now.weekday == getClosingDayNumber() || now.weekday == getOpeningDayNumber() + + // Set default options const defaults = { guardDuration: { minutes: 30 }, - messageDuration: { - minutes: 60 + bannerDuration: { + hours: 3, }, location: { lat: 0, @@ -184,13 +207,17 @@ const SunsetToSunset = (() => { debug: false } - // Get options set in HTML - const stsContainer = document.querySelector('template#sts-settings') - let options = stsContainer != null ? JSON.parse(stsContainer.dataset.settings) : {} + let options = {} + + if (stsContainer.dataset.settings) { + options = JSON.parse(stsContainer.dataset.settings) + } // Merge options with defaults options = extend(extend({}, defaults), options) + const html = document.getElementsByTagName('html')[0] + // Hide `html` until we have determined what things need to be rendered. if (activateSunsetWatch || options.simulateTime) { html.style.display = "none"; @@ -223,14 +250,7 @@ const SunsetToSunset = (() => { // Get closing sunset const getClosingSunset = () => { - let daysToClosing - - - if (options.simulateTime) { - daysToClosing = 0 - } else { - daysToClosing = closingDayNumber - now.weekday - } + let daysToClosing = getClosingDayNumber() - now.weekday const closingDate = DateTime.fromISO(now.plus({ days: daysToClosing @@ -243,13 +263,7 @@ const SunsetToSunset = (() => { // Get opening sunset const getOpeningSunset = () => { - let daysToOpening - - if (options.simulateTime) { - daysToOpening = 1 - } else { - daysToOpening = openingDayNumber - now.weekday - } + let daysToOpening = getOpeningDayNumber() - now.weekday const openingDate = DateTime.fromISO(now.plus({ days: daysToOpening @@ -261,8 +275,8 @@ const SunsetToSunset = (() => { } // Get message minutes - const getMessageDuration = () => { - let duration = Duration.fromObject(options.messageDuration) + const getBannerDuration = () => { + let duration = Duration.fromObject(options.bannerDuration) return duration } @@ -274,7 +288,7 @@ const SunsetToSunset = (() => { //Get message time const getMessageTime = (date) => { - return date.minus(getMessageDuration()) + return date.minus(getBannerDuration()) } // Get guard time. `date` is a Luxon DateTime object. @@ -305,44 +319,47 @@ const SunsetToSunset = (() => { // Only run if today is Friday or Sabbath if ( activateSunsetWatch || options.simulateTime) { + let preparationDay = false + let bannerUp = false + let duringSabbath = false + let afterSabbath = false + + // Check if we are simulating the time + if (options.simulateTime) { + switch (options.simulateTime) { + case 'preparation-day': + preparationDay = true + break; + + case 'banner-up': + bannerUp = true + break; + + case 'during-sabbath': + duringSabbath = true + break; + + case 'after-sabbath': + afterSabbath = true + break; + + default: + break; + } + } + getTimes().then(([closingSunset, openingSunset]) => { // Set guard times const closing = getGuardTime(closingSunset, 'closing') const opening = getGuardTime(openingSunset, 'opening') - let preparationDay = false - let bannerUp = false - let duringSabbath = false - let afterSabbath = false - - // Check if we are simulating the time - if (options.simulateTime) { - switch (options.simulateTime) { - case 'preparation-day': - preparationDay = true - break; - - case 'banner-up': - bannerUp = true - break; - - case 'during-sabbath': - duringSabbath = true - break; - - case 'after-sabbath': - afterSabbath = true - break; - - default: - break; - } - } else { + if (!options.simulateTime) { + // Set time checks preparationDay = now < getMessageTime(closing) bannerUp = now > getMessageTime(closing) && now < closing - duringSabbath = now >= closing && now <= opening && now.weekday >= closingDayNumber - afterSabbath = now > opening && now >= openingDayNumber + duringSabbath = now >= closing && now <= opening && now.weekday >= getClosingDayNumber() + afterSabbath = now > opening && now >= getOpeningDayNumber() } // Is is during the week before the time to show the banner? @@ -442,11 +459,13 @@ const SunsetToSunset = (() => { console.group(`Sunset to Sunset times`) console.table(times) console.groupEnd() - + } - }) } else { + // Unhide `html` after we determine what things need to be rendered. + html.style.display = "block" + console.log('Sunset to Sunset: Exiting because today is not closing day') } diff --git a/src/tests/index.html b/src/tests/index.html index 1fa6716..fe35fd3 100644 --- a/src/tests/index.html +++ b/src/tests/index.html @@ -9,7 +9,7 @@ - +
@@ -841,17 +841,17 @@In a world that seems to be spinning faster every day, we choose to stop and rest every Sabbath (Saturday). It’s a day for us to relax, refresh, diff --git a/vite.config.js b/vite.config.js index a39145d..7dc91b1 100644 --- a/vite.config.js +++ b/vite.config.js @@ -11,7 +11,7 @@ export default ({ command }) => ({ outDir: "../dist/", rollupOptions: { input: { - "sunset-to-sunset": "./src/js/main.js", + "sunset-to-sunset": "./src/js/sunset-to-sunset.js", test: "./src/tests/index.html", }, output: {