isad-widget
Version:
Display a configurable banner-link to promote International Stuttering Awareness Day (ISAD) - 22nd October.
160 lines (116 loc) • 4.76 kB
JavaScript
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
/*!
isad-widget.js | ©2017 Nick Freear | License: MIT | NOT an official widget!
https://github.com/nfreear/isad-widget
*/
var YEAR = new Date().getFullYear();
// International stuttering awareness day (ISAD) occurs each year on 22nd October.
var ISAD_DATE = new Date(YEAR, (10 - 1), 22, 0, 0, 0, 0);
// TEST: var ISAD_DATE = new Date(YEAR, (9 - 1), 22, 0, 0, 0, 0);
var methods = require('./src/methods');
require('./src/widget-src')(ISAD_DATE, methods);
// End.
},{"./src/methods":2,"./src/widget-src":3}],2:[function(require,module,exports){
// Methods | © 2017 Nick Freear.
var D = window.document;
module.exports = {
extend: extend,
addDays: function (dateOb, days) {
return dateOb.getTime() + (days * 24 * 60 * 60 * 1000); // Milliseconds.
},
setWeekdayLocal: function (CFG) {
var weekdays = CFG.texts[ CFG.lang ].weekdays;
CFG.day = weekdays[ CFG.date.getDay() ];
return CFG;
},
setHTML: function (CFG) {
var elem = D.getElementById(CFG.id);
elem.lang = CFG.lang;
elem.dir = CFG.dir;
elem.setAttribute('role', 'alert');
elem.className = 'isad-widget-js' + (CFG.point_bsa ? ' bsa' : '');
elem.innerHTML = CFG.message;
},
addStylesheet: function (CFG) {
var styleEl = D.createElement('link');
styleEl.rel = 'stylesheet';
styleEl.type = 'text/css';
styleEl.href = decideStyleUrl(CFG);
D.head.appendChild(styleEl);
}
};
// JuhQ (16 July 2015): https://gist.github.com/pbojinov/8f3765b672efec122f66#gistcomment-1493930
function extend () {
var extended = {};
var key;
var prop;
for (key in arguments) {
var argument = arguments[ key ];
for (prop in argument) {
if (Object.prototype.hasOwnProperty.call(argument, prop)) {
extended[ prop ] = argument[ prop ];
}
}
}
return extended;
}
function decideStyleUrl (CFG) {
var scriptEl = D.querySelector(CFG.script_select);
CFG.script_url = scriptEl.src;
// Support for 'unpkg' CDN short URL.
if (/@\^?\d\.\d\.\d(-[\w.]+)?(#|_.js|$)/.test(CFG.script_url)) {
console.warn('ISAD: npm @version found');
CFG.style_url = CFG.style_url.replace('/../..', '');
CFG.script_url = CFG.script_url.replace(/(#.*|_\.js)/, '');
}
return CFG.script_url + CFG.style_url;
}
},{}],3:[function(require,module,exports){
// ISAD widget main function | © 2017 Nick Freear.
var L = window.location;
var D = window.document;
module.exports = function (ISAD_DATE, methods) {
'use strict';
var TODAY = new Date();
var defaults = {
id: 'isad-widget',
lang: 'en',
dir: 'ltr',
date: ISAD_DATE,
texts: {
en: {
p1: '<i class="i-p1">The 22nd October is <a href="{u}">International Stuttering Awareness Day (ISAD)</a>.</i>',
theme: '<em class="i-theme">2017 theme: <q>a world that understands stuttering.</q></em>'
}
},
url: 'http://isastutter.org/what-we-do/isad?utm_source=github&utm_campaign=isad-widget',
url_bsa: 'https://stammering.org/isad?utm_source=github&utm_campaign=isad-widget',
point_bsa: false,
script_select: 'script[ src *= "/isad-widget" ]',
style_url: '/../../style/isad-widget.css',
days_before: 22,
days_after: 7,
force: /[?&]isad.?widget=f(orce)?/i.test(L.search)
};
var configEl = D.querySelector('[ data-isad-widget ]');
var config = configEl ? JSON.parse(configEl.getAttribute('data-isad-widget')) : {};
var isad = methods.extend(defaults, config);
isad.show_date = methods.addDays(ISAD_DATE, -isad.days_before);
isad.hide_date = methods.addDays(ISAD_DATE, isad.days_after);
isad.diff_show = TODAY - isad.show_date;
isad.diff_hide = TODAY - isad.hide_date;
isad.should_show = (isad.diff_show >= 0 && isad.diff_hide < 0);
if (!isad.should_show && !isad.force) {
return console.warn('ISAD: no-show', isad);
}
var lang = isad.texts[ isad.lang ] ? isad.lang : 'en';
var texts = isad.texts[ lang ];
isad.url = isad.point_bsa ? isad.url_bsa : isad.url;
isad.message = (texts.p1 + texts.theme).replace('{u}', isad.url);
console.warn('isad-widget:', isad);
methods.addStylesheet(isad);
methods.setHTML(isad);
window.console && console.log('Happy ISAD! ~ http://isastutter.org/what-we-do/isad');
return isad;
};
},{}]},{},[1]);