You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

725 lines
34 KiB

  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('../../pfelement/dist/pfelement.umd')) :
  3. typeof define === 'function' && define.amd ? define(['../../pfelement/dist/pfelement.umd'], factory) :
  4. (global.PfeAutocomplete = factory(global.PFElement));
  5. }(this, (function (PFElement) { 'use strict';
  6. PFElement = PFElement && PFElement.hasOwnProperty('default') ? PFElement['default'] : PFElement;
  7. var classCallCheck = function (instance, Constructor) {
  8. if (!(instance instanceof Constructor)) {
  9. throw new TypeError("Cannot call a class as a function");
  10. }
  11. };
  12. var createClass = function () {
  13. function defineProperties(target, props) {
  14. for (var i = 0; i < props.length; i++) {
  15. var descriptor = props[i];
  16. descriptor.enumerable = descriptor.enumerable || false;
  17. descriptor.configurable = true;
  18. if ("value" in descriptor) descriptor.writable = true;
  19. Object.defineProperty(target, descriptor.key, descriptor);
  20. }
  21. }
  22. return function (Constructor, protoProps, staticProps) {
  23. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  24. if (staticProps) defineProperties(Constructor, staticProps);
  25. return Constructor;
  26. };
  27. }();
  28. var get = function get(object, property, receiver) {
  29. if (object === null) object = Function.prototype;
  30. var desc = Object.getOwnPropertyDescriptor(object, property);
  31. if (desc === undefined) {
  32. var parent = Object.getPrototypeOf(object);
  33. if (parent === null) {
  34. return undefined;
  35. } else {
  36. return get(parent, property, receiver);
  37. }
  38. } else if ("value" in desc) {
  39. return desc.value;
  40. } else {
  41. var getter = desc.get;
  42. if (getter === undefined) {
  43. return undefined;
  44. }
  45. return getter.call(receiver);
  46. }
  47. };
  48. var inherits = function (subClass, superClass) {
  49. if (typeof superClass !== "function" && superClass !== null) {
  50. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  51. }
  52. subClass.prototype = Object.create(superClass && superClass.prototype, {
  53. constructor: {
  54. value: subClass,
  55. enumerable: false,
  56. writable: true,
  57. configurable: true
  58. }
  59. });
  60. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  61. };
  62. var possibleConstructorReturn = function (self, call) {
  63. if (!self) {
  64. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  65. }
  66. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  67. };
  68. /*!
  69. * PatternFly Elements: PfeAutocomplete 1.0.0-prerelease.55
  70. * @license
  71. * Copyright 2020 Red Hat, Inc.
  72. *
  73. * Permission is hereby granted, free of charge, to any person obtaining a copy
  74. * of this software and associated documentation files (the "Software"), to deal
  75. * in the Software without restriction, including without limitation the rights
  76. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  77. * copies of the Software, and to permit persons to whom the Software is
  78. * furnished to do so, subject to the following conditions:
  79. *
  80. * The above copyright notice and this permission notice shall be included in
  81. * all copies or substantial portions of the Software.
  82. *
  83. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  84. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  85. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  86. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  87. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  88. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  89. * SOFTWARE.
  90. *
  91. */
  92. var KEYCODE = {
  93. ENTER: 13,
  94. DOWN: 40,
  95. UP: 38,
  96. ESC: 27
  97. };
  98. // use this variable to debounce api call when user types very fast
  99. var throttle = false;
  100. var PfeAutocomplete = function (_PFElement) {
  101. inherits(PfeAutocomplete, _PFElement);
  102. createClass(PfeAutocomplete, [{
  103. key: "html",
  104. get: function get$$1() {
  105. return "<style>.sr-only{position:absolute;overflow:hidden;clip:rect(0,0,0,0);height:1px;width:1px;margin:-1px;padding:0;border:0}@media screen and (-ms-high-contrast:active),screen and (-ms-high-contrast:none){:host{color:#151515!important}}:host([on=dark]){--pfe-broadcasted--text:var(--pfe-theme--color--text--on-dark, #fff);--pfe-broadcasted--link:var(--pfe-theme--color--link--on-dark, #73bcf7);--pfe-broadcasted--link--hover:var(--pfe-theme--color--link--hover--on-dark, #bee1f4);--pfe-broadcasted--link--focus:var(--pfe-theme--color--link--focus--on-dark, #bee1f4);--pfe-broadcasted--link--visited:var(--pfe-theme--color--link--visited--on-dark, #bee1f4);--pfe-broadcasted--link-decoration:var(--pfe-theme--link-decoration--on-dark, none);--pfe-broadcasted--link-decoration--hover:var(--pfe-theme--link-decoration--hover--on-dark, underline);--pfe-broadcasted--link-decoration--focus:var(--pfe-theme--link-decoration--focus--on-dark, underline);--pfe-broadcasted--link-decoration--visited:var(--pfe-theme--link-decoration--visited--on-dark, none)}:host([on=saturated]){--pfe-broadcasted--text:var(--pfe-theme--color--text--on-saturated, #fff);--pfe-broadcasted--link:var(--pfe-theme--color--link--on-saturated, #fff);--pfe-broadcasted--link--hover:var(--pfe-theme--color--link--hover--on-saturated, #fafafa);--pfe-broadcasted--link--focus:var(--pfe-theme--color--link--focus--on-saturated, #fafafa);--pfe-broadcasted--link--visited:var(--pfe-theme--color--link--visited--on-saturated, #8476d1);--pfe-broadcasted--link-decoration:var(--pfe-theme--link-decoration--on-saturated, underline);--pfe-broadcasted--link-decoration--hover:var(--pfe-theme--link-decoration--hover--on-saturated, underline);--pfe-broadcasted--link-decoration--focus:var(--pfe-theme--link-decoration--focus--on-saturated, underline);--pfe-broadcasted--link-decoration--visited:var(--pfe-theme--link-decoration--visited--on-saturated, underline)}:host([on=light]){--pfe-broadcasted--text:var(--pfe-theme--color--text, #151515);--pfe-broadcasted--link:var(--pfe-theme--color--link, #06c);--pfe-broadcasted--link--hover:var(--pfe-theme--color--link--hover, #004080);--pfe-broadcasted--link--focus:var(--pfe-theme--color--link--focus, #004080);--pfe-broadcasted--link--visited:var(--pfe-theme--color--link--visited, #6753ac);--pfe-broadcasted--link-decoration:var(--pfe-theme--link-decoration, none);--pfe-broadcasted--link-decoration--hover:var(--pfe-theme--link-decoration--hover, underline);--pfe-broadcasted--link-decoration--focus:var(--pfe-theme--link-decoration--focus, underline);--pfe-broadcasted--link-decoration--visited:var(--pfe-theme--link-decoration--visited, none)}:host{display:block;position:relative;font-family:Overpass,Overpass,Helvetica,helvetica,arial,sans-serif;font-family:var(--pfe-theme--font-family, \"Overpass\", Overpass, Helvetica, helvetica, arial, sans-serif)}#input-box-wrapper{border-color:#0277bd;border-color:var(--pfe-theme--color--feedback--info,#0277bd)}#input-box-wrapper ::slotted(input){width:100%;-webkit-box-flex:1;-webkit-flex:1;-ms-flex:1;flex:1;-webkit-box-shadow:inset 0 0 .625rem 0 #fafafa!important;box-shadow:inset 0 0 .625rem 0 #fafafa!important;-webkit-box-shadow:var(--pfe-autocomplete--BoxShadow,var(--pfe-theme--box-shadow--inset,inset 0 0 .625rem 0 #fafafa))!important;box-shadow:var(--pfe-autocomplete--BoxShadow,var(--pfe-theme--box-shadow--inset,inset 0 0 .625rem 0 #fafafa))!important;padding-left:10px;padding-right:30px;border-radius:2px;border-radius:var(--pfe-theme--ui--border-radius,2px);background-color:#fff;background-color:var(--pfe-autocomplete--BackgroundColor,var(--pfe-theme--color--surface--lightest,#fff));border:1px solid #d2d2d2;border:var(--pfe-autocomplete--Border,var(--pfe-theme--ui--border-width,1px) var(--pfe-theme--ui--border-style,solid) var(--pfe-theme--color--surface--border,#d2d2d2));font-size:16px;font-size:var(--pfe-theme--font-size,16px);height:calc(20px * 2);height:calc(var(--pfe-theme--ui--element--size,20px) * 2);-webkit-transition:border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;transition:bor
  106. }
  107. }, {
  108. key: "schemaUrl",
  109. get: function get$$1() {
  110. return "pfe-autocomplete.json";
  111. }
  112. }, {
  113. key: "templateUrl",
  114. get: function get$$1() {
  115. return "pfe-autocomplete.html";
  116. }
  117. }, {
  118. key: "styleUrl",
  119. get: function get$$1() {
  120. return "pfe-autocomplete.scss";
  121. }
  122. }], [{
  123. key: "version",
  124. get: function get$$1() {
  125. return "1.0.0-prerelease.55";
  126. }
  127. }, {
  128. key: "properties",
  129. get: function get$$1() {
  130. return { "debounce_timer": { "title": "Debounce", "description": "The amount of time that should pass before the next API call is made", "type": "string", "prefixed": false }, "init_value": { "title": "Initial value", "description": "An initial value to show in the input field", "type": "string", "prefixed": false }, "is_disabled": { "title": "Is disabled", "description": "Disable the input", "type": "boolean", "prefixed": false } };
  131. }
  132. }, {
  133. key: "slots",
  134. get: function get$$1() {
  135. return { "content": { "title": "Content", "type": "array", "namedSlot": false, "items": { "oneOf": [{ "$ref": "input" }] }, "required": true } };
  136. }
  137. }, {
  138. key: "tag",
  139. get: function get$$1() {
  140. return "pfe-autocomplete";
  141. }
  142. }, {
  143. key: "events",
  144. get: function get$$1() {
  145. return {
  146. search: this.tag + ":search-event",
  147. select: this.tag + ":option-selected",
  148. optionsShown: this.tag + ":options-shown",
  149. slotchange: "slotchange"
  150. };
  151. }
  152. }]);
  153. function PfeAutocomplete() {
  154. classCallCheck(this, PfeAutocomplete);
  155. var _this = possibleConstructorReturn(this, (PfeAutocomplete.__proto__ || Object.getPrototypeOf(PfeAutocomplete)).call(this, PfeAutocomplete));
  156. _this._slotchangeHandler = _this._slotchangeHandler.bind(_this);
  157. _this._slot = _this.shadowRoot.querySelector("slot");
  158. _this._slot.addEventListener(PfeAutocomplete.events.slotchange, _this._slotchangeHandler);
  159. return _this;
  160. }
  161. createClass(PfeAutocomplete, [{
  162. key: "connectedCallback",
  163. value: function connectedCallback() {
  164. get(PfeAutocomplete.prototype.__proto__ || Object.getPrototypeOf(PfeAutocomplete.prototype), "connectedCallback", this).call(this);
  165. this.loading = false;
  166. this.debounce = this.debounce || 300;
  167. this._ariaAnnounceTemplate = "There are ${numOptions} suggestions. Use the up and down arrows to browse.";
  168. // clear button
  169. this._clearBtn = this.shadowRoot.querySelector(".clear-search");
  170. this._clearBtn.addEventListener("click", this._clear.bind(this));
  171. // search button
  172. this._searchBtn = this.shadowRoot.querySelector(".search-button");
  173. this._searchBtn.addEventListener("click", this._search.bind(this));
  174. this._dropdown = this.shadowRoot.querySelector("#dropdown");
  175. this._dropdown.data = [];
  176. this.activeIndex = null;
  177. this.addEventListener("keyup", this._inputKeyUp.bind(this));
  178. // these two events, fire search
  179. this.addEventListener(PfeAutocomplete.events.search, this._closeDroplist.bind(this));
  180. this.addEventListener(PfeAutocomplete.events.select, this._optionSelected.bind(this));
  181. }
  182. }, {
  183. key: "disconnectedCallback",
  184. value: function disconnectedCallback() {
  185. this.removeEventListener("keyup", this._inputKeyUp);
  186. this.removeEventListener(PfeAutocomplete.events.search, this._closeDroplist);
  187. this.removeEventListener(PfeAutocomplete.events.select, this._optionSelected);
  188. this._slot.removeEventListener(PfeAutocomplete.events.slotchange, this._slotchangeHandler);
  189. if (this._input) {
  190. this._input.removeEventListener("input", this._inputChanged);
  191. this._input.removeEventListener("blur", this._closeDroplist);
  192. }
  193. this._clearBtn.removeEventListener("click", this._clear);
  194. this._searchBtn.removeEventListener("click", this._search);
  195. }
  196. }, {
  197. key: "attributeChangedCallback",
  198. value: function attributeChangedCallback(attr, oldVal, newVal) {
  199. get(PfeAutocomplete.prototype.__proto__ || Object.getPrototypeOf(PfeAutocomplete.prototype), "attributeChangedCallback", this).call(this);
  200. var slotNodes = this.shadowRoot.querySelector("slot").assignedNodes();
  201. var slotElems = slotNodes.filter(function (n) {
  202. return n.nodeType === Node.ELEMENT_NODE;
  203. });
  204. var _input = slotElems[0];
  205. var _clearBtn = this.shadowRoot.querySelector(".clear-search");
  206. var _searchBtn = this.shadowRoot.querySelector(".search-button");
  207. switch (attr) {
  208. case "loading":
  209. if (!this.loading || _input.value === "") {
  210. this.shadowRoot.querySelector(".loading").setAttribute("hidden", "");
  211. } else {
  212. this.shadowRoot.querySelector(".loading").removeAttribute("hidden");
  213. }
  214. break;
  215. case "init-value":
  216. if (this["init-value"] !== newVal) {
  217. // set inputbox and buttons in the inner component
  218. _input.value = newVal;
  219. if (newVal !== "" && !this.isDisabled) {
  220. _searchBtn.removeAttribute("disabled");
  221. _clearBtn.removeAttribute("hidden");
  222. } else {
  223. _searchBtn.setAttribute("disabled", "");
  224. _clearBtn.setAttribute("hidden", "");
  225. }
  226. }
  227. break;
  228. case "is-disabled":
  229. if (this.isDisabled) {
  230. _clearBtn.setAttribute("disabled", "");
  231. _searchBtn.setAttribute("disabled", "");
  232. _input.setAttribute("disabled", "");
  233. } else {
  234. _clearBtn.removeAttribute("disabled");
  235. _searchBtn.removeAttribute("disabled");
  236. _input.removeAttribute("disabled");
  237. }
  238. break;
  239. }
  240. }
  241. }, {
  242. key: "_slotchangeHandler",
  243. value: function _slotchangeHandler() {
  244. // input box
  245. var slotNodes = this.shadowRoot.querySelector("slot").assignedNodes();
  246. var slotElems = slotNodes.filter(function (n) {
  247. return n.nodeType === Node.ELEMENT_NODE;
  248. });
  249. if (slotElems.length === 0) {
  250. console.error(PfeAutocomplete.tag + ": There must be a input tag in the light DOM");
  251. return;
  252. }
  253. this._input = slotElems[0];
  254. if (this._input.tagName.toLowerCase() !== "input") {
  255. console.error(PfeAutocomplete.tag + ": The only child in the light DOM must be an input tag");
  256. return;
  257. }
  258. this._input.addEventListener("input", this._inputChanged.bind(this));
  259. this._input.addEventListener("blur", this._closeDroplist.bind(this));
  260. this._input.setAttribute("role", "combobox");
  261. if (!this._input.hasAttribute("aria-label")) {
  262. this._input.setAttribute("aria-label", "Search");
  263. }
  264. this._input.setAttribute("aria-autocomplete", "both");
  265. this._input.setAttribute("aria-haspopup", "true");
  266. this._input.setAttribute("type", "search");
  267. this._input.setAttribute("autocomplete", "off");
  268. this._input.setAttribute("autocorrect", "off");
  269. this._input.setAttribute("autocapitalize", "off");
  270. this._input.setAttribute("spellcheck", "false");
  271. this._dropdown._ariaAnnounceTemplate = this.getAttribute("aria-announce-template") || this._ariaAnnounceTemplate;
  272. }
  273. }, {
  274. key: "_inputChanged",
  275. value: function _inputChanged() {
  276. var _this2 = this;
  277. if (this._input.value === "") {
  278. this._searchBtn.setAttribute("disabled", "");
  279. this._clearBtn.setAttribute("hidden", "");
  280. this._reset();
  281. return;
  282. } else {
  283. if (!this._input.hasAttribute("disabled")) {
  284. this._searchBtn.removeAttribute("disabled");
  285. }
  286. this._clearBtn.removeAttribute("hidden");
  287. }
  288. if (throttle === false) {
  289. throttle = true;
  290. window.setTimeout(function () {
  291. _this2._sendAutocompleteRequest(_this2._input.value);
  292. throttle = false;
  293. }, this.debounce);
  294. }
  295. }
  296. }, {
  297. key: "_clear",
  298. value: function _clear() {
  299. this._input.value = "";
  300. this._clearBtn.setAttribute("hidden", "");
  301. this._searchBtn.setAttribute("disabled", "");
  302. this._input.focus();
  303. }
  304. }, {
  305. key: "_search",
  306. value: function _search() {
  307. this._doSearch(this._input.value);
  308. }
  309. }, {
  310. key: "_closeDroplist",
  311. value: function _closeDroplist() {
  312. this._dropdown.open = null;
  313. this._dropdown.removeAttribute("active-index");
  314. }
  315. }, {
  316. key: "_openDroplist",
  317. value: function _openDroplist() {
  318. this.activeIndex = null;
  319. this._dropdown.setAttribute("open", true);
  320. this._dropdown.setAttribute("active-index", null);
  321. this.emitEvent(PfeAutocomplete.events.optionsShown, {
  322. composed: true
  323. });
  324. }
  325. }, {
  326. key: "_optionSelected",
  327. value: function _optionSelected(e) {
  328. var selectedValue = e.detail.optionValue;
  329. // update input box with selected value from options list
  330. this._input.value = selectedValue;
  331. // send search request
  332. this._doSearch(selectedValue);
  333. }
  334. }, {
  335. key: "_doSearch",
  336. value: function _doSearch(searchQuery) {
  337. this.emitEvent(PfeAutocomplete.events.search, {
  338. detail: { searchValue: searchQuery },
  339. composed: true
  340. });
  341. this._reset();
  342. this.selectedValue = searchQuery;
  343. }
  344. }, {
  345. key: "_sendAutocompleteRequest",
  346. value: function _sendAutocompleteRequest(input) {
  347. if (!this.autocompleteRequest) return;
  348. this.autocompleteRequest({ query: input }, this._autocompleteCallback.bind(this));
  349. }
  350. }, {
  351. key: "_autocompleteCallback",
  352. value: function _autocompleteCallback(response) {
  353. this._dropdown.data = response;
  354. this._dropdown.reflow = true;
  355. response.length !== 0 ? this._openDroplist() : this._closeDroplist();
  356. }
  357. }, {
  358. key: "_reset",
  359. value: function _reset() {
  360. this._dropdown.activeIndex = null;
  361. this._input.setAttribute("aria-activedescendant", "");
  362. this._dropdown.data = [];
  363. this._closeDroplist();
  364. }
  365. }, {
  366. key: "_activeOption",
  367. value: function _activeOption(activeIndex) {
  368. if (activeIndex === null || activeIndex === "null") return;
  369. return this._dropdown.shadowRoot.querySelector("li:nth-child(" + (parseInt(activeIndex, 10) + 1) + ")").innerHTML;
  370. }
  371. }, {
  372. key: "_inputKeyUp",
  373. value: function _inputKeyUp(e) {
  374. var key = e.keyCode;
  375. if (this._dropdown.data.length === 0 && key !== KEYCODE.DOWN && key !== KEYCODE.UP && key !== KEYCODE.ENTER && key !== KEYCODE.ESC) return;
  376. var activeIndex = this._dropdown.activeIndex;
  377. var optionsLength = this._dropdown.data.length;
  378. if (key == KEYCODE.ESC) {
  379. this._closeDroplist();
  380. } else if (key === KEYCODE.UP) {
  381. if (!this._dropdown.open) {
  382. return;
  383. }
  384. activeIndex = activeIndex === null || activeIndex === "null" ? optionsLength : parseInt(activeIndex, 10);
  385. activeIndex -= 1;
  386. if (activeIndex < 0) {
  387. activeIndex = optionsLength - 1;
  388. }
  389. this._input.value = this._activeOption(activeIndex);
  390. } else if (key === KEYCODE.DOWN) {
  391. if (!this._dropdown.open) {
  392. return;
  393. }
  394. activeIndex = activeIndex === null || activeIndex === "null" ? -1 : parseInt(activeIndex, 10);
  395. activeIndex += 1;
  396. if (activeIndex > optionsLength - 1) {
  397. activeIndex = 0;
  398. }
  399. this._input.value = this._activeOption(activeIndex);
  400. } else if (key === KEYCODE.ENTER) {
  401. if (this._activeOption(activeIndex)) {
  402. this.emitEvent(PfeAutocomplete.events.select, {
  403. detail: { optionValue: this._activeOption(activeIndex) },
  404. composed: true
  405. });
  406. return;
  407. }
  408. var selectedValue = this._input.value;
  409. this._doSearch(selectedValue);
  410. return;
  411. }
  412. if (activeIndex !== null && activeIndex !== "null") {
  413. this._input.setAttribute("aria-activedescendant", "option-" + activeIndex);
  414. } else {
  415. this._input.setAttribute("aria-activedescendant", "");
  416. }
  417. this.activeIndex = activeIndex;
  418. this._dropdown.activeIndex = activeIndex;
  419. }
  420. }, {
  421. key: "selectedValue",
  422. get: function get$$1() {
  423. return this.getAttribute("selected-value");
  424. },
  425. set: function set$$1(val) {
  426. this.setAttribute("selected-value", val);
  427. }
  428. }, {
  429. key: "isDisabled",
  430. set: function set$$1(value) {
  431. if (value) {
  432. this.setAttribute("is-disabled", "");
  433. } else {
  434. this.removeAttribute("is-disabled");
  435. }
  436. },
  437. get: function get$$1() {
  438. return this.hasAttribute("is-disabled");
  439. }
  440. }, {
  441. key: "loading",
  442. set: function set$$1(value) {
  443. var loading = Boolean(value);
  444. if (loading) {
  445. this.setAttribute("loading", "");
  446. } else {
  447. this.removeAttribute("loading");
  448. }
  449. },
  450. get: function get$$1() {
  451. return this.hasAttribute("loading");
  452. }
  453. }, {
  454. key: "initValue",
  455. get: function get$$1() {
  456. return this.getAttribute("init-value");
  457. },
  458. set: function set$$1(val) {
  459. this.setAttribute("init-value", val);
  460. }
  461. }, {
  462. key: "debounce",
  463. get: function get$$1() {
  464. return this.getAttribute("debounce");
  465. },
  466. set: function set$$1(val) {
  467. this.setAttribute("debounce", val);
  468. }
  469. }], [{
  470. key: "observedAttributes",
  471. get: function get$$1() {
  472. return ["init-value", "loading", "is-disabled"];
  473. }
  474. }]);
  475. return PfeAutocomplete;
  476. }(PFElement);
  477. /*
  478. * - Attributes ------------------------------------
  479. * open | Set when the combo box dropdown is open
  480. * active-index | Set selected option
  481. * reflow | Re-renders the dropdown
  482. * - Events ----------------------------------------
  483. * pfe-autocomplete:option-selected | Fires when an option is selected.
  484. event.details.optionValue contains the selected value.
  485. */
  486. var PfeSearchDroplist = function (_PFElement2) {
  487. inherits(PfeSearchDroplist, _PFElement2);
  488. createClass(PfeSearchDroplist, [{
  489. key: "html",
  490. get: function get$$1() {
  491. return "<style>:host{position:relative;display:none;font-family:Overpass,Overpass,Helvetica,helvetica,arial,sans-serif;font-family:var(--pfe-theme--font-family, \"Overpass\", Overpass, Helvetica, helvetica, arial, sans-serif);font-size:16px;font-size:var(--pfe-theme--font-size,16px);line-height:1.5;line-height:var(--pfe-theme--line-height,1.5)}:host([open]){display:block}.sr-only{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);border:0}.droplist{position:absolute;top:100%;left:0;right:0;max-height:250px;z-index:9999;overflow-y:scroll;overflow-x:hidden;border:1px solid #ccc;background-color:#fff}ul{font-family:Overpass,Overpass,Helvetica,helvetica,arial,sans-serif;font-family:var(--pfe-theme--font-family, \"Overpass\", Overpass, Helvetica, helvetica, arial, sans-serif);font-size:16px;font-size:var(--pfe-theme--font-size,16px);line-height:1.5;line-height:var(--pfe-theme--line-height,1.5);border-top:none;margin:0;padding:0;list-style:none;cursor:pointer}ul li{display:list-item;cursor:pointer;padding:10px;margin:0}ul li.active{background-color:#f0f0f0;background-color:var(--pfe-theme--color--surface--lighter,#f0f0f0)}\n/*# sourceMappingURL=pfe-search-droplist.min.css.map */\n</style><div class=\"suggestions-aria-help sr-only\" aria-hidden=\"false\" role=\"status\"></div>\n<div class=\"droplist\">\n <ul role=\"listbox\" tabindex=\"-1\">\n </ul>\n</div>";
  492. }
  493. }, {
  494. key: "templateUrl",
  495. get: function get$$1() {
  496. return "pfe-search-droplist.html";
  497. }
  498. }, {
  499. key: "styleUrl",
  500. get: function get$$1() {
  501. return "pfe-search-droplist.scss";
  502. }
  503. }], [{
  504. key: "version",
  505. get: function get$$1() {
  506. return "1.0.0-prerelease.55";
  507. }
  508. }, {
  509. key: "tag",
  510. get: function get$$1() {
  511. return "pfe-search-droplist";
  512. }
  513. }]);
  514. function PfeSearchDroplist() {
  515. classCallCheck(this, PfeSearchDroplist);
  516. return possibleConstructorReturn(this, (PfeSearchDroplist.__proto__ || Object.getPrototypeOf(PfeSearchDroplist)).call(this, PfeSearchDroplist));
  517. }
  518. createClass(PfeSearchDroplist, [{
  519. key: "connectedCallback",
  520. value: function connectedCallback() {
  521. get(PfeSearchDroplist.prototype.__proto__ || Object.getPrototypeOf(PfeSearchDroplist.prototype), "connectedCallback", this).call(this);
  522. this._ariaAnnounce = this.shadowRoot.querySelector(".suggestions-aria-help");
  523. this.activeIndex = null;
  524. this._ul = this.shadowRoot.querySelector("ul");
  525. this._ul.addEventListener("mousedown", this._optionSelected.bind(this));
  526. }
  527. }, {
  528. key: "disconnectedCallback",
  529. value: function disconnectedCallback() {
  530. this._ul.removeEventListener("mousedown", this._optionSelected);
  531. }
  532. }, {
  533. key: "_optionSelected",
  534. value: function _optionSelected(e) {
  535. if (e.target.tagName === "LI") {
  536. this.emitEvent(PfeAutocomplete.events.select, {
  537. detail: { optionValue: e.target.innerText },
  538. composed: true
  539. });
  540. }
  541. }
  542. }, {
  543. key: "_renderOptions",
  544. value: function _renderOptions() {
  545. this.reflow = "";
  546. var options = this.data;
  547. var ariaAnnounceText = "";
  548. if (this._ariaAnnounceTemplate) {
  549. ariaAnnounceText = this._ariaAnnounceTemplate.replace("${numOptions}", options.length);
  550. }
  551. this._ariaAnnounce.textContent = ariaAnnounceText;
  552. this._ariaAnnounce.setAttribute("aria-live", "polite");
  553. this._ul.innerHTML = "" + options.map(function (item, index) {
  554. return "<li id=\"option-" + index + "\" role=\"option\" tabindex=\"-1\" value=\"" + item + "\">" + item + "</li>";
  555. }).join("");
  556. }
  557. }, {
  558. key: "attributeChangedCallback",
  559. value: function attributeChangedCallback(attr, oldVal, newVal) {
  560. get(PfeSearchDroplist.prototype.__proto__ || Object.getPrototypeOf(PfeSearchDroplist.prototype), "attributeChangedCallback", this).call(this);
  561. if (this[name] !== newVal) {
  562. this[name] = newVal;
  563. }
  564. if (attr === "active-index" && oldVal !== newVal) {
  565. this._activeIndexChanged();
  566. }
  567. if (attr === "reflow") {
  568. this._renderOptions();
  569. }
  570. }
  571. }, {
  572. key: "_activeIndexChanged",
  573. value: function _activeIndexChanged() {
  574. if (!this.data || this.data.length === 0 || this.activeIndex === null || this.activeIndex === "null") return;
  575. // remove active class
  576. if (this._ul.querySelector(".active")) {
  577. this._ul.querySelector(".active").classList.remove("active");
  578. }
  579. // add active class to selected option
  580. var activeOption = this._ul.querySelector("li:nth-child(" + (parseInt(this.activeIndex, 10) + 1) + ")");
  581. activeOption.classList.add("active");
  582. // scroll to selected element when selected item with keyboard is out of view
  583. var ulWrapper = this.shadowRoot.querySelector(".droplist");
  584. var activeOptionHeight = activeOption.offsetHeight;
  585. activeOptionHeight += parseInt(window.getComputedStyle(activeOption).getPropertyValue("margin-bottom"), 10);
  586. ulWrapper.scrollTop = activeOption.offsetTop - ulWrapper.offsetHeight + activeOptionHeight;
  587. }
  588. }, {
  589. key: "open",
  590. get: function get$$1() {
  591. return this.hasAttribute("open");
  592. },
  593. set: function set$$1(val) {
  594. val = Boolean(val);
  595. if (val) {
  596. this.setAttribute("open", "");
  597. } else {
  598. this.removeAttribute("open");
  599. }
  600. }
  601. }, {
  602. key: "activeIndex",
  603. get: function get$$1() {
  604. return this.getAttribute("active-index");
  605. },
  606. set: function set$$1(val) {
  607. this.setAttribute("active-index", val);
  608. }
  609. }, {
  610. key: "reflow",
  611. get: function get$$1() {
  612. return this.hasAttribute("reflow");
  613. },
  614. set: function set$$1(val) {
  615. val = Boolean(val);
  616. if (val) {
  617. this.setAttribute("reflow", "");
  618. } else {
  619. this.removeAttribute("reflow");
  620. }
  621. }
  622. }], [{
  623. key: "observedAttributes",
  624. get: function get$$1() {
  625. return ["open", "reflow", "active-index"];
  626. }
  627. }]);
  628. return PfeSearchDroplist;
  629. }(PFElement);
  630. PFElement.create(PfeSearchDroplist);
  631. PFElement.create(PfeAutocomplete);
  632. return PfeAutocomplete;
  633. })));
  634. //# sourceMappingURL=pfe-autocomplete.umd.js.map