@nrk/core-input
Advanced tools
Comparing version 1.2.0 to 1.2.1
'use strict'; | ||
var name = "@nrk/core-input"; | ||
var version = "1.1.3"; | ||
var version = "1.2.0"; | ||
@@ -187,3 +187,3 @@ var IS_BROWSER = typeof window !== 'undefined'; | ||
function onFilter (input, detail) { | ||
if (dispatchEvent(input, 'input.filter', detail) && !ajax(input)) { | ||
if (dispatchEvent(input, 'input.filter', detail) && ajax(input) === false) { | ||
queryAll(ITEM, input.nextElementSibling).reduce(function (acc, item) { | ||
@@ -217,17 +217,17 @@ var list = item.parentElement.nodeName === 'LI' && item.parentElement; | ||
var url = input.getAttribute(UUID); | ||
var req = ajax.req = ajax.req || new window.XMLHttpRequest(); | ||
var xhr = ajax.xhr = ajax.xhr || new window.XMLHttpRequest(); | ||
if (!url) { return false } | ||
clearTimeout(ajax.timer); // Clear previous search | ||
req.abort(); // Abort previous request | ||
req.onload = function () { | ||
try { req.responseJSON = JSON.parse(req.responseText); } catch (err) { req.responseJSON = false; } | ||
dispatchEvent(input, 'input.ajax', req); | ||
xhr.abort(); // Abort previous request | ||
xhr.onload = function () { | ||
try { xhr.responseJSON = JSON.parse(xhr.responseText); } catch (err) { xhr.responseJSON = false; } | ||
dispatchEvent(input, 'input.ajax', xhr); | ||
}; | ||
ajax.timer = setTimeout(function () { | ||
if (!input.value) { return } // Abort if input is empty | ||
if (dispatchEvent(input, 'input.ajax.beforeSend', req)) { | ||
req.open('GET', url.replace('{{value}}', window.encodeURIComponent(input.value)), true); | ||
req.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); // https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Requested-With | ||
req.send(); | ||
if (dispatchEvent(input, 'input.ajax.beforeSend', xhr)) { | ||
xhr.open('GET', url.replace('{{value}}', window.encodeURIComponent(input.value)), true); | ||
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); // https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Requested-With | ||
xhr.send(); | ||
} | ||
@@ -234,0 +234,0 @@ }, AJAX_DEBOUNCE); // Debounce request |
@@ -93,3 +93,3 @@ import { name, version } from './package.json' | ||
function onFilter (input, detail) { | ||
if (dispatchEvent(input, 'input.filter', detail) && !ajax(input)) { | ||
if (dispatchEvent(input, 'input.filter', detail) && ajax(input) === false) { | ||
queryAll(ITEM, input.nextElementSibling).reduce((acc, item) => { | ||
@@ -121,19 +121,19 @@ const list = item.parentElement.nodeName === 'LI' && item.parentElement | ||
const url = input.getAttribute(UUID) | ||
const req = ajax.req = ajax.req || new window.XMLHttpRequest() | ||
const xhr = ajax.xhr = ajax.xhr || new window.XMLHttpRequest() | ||
if (!url) return false | ||
clearTimeout(ajax.timer) // Clear previous search | ||
req.abort() // Abort previous request | ||
req.onload = () => { | ||
try { req.responseJSON = JSON.parse(req.responseText) } catch (err) { req.responseJSON = false } | ||
dispatchEvent(input, 'input.ajax', req) | ||
xhr.abort() // Abort previous request | ||
xhr.onload = () => { | ||
try { xhr.responseJSON = JSON.parse(xhr.responseText) } catch (err) { xhr.responseJSON = false } | ||
dispatchEvent(input, 'input.ajax', xhr) | ||
} | ||
ajax.timer = setTimeout(() => { | ||
if (!input.value) return // Abort if input is empty | ||
if (dispatchEvent(input, 'input.ajax.beforeSend', req)) { | ||
req.open('GET', url.replace('{{value}}', window.encodeURIComponent(input.value)), true) | ||
req.setRequestHeader('X-Requested-With', 'XMLHttpRequest') // https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Requested-With | ||
req.send() | ||
if (dispatchEvent(input, 'input.ajax.beforeSend', xhr)) { | ||
xhr.open('GET', url.replace('{{value}}', window.encodeURIComponent(input.value)), true) | ||
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest') // https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Requested-With | ||
xhr.send() | ||
} | ||
}, AJAX_DEBOUNCE) // Debounce request | ||
} |
@@ -1,3 +0,3 @@ | ||
/*! @nrk/core-input v1.1.3 - Copyright (c) 2017-2018 NRK */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("react"),require("prop-types")):"function"==typeof define&&define.amd?define(["react","prop-types"],t):e.CoreInput=t(e.React,e.PropTypes)}(this,function(a,e){"use strict";a=a&&a.hasOwnProperty("default")?a.default:a,e=e&&e.hasOwnProperty("default")?e.default:e;var t="undefined"!=typeof window,i=(t&&/(android)/i.test(navigator.userAgent),t&&/iPad|iPhone|iPod/.test(String(navigator.platform))),r=function(e){void 0===e&&(e=!1);try{window.addEventListener("test",null,{get passive(){e=!0}})}catch(e){}return e}();function n(e,t,n,o){(void 0===o&&(o=!1),"undefined"==typeof window||window[e=e+"-"+t])||(r||"object"!=typeof o||(o=Boolean(o.capture)),("resize"===t||"load"===t?window:document).addEventListener(window[e]=t,n,o))}var o={"&":"&","<":"<",">":">",'"':""","/":"/","'":"'"};function u(e){return String(e||"").replace(/[&<>"'/]/g,function(e){return o[e]})}var l="prevent_recursive_dispatch_maximum_callstack";function c(e,t,n){void 0===n&&(n={});var o,r=""+l+t;if(e[r])return!0;e[r]=!0,"function"==typeof window.CustomEvent?o=new window.CustomEvent(t,{bubbles:!0,cancelable:!0,detail:n}):(o=document.createEvent("CustomEvent")).initCustomEvent(t,!0,!0,n);var i=e.dispatchEvent(o);return e[r]=null,i}function s(e,t){if(void 0===t&&(t=document),e){if(e.nodeType)return[e];if("string"==typeof e)return[].slice.call(t.querySelectorAll(e));if(e.length)return[].slice.call(e)}return[]}var p="data-@nrk/core-input-1.1.3".replace(/\W+/g,"-"),d=13,f=27,h=33,v=34,m=35,y=36,b=38,g=40,x='[tabindex="-1"]',E=500;function w(e,t){var o="object"==typeof t?t:{content:t},r="string"==typeof o.content;return s(e).map(function(e){var t=e.nextElementSibling,n=void 0===o.ajax?e.getAttribute(p):o.ajax;return e.setAttribute(p,n||""),e.setAttribute(i?"data-role":"role","combobox"),e.setAttribute("aria-autocomplete","list"),e.setAttribute("autocomplete","off"),r&&(t.innerHTML=o.content),s("a,button",t).forEach(C),j(e,o.open),e})}function A(a){a.ctrlKey||a.altKey||a.metaKey||a.defaultPrevented||s("["+p+"]").forEach(function(e){var t,n,o=e.nextElementSibling,r=e===a.target||o.contains(a.target),i="click"===a.type&&r&&s(x,o).filter(function(e){return e.contains(a.target)})[0];i?(t=e,n={relatedTarget:o,currentTarget:i,value:i.value||i.textContent.trim()},c(t,"input.select",n)&&(t.value=n.value,t.focus(),j(t,!1))):j(e,r)})}function S(e,t){var n=e.nextElementSibling,o=[e].concat(s(x+":not([hidden])",n)),r=t.keyCode===f&&"true"===e.getAttribute("aria-expanded"),i=o.indexOf(document.activeElement),a=!1;t.keyCode===g?a=o[i+1]||o[0]:t.keyCode===b?a=o[i-1]||o.pop():n.contains(t.target)&&(t.keyCode===m||t.keyCode===v?a=o.pop():t.keyCode===y||t.keyCode===h?a=o[1]:t.keyCode!==d&&e.focus()),j(e,t.keyCode!==f),(!1!==a||r)&&t.preventDefault(),a&&a.focus()}function j(e,t){var n;void 0===t&&(t="true"===e.getAttribute("aria-expanded")),n=function(){e.nextElementSibling[t?"removeAttribute":"setAttribute"]("hidden",""),e.setAttribute("aria-expanded",t)},(window.requestAnimationFrame||window.setTimeout)(n)}function C(e,t,n){e.setAttribute("aria-label",e.textContent.trim()+", "+(t+1)+" av "+n.length),e.setAttribute("tabindex","-1"),e.setAttribute("type","button")}function k(e){var t=e.getAttribute(p),n=k.req=k.req||new window.XMLHttpRequest;if(!t)return!1;clearTimeout(k.timer),n.abort(),n.onload=function(){try{n.responseJSON=JSON.parse(n.responseText)}catch(e){n.responseJSON=!1}c(e,"input.ajax",n)},k.timer=setTimeout(function(){e.value&&c(e,"input.ajax.beforeSend",n)&&(n.open("GET",t.replace("{{value}}",window.encodeURIComponent(e.value)),!0),n.setRequestHeader("X-Requested-With","XMLHttpRequest"),n.send())},E)}w.escapeHTML=u,w.highlight=function(e,t){var n=t.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&");return u(e).replace(new RegExp(n||".^","gi"),"<mark>$&</mark>")},n(p,"click",A),n(p,"focus",A,!0),n(p,"input",function(e){var i,t,n=e.target;n.hasAttribute(p)&&(t={relatedTarget:(i=n).nextElementSibling},c(i,"input.filter",t)&&!k(i)&&s(x,i.nextElementSibling).reduce(function(e,t){var n="LI"===t.parentElement.nodeName&&t.parentElement,o=-1!==t.textContent.toLowerCase().indexOf(i.value.toLowerCase()),r=o?"removeAttribute":"setAttribute";return n&&n[r]("hidden",""),t[r]("hidden",""),o?e.concat(t):e},[]).forEach(C))}),n(p,"keydown",function(e){if(!(e.ctrlKey||e.altKey||e.metaKey)){if(e.target.hasAttribute(p))return S(e.target,e);for(var t=e.target,n=void 0;t;t=t.parentElement)if((n=t.previousElementSibling)&&n.hasAttribute(p))return S(n,e)}},!0);var L=function(t){function i(e){t.call(this,e),this.onFilter=this.onFilter.bind(this),this.onSelect=this.onSelect.bind(this),this.onAjax=this.onAjax.bind(this)}t&&(i.__proto__=t),(i.prototype=Object.create(t&&t.prototype)).constructor=i;var e={defaultProps:{configurable:!0}};return e.defaultProps.get=function(){return{open:null,ajax:null,onAjax:null,onFilter:null,onSelect:null}},i.prototype.componentDidMount=function(){this.el.addEventListener("input.filter",this.onFilter),this.el.addEventListener("input.select",this.onSelect),this.el.addEventListener("input.ajax",this.onAjax),w(this.el.firstElementChild,this.props)},i.prototype.componentDidUpdate=function(){w(this.el.firstElementChild)},i.prototype.componentWillUnmount=function(){this.el.removeEventListener("input.filter",this.onFilter),this.el.removeEventListener("input.select",this.onSelect),this.el.removeEventListener("input.ajax",this.onAjax)},i.prototype.onFilter=function(e){this.props.onFilter&&this.props.onFilter(e)},i.prototype.onSelect=function(e){this.props.onSelect&&this.props.onSelect(e)},i.prototype.onAjax=function(e){this.props.onAjax&&this.props.onAjax(e)},i.prototype.render=function(){var n,o,e,r=this;return a.createElement("div",(n=this.props,o=i.defaultProps,void 0===(e={ref:function(e){return r.el=e}})&&(e={}),Object.keys(n).reduce(function(e,t){return o.hasOwnProperty(t)||(e[t]=n[t]),e},e)),a.Children.map(this.props.children,function(e,t){return 0===t?a.cloneElement(e,{"aria-expanded":String(Boolean(r.props.open))}):1===t?a.cloneElement(e,{hidden:!r.props.open}):e}))},Object.defineProperties(i,e),i}(a.Component);return L.Highlight=function(e){var t=e.text,n=e.query;return void 0===n&&(n=""),a.createElement("span",{dangerouslySetInnerHTML:{__html:w.highlight(t,n)}})},L.propTypes={onFilter:e.func,onSelect:e.func,onAjax:e.func,open:e.bool,ajax:e.oneOfType([e.string,e.object])},L}); | ||
/*! @nrk/core-input v1.2.0 - Copyright (c) 2017-2019 NRK */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("react"),require("prop-types")):"function"==typeof define&&define.amd?define(["react","prop-types"],t):(e=e||self).CoreInput=t(e.React,e.PropTypes)}(this,function(a,e){"use strict";a=a&&a.hasOwnProperty("default")?a.default:a,e=e&&e.hasOwnProperty("default")?e.default:e;var t="undefined"!=typeof window,i=(t&&/(android)/i.test(navigator.userAgent),t&&/iPad|iPhone|iPod/.test(String(navigator.platform))),r=function(e){void 0===e&&(e=!1);try{window.addEventListener("test",null,{get passive(){e=!0}})}catch(e){}return e}();function n(e,t,n,o){(void 0===o&&(o=!1),"undefined"==typeof window||window[e=e+"-"+t])||(r||"object"!=typeof o||(o=Boolean(o.capture)),("resize"===t||"load"===t?window:document).addEventListener(window[e]=t,n,o))}var o={"&":"&","<":"<",">":">",'"':""","/":"/","'":"'"};function u(e){return String(e||"").replace(/[&<>"'/]/g,function(e){return o[e]})}var l="prevent_recursive_dispatch_maximum_callstack";function s(e,t,n){void 0===n&&(n={});var o,r=""+l+t;if(e[r])return!0;e[r]=!0,"function"==typeof window.CustomEvent?o=new window.CustomEvent(t,{bubbles:!0,cancelable:!0,detail:n}):(o=document.createEvent("CustomEvent")).initCustomEvent(t,!0,!0,n);var i=e.dispatchEvent(o);return e[r]=null,i}function c(e,t){if(void 0===t&&(t=document),e){if(e.nodeType)return[e];if("string"==typeof e)return[].slice.call(t.querySelectorAll(e));if(e.length)return[].slice.call(e)}return[]}var p="data-@nrk/core-input-1.2.0".replace(/\W+/g,"-"),d=13,f=27,h=33,v=34,m=35,y=36,b=38,g=40,x='[tabindex="-1"]',E=500;function w(e,t){var o="object"==typeof t?t:{content:t},r="string"==typeof o.content;return c(e).map(function(e){var t=e.nextElementSibling,n=void 0===o.ajax?e.getAttribute(p):o.ajax;return e.setAttribute(p,n||""),e.setAttribute(i?"data-role":"role","combobox"),e.setAttribute("aria-autocomplete","list"),e.setAttribute("autocomplete","off"),r&&(t.innerHTML=o.content),c("a,button",t).forEach(C),j(e,o.open),e})}function A(a){a.ctrlKey||a.altKey||a.metaKey||a.defaultPrevented||c("["+p+"]").forEach(function(e){var t,n,o=e.nextElementSibling,r=e===a.target||o.contains(a.target),i="click"===a.type&&r&&c(x,o).filter(function(e){return e.contains(a.target)})[0];i?(t=e,n={relatedTarget:o,currentTarget:i,value:i.value||i.textContent.trim()},s(t,"input.select",n)&&(t.value=n.value,t.focus(),j(t,!1))):j(e,r)})}function S(e,t){var n=e.nextElementSibling,o=[e].concat(c(x+":not([hidden])",n)),r=t.keyCode===f&&"true"===e.getAttribute("aria-expanded"),i=o.indexOf(document.activeElement),a=!1;t.keyCode===g?a=o[i+1]||o[0]:t.keyCode===b?a=o[i-1]||o.pop():n.contains(t.target)&&(t.keyCode===m||t.keyCode===v?a=o.pop():t.keyCode===y||t.keyCode===h?a=o[1]:t.keyCode!==d&&e.focus()),j(e,t.keyCode!==f),(!1!==a||r)&&t.preventDefault(),a&&a.focus()}function j(e,t){var n;void 0===t&&(t="true"===e.getAttribute("aria-expanded")),n=function(){e.nextElementSibling[t?"removeAttribute":"setAttribute"]("hidden",""),e.setAttribute("aria-expanded",t)},(window.requestAnimationFrame||window.setTimeout)(n)}function C(e,t,n){e.setAttribute("aria-label",e.textContent.trim()+", "+(t+1)+" av "+n.length),e.setAttribute("tabindex","-1"),e.setAttribute("type","button")}function k(e){var t=e.getAttribute(p),n=k.xhr=k.xhr||new window.XMLHttpRequest;if(!t)return!1;clearTimeout(k.timer),n.abort(),n.onload=function(){try{n.responseJSON=JSON.parse(n.responseText)}catch(e){n.responseJSON=!1}s(e,"input.ajax",n)},k.timer=setTimeout(function(){e.value&&s(e,"input.ajax.beforeSend",n)&&(n.open("GET",t.replace("{{value}}",window.encodeURIComponent(e.value)),!0),n.setRequestHeader("X-Requested-With","XMLHttpRequest"),n.send())},E)}w.escapeHTML=u,w.highlight=function(e,t){var n=t.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&");return u(e).replace(new RegExp(n||".^","gi"),"<mark>$&</mark>")},n(p,"click",A),n(p,"focus",A,!0),n(p,"input",function(e){var i,t,n=e.target;n.hasAttribute(p)&&(t={relatedTarget:(i=n).nextElementSibling},s(i,"input.filter",t)&&!1===k(i)&&c(x,i.nextElementSibling).reduce(function(e,t){var n="LI"===t.parentElement.nodeName&&t.parentElement,o=-1!==t.textContent.toLowerCase().indexOf(i.value.toLowerCase()),r=o?"removeAttribute":"setAttribute";return n&&n[r]("hidden",""),t[r]("hidden",""),o?e.concat(t):e},[]).forEach(C))}),n(p,"keydown",function(e){if(!(e.ctrlKey||e.altKey||e.metaKey)){if(e.target.hasAttribute(p))return S(e.target,e);for(var t=e.target,n=void 0;t;t=t.parentElement)if((n=t.previousElementSibling)&&n.hasAttribute(p))return S(n,e)}},!0);var L=function(t){function i(e){t.call(this,e),this.onFilter=this.onFilter.bind(this),this.onSelect=this.onSelect.bind(this),this.onAjax=this.onAjax.bind(this)}t&&(i.__proto__=t),(i.prototype=Object.create(t&&t.prototype)).constructor=i;var e={defaultProps:{configurable:!0}};return e.defaultProps.get=function(){return{open:null,ajax:null,onAjax:null,onFilter:null,onSelect:null}},i.prototype.componentDidMount=function(){this.el.addEventListener("input.filter",this.onFilter),this.el.addEventListener("input.select",this.onSelect),this.el.addEventListener("input.ajax",this.onAjax),w(this.el.firstElementChild,this.props)},i.prototype.componentDidUpdate=function(){w(this.el.firstElementChild)},i.prototype.componentWillUnmount=function(){this.el.removeEventListener("input.filter",this.onFilter),this.el.removeEventListener("input.select",this.onSelect),this.el.removeEventListener("input.ajax",this.onAjax)},i.prototype.onFilter=function(e){this.props.onFilter&&this.props.onFilter(e)},i.prototype.onSelect=function(e){this.props.onSelect&&this.props.onSelect(e)},i.prototype.onAjax=function(e){this.props.onAjax&&this.props.onAjax(e)},i.prototype.render=function(){var n,o,e,r=this;return a.createElement("div",(n=this.props,o=i.defaultProps,void 0===(e={ref:function(e){return r.el=e}})&&(e={}),Object.keys(n).reduce(function(e,t){return o.hasOwnProperty(t)||(e[t]=n[t]),e},e)),a.Children.map(this.props.children,function(e,t){return 0===t?a.cloneElement(e,{"aria-expanded":String(Boolean(r.props.open))}):1===t?a.cloneElement(e,{hidden:!r.props.open}):e}))},Object.defineProperties(i,e),i}(a.Component);return L.Highlight=function(e){var t=e.text,n=e.query;return void 0===n&&(n=""),a.createElement("span",{dangerouslySetInnerHTML:{__html:w.highlight(t,n)}})},L.propTypes={onFilter:e.func,onSelect:e.func,onAjax:e.func,open:e.bool,ajax:e.oneOfType([e.string,e.object])},L}); | ||
//# sourceMappingURL=core-input.jsx.js.map |
@@ -1,3 +0,3 @@ | ||
/*! @nrk/core-input v1.1.3 - Copyright (c) 2017-2018 NRK */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.coreInput=t()}(this,function(){"use strict";var e="undefined"!=typeof window,o=(e&&/(android)/i.test(navigator.userAgent),e&&/iPad|iPhone|iPod/.test(String(navigator.platform))),i=function(e){void 0===e&&(e=!1);try{window.addEventListener("test",null,{get passive(){e=!0}})}catch(e){}return e}();function t(e,t,n,r){(void 0===r&&(r=!1),"undefined"==typeof window||window[e=e+"-"+t])||(i||"object"!=typeof r||(r=Boolean(r.capture)),("resize"===t||"load"===t?window:document).addEventListener(window[e]=t,n,r))}var n={"&":"&","<":"<",">":">",'"':""","/":"/","'":"'"};function r(e){return String(e||"").replace(/[&<>"'/]/g,function(e){return n[e]})}var a="prevent_recursive_dispatch_maximum_callstack";function u(e,t,n){void 0===n&&(n={});var r,i=""+a+t;if(e[i])return!0;e[i]=!0,"function"==typeof window.CustomEvent?r=new window.CustomEvent(t,{bubbles:!0,cancelable:!0,detail:n}):(r=document.createEvent("CustomEvent")).initCustomEvent(t,!0,!0,n);var o=e.dispatchEvent(r);return e[i]=null,o}function c(e,t){if(void 0===t&&(t=document),e){if(e.nodeType)return[e];if("string"==typeof e)return[].slice.call(t.querySelectorAll(e));if(e.length)return[].slice.call(e)}return[]}var d="data-@nrk/core-input-1.1.3".replace(/\W+/g,"-"),l=13,f=27,s=33,p=34,v=35,m=36,b=38,g=40,y='[tabindex="-1"]',w=500;function x(e,t){var r="object"==typeof t?t:{content:t},i="string"==typeof r.content;return c(e).map(function(e){var t=e.nextElementSibling,n=void 0===r.ajax?e.getAttribute(d):r.ajax;return e.setAttribute(d,n||""),e.setAttribute(o?"data-role":"role","combobox"),e.setAttribute("aria-autocomplete","list"),e.setAttribute("autocomplete","off"),i&&(t.innerHTML=r.content),c("a,button",t).forEach(C),A(e,r.open),e})}function E(a){a.ctrlKey||a.altKey||a.metaKey||a.defaultPrevented||c("["+d+"]").forEach(function(e){var t,n,r=e.nextElementSibling,i=e===a.target||r.contains(a.target),o="click"===a.type&&i&&c(y,r).filter(function(e){return e.contains(a.target)})[0];o?(t=e,n={relatedTarget:r,currentTarget:o,value:o.value||o.textContent.trim()},u(t,"input.select",n)&&(t.value=n.value,t.focus(),A(t,!1))):A(e,i)})}function h(e,t){var n=e.nextElementSibling,r=[e].concat(c(y+":not([hidden])",n)),i=t.keyCode===f&&"true"===e.getAttribute("aria-expanded"),o=r.indexOf(document.activeElement),a=!1;t.keyCode===g?a=r[o+1]||r[0]:t.keyCode===b?a=r[o-1]||r.pop():n.contains(t.target)&&(t.keyCode===v||t.keyCode===p?a=r.pop():t.keyCode===m||t.keyCode===s?a=r[1]:t.keyCode!==l&&e.focus()),A(e,t.keyCode!==f),(!1!==a||i)&&t.preventDefault(),a&&a.focus()}function A(e,t){var n;void 0===t&&(t="true"===e.getAttribute("aria-expanded")),n=function(){e.nextElementSibling[t?"removeAttribute":"setAttribute"]("hidden",""),e.setAttribute("aria-expanded",t)},(window.requestAnimationFrame||window.setTimeout)(n)}function C(e,t,n){e.setAttribute("aria-label",e.textContent.trim()+", "+(t+1)+" av "+n.length),e.setAttribute("tabindex","-1"),e.setAttribute("type","button")}function k(e){var t=e.getAttribute(d),n=k.req=k.req||new window.XMLHttpRequest;if(!t)return!1;clearTimeout(k.timer),n.abort(),n.onload=function(){try{n.responseJSON=JSON.parse(n.responseText)}catch(e){n.responseJSON=!1}u(e,"input.ajax",n)},k.timer=setTimeout(function(){e.value&&u(e,"input.ajax.beforeSend",n)&&(n.open("GET",t.replace("{{value}}",window.encodeURIComponent(e.value)),!0),n.setRequestHeader("X-Requested-With","XMLHttpRequest"),n.send())},w)}return x.escapeHTML=r,x.highlight=function(e,t){var n=t.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&");return r(e).replace(new RegExp(n||".^","gi"),"<mark>$&</mark>")},t(d,"click",E),t(d,"focus",E,!0),t(d,"input",function(e){var o,t,n=e.target;n.hasAttribute(d)&&(t={relatedTarget:(o=n).nextElementSibling},u(o,"input.filter",t)&&!k(o)&&c(y,o.nextElementSibling).reduce(function(e,t){var n="LI"===t.parentElement.nodeName&&t.parentElement,r=-1!==t.textContent.toLowerCase().indexOf(o.value.toLowerCase()),i=r?"removeAttribute":"setAttribute";return n&&n[i]("hidden",""),t[i]("hidden",""),r?e.concat(t):e},[]).forEach(C))}),t(d,"keydown",function(e){if(!(e.ctrlKey||e.altKey||e.metaKey)){if(e.target.hasAttribute(d))return h(e.target,e);for(var t=e.target,n=void 0;t;t=t.parentElement)if((n=t.previousElementSibling)&&n.hasAttribute(d))return h(n,e)}},!0),x}); | ||
/*! @nrk/core-input v1.2.0 - Copyright (c) 2017-2019 NRK */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).coreInput=t()}(this,function(){"use strict";var e="undefined"!=typeof window,o=(e&&/(android)/i.test(navigator.userAgent),e&&/iPad|iPhone|iPod/.test(String(navigator.platform))),i=function(e){void 0===e&&(e=!1);try{window.addEventListener("test",null,{get passive(){e=!0}})}catch(e){}return e}();function t(e,t,n,r){(void 0===r&&(r=!1),"undefined"==typeof window||window[e=e+"-"+t])||(i||"object"!=typeof r||(r=Boolean(r.capture)),("resize"===t||"load"===t?window:document).addEventListener(window[e]=t,n,r))}var n={"&":"&","<":"<",">":">",'"':""","/":"/","'":"'"};function r(e){return String(e||"").replace(/[&<>"'/]/g,function(e){return n[e]})}var a="prevent_recursive_dispatch_maximum_callstack";function u(e,t,n){void 0===n&&(n={});var r,i=""+a+t;if(e[i])return!0;e[i]=!0,"function"==typeof window.CustomEvent?r=new window.CustomEvent(t,{bubbles:!0,cancelable:!0,detail:n}):(r=document.createEvent("CustomEvent")).initCustomEvent(t,!0,!0,n);var o=e.dispatchEvent(r);return e[i]=null,o}function c(e,t){if(void 0===t&&(t=document),e){if(e.nodeType)return[e];if("string"==typeof e)return[].slice.call(t.querySelectorAll(e));if(e.length)return[].slice.call(e)}return[]}var d="data-@nrk/core-input-1.2.0".replace(/\W+/g,"-"),l=13,f=27,s=33,p=34,v=35,m=36,b=38,g=40,y='[tabindex="-1"]',w=500;function x(e,t){var r="object"==typeof t?t:{content:t},i="string"==typeof r.content;return c(e).map(function(e){var t=e.nextElementSibling,n=void 0===r.ajax?e.getAttribute(d):r.ajax;return e.setAttribute(d,n||""),e.setAttribute(o?"data-role":"role","combobox"),e.setAttribute("aria-autocomplete","list"),e.setAttribute("autocomplete","off"),i&&(t.innerHTML=r.content),c("a,button",t).forEach(C),A(e,r.open),e})}function h(a){a.ctrlKey||a.altKey||a.metaKey||a.defaultPrevented||c("["+d+"]").forEach(function(e){var t,n,r=e.nextElementSibling,i=e===a.target||r.contains(a.target),o="click"===a.type&&i&&c(y,r).filter(function(e){return e.contains(a.target)})[0];o?(t=e,n={relatedTarget:r,currentTarget:o,value:o.value||o.textContent.trim()},u(t,"input.select",n)&&(t.value=n.value,t.focus(),A(t,!1))):A(e,i)})}function E(e,t){var n=e.nextElementSibling,r=[e].concat(c(y+":not([hidden])",n)),i=t.keyCode===f&&"true"===e.getAttribute("aria-expanded"),o=r.indexOf(document.activeElement),a=!1;t.keyCode===g?a=r[o+1]||r[0]:t.keyCode===b?a=r[o-1]||r.pop():n.contains(t.target)&&(t.keyCode===v||t.keyCode===p?a=r.pop():t.keyCode===m||t.keyCode===s?a=r[1]:t.keyCode!==l&&e.focus()),A(e,t.keyCode!==f),(!1!==a||i)&&t.preventDefault(),a&&a.focus()}function A(e,t){var n;void 0===t&&(t="true"===e.getAttribute("aria-expanded")),n=function(){e.nextElementSibling[t?"removeAttribute":"setAttribute"]("hidden",""),e.setAttribute("aria-expanded",t)},(window.requestAnimationFrame||window.setTimeout)(n)}function C(e,t,n){e.setAttribute("aria-label",e.textContent.trim()+", "+(t+1)+" av "+n.length),e.setAttribute("tabindex","-1"),e.setAttribute("type","button")}function k(e){var t=e.getAttribute(d),n=k.xhr=k.xhr||new window.XMLHttpRequest;if(!t)return!1;clearTimeout(k.timer),n.abort(),n.onload=function(){try{n.responseJSON=JSON.parse(n.responseText)}catch(e){n.responseJSON=!1}u(e,"input.ajax",n)},k.timer=setTimeout(function(){e.value&&u(e,"input.ajax.beforeSend",n)&&(n.open("GET",t.replace("{{value}}",window.encodeURIComponent(e.value)),!0),n.setRequestHeader("X-Requested-With","XMLHttpRequest"),n.send())},w)}return x.escapeHTML=r,x.highlight=function(e,t){var n=t.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&");return r(e).replace(new RegExp(n||".^","gi"),"<mark>$&</mark>")},t(d,"click",h),t(d,"focus",h,!0),t(d,"input",function(e){var o,t,n=e.target;n.hasAttribute(d)&&(t={relatedTarget:(o=n).nextElementSibling},u(o,"input.filter",t)&&!1===k(o)&&c(y,o.nextElementSibling).reduce(function(e,t){var n="LI"===t.parentElement.nodeName&&t.parentElement,r=-1!==t.textContent.toLowerCase().indexOf(o.value.toLowerCase()),i=r?"removeAttribute":"setAttribute";return n&&n[i]("hidden",""),t[i]("hidden",""),r?e.concat(t):e},[]).forEach(C))}),t(d,"keydown",function(e){if(!(e.ctrlKey||e.altKey||e.metaKey)){if(e.target.hasAttribute(d))return E(e.target,e);for(var t=e.target,n=void 0;t;t=t.parentElement)if((n=t.previousElementSibling)&&n.hasAttribute(d))return E(n,e)}},!0),x}); | ||
//# sourceMappingURL=core-input.min.js.map |
24
jsx.js
@@ -9,3 +9,3 @@ 'use strict'; | ||
var name = "@nrk/core-input"; | ||
var version = "1.1.3"; | ||
var version = "1.2.0"; | ||
@@ -208,3 +208,3 @@ var IS_BROWSER = typeof window !== 'undefined'; | ||
function onFilter (input, detail) { | ||
if (dispatchEvent(input, 'input.filter', detail) && !ajax(input)) { | ||
if (dispatchEvent(input, 'input.filter', detail) && ajax(input) === false) { | ||
queryAll(ITEM, input.nextElementSibling).reduce(function (acc, item) { | ||
@@ -238,17 +238,17 @@ var list = item.parentElement.nodeName === 'LI' && item.parentElement; | ||
var url = input.getAttribute(UUID); | ||
var req = ajax.req = ajax.req || new window.XMLHttpRequest(); | ||
var xhr = ajax.xhr = ajax.xhr || new window.XMLHttpRequest(); | ||
if (!url) { return false } | ||
clearTimeout(ajax.timer); // Clear previous search | ||
req.abort(); // Abort previous request | ||
req.onload = function () { | ||
try { req.responseJSON = JSON.parse(req.responseText); } catch (err) { req.responseJSON = false; } | ||
dispatchEvent(input, 'input.ajax', req); | ||
xhr.abort(); // Abort previous request | ||
xhr.onload = function () { | ||
try { xhr.responseJSON = JSON.parse(xhr.responseText); } catch (err) { xhr.responseJSON = false; } | ||
dispatchEvent(input, 'input.ajax', xhr); | ||
}; | ||
ajax.timer = setTimeout(function () { | ||
if (!input.value) { return } // Abort if input is empty | ||
if (dispatchEvent(input, 'input.ajax.beforeSend', req)) { | ||
req.open('GET', url.replace('{{value}}', window.encodeURIComponent(input.value)), true); | ||
req.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); // https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Requested-With | ||
req.send(); | ||
if (dispatchEvent(input, 'input.ajax.beforeSend', xhr)) { | ||
xhr.open('GET', url.replace('{{value}}', window.encodeURIComponent(input.value)), true); | ||
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); // https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Requested-With | ||
xhr.send(); | ||
} | ||
@@ -258,3 +258,3 @@ }, AJAX_DEBOUNCE); // Debounce request | ||
var Input = (function (superclass) { | ||
var Input = /*@__PURE__*/(function (superclass) { | ||
function Input (props) { | ||
@@ -261,0 +261,0 @@ superclass.call(this, props); |
@@ -5,3 +5,3 @@ { | ||
"author": "NRK <opensource@nrk.no> (https://www.nrk.no/)", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"license": "MIT", | ||
@@ -8,0 +8,0 @@ "main": "core-input.cjs.js", |
# Core Input | ||
## `@nrk/core-input` enhances `<input>` fields with keyboard accessible functionality for autocomplete suggestions, search results and smart select box abilities. | ||
> `@nrk/core-input` enhances `<input>` fields with keyboard accessible functionality for autocomplete suggestions, search results and smart select box abilities. | ||
--- | ||
## Installation | ||
@@ -17,4 +17,4 @@ | ||
--- | ||
<!--demo | ||
@@ -62,4 +62,4 @@ <script src="core-input/core-input.min.js"></script> | ||
--- | ||
## Usage | ||
@@ -70,2 +70,3 @@ | ||
<small><b>Important:</b> Always use `coreInput.escapeHTML(String)` to safely render data from API or user.</small> | ||
<small><b>Important:</b> Results will always be rendered in the element directly after the `<input>`</small> | ||
@@ -96,2 +97,4 @@ ```html | ||
<small><b>Important:</b> First direct of `<CoreToggle>` must be a `<input>` and next direct child will be used for results</small> | ||
```js | ||
@@ -114,4 +117,4 @@ import CoreInput from '@nrk/core-input/jsx' | ||
--- | ||
## Events | ||
@@ -174,4 +177,4 @@ | ||
--- | ||
## Styling | ||
@@ -193,4 +196,4 @@ All styling in documentation is example only. Both the `<button>` and content element receive attributes reflecting the current toggle state: | ||
--- | ||
## Ajax | ||
@@ -204,4 +207,4 @@ | ||
--- | ||
## Demo: Ajax | ||
@@ -281,4 +284,4 @@ | ||
--- | ||
## Demo: Lazy | ||
@@ -343,4 +346,4 @@ Hybrid solution; lazy load items, but let `core-input` still handle filtering: | ||
--- | ||
## Demo: Dynamic | ||
@@ -404,4 +407,4 @@ Synchronous operation; dynamically populating items based input value: | ||
--- | ||
## FAQ | ||
@@ -408,0 +411,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
866
416
104799