banner-rotator-webpack-plugin
Advanced tools
Comparing version 0.2.0 to 0.2.1
'use strict'; | ||
var defaultConfig = { | ||
closeEventName: 'banner-rotator-banner-close', | ||
closedBannersStorageKey: 'banner-rotator-closed-banners' | ||
}; | ||
var closedBannersStorageKey = 'banner-rotator-closed-banners'; | ||
@@ -12,3 +9,3 @@ /** | ||
var ClosedBannersStorage = function ClosedBannersStorage(key) { | ||
if ( key === void 0 ) key = defaultConfig.closedBannersStorageKey; | ||
if ( key === void 0 ) key = closedBannersStorageKey; | ||
@@ -15,0 +12,0 @@ this.storageKey = key; |
'use strict'; | ||
var closeEventName = 'banner-rotator-banner-close'; | ||
/** | ||
* @param {string} name | ||
* @param {*} data | ||
* @param {string} bannerId | ||
* @param {string} [eventName] | ||
*/ | ||
function dispatchCustomEvent(name, data) { | ||
function dispatchCloseEvent(bannerId, eventName) { | ||
if ( eventName === void 0 ) eventName = closeEventName; | ||
var event; | ||
if (window.CustomEvent) { | ||
event = new CustomEvent(name, { detail: data }); | ||
event = new CustomEvent(eventName, { detail: bannerId }); | ||
} else { | ||
event = document.createEvent('CustomEvent'); | ||
event.initCustomEvent(name, false, false, data); | ||
event.initCustomEvent(eventName, false, false, bannerId); | ||
} | ||
@@ -20,18 +24,3 @@ | ||
var defaultConfig = { | ||
closeEventName: 'banner-rotator-banner-close', | ||
closedBannersStorageKey: 'banner-rotator-closed-banners' | ||
}; | ||
/** | ||
* @param {string} bannerId | ||
* @param {string} [eventName] | ||
*/ | ||
function dispatchCloseEvent(bannerId, eventName) { | ||
if ( eventName === void 0 ) eventName = defaultConfig.closeEventName; | ||
dispatchCustomEvent(eventName, bannerId); | ||
} | ||
module.exports = dispatchCloseEvent; | ||
//# sourceMappingURL=dispatch-close-event.js.map |
@@ -177,7 +177,11 @@ 'use strict'; | ||
var defaultConfig = { | ||
closeEventName: 'banner-rotator-banner-close', | ||
closedBannersStorageKey: 'banner-rotator-closed-banners' | ||
}; | ||
var closeEventName = 'banner-rotator-banner-close'; | ||
var closedBannersStorageKey = 'banner-rotator-closed-banners'; | ||
var defaultConfig = Object.freeze({ | ||
closeEventName: closeEventName, | ||
closedBannersStorageKey: closedBannersStorageKey | ||
}); | ||
/** | ||
@@ -187,3 +191,3 @@ * localStorage based storage to keep info about closed banners | ||
var ClosedBannersStorage = function ClosedBannersStorage(key) { | ||
if ( key === void 0 ) key = defaultConfig.closedBannersStorageKey; | ||
if ( key === void 0 ) key = closedBannersStorageKey; | ||
@@ -269,70 +273,2 @@ this.storageKey = key; | ||
/** | ||
* @param {string} name | ||
* @param {*} data | ||
*/ | ||
/** | ||
* @param {Date} [rangeStart] | ||
* @param {Date} [rangeEnd] | ||
* @param {Date} date | ||
* @return {boolean} | ||
*/ | ||
var isRangeContainsDate = function (rangeStart, rangeEnd, date) { | ||
var time = date.getTime(); | ||
var startTime = rangeStart && rangeStart.getTime(); | ||
var endTime = rangeEnd && rangeEnd.getTime(); | ||
var greaterThanStart = startTime ? time >= startTime : true; | ||
var lessThanEnd = endTime ? time <= endTime : true; | ||
return greaterThanStart && lessThanEnd; | ||
}; | ||
/* eslint-disable no-magic-numbers */ | ||
var DATE_REGEXP = /^(\d{4})-(\d{2})-(\d{2})(?:\s(\d{2}):(\d{2}))?$/; | ||
/** | ||
* @param {string} dateString Human readable date, e.g. '2017-10-12 14:48' | ||
* @param {boolean} [parseAsEndDate=false] | ||
* @return {Date} | ||
*/ | ||
function parseDate(dateString, parseAsEndDate) { | ||
if ( parseAsEndDate === void 0 ) parseAsEndDate = false; | ||
var date = new Date(); | ||
if (!DATE_REGEXP.test(dateString)) { | ||
throw new Error(("Invalid date: " + dateString)); | ||
} | ||
var matches = dateString.match(DATE_REGEXP) | ||
.filter(function (val, index) { return index > 0 && val !== undefined; }) | ||
.map(function (val) { return parseInt(val, 10); }); | ||
var year = matches[0]; | ||
var month = matches[1]; | ||
var day = matches[2]; | ||
var hours = matches[3]; | ||
var minutes = matches[4]; | ||
date.setYear(year); | ||
date.setMonth(month - 1); | ||
date.setDate(day); | ||
date.setHours(0); | ||
date.setMinutes(0); | ||
date.setSeconds(0); | ||
if (parseAsEndDate) { | ||
date.setHours(23); | ||
date.setMinutes(59); | ||
} | ||
if (hours && minutes) { | ||
date.setHours(hours); | ||
date.setMinutes(minutes); | ||
} | ||
return date; | ||
} | ||
var globToRegexp = function (glob, opts) { | ||
@@ -538,23 +474,2 @@ if (typeof glob !== 'string') { | ||
/** | ||
* @param {Array<Banner>} banners | ||
* @return {Array<Banner>} | ||
*/ | ||
function normalizeBanners(banners) { | ||
banners.forEach(function (banner) { | ||
var startDate = banner.startDate; | ||
var endDate = banner.endDate; | ||
if (startDate) { | ||
banner.startDate = parseDate(startDate); | ||
} | ||
if (endDate) { | ||
banner.endDate = parseDate(endDate); | ||
} | ||
}); | ||
return banners; | ||
} | ||
/** | ||
* @typedef {Object} BannerRotatorFilterCriteria | ||
@@ -574,8 +489,15 @@ * @property {Date} date | ||
this.closedBannersStorage = cfg.closedBannersStorage || new ClosedBannersStorage(); | ||
this.banners = normalizeBanners(cfg.banners || __BANNER_ROTATOR_BANNERS_CONFIG__); // eslint-disable-line no-undef | ||
this.banners = cfg.banners || __BANNER_ROTATOR_BANNERS_CONFIG__; // eslint-disable-line no-undef | ||
this._handleBannerClose = function (e) { | ||
this$1.closeBanner(e.detail); | ||
}; | ||
this.banners.forEach(function (b) { | ||
if (typeof b.startDate === 'string') { | ||
b.startDate = new Date(b.startDate); | ||
} | ||
if (typeof b.endDate === 'string') { | ||
b.endDate = new Date(b.endDate); | ||
} | ||
}); | ||
this._handleBannerClose = function (e) { return this$1.closeBanner(e.detail); }; | ||
window.addEventListener(cfg.closeEventName, this._handleBannerClose); | ||
@@ -586,6 +508,34 @@ }; | ||
* @param {BannerRotatorFilterCriteria} [filterCriteria] | ||
* @return {Promise<Array<Banner>>} | ||
* @return {Promise<Banner[]>} | ||
*/ | ||
BannerRotator.getUTCDate = function getUTCDate () { | ||
var now = new Date(); | ||
return new Date( | ||
now.getUTCFullYear(), | ||
now.getUTCMonth(), | ||
now.getUTCDate(), | ||
now.getUTCHours(), | ||
now.getUTCMinutes(), | ||
now.getUTCSeconds(), | ||
now.getUTCMilliseconds() | ||
); | ||
}; | ||
/** | ||
* @param {Date} [rangeStart] | ||
* @param {Date} [rangeEnd] | ||
* @param {Date} date | ||
* @return {boolean} | ||
*/ | ||
BannerRotator.isRangeContainsDate = function isRangeContainsDate (rangeStart, rangeEnd, date) { | ||
var time = date.getTime(); | ||
var startTime = rangeStart && rangeStart.getTime(); | ||
var endTime = rangeEnd && rangeEnd.getTime(); | ||
var greaterThanStart = startTime ? time >= startTime : true; | ||
var lessThanEnd = endTime ? time <= endTime : true; | ||
return greaterThanStart && lessThanEnd; | ||
}; | ||
BannerRotator.prototype.run = function run (filterCriteria) { | ||
var banners = this.getBanners(filterCriteria); | ||
var banners = this.filterBanners(filterCriteria); | ||
var promises = banners.map(function (banner) { return banner.load() | ||
@@ -599,12 +549,12 @@ .then(function (module) { return (banner.module = module); }) | ||
/** | ||
* @param {BannerRotatorFilterCriteria} [filterCriteria] | ||
* @param {BannerRotatorFilterCriteria} [criteria] | ||
* @return {Array<Banner>} | ||
*/ | ||
BannerRotator.prototype.getBanners = function getBanners (filterCriteria) { | ||
BannerRotator.prototype.filterBanners = function filterBanners (criteria) { | ||
var this$1 = this; | ||
if ( filterCriteria === void 0 ) filterCriteria = {}; | ||
if ( criteria === void 0 ) criteria = {}; | ||
var date = filterCriteria.date; if ( date === void 0 ) date = new Date(); | ||
var location = filterCriteria.location; if ( location === void 0 ) location = window.location.pathname; | ||
var countryCode = filterCriteria.countryCode; | ||
var date = criteria.date; if ( date === void 0 ) date = BannerRotator.getUTCDate(); | ||
var location = criteria.location; if ( location === void 0 ) location = window.location.pathname; | ||
var countryCode = criteria.countryCode; | ||
@@ -620,3 +570,3 @@ return this.banners.filter(function (banner) { | ||
var isDisabled = typeof disabled === 'boolean' ? disabled : false; | ||
var matchDate = isRangeContainsDate(startDate, endDate, date); | ||
var matchDate = BannerRotator.isRangeContainsDate(startDate, endDate, date); | ||
var matchLocation = locations && location ? globMatcher(locations, location) : true; | ||
@@ -623,0 +573,0 @@ var matchCountry = countries && countryCode ? globMatcher(countries, countryCode) : true; |
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="0.2.1"></a> | ||
## [0.2.1](https://github.com/kisenka/banner-rotator-webpack-plugin/compare/v0.2.0...v0.2.1) (2018-08-06) | ||
### Bug Fixes | ||
* fix date parsing ([4016eee](https://github.com/kisenka/banner-rotator-webpack-plugin/commit/4016eee)) | ||
<a name="0.2.0"></a> | ||
@@ -7,0 +17,0 @@ # [0.2.0](https://github.com/kisenka/banner-rotator-webpack-plugin/compare/v0.1.4...v0.2.0) (2018-07-27) |
{ | ||
"name": "banner-rotator-webpack-plugin", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "", | ||
@@ -43,2 +43,3 @@ "keywords": [ | ||
"eslint": "4.8.0", | ||
"glob": "^7.1.2", | ||
"husky": "0.14.3", | ||
@@ -45,0 +46,0 @@ "karma": "^1.7.1", |
@@ -1,2 +0,2 @@ | ||
import defaultConfig from './config'; | ||
import { closedBannersStorageKey } from './config'; | ||
@@ -7,3 +7,3 @@ /** | ||
export default class ClosedBannersStorage { | ||
constructor(key = defaultConfig.closedBannersStorageKey) { | ||
constructor(key = closedBannersStorageKey) { | ||
this.storageKey = key; | ||
@@ -10,0 +10,0 @@ |
@@ -1,4 +0,2 @@ | ||
export default { | ||
closeEventName: 'banner-rotator-banner-close', | ||
closedBannersStorageKey: 'banner-rotator-closed-banners' | ||
}; | ||
export const closeEventName = 'banner-rotator-banner-close'; | ||
export const closedBannersStorageKey = 'banner-rotator-closed-banners'; |
@@ -1,3 +0,2 @@ | ||
import dispatchCustomEvent from './utils/dispatch-custom-event'; | ||
import defaultConfig from './config'; | ||
import { closeEventName } from './config'; | ||
@@ -8,4 +7,13 @@ /** | ||
*/ | ||
export default function dispatchCloseEvent(bannerId, eventName = defaultConfig.closeEventName) { | ||
dispatchCustomEvent(eventName, bannerId); | ||
export default function dispatchCloseEvent(bannerId, eventName = closeEventName) { | ||
let event; | ||
if (window.CustomEvent) { | ||
event = new CustomEvent(eventName, { detail: bannerId }); | ||
} else { | ||
event = document.createEvent('CustomEvent'); | ||
event.initCustomEvent(eventName, false, false, bannerId); | ||
} | ||
window.dispatchEvent(event); | ||
} |
import merge from 'merge-options'; | ||
import ClosedStorage from './closed-banners-storage'; | ||
import { isRangeContainsDate, globMatcher, normalizeBanners } from './utils'; | ||
import defaultConfig from './config'; | ||
import globMatcher from './glob-matcher'; | ||
import * as defaultConfig from './config'; | ||
@@ -15,2 +15,33 @@ /** | ||
export default class BannerRotator { | ||
/** | ||
* @return {Date} | ||
*/ | ||
static getUTCDate() { | ||
const now = new Date(); | ||
return new Date( | ||
now.getUTCFullYear(), | ||
now.getUTCMonth(), | ||
now.getUTCDate(), | ||
now.getUTCHours(), | ||
now.getUTCMinutes(), | ||
now.getUTCSeconds(), | ||
now.getUTCMilliseconds() | ||
); | ||
} | ||
/** | ||
* @param {Date} [rangeStart] | ||
* @param {Date} [rangeEnd] | ||
* @param {Date} date | ||
* @return {boolean} | ||
*/ | ||
static isRangeContainsDate(rangeStart, rangeEnd, date) { | ||
const time = date.getTime(); | ||
const startTime = rangeStart && rangeStart.getTime(); | ||
const endTime = rangeEnd && rangeEnd.getTime(); | ||
const greaterThanStart = startTime ? time >= startTime : true; | ||
const lessThanEnd = endTime ? time <= endTime : true; | ||
return greaterThanStart && lessThanEnd; | ||
} | ||
constructor(config = {}) { | ||
@@ -21,8 +52,15 @@ const cfg = merge(defaultConfig, config); | ||
this.closedBannersStorage = cfg.closedBannersStorage || new ClosedStorage(); | ||
this.banners = normalizeBanners(cfg.banners || __BANNER_ROTATOR_BANNERS_CONFIG__); // eslint-disable-line no-undef | ||
this.banners = cfg.banners || __BANNER_ROTATOR_BANNERS_CONFIG__; // eslint-disable-line no-undef | ||
this._handleBannerClose = e => { | ||
this.closeBanner(e.detail); | ||
}; | ||
this.banners.forEach(b => { | ||
if (typeof b.startDate === 'string') { | ||
b.startDate = new Date(b.startDate); | ||
} | ||
if (typeof b.endDate === 'string') { | ||
b.endDate = new Date(b.endDate); | ||
} | ||
}); | ||
this._handleBannerClose = e => this.closeBanner(e.detail); | ||
window.addEventListener(cfg.closeEventName, this._handleBannerClose); | ||
@@ -33,6 +71,6 @@ } | ||
* @param {BannerRotatorFilterCriteria} [filterCriteria] | ||
* @return {Promise<Array<Banner>>} | ||
* @return {Promise<Banner[]>} | ||
*/ | ||
run(filterCriteria) { | ||
const banners = this.getBanners(filterCriteria); | ||
const banners = this.filterBanners(filterCriteria); | ||
const promises = banners.map(banner => banner.load() | ||
@@ -46,11 +84,11 @@ .then(module => (banner.module = module)) | ||
/** | ||
* @param {BannerRotatorFilterCriteria} [filterCriteria] | ||
* @param {BannerRotatorFilterCriteria} [criteria] | ||
* @return {Array<Banner>} | ||
*/ | ||
getBanners(filterCriteria = {}) { | ||
filterBanners(criteria = {}) { | ||
const { | ||
date = new Date(), | ||
date = BannerRotator.getUTCDate(), | ||
location = window.location.pathname, | ||
countryCode | ||
} = filterCriteria; | ||
} = criteria; | ||
@@ -61,3 +99,3 @@ return this.banners.filter(banner => { | ||
const isDisabled = typeof disabled === 'boolean' ? disabled : false; | ||
const matchDate = isRangeContainsDate(startDate, endDate, date); | ||
const matchDate = BannerRotator.isRangeContainsDate(startDate, endDate, date); | ||
const matchLocation = locations && location ? globMatcher(locations, location) : true; | ||
@@ -64,0 +102,0 @@ const matchCountry = countries && countryCode ? globMatcher(countries, countryCode) : true; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
88729
30
24
1287