amp-selector-0.1.js 9.9 KB
;
(self.AMP=self.AMP||[]).push({m:0,v:"2402231941000",n:"amp-selector",ev:"0.1",l:!0,f:function(t,e){!function(){var e;function i(){return e||(e=Promise.resolve(void 0))}function n(t,e){return(n=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function r(t){return(r=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function o(t){return(o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function s(t,e){if(e&&("object"===o(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)}function u(t){return t?Array.prototype.slice.call(t):[]}function c(t,e){if(t.length!==e.length)return!1;for(var i=0;i<t.length;i++)if(t[i]!==e[i])return!1;return!0}Array.isArray;var a=Object.prototype,h=(a.hasOwnProperty,a.toString,1),l="Enter",f="ArrowLeft",d="ArrowUp",v="ArrowRight",p="ArrowDown",b="Home",m="End";function y(t){return"rtl"==(t.body.getAttribute("dir")||t.documentElement.getAttribute("dir")||"ltr")}self.__AMP_LOG=self.__AMP_LOG||{user:null,dev:null,userForEmbed:null};var w=self.__AMP_LOG;function g(t){return function(t,e){throw new Error("failed to call initLogConstructor")}()}function k(t,e,i,n,r,o,s,u,c,a,h){return t}function j(t,e,i,n,r,o,s,u,c,a,h){return(w.user||(w.user=g()),void w.user.win?w.userForEmbed||(w.userForEmbed=g()):w.user).assert(t,e,i,n,r,o,s,u,c,a,h)}function O(t){return t.nodeType?(i=t,e=(i.ownerDocument||i).defaultView,function(t,e){return x(t=function(t){return t.__AMP_TOP||(t.__AMP_TOP=t)}(t),"ampdoc")}(e)).getAmpDoc(t):t;var e,i}function x(t,e){k(E(t,e));var i=function(t){var e=t.__AMP_SERVICES;return e||(e=t.__AMP_SERVICES={}),e}(t)[e];return i.obj||(k(i.ctor),k(i.context),i.obj=new i.ctor(i.context),k(i.obj),i.context=null,i.resolve&&i.resolve(i.obj)),i.obj}function E(t,e){var i=t.__AMP_SERVICES&&t.__AMP_SERVICES[e];return!(!i||!i.ctor)}var R="amp-selector",A="none",S="select",U=function(t){!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}}),e&&n(t,e)}(w,t);var e,o,a=(e=w,o=function(){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(){var t,i=r(e);if(o){var n=r(this).constructor;t=Reflect.construct(i,arguments,n)}else t=i.apply(this,arguments);return s(this,t)});function w(t){var e;return(e=a.call(this,t)).Qq=!1,e.Zq=[],e.Nt=[],e.dJ=[],e.Ye=null,e.bJ=0,e.yJ=A,e}w.prerenderAllowed=function(){return!0};var g=w.prototype;return g.isLayoutSupported=function(){return!0},g.buildCallback=function(){var t,e,i,n,r,o=this;this.Ye=(t=this.element,e="action",i=O(t),E(r=(n=O(i)).isSingleDoc()?n.win:n,e)?x(r,e):null),this.Qq=this.element.hasAttribute("multiple"),this.element.hasAttribute("role")||this.element.setAttribute("role","listbox"),this.Qq&&this.element.setAttribute("aria-multiselectable","true"),this.element.hasAttribute("disabled")&&this.element.setAttribute("aria-disabled","true");var s,u=this.element.getAttribute("keyboard-select-mode");u?(j("none"===(s=u=u.toLowerCase())||"focus"===s||"select"===s,"Unknown keyboard-select-mode: ".concat(u)),j(!(this.Qq&&u==S),"[keyboard-select-mode=select] not supported for multiple selection amp-selector")):u=A,this.yJ=u,this.registerAction("clear",this.wJ.bind(this)),this.Ls(),this.element.addEventListener("click",this.gJ.bind(this)),this.element.addEventListener("keydown",this.kJ.bind(this)),this.registerAction("selectUp",(function(t){var e=t.args,i=t.trust,n=e&&void 0!==e.delta?-e.delta:-1;o.jJ(n,i)}),h),this.registerAction("selectDown",(function(t){var e=t.args,i=t.trust,n=e&&void 0!==e.delta?e.delta:1;o.jJ(n,i)}),h),this.registerAction("toggle",(function(t){var e=t.args,i=t.trust;return j(e.index>=0,"'index' must be greater than 0"),j(e.index<o.Nt.length,"'index' must be less than the length of options in the <amp-selector>"),e&&void 0!==e.index?o.sL(e.index,e.value,i):Promise.reject("'index' must be specified")}),h),this.Ye.addToAllowlist(R,["clear","selectDown","selectUp","toggle"],["email"]),this.element.addEventListener("amp:dom-update",this.OJ.bind(this))},g.mutatedAttributesCallback=function(t){var e=t.selected;void 0!==e&&this.xJ(e);var i=t.disabled;void 0!==i&&(i?this.element.setAttribute("aria-disabled","true"):this.element.removeAttribute("aria-disabled"))},g.xJ=function(t){var e=Array.isArray(t)?t:[t];if(null!==t&&0!=e.length){if(this.Qq||(e=e.slice(0,1)),!c(this.EJ().sort(),e.sort())){for(var i=e.reduce((function(t,e){return t[e]=!0,t}),Object.create(null)),n=0;n<this.Nt.length;n++){var r=this.Nt[n];i[r.getAttribute("option")]?this.RJ(r):this.AJ(r)}this.SJ(),this.UJ()}}else this.wJ()},g.SJ=function(t){if(this.yJ!=A){this.Nt.forEach((function(t){t.tabIndex=-1}));var e=t;e||(e=this.Qq?this.Nt[0]:this.Zq[0]||this.Nt[0]),e&&(this.bJ=this.Nt.indexOf(e),e.tabIndex=0)}},g.OJ=function(t){var e=u(this.element.querySelectorAll("[option]"));c(this.Nt,e)||this.Ls(e)},g.Ls=function(t){var e=this;this.Zq.length=0;var i=t||u(this.element.querySelectorAll("[option]"));i.forEach((function(t){t.hasAttribute("role")||t.setAttribute("role","option"),t.hasAttribute("disabled")&&t.setAttribute("aria-disabled","true"),t.hasAttribute("selected")?e.RJ(t):e.AJ(t),t.tabIndex=0})),this.Nt=i,this.SJ(),this.UJ()},g.UJ=function(){var t=this,e=[],i=this.element.getAttribute("name");if(!i||this.element.hasAttribute("disabled"))return e;var n=this.element.getAttribute("form");this.dJ.forEach((function(e){t.element.removeChild(e)})),this.dJ=[];var r=this.win.document,o=r.createDocumentFragment();return this.Zq.forEach((function(s){if(!s.hasAttribute("disabled")){var u=r.createElement("input"),c=s.getAttribute("option");u.setAttribute("type","hidden"),u.setAttribute("name",i),u.setAttribute("value",c),n&&u.setAttribute("form",n),t.dJ.push(u),o.appendChild(u),e.push(c)}})),this.element.appendChild(o),e},g.DJ=function(t){var e=this;t.hasAttribute("disabled")||this.mutateElement((function(){t.hasAttribute("selected")?e.Qq&&(e.AJ(t),e.UJ()):(e.RJ(t),e.UJ()),e.SJ(t),e.LJ(t,3)}))},g.EJ=function(){return this.Zq.map((function(t){return t.getAttribute("option")}))},g.gJ=function(t){if(!this.element.hasAttribute("disabled")){var e,i=t.target;i&&(i.hasAttribute("option")||("[option]",i=(e=i).closest?e.closest("[option]"):function(t,e,i){var n;for(n=t;n&&void 0!==n;n=n.parentElement)if(e(n))return n;return null}(e,(function(t){return function(t,e){var i=t.matches||t.webkitMatchesSelector||t.mozMatchesSelector||t.msMatchesSelector||t.oMatchesSelector;return!!i&&i.call(t,"[option]")}(t)}))),i&&this.DJ(i))}},g.sL=function(t,e,n){var r=this,o=this.Nt[t],s=o.hasAttribute("selected"),u=void 0!==e?e:!s,c=this.Nt.indexOf(this.Zq[0]);return u===s?i():this.mutateElement((function(){if(c!==t){r.RJ(o);var e=r.Nt[c];e&&r.AJ(e)}else r.AJ(o);r.LJ(o,n)}))},g.LJ=function(t,e){var i="select",n=function(t,e,i,n){var r={detail:i};if(Object.assign(r,void 0),"function"==typeof t.CustomEvent)return new t.CustomEvent(e,r);var o=t.document.createEvent("CustomEvent");return o.initCustomEvent(e,!!r.bubbles,!!r.cancelable,i),o}(this.win,"amp-selector.".concat(i),{"targetOption":t.getAttribute("option"),"selectedOptions":this.EJ()});this.Ye.trigger(this.element,i,n,e)},g.jJ=function(t,e){var i,n,r=this.Nt.indexOf(this.Zq[0]),o=(i=-1===r&&t<0?t:r+t,n=this.Nt.length,i>0&&n>0?i%n:(i%n+n)%n),s=this.Nt[o];this.RJ(s);var u=this.Nt[r];u&&this.AJ(u),this.UJ(),this.LJ(s,e)},g.kJ=function(t){if(this.element.hasAttribute("disabled"))return i();switch(t.key){case f:case d:case v:case p:case b:case m:return this.yJ!=A?this.PJ(t):i();case l:case" ":return this.BJ(t),i()}return i()},g.PJ=function(t){var e=this,n=this.win.document,r=0;switch(t.key){case f:r=y(n)?1:-1;break;case d:r=-1;break;case v:r=y(n)?-1:1;break;case p:case b:r=1;break;case m:r=-1;break;default:return i()}return t.preventDefault(),this.Nt[this.bJ].tabIndex=-1,this.CJ().then((function(i){var n,o,s,u,c=e.bJ;switch(t.key){case b:e.bJ=e.Nt.length-1;break;case m:e.bJ=0}do{e.bJ=(e.bJ+r)%e.Nt.length,e.bJ<0&&(e.bJ=e.bJ+e.Nt.length)}while(n=e.Nt[e.bJ],s=(o=i[e.bJ]).height,u=o.width,(n.hidden||0==u||0==s)&&e.bJ!=c);var a=e.Nt[e.bJ];a.tabIndex=0,function(t){try{t.focus()}catch(t){}}(a);var h=e.Nt[e.bJ];e.yJ==S&&e.DJ(h)}))},g.BJ=function(t){var e=t.key;if((" "==e||e==l)&&this.Nt.includes(t.target)){t.preventDefault();var i=t.target;this.DJ(i)}},g.AJ=function(t){t.removeAttribute("selected"),t.setAttribute("aria-selected","false");var e=this.Zq.indexOf(t);-1!==e&&this.Zq.splice(e,1)},g.wJ=function(){for(;this.Zq.length>0;){var t=this.Zq.pop();this.AJ(t)}this.UJ()},g.RJ=function(t){this.Zq.includes(t)||(this.Qq||this.wJ(),t.setAttribute("selected",""),t.setAttribute("aria-selected","true"),this.Zq.push(t))},g.getElementsForTesting=function(){return this.Nt},g.getSelectedElementsForTesting=function(){return this.Zq},g.CJ=function(){var t=this;return this.measureElement((function(){return t.Nt.map((function(t){return t.getBoundingClientRect()}))}))},w}(t.BaseElement);t.registerElement(R,U,"amp-selector [option]{cursor:pointer}amp-selector [option][selected]{cursor:auto;outline:1px solid rgba(0,0,0,.7)}amp-selector[multiple] [option][selected]{cursor:pointer;outline:1px solid rgba(0,0,0,.7)}amp-selector [disabled][option],amp-selector [selected][disabled],amp-selector[disabled] [option],amp-selector[disabled] [selected]{cursor:auto;outline:none}\n/*# sourceURL=/extensions/amp-selector/0.1/amp-selector.css*/")}();
/*! https://mths.be/cssescape v1.5.1 by @mathias | MIT license */}});
//# sourceMappingURL=amp-selector-0.1.js.map