@nrk/core-datepicker
Advanced tools
Comparing version
@@ -1,2 +0,2 @@ | ||
/*! @nrk/core-datepicker v4.0.1 - Copyright (c) 2017-2022 NRK */ | ||
/*! @nrk/core-datepicker v4.0.2 - Copyright (c) 2017-2022 NRK */ | ||
'use strict'; | ||
@@ -354,2 +354,38 @@ | ||
/** | ||
* Handlers to fill in date value depending on type of value (as key) selected | ||
* e.g. resolve if same day in month can be filled when selecting next month | ||
*/ | ||
var FILL = { | ||
month: function month(self, value) { | ||
if (!self.disabled(value)) return value; | ||
var firstAvailableDate = daysInMonth(self.parse(value)).filter(function (day) { | ||
return !self.disabled(day); | ||
})[0]; | ||
return firstAvailableDate || value; | ||
}, | ||
"null": function _null(_self, value) { | ||
return value; | ||
} | ||
}; | ||
/** | ||
* Handlers to resolve if entity with type of value (as key) is disabled | ||
* e.g. resolve if a month can be selected or is disabled | ||
*/ | ||
var DISABLED = { | ||
month: function month(self, value) { | ||
var allDays = daysInMonth(self.parse(value)); | ||
var allDaysDisabled = allDays.map(function (day) { | ||
return self.disabled(day); | ||
}).reduce(function (a, b) { | ||
return a && b; | ||
}); | ||
return allDaysDisabled; | ||
}, | ||
"null": function _null(self, value) { | ||
return self.disabled(value); | ||
} | ||
}; | ||
var MASK = { | ||
@@ -436,3 +472,4 @@ year: '*-m-d', | ||
if (event.type === 'change') { | ||
this.date = MASK[event.target.getAttribute('data-type')].replace('*', event.target.value); | ||
var changeMask = MASK[event.target.getAttribute('data-type')].replace('*', event.target.value); | ||
this.date = FILL[event.target.getAttribute('data-fill')](this, changeMask); | ||
} else if (event.type === 'click') { | ||
@@ -624,7 +661,14 @@ var button = closest(event.target, 'button[value]'); | ||
} | ||
/** | ||
* | ||
* @param {CoreDatepicker} self | ||
* @param {HTMLSelectElement} select | ||
*/ | ||
function setupSelect(self, select) { | ||
if (!select.firstElementChild) { | ||
select._autofill = true; | ||
select.innerHTML = self.months.map(function (name, month) { | ||
select.setAttribute('data-fill', 'month'); | ||
select.innerHTML = self.months.map(function (_, month) { | ||
return "<option value=\"y-".concat(month + 1, "-d\"></option>"); | ||
@@ -634,9 +678,30 @@ }).join(''); | ||
var disabled = DISABLED[select.getAttribute('data-fill')]; | ||
queryAll(select.children).forEach(function (option, month) { | ||
if (select._autofill) option.textContent = self.months[month]; | ||
option.disabled = self.disabled(option.value); | ||
option.disabled = disabled(self, option.value); | ||
option.selected = !self.diff(option.value); | ||
}); | ||
} | ||
/** | ||
* Returns array of days in the month containing the dateInMonth param | ||
* @param {Date} dateInMonth | ||
* @returns {Date[]} Array of days in the month in question | ||
*/ | ||
function daysInMonth(dateInMonth) { | ||
var date = new Date(dateInMonth); | ||
date.setDate(1); | ||
var month = date.getMonth(); | ||
var days = []; | ||
while (date.getMonth() === month) { | ||
days.push(new Date(date)); | ||
date.setDate(date.getDate() + 1); | ||
} | ||
return days; | ||
} | ||
module.exports = CoreDatepicker; |
import { addStyle, closest, dispatchEvent, toggleAttribute, queryAll } from '../utils' | ||
import parse from '@nrk/simple-date-parse' | ||
/** | ||
* Handlers to fill in date value depending on type of value (as key) selected | ||
* e.g. resolve if same day in month can be filled when selecting next month | ||
*/ | ||
const FILL = { | ||
month: (self, value) => { | ||
if (!self.disabled(value)) return value | ||
const firstAvailableDate = daysInMonth(self.parse(value)).filter(day => !self.disabled(day))[0] | ||
return firstAvailableDate || value | ||
}, | ||
null: (_self, value) => value | ||
} | ||
/** | ||
* Handlers to resolve if entity with type of value (as key) is disabled | ||
* e.g. resolve if a month can be selected or is disabled | ||
*/ | ||
const DISABLED = { | ||
month: (self, value) => { | ||
const allDays = daysInMonth(self.parse(value)) | ||
const allDaysDisabled = allDays.map(day => self.disabled(day)).reduce((a, b) => a && b) | ||
return allDaysDisabled | ||
}, | ||
null: (self, value) => self.disabled(value) | ||
} | ||
const MASK = { year: '*-m-d', month: 'y-*-d', day: 'y-m-*', hour: '*:m', minute: 'h:*', second: 'h:m:*', timestamp: '*', null: '*' } | ||
@@ -46,3 +71,4 @@ const KEYS = { 33: '-1month', 34: '+1month', 35: 'y-m-99', 36: 'y-m-1', 37: '-1day', 38: '-1week', 39: '+1day', 40: '+1week' } | ||
if (event.type === 'change') { | ||
this.date = MASK[event.target.getAttribute('data-type')].replace('*', event.target.value) | ||
const changeMask = MASK[event.target.getAttribute('data-type')].replace('*', event.target.value) | ||
this.date = FILL[event.target.getAttribute('data-fill')](this, changeMask) | ||
} else if (event.type === 'click') { | ||
@@ -169,15 +195,39 @@ const button = closest(event.target, 'button[value]') | ||
/** | ||
* | ||
* @param {CoreDatepicker} self | ||
* @param {HTMLSelectElement} select | ||
*/ | ||
function setupSelect (self, select) { | ||
if (!select.firstElementChild) { | ||
select._autofill = true | ||
select.innerHTML = self.months.map((name, month) => | ||
select.setAttribute('data-fill', 'month') | ||
select.innerHTML = self.months.map((_, month) => | ||
`<option value="y-${month + 1}-d"></option>` | ||
).join('') | ||
} | ||
const disabled = DISABLED[select.getAttribute('data-fill')] | ||
queryAll(select.children).forEach((option, month) => { | ||
if (select._autofill) option.textContent = self.months[month] | ||
option.disabled = self.disabled(option.value) | ||
option.disabled = disabled(self, option.value) | ||
option.selected = !self.diff(option.value) | ||
}) | ||
} | ||
/** | ||
* Returns array of days in the month containing the dateInMonth param | ||
* @param {Date} dateInMonth | ||
* @returns {Date[]} Array of days in the month in question | ||
*/ | ||
function daysInMonth (dateInMonth) { | ||
const date = new Date(dateInMonth) | ||
date.setDate(1) | ||
const month = date.getMonth() | ||
const days = [] | ||
while (date.getMonth() === month) { | ||
days.push(new Date(date)) | ||
date.setDate(date.getDate() + 1) | ||
} | ||
return days | ||
} |
@@ -1,2 +0,2 @@ | ||
/*! @nrk/core-datepicker v4.0.1 - Copyright (c) 2017-2022 NRK */ | ||
/*! @nrk/core-datepicker v4.0.2 - Copyright (c) 2017-2022 NRK */ | ||
(function (global, factory) { | ||
@@ -362,2 +362,38 @@ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('react')) : | ||
/** | ||
* Handlers to fill in date value depending on type of value (as key) selected | ||
* e.g. resolve if same day in month can be filled when selecting next month | ||
*/ | ||
var FILL = { | ||
month: function month(self, value) { | ||
if (!self.disabled(value)) return value; | ||
var firstAvailableDate = daysInMonth(self.parse(value)).filter(function (day) { | ||
return !self.disabled(day); | ||
})[0]; | ||
return firstAvailableDate || value; | ||
}, | ||
"null": function _null(_self, value) { | ||
return value; | ||
} | ||
}; | ||
/** | ||
* Handlers to resolve if entity with type of value (as key) is disabled | ||
* e.g. resolve if a month can be selected or is disabled | ||
*/ | ||
var DISABLED = { | ||
month: function month(self, value) { | ||
var allDays = daysInMonth(self.parse(value)); | ||
var allDaysDisabled = allDays.map(function (day) { | ||
return self.disabled(day); | ||
}).reduce(function (a, b) { | ||
return a && b; | ||
}); | ||
return allDaysDisabled; | ||
}, | ||
"null": function _null(self, value) { | ||
return self.disabled(value); | ||
} | ||
}; | ||
var MASK = { | ||
@@ -444,3 +480,4 @@ year: '*-m-d', | ||
if (event.type === 'change') { | ||
this.date = MASK[event.target.getAttribute('data-type')].replace('*', event.target.value); | ||
var changeMask = MASK[event.target.getAttribute('data-type')].replace('*', event.target.value); | ||
this.date = FILL[event.target.getAttribute('data-fill')](this, changeMask); | ||
} else if (event.type === 'click') { | ||
@@ -632,7 +669,14 @@ var button = closest$1(event.target, 'button[value]'); | ||
} | ||
/** | ||
* | ||
* @param {CoreDatepicker} self | ||
* @param {HTMLSelectElement} select | ||
*/ | ||
function setupSelect(self, select) { | ||
if (!select.firstElementChild) { | ||
select._autofill = true; | ||
select.innerHTML = self.months.map(function (name, month) { | ||
select.setAttribute('data-fill', 'month'); | ||
select.innerHTML = self.months.map(function (_, month) { | ||
return "<option value=\"y-".concat(month + 1, "-d\"></option>"); | ||
@@ -642,11 +686,32 @@ }).join(''); | ||
var disabled = DISABLED[select.getAttribute('data-fill')]; | ||
queryAll(select.children).forEach(function (option, month) { | ||
if (select._autofill) option.textContent = self.months[month]; | ||
option.disabled = self.disabled(option.value); | ||
option.disabled = disabled(self, option.value); | ||
option.selected = !self.diff(option.value); | ||
}); | ||
} | ||
/** | ||
* Returns array of days in the month containing the dateInMonth param | ||
* @param {Date} dateInMonth | ||
* @returns {Date[]} Array of days in the month in question | ||
*/ | ||
var version = "4.0.1"; | ||
function daysInMonth(dateInMonth) { | ||
var date = new Date(dateInMonth); | ||
date.setDate(1); | ||
var month = date.getMonth(); | ||
var days = []; | ||
while (date.getMonth() === month) { | ||
days.push(new Date(date)); | ||
date.setDate(date.getDate() + 1); | ||
} | ||
return days; | ||
} | ||
var version = "4.0.2"; | ||
/** | ||
@@ -653,0 +718,0 @@ * closest |
@@ -1,3 +0,3 @@ | ||
/*! @nrk/core-datepicker v4.0.1 - Copyright (c) 2017-2022 NRK */ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).coreDatepicker=e()}(this,(function(){"use strict";function t(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function e(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function n(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function r(t){return r=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},r(t)}function i(t,e){return i=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},i(t,e)}function o(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}function a(t,e,n){return a=o()?Reflect.construct.bind():function(t,e,n){var r=[null];r.push.apply(r,e);var o=new(Function.bind.apply(t,r));return n&&i(o,n.prototype),o},a.apply(null,arguments)}function u(t){var e="function"==typeof Map?new Map:void 0;return u=function(t){if(null===t||(n=t,-1===Function.toString.call(n).indexOf("[native code]")))return t;var n;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,o)}function o(){return a(t,arguments,r(this).constructor)}return o.prototype=Object.create(t.prototype,{constructor:{value:o,enumerable:!1,writable:!0,configurable:!0}}),i(o,t)},u(t)}function s(t,e){if(e&&("object"==typeof e||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}var c="undefined"!=typeof window,l=c&&void 0!==window.navigator;l&&/(android)/i.test(navigator.userAgent),l&&/iPad|iPhone|iPod/.test(String(navigator.platform)),c||global.HTMLElement||(global.HTMLElement=function(){return n((function e(){t(this,e)}))}());var d,f,h=(d="undefined"==typeof window?{}:window.Element.prototype,f=d.matches||d.msMatchesSelector||d.webkitMatchesSelector,d.closest?function(t,e){return t.closest(e)}:function(t,e){for(t.correspondingUseElement&&(t=t.correspondingUseElement);t;t=t.parentElement)if(f.call(t,e))return t;return null});function p(t,e){var n,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},i="prevent_recursive_dispatch_maximum_callstack".concat(e);if(t[i])return!0;t[i]=!0,"function"==typeof window.CustomEvent?n=new window.CustomEvent(e,{bubbles:!0,cancelable:!0,detail:r}):(n=document.createEvent("CustomEvent")).initCustomEvent(e,!0,!0,r);var o=t.dispatchEvent(n);return t[i]=null,o}function y(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:document;if(t){if(t.nodeType)return[t];if("string"==typeof t)return[].slice.call(e.querySelectorAll(t));if(t.length)return[].slice.call(t)}return[]}"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self&&self;var m={exports:{}};!function(t,e){var n,r,i,o;t.exports=(n={year:"FullYear",month:"Month",week:"Date",day:"Date",hour:"Hours",minute:"Minutes",second:"Seconds"},r=/([+-]\s*\d+)\s*(second|minute|hour|day|week|month|year)|(mon)|(tue)|(wed)|(thu)|(fri)|(sat)|(sun)/g,i=/([-\dy]+)[-/.]([\dm]+)[-/.]([\dd]+)/,o=/([\dh]+):([\dm]+):?([\ds]+)?/,function(t,e){if(isFinite(t))return new Date(Number(t));var a=String(t).toLowerCase(),u=new Date(isFinite(e)&&-1===a.indexOf("now")?Number(e):Date.now()),s=a.match(i)||[],c=s[1];void 0===c&&(c="y");var l=s[2];void 0===l&&(l="m");var d=s[3];void 0===d&&(d="d");var f=a.match(o)||[],h=f[1];void 0===h&&(h="h");var p=f[2];void 0===p&&(p="m");var y=f[3];void 0===y&&(y="s");var m={year:c,month:l,day:d,hour:h,minute:p,second:y};Object.keys(m).forEach((function(t){var e="month"===t?1:0,r=String(u["get"+n[t]]()+e);m[t]=m[t].replace(/[^-\d]+/g,(function(t,e,n){return e?r.substr(r.length-n.length+e,t.length):r.substr(0,Math.max(0,r.length-n.length+t.length))}))-e}));var b=new Date(m.year,Math.min(12,m.month+1),0).getDate();for(u.setFullYear(m.year,Math.min(11,m.month),Math.max(1,Math.min(b,m.day))),u.setHours(Math.min(24,m.hour),Math.min(59,m.minute),Math.min(59,m.second));null!==(m=r.exec(a));){var g=m[2],v=String(m[1]).replace(/\s/g,"")*("week"===g?7:1),w=m.slice(2).indexOf(m[0]),k=u.getDate();g?u["set"+n[g]](u["get"+n[g]]()+v):u.setDate(u.getDate()-(u.getDay()||7)+w),"month"!==g&&"year"!==g||k===u.getDate()||u.setDate(0)}return u})}(m);var b=m.exports,g={year:"*-m-d",month:"y-*-d",day:"y-m-*",hour:"*:m",minute:"h:*",second:"h:m:*",timestamp:"*",null:"*"},v={33:"-1month",34:"+1month",35:"y-m-99",36:"y-m-1",37:"-1day",38:"-1week",39:"+1day",40:"+1week"},w=function(e){!function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&i(t,e)}(l,e);var a,u,c=(a=l,u=o(),function(){var t,e=r(a);if(u){var n=r(this).constructor;t=Reflect.construct(e,arguments,n)}else t=e.apply(this,arguments);return s(this,t)});function l(){return t(this,l),c.apply(this,arguments)}return n(l,[{key:"connectedCallback",value:function(){var t,e,n,r,i=this;this._date=this.date,document.addEventListener("click",this),document.addEventListener("change",this),document.addEventListener("keydown",this),setTimeout((function(){return i.attributeChangedCallback()})),t=this.nodeName,e="".concat(this.nodeName,"{display:block}"),n="style-".concat(t.toLowerCase()),r=e.replace(/\/\*[^!][^*]*\*\//g,"").replace(/\s*(^|[:;,{}]|$)\s*/g,"$1"),document.getElementById(n)||document.head.insertAdjacentHTML("afterbegin",'<style id="'.concat(n,'">').concat(r,"</style>"))}},{key:"disconnectedCallback",value:function(){this._date=this._disabled=null,document.removeEventListener("click",this),document.removeEventListener("change",this),document.removeEventListener("keydown",this)}},{key:"attributeChangedCallback",value:function(t,e,n){if(this.parentNode){if(this.disabled(this.date)&&!this.disabled(this._date))return this.date=this._date;(this.diff(this.date)||null===e&&"0"===n||"0"===e&&null===n)&&p(this,"datepicker.change",this._date=this.date),_("button",this,E),_("select",this,j),_("input",this,A),_("table",this,M)}}},{key:"handleEvent",value:function(t){if(!(t.defaultPrevented||t.ctrlKey||t.metaKey||t.shiftKey||t.altKey||"keydown"===t.type&&!v[t.keyCode])&&(this.contains(t.target)||h(t.target,'[for="'.concat(this.id,'"],[data-for="').concat(this.id,'"]'))))if("change"===t.type)this.date=g[t.target.getAttribute("data-type")].replace("*",t.target.value);else if("click"===t.type){var e=h(t.target,"button[value]"),n=h(t.target,"table");e&&(this.date=e.value),e&&n&&p(this,"datepicker.click.day")}else if("keydown"===t.type){var r=h(t.target,"table");r&&(this.date=v[t.keyCode],r.querySelector("[autofocus]").focus(),t.preventDefault())}}},{key:"diff",value:function(t){return this.parse(t).getTime()-this.timestamp}},{key:"parse",value:function(t,e){return b(t,e||this._date||Date.now())}},{key:"disabled",get:function(){return this._disabled||Function.prototype},set:function(t){var e=this;this._disabled="function"!=typeof t?function(){return Boolean(t)}:function(n){return null!==n&&t(e.parse(n),e)},this.attributeChangedCallback()}},{key:"timestamp",get:function(){return this._date?this._date.getTime():null}},{key:"year",get:function(){return this._date?String(this._date.getFullYear()):null}},{key:"month",get:function(){return this._date?k(this._date.getMonth()+1):null}},{key:"day",get:function(){return this._date?k(this._date.getDate()):null}},{key:"hour",get:function(){return this._date?k(this._date.getHours()):null}},{key:"minute",get:function(){return this._date?k(this._date.getMinutes()):null}},{key:"second",get:function(){return this._date?k(this._date.getSeconds()):null}},{key:"date",get:function(){var t=this.getAttribute("date");if(!t){if(!(t=this.getAttribute("timestamp")))return null;this.removeAttribute("timestamp"),console.warn(this,"uses deprecated `timestamp` attribute. Please use `date` as specified in the docs (https://static.nrk.no/core-components/latest/index.html?core-datepicker/readme.md). Note that the attribute has been removed to avoid confusion with the `date` attribute")}return this.parse(t)},set:function(t){return null===t?this.removeAttribute("date"):this.setAttribute("date",this.parse(t).getTime())}},{key:"months",get:function(){return(this.getAttribute("months")||"januar,februar,mars,april,mai,juni,juli,august,september,oktober,november,desember").split(/\s*,\s*/)},set:function(t){this.setAttribute("months",[].concat(t).join(","))}},{key:"days",get:function(){return(this.getAttribute("days")||"man,tirs,ons,tors,fre,lør,søn").split(/\s*,\s*/)},set:function(t){this.setAttribute("days",[].concat(t).join(","))}}],[{key:"observedAttributes",get:function(){return["date","months","days"]}}]),l}(u(HTMLElement)),k=function(t){return"0".concat(t).slice(-2)},_=function(t,e,n){return[].forEach.call(document.getElementsByTagName(t),(function(t){(e.contains(t)||e.id===(t.getAttribute("data-for")||t.getAttribute("for")))&&n(e,t)}))};function E(t,e){e.value&&(e.type="button",e.disabled=t.disabled(e.value))}function A(t,e){var n=e.getAttribute("data-type")||e.getAttribute("type");"radio"===n||"checkbox"===n?(e.disabled=t.disabled(e.value),e.checked=!t.diff(e.value)):g[n]&&(e.setAttribute("type","number"),e.setAttribute("data-type",n),e.value=t[n])}function M(t,e){e.firstElementChild||(e.innerHTML="\n <caption></caption><thead><tr>".concat(Array(8).join("</th><th>"),"</tr></thead>\n <tbody>").concat(Array(7).join("<tr>".concat(Array(8).join('<td><button type="button"></button></td>'),"</tr>")),"</tbody>"));var n=new Date,r=t._date||n,i=r.getMonth(),o=t.parse("y-m-1 mon",r);e.caption.textContent="".concat(t.months[i],", ").concat(r.getFullYear()),y("th",e).forEach((function(e,n){return e.textContent=t.days[n]})),y("button",e).forEach((function(e){var r=o.getDate()===n.getDate()&&o.getMonth()===n.getMonth()&&o.getFullYear()===n.getFullYear(),a=null===t._date?r:!t.diff(o),u=o.getDate(),s=o.getMonth();e.textContent=u,e.value="".concat(o.getFullYear(),"-").concat(s+1,"-").concat(u),e.disabled=t.disabled(o),e.setAttribute("tabindex",Number(a)-1),e.setAttribute("data-adjacent",i!==s),e.setAttribute("aria-label","".concat(u,". ").concat(t.months[s])),e.setAttribute("aria-current",r&&"date"),function(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:!this.hasAttribute(e);!n===t.hasAttribute(e)&&t[n?"setAttribute":"removeAttribute"](e,"")}(e,"autofocus",a),o.setDate(u+1)}))}function j(t,e){e.firstElementChild||(e._autofill=!0,e.innerHTML=t.months.map((function(t,e){return'<option value="y-'.concat(e+1,'-d"></option>')})).join("")),y(e.children).forEach((function(n,r){e._autofill&&(n.textContent=t.months[r]),n.disabled=t.disabled(n.value),n.selected=!t.diff(n.value)}))}return w})),window.customElements.define("core-datepicker",coreDatepicker); | ||
/*! @nrk/core-datepicker v4.0.2 - Copyright (c) 2017-2022 NRK */ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).coreDatepicker=e()}(this,(function(){"use strict";function t(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function e(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function n(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function r(t){return r=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},r(t)}function i(t,e){return i=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},i(t,e)}function o(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}function a(t,e,n){return a=o()?Reflect.construct.bind():function(t,e,n){var r=[null];r.push.apply(r,e);var o=new(Function.bind.apply(t,r));return n&&i(o,n.prototype),o},a.apply(null,arguments)}function u(t){var e="function"==typeof Map?new Map:void 0;return u=function(t){if(null===t||(n=t,-1===Function.toString.call(n).indexOf("[native code]")))return t;var n;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,o)}function o(){return a(t,arguments,r(this).constructor)}return o.prototype=Object.create(t.prototype,{constructor:{value:o,enumerable:!1,writable:!0,configurable:!0}}),i(o,t)},u(t)}function s(t,e){if(e&&("object"==typeof e||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}var c="undefined"!=typeof window,l=c&&void 0!==window.navigator;l&&/(android)/i.test(navigator.userAgent),l&&/iPad|iPhone|iPod/.test(String(navigator.platform)),c||global.HTMLElement||(global.HTMLElement=function(){return n((function e(){t(this,e)}))}());var d,f,h=(d="undefined"==typeof window?{}:window.Element.prototype,f=d.matches||d.msMatchesSelector||d.webkitMatchesSelector,d.closest?function(t,e){return t.closest(e)}:function(t,e){for(t.correspondingUseElement&&(t=t.correspondingUseElement);t;t=t.parentElement)if(f.call(t,e))return t;return null});function p(t,e){var n,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},i="prevent_recursive_dispatch_maximum_callstack".concat(e);if(t[i])return!0;t[i]=!0,"function"==typeof window.CustomEvent?n=new window.CustomEvent(e,{bubbles:!0,cancelable:!0,detail:r}):(n=document.createEvent("CustomEvent")).initCustomEvent(e,!0,!0,r);var o=t.dispatchEvent(n);return t[i]=null,o}function m(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:document;if(t){if(t.nodeType)return[t];if("string"==typeof t)return[].slice.call(e.querySelectorAll(t));if(t.length)return[].slice.call(t)}return[]}"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self&&self;var y={exports:{}};!function(t,e){var n,r,i,o;t.exports=(n={year:"FullYear",month:"Month",week:"Date",day:"Date",hour:"Hours",minute:"Minutes",second:"Seconds"},r=/([+-]\s*\d+)\s*(second|minute|hour|day|week|month|year)|(mon)|(tue)|(wed)|(thu)|(fri)|(sat)|(sun)/g,i=/([-\dy]+)[-/.]([\dm]+)[-/.]([\dd]+)/,o=/([\dh]+):([\dm]+):?([\ds]+)?/,function(t,e){if(isFinite(t))return new Date(Number(t));var a=String(t).toLowerCase(),u=new Date(isFinite(e)&&-1===a.indexOf("now")?Number(e):Date.now()),s=a.match(i)||[],c=s[1];void 0===c&&(c="y");var l=s[2];void 0===l&&(l="m");var d=s[3];void 0===d&&(d="d");var f=a.match(o)||[],h=f[1];void 0===h&&(h="h");var p=f[2];void 0===p&&(p="m");var m=f[3];void 0===m&&(m="s");var y={year:c,month:l,day:d,hour:h,minute:p,second:m};Object.keys(y).forEach((function(t){var e="month"===t?1:0,r=String(u["get"+n[t]]()+e);y[t]=y[t].replace(/[^-\d]+/g,(function(t,e,n){return e?r.substr(r.length-n.length+e,t.length):r.substr(0,Math.max(0,r.length-n.length+t.length))}))-e}));var b=new Date(y.year,Math.min(12,y.month+1),0).getDate();for(u.setFullYear(y.year,Math.min(11,y.month),Math.max(1,Math.min(b,y.day))),u.setHours(Math.min(24,y.hour),Math.min(59,y.minute),Math.min(59,y.second));null!==(y=r.exec(a));){var g=y[2],v=String(y[1]).replace(/\s/g,"")*("week"===g?7:1),w=y.slice(2).indexOf(y[0]),k=u.getDate();g?u["set"+n[g]](u["get"+n[g]]()+v):u.setDate(u.getDate()-(u.getDay()||7)+w),"month"!==g&&"year"!==g||k===u.getDate()||u.setDate(0)}return u})}(y);var b=y.exports,g={month:function(t,e){return t.disabled(e)&&x(t.parse(e)).filter((function(e){return!t.disabled(e)}))[0]||e},null:function(t,e){return e}},v={month:function(t,e){return x(t.parse(e)).map((function(e){return t.disabled(e)})).reduce((function(t,e){return t&&e}))},null:function(t,e){return t.disabled(e)}},w={year:"*-m-d",month:"y-*-d",day:"y-m-*",hour:"*:m",minute:"h:*",second:"h:m:*",timestamp:"*",null:"*"},k={33:"-1month",34:"+1month",35:"y-m-99",36:"y-m-1",37:"-1day",38:"-1week",39:"+1day",40:"+1week"},_=function(e){!function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&i(t,e)}(l,e);var a,u,c=(a=l,u=o(),function(){var t,e=r(a);if(u){var n=r(this).constructor;t=Reflect.construct(e,arguments,n)}else t=e.apply(this,arguments);return s(this,t)});function l(){return t(this,l),c.apply(this,arguments)}return n(l,[{key:"connectedCallback",value:function(){var t,e,n,r,i=this;this._date=this.date,document.addEventListener("click",this),document.addEventListener("change",this),document.addEventListener("keydown",this),setTimeout((function(){return i.attributeChangedCallback()})),t=this.nodeName,e="".concat(this.nodeName,"{display:block}"),n="style-".concat(t.toLowerCase()),r=e.replace(/\/\*[^!][^*]*\*\//g,"").replace(/\s*(^|[:;,{}]|$)\s*/g,"$1"),document.getElementById(n)||document.head.insertAdjacentHTML("afterbegin",'<style id="'.concat(n,'">').concat(r,"</style>"))}},{key:"disconnectedCallback",value:function(){this._date=this._disabled=null,document.removeEventListener("click",this),document.removeEventListener("change",this),document.removeEventListener("keydown",this)}},{key:"attributeChangedCallback",value:function(t,e,n){if(this.parentNode){if(this.disabled(this.date)&&!this.disabled(this._date))return this.date=this._date;(this.diff(this.date)||null===e&&"0"===n||"0"===e&&null===n)&&p(this,"datepicker.change",this._date=this.date),A("button",this,D),A("select",this,C),A("input",this,M),A("table",this,j)}}},{key:"handleEvent",value:function(t){if(!(t.defaultPrevented||t.ctrlKey||t.metaKey||t.shiftKey||t.altKey||"keydown"===t.type&&!k[t.keyCode])&&(this.contains(t.target)||h(t.target,'[for="'.concat(this.id,'"],[data-for="').concat(this.id,'"]'))))if("change"===t.type){var e=w[t.target.getAttribute("data-type")].replace("*",t.target.value);this.date=g[t.target.getAttribute("data-fill")](this,e)}else if("click"===t.type){var n=h(t.target,"button[value]"),r=h(t.target,"table");n&&(this.date=n.value),n&&r&&p(this,"datepicker.click.day")}else if("keydown"===t.type){var i=h(t.target,"table");i&&(this.date=k[t.keyCode],i.querySelector("[autofocus]").focus(),t.preventDefault())}}},{key:"diff",value:function(t){return this.parse(t).getTime()-this.timestamp}},{key:"parse",value:function(t,e){return b(t,e||this._date||Date.now())}},{key:"disabled",get:function(){return this._disabled||Function.prototype},set:function(t){var e=this;this._disabled="function"!=typeof t?function(){return Boolean(t)}:function(n){return null!==n&&t(e.parse(n),e)},this.attributeChangedCallback()}},{key:"timestamp",get:function(){return this._date?this._date.getTime():null}},{key:"year",get:function(){return this._date?String(this._date.getFullYear()):null}},{key:"month",get:function(){return this._date?E(this._date.getMonth()+1):null}},{key:"day",get:function(){return this._date?E(this._date.getDate()):null}},{key:"hour",get:function(){return this._date?E(this._date.getHours()):null}},{key:"minute",get:function(){return this._date?E(this._date.getMinutes()):null}},{key:"second",get:function(){return this._date?E(this._date.getSeconds()):null}},{key:"date",get:function(){var t=this.getAttribute("date");if(!t){if(!(t=this.getAttribute("timestamp")))return null;this.removeAttribute("timestamp"),console.warn(this,"uses deprecated `timestamp` attribute. Please use `date` as specified in the docs (https://static.nrk.no/core-components/latest/index.html?core-datepicker/readme.md). Note that the attribute has been removed to avoid confusion with the `date` attribute")}return this.parse(t)},set:function(t){return null===t?this.removeAttribute("date"):this.setAttribute("date",this.parse(t).getTime())}},{key:"months",get:function(){return(this.getAttribute("months")||"januar,februar,mars,april,mai,juni,juli,august,september,oktober,november,desember").split(/\s*,\s*/)},set:function(t){this.setAttribute("months",[].concat(t).join(","))}},{key:"days",get:function(){return(this.getAttribute("days")||"man,tirs,ons,tors,fre,lør,søn").split(/\s*,\s*/)},set:function(t){this.setAttribute("days",[].concat(t).join(","))}}],[{key:"observedAttributes",get:function(){return["date","months","days"]}}]),l}(u(HTMLElement)),E=function(t){return"0".concat(t).slice(-2)},A=function(t,e,n){return[].forEach.call(document.getElementsByTagName(t),(function(t){(e.contains(t)||e.id===(t.getAttribute("data-for")||t.getAttribute("for")))&&n(e,t)}))};function D(t,e){e.value&&(e.type="button",e.disabled=t.disabled(e.value))}function M(t,e){var n=e.getAttribute("data-type")||e.getAttribute("type");"radio"===n||"checkbox"===n?(e.disabled=t.disabled(e.value),e.checked=!t.diff(e.value)):w[n]&&(e.setAttribute("type","number"),e.setAttribute("data-type",n),e.value=t[n])}function j(t,e){e.firstElementChild||(e.innerHTML="\n <caption></caption><thead><tr>".concat(Array(8).join("</th><th>"),"</tr></thead>\n <tbody>").concat(Array(7).join("<tr>".concat(Array(8).join('<td><button type="button"></button></td>'),"</tr>")),"</tbody>"));var n=new Date,r=t._date||n,i=r.getMonth(),o=t.parse("y-m-1 mon",r);e.caption.textContent="".concat(t.months[i],", ").concat(r.getFullYear()),m("th",e).forEach((function(e,n){return e.textContent=t.days[n]})),m("button",e).forEach((function(e){var r=o.getDate()===n.getDate()&&o.getMonth()===n.getMonth()&&o.getFullYear()===n.getFullYear(),a=null===t._date?r:!t.diff(o),u=o.getDate(),s=o.getMonth();e.textContent=u,e.value="".concat(o.getFullYear(),"-").concat(s+1,"-").concat(u),e.disabled=t.disabled(o),e.setAttribute("tabindex",Number(a)-1),e.setAttribute("data-adjacent",i!==s),e.setAttribute("aria-label","".concat(u,". ").concat(t.months[s])),e.setAttribute("aria-current",r&&"date"),function(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:!this.hasAttribute(e);!n===t.hasAttribute(e)&&t[n?"setAttribute":"removeAttribute"](e,"")}(e,"autofocus",a),o.setDate(u+1)}))}function C(t,e){e.firstElementChild||(e._autofill=!0,e.setAttribute("data-fill","month"),e.innerHTML=t.months.map((function(t,e){return'<option value="y-'.concat(e+1,'-d"></option>')})).join(""));var n=v[e.getAttribute("data-fill")];m(e.children).forEach((function(r,i){e._autofill&&(r.textContent=t.months[i]),r.disabled=n(t,r.value),r.selected=!t.diff(r.value)}))}function x(t){var e=new Date(t);e.setDate(1);for(var n=e.getMonth(),r=[];e.getMonth()===n;)r.push(new Date(e)),e.setDate(e.getDate()+1);return r}return _})),window.customElements.define("core-datepicker",coreDatepicker); | ||
//# sourceMappingURL=core-datepicker.min.js.map |
@@ -1,2 +0,2 @@ | ||
/*! @nrk/core-datepicker v4.0.1 - Copyright (c) 2017-2022 NRK */ | ||
/*! @nrk/core-datepicker v4.0.2 - Copyright (c) 2017-2022 NRK */ | ||
'use strict'; | ||
@@ -1379,2 +1379,90 @@ | ||
}))); | ||
it('has month enabled if one day is disabled', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee20() { | ||
return _regeneratorRuntime().wrap(function _callee20$(_context20) { | ||
while (1) { | ||
switch (_context20.prev = _context20.next) { | ||
case 0: | ||
_context20.next = 2; | ||
return browser.executeScript(function () { | ||
document.body.innerHTML = "\n <core-datepicker date=\"".concat(new Date('2019-05-06').getTime(), "\">\n <select></select>\n </core-datepicker>\n "); | ||
var disabledDate = new Date('2019-09-06'); | ||
document.querySelector('core-datepicker').disabled = function (date) { | ||
return date.valueOf() === disabledDate.valueOf(); | ||
}; | ||
}); | ||
case 2: | ||
_context20.next = 4; | ||
return expect(prop('option[value="y-9-d"]', 'disabled')).toEqual('false'); | ||
case 4: | ||
case "end": | ||
return _context20.stop(); | ||
} | ||
} | ||
}, _callee20); | ||
}))); | ||
it('has month disabled if all days are disabled', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee21() { | ||
return _regeneratorRuntime().wrap(function _callee21$(_context21) { | ||
while (1) { | ||
switch (_context21.prev = _context21.next) { | ||
case 0: | ||
_context21.next = 2; | ||
return browser.executeScript(function () { | ||
document.body.innerHTML = "\n <core-datepicker date=\"".concat(new Date('2019-05-06').getTime(), "\">\n <select></select>\n </core-datepicker>\n "); | ||
document.querySelector('core-datepicker').disabled = function (date) { | ||
return date.getMonth() === 8; | ||
}; | ||
}); | ||
case 2: | ||
_context21.next = 4; | ||
return expect(prop('option[value="y-9-d"]', 'disabled')).toEqual('true'); | ||
case 4: | ||
case "end": | ||
return _context21.stop(); | ||
} | ||
} | ||
}, _callee21); | ||
}))); | ||
it('selects first available date in month', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee22() { | ||
var browserDate; | ||
return _regeneratorRuntime().wrap(function _callee22$(_context22) { | ||
while (1) { | ||
switch (_context22.prev = _context22.next) { | ||
case 0: | ||
_context22.next = 2; | ||
return browser.executeScript(function () { | ||
document.body.innerHTML = "\n <core-datepicker date=\"".concat(new Date('2019-12-09T00:00:00.00Z').getTime(), "\">\n <select></select>\n <input type=\"timestamp\">\n </core-datepicker>\n "); | ||
document.querySelector('core-datepicker').disabled = function (date) { | ||
return date.getMonth() === 10 && !(date < new Date('2019-11-06') && date > new Date('2019-11-03')); | ||
}; | ||
}); | ||
case 2: | ||
_context22.next = 4; | ||
return $('option[value="y-11-d"]').click(); | ||
case 4: | ||
_context22.next = 6; | ||
return browser.executeScript(function () { | ||
return new Date('2019-11-04T00:00:00.00Z').getTime(); | ||
}); | ||
case 6: | ||
browserDate = _context22.sent; | ||
_context22.next = 9; | ||
return expect(prop('input[data-type="timestamp"]', 'value')).toEqual(String(browserDate)); | ||
case 9: | ||
case "end": | ||
return _context22.stop(); | ||
} | ||
} | ||
}, _callee22); | ||
}))); | ||
}); |
@@ -355,2 +355,49 @@ import parse from '@nrk/simple-date-parse' | ||
}) | ||
it('has month enabled if one day is disabled', async () => { | ||
await browser.executeScript(() => { | ||
document.body.innerHTML = ` | ||
<core-datepicker date="${new Date('2019-05-06').getTime()}"> | ||
<select></select> | ||
</core-datepicker> | ||
` | ||
const disabledDate = new Date('2019-09-06') | ||
document.querySelector('core-datepicker').disabled = (date) => { | ||
return date.valueOf() === disabledDate.valueOf() | ||
} | ||
}) | ||
await expect(prop('option[value="y-9-d"]', 'disabled')).toEqual('false') | ||
}) | ||
it('has month disabled if all days are disabled', async () => { | ||
await browser.executeScript(() => { | ||
document.body.innerHTML = ` | ||
<core-datepicker date="${new Date('2019-05-06').getTime()}"> | ||
<select></select> | ||
</core-datepicker> | ||
` | ||
document.querySelector('core-datepicker').disabled = (date) => { | ||
return date.getMonth() === 8 | ||
} | ||
}) | ||
await expect(prop('option[value="y-9-d"]', 'disabled')).toEqual('true') | ||
}) | ||
it('selects first available date in month', async () => { | ||
await browser.executeScript(() => { | ||
document.body.innerHTML = ` | ||
<core-datepicker date="${new Date('2019-12-09T00:00:00.00Z').getTime()}"> | ||
<select></select> | ||
<input type="timestamp"> | ||
</core-datepicker> | ||
` | ||
document.querySelector('core-datepicker').disabled = (date) => { | ||
return date.getMonth() === 10 && !(date < new Date('2019-11-06') && date > new Date('2019-11-03')) | ||
} | ||
}) | ||
await $('option[value="y-11-d"]').click() | ||
const browserDate = await browser.executeScript(() => new Date('2019-11-04T00:00:00.00Z').getTime()) | ||
await expect(prop('input[data-type="timestamp"]', 'value')).toEqual(String(browserDate)) | ||
}) | ||
}) |
75
jsx.js
@@ -1,2 +0,2 @@ | ||
/*! @nrk/core-datepicker v4.0.1 - Copyright (c) 2017-2022 NRK */ | ||
/*! @nrk/core-datepicker v4.0.2 - Copyright (c) 2017-2022 NRK */ | ||
'use strict'; | ||
@@ -360,2 +360,38 @@ | ||
/** | ||
* Handlers to fill in date value depending on type of value (as key) selected | ||
* e.g. resolve if same day in month can be filled when selecting next month | ||
*/ | ||
var FILL = { | ||
month: function month(self, value) { | ||
if (!self.disabled(value)) return value; | ||
var firstAvailableDate = daysInMonth(self.parse(value)).filter(function (day) { | ||
return !self.disabled(day); | ||
})[0]; | ||
return firstAvailableDate || value; | ||
}, | ||
"null": function _null(_self, value) { | ||
return value; | ||
} | ||
}; | ||
/** | ||
* Handlers to resolve if entity with type of value (as key) is disabled | ||
* e.g. resolve if a month can be selected or is disabled | ||
*/ | ||
var DISABLED = { | ||
month: function month(self, value) { | ||
var allDays = daysInMonth(self.parse(value)); | ||
var allDaysDisabled = allDays.map(function (day) { | ||
return self.disabled(day); | ||
}).reduce(function (a, b) { | ||
return a && b; | ||
}); | ||
return allDaysDisabled; | ||
}, | ||
"null": function _null(self, value) { | ||
return self.disabled(value); | ||
} | ||
}; | ||
var MASK = { | ||
@@ -442,3 +478,4 @@ year: '*-m-d', | ||
if (event.type === 'change') { | ||
this.date = MASK[event.target.getAttribute('data-type')].replace('*', event.target.value); | ||
var changeMask = MASK[event.target.getAttribute('data-type')].replace('*', event.target.value); | ||
this.date = FILL[event.target.getAttribute('data-fill')](this, changeMask); | ||
} else if (event.type === 'click') { | ||
@@ -630,7 +667,14 @@ var button = closest$1(event.target, 'button[value]'); | ||
} | ||
/** | ||
* | ||
* @param {CoreDatepicker} self | ||
* @param {HTMLSelectElement} select | ||
*/ | ||
function setupSelect(self, select) { | ||
if (!select.firstElementChild) { | ||
select._autofill = true; | ||
select.innerHTML = self.months.map(function (name, month) { | ||
select.setAttribute('data-fill', 'month'); | ||
select.innerHTML = self.months.map(function (_, month) { | ||
return "<option value=\"y-".concat(month + 1, "-d\"></option>"); | ||
@@ -640,11 +684,32 @@ }).join(''); | ||
var disabled = DISABLED[select.getAttribute('data-fill')]; | ||
queryAll(select.children).forEach(function (option, month) { | ||
if (select._autofill) option.textContent = self.months[month]; | ||
option.disabled = self.disabled(option.value); | ||
option.disabled = disabled(self, option.value); | ||
option.selected = !self.diff(option.value); | ||
}); | ||
} | ||
/** | ||
* Returns array of days in the month containing the dateInMonth param | ||
* @param {Date} dateInMonth | ||
* @returns {Date[]} Array of days in the month in question | ||
*/ | ||
var version = "4.0.1"; | ||
function daysInMonth(dateInMonth) { | ||
var date = new Date(dateInMonth); | ||
date.setDate(1); | ||
var month = date.getMonth(); | ||
var days = []; | ||
while (date.getMonth() === month) { | ||
days.push(new Date(date)); | ||
date.setDate(date.getDate() + 1); | ||
} | ||
return days; | ||
} | ||
var version = "4.0.2"; | ||
/** | ||
@@ -651,0 +716,0 @@ * closest |
@@ -5,3 +5,3 @@ { | ||
"author": "NRK <opensource@nrk.no> (https://www.nrk.no/)", | ||
"version": "4.0.1", | ||
"version": "4.0.2", | ||
"license": "MIT", | ||
@@ -8,0 +8,0 @@ "main": "core-datepicker.cjs.js", |
@@ -169,3 +169,4 @@ # Core Datepicker | ||
document.getElementById('my-datepicker').disabled = (date) => { | ||
var oneWeekFromNow = (new Date()).setDate(new Date().getDate() + 7) | ||
var oneWeekFromNow = new Date() | ||
oneWeekFromNow.setDate(new Date().getDate() + 7) | ||
return date > oneWeekFromNow | ||
@@ -172,0 +173,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
275368
7.39%3947
9.58%445
0.23%