|
163 | 163 | };
|
164 | 164 | }
|
165 | 165 |
|
166 |
| - var configure = (function (ctx) { |
167 |
| - var id = ctx.id, |
168 |
| - name = ctx.name, |
169 |
| - options = ctx.options, |
170 |
| - resultsList = ctx.resultsList, |
171 |
| - resultItem = ctx.resultItem; |
172 |
| - for (var option in options) { |
173 |
| - if (_typeof(options[option]) === "object") { |
174 |
| - if (!ctx[option]) ctx[option] = {}; |
175 |
| - for (var subOption in options[option]) { |
176 |
| - ctx[option][subOption] = options[option][subOption]; |
177 |
| - } |
178 |
| - } else { |
179 |
| - ctx[option] = options[option]; |
180 |
| - } |
181 |
| - } |
182 |
| - ctx.selector = ctx.selector || "#" + name; |
183 |
| - resultsList.destination = resultsList.destination || ctx.selector; |
184 |
| - resultsList.id = resultsList.id || name + "_list_" + id; |
185 |
| - resultItem.id = resultItem.id || name + "_result"; |
186 |
| - ctx.input = typeof ctx.selector === "string" ? document.querySelector(ctx.selector) : ctx.selector(); |
187 |
| - }); |
188 |
| - |
189 | 166 | var select$1 = function select(element) {
|
190 |
| - return typeof element === "string" ? document.querySelector(element) : element; |
| 167 | + return typeof element === "string" ? document.querySelector(element) : element(); |
191 | 168 | };
|
192 | 169 | var create = function create(tag, options) {
|
193 | 170 | var el = typeof tag === "string" ? document.createElement(tag) : tag;
|
|
198 | 175 | } else if (key === "dest") {
|
199 | 176 | select$1(val[0]).insertAdjacentElement(val[1], el);
|
200 | 177 | } else if (key === "around") {
|
201 |
| - var ref = select$1(val); |
| 178 | + var ref = val; |
202 | 179 | ref.parentNode.insertBefore(el, ref);
|
203 | 180 | el.append(ref);
|
204 | 181 | if (ref.getAttribute("autofocus") != null) ref.focus();
|
|
237 | 214 | })).outerHTML;
|
238 | 215 | };
|
239 | 216 |
|
| 217 | + var configure = (function (ctx) { |
| 218 | + var name = ctx.name, |
| 219 | + options = ctx.options, |
| 220 | + resultsList = ctx.resultsList, |
| 221 | + resultItem = ctx.resultItem; |
| 222 | + for (var option in options) { |
| 223 | + if (_typeof(options[option]) === "object") { |
| 224 | + if (!ctx[option]) ctx[option] = {}; |
| 225 | + for (var subOption in options[option]) { |
| 226 | + ctx[option][subOption] = options[option][subOption]; |
| 227 | + } |
| 228 | + } else { |
| 229 | + ctx[option] = options[option]; |
| 230 | + } |
| 231 | + } |
| 232 | + ctx.selector = ctx.selector || "#" + name; |
| 233 | + resultsList.destination = resultsList.destination || ctx.selector; |
| 234 | + resultsList.id = resultsList.id || name + "_list_" + ctx.id; |
| 235 | + resultItem.id = resultItem.id || name + "_result"; |
| 236 | + ctx.input = select$1(ctx.selector); |
| 237 | + }); |
| 238 | + |
240 | 239 | var eventEmitter = (function (name, ctx) {
|
241 | 240 | ctx.input.dispatchEvent(new CustomEvent(name, {
|
242 | 241 | bubbles: true,
|
|
301 | 300 | };
|
302 | 301 | var findMatches = function findMatches(query, ctx) {
|
303 | 302 | var data = ctx.data,
|
304 |
| - searchEngine = ctx.searchEngine, |
305 |
| - diacritics = ctx.diacritics, |
306 |
| - resultsList = ctx.resultsList, |
307 |
| - resultItem = ctx.resultItem; |
| 303 | + searchEngine = ctx.searchEngine; |
308 | 304 | var matches = [];
|
309 | 305 | data.store.forEach(function (value, index) {
|
310 | 306 | var find = function find(key) {
|
311 | 307 | var record = key ? value[key] : value;
|
312 | 308 | var match = typeof searchEngine === "function" ? searchEngine(query, record) : search(query, record, {
|
313 | 309 | mode: searchEngine,
|
314 |
| - diacritics: diacritics, |
315 |
| - highlight: resultItem.highlight |
| 310 | + diacritics: ctx.diacritics, |
| 311 | + highlight: ctx.resultItem.highlight |
316 | 312 | });
|
317 | 313 | if (!match) return;
|
318 | 314 | var result = {
|
|
340 | 336 | }
|
341 | 337 | });
|
342 | 338 | if (data.filter) matches = data.filter(matches);
|
343 |
| - var results = matches.slice(0, resultsList.maxResults); |
| 339 | + var results = matches.slice(0, ctx.resultsList.maxResults); |
344 | 340 | ctx.feedback = {
|
345 | 341 | query: query,
|
346 | 342 | matches: matches,
|
|
362 | 358 | list = ctx.list,
|
363 | 359 | resultItem = ctx.resultItem,
|
364 | 360 | feedback = ctx.feedback;
|
365 |
| - feedback.query; |
366 |
| - var matches = feedback.matches, |
| 361 | + var matches = feedback.matches, |
367 | 362 | results = feedback.results;
|
368 | 363 | ctx.cursor = -1;
|
369 | 364 | list.innerHTML = "";
|
370 | 365 | if (matches.length || resultsList.noResults) {
|
371 |
| - var fragment = document.createDocumentFragment(); |
| 366 | + var fragment = new DocumentFragment(); |
372 | 367 | results.forEach(function (result, index) {
|
373 | 368 | var element = create(resultItem.tag, _objectSpread2({
|
374 | 369 | id: "".concat(resultItem.id, "_").concat(index),
|
|
403 | 398 | eventEmitter("close", ctx);
|
404 | 399 | };
|
405 | 400 | var goTo = function goTo(index, ctx) {
|
406 |
| - var list = ctx.list, |
407 |
| - resultItem = ctx.resultItem; |
408 |
| - var results = list.getElementsByTagName(resultItem.tag); |
| 401 | + var resultItem = ctx.resultItem; |
| 402 | + var results = ctx.list.getElementsByTagName(resultItem.tag); |
409 | 403 | var cls = resultItem.selected ? resultItem.selected.split(" ") : false;
|
410 | 404 | if (ctx.isOpen && results.length) {
|
411 | 405 | var _results$index$classL;
|
|
421 | 415 | results[index].setAttribute(Selected, true);
|
422 | 416 | if (cls) (_results$index$classL = results[index].classList).add.apply(_results$index$classL, _toConsumableArray(cls));
|
423 | 417 | ctx.input.setAttribute(Active, results[ctx.cursor].id);
|
424 |
| - list.scrollTop = results[index].offsetTop - list.clientHeight + results[index].clientHeight + 5; |
| 418 | + ctx.list.scrollTop = results[index].offsetTop - ctx.list.clientHeight + results[index].clientHeight + 5; |
425 | 419 | ctx.feedback.cursor = ctx.cursor;
|
426 | 420 | feedback(ctx, index);
|
427 | 421 | eventEmitter("navigate", ctx);
|
|
476 | 470 | function start (ctx, q) {
|
477 | 471 | var _this = this;
|
478 | 472 | return new Promise(function ($return, $error) {
|
479 |
| - var input, query, trigger, threshold, resultsList, queryVal, condition; |
480 |
| - input = ctx.input; |
481 |
| - query = ctx.query; |
482 |
| - trigger = ctx.trigger; |
483 |
| - threshold = ctx.threshold; |
484 |
| - resultsList = ctx.resultsList; |
485 |
| - queryVal = q || getQuery(input); |
486 |
| - queryVal = query ? query(queryVal) : queryVal; |
487 |
| - condition = checkTrigger(queryVal, trigger, threshold); |
| 473 | + var queryVal, condition; |
| 474 | + queryVal = q || getQuery(ctx.input); |
| 475 | + queryVal = ctx.query ? query(queryVal) : queryVal; |
| 476 | + condition = checkTrigger(queryVal, ctx.trigger, ctx.threshold); |
488 | 477 | if (condition) {
|
489 | 478 | return getData(ctx).then(function ($await_2) {
|
490 | 479 | try {
|
491 | 480 | if (ctx.feedback instanceof Error) return $return();
|
492 | 481 | findMatches(queryVal, ctx);
|
493 |
| - if (resultsList) render(ctx); |
| 482 | + if (ctx.resultsList) render(ctx); |
494 | 483 | return $If_1.call(_this);
|
495 | 484 | } catch ($boundEx) {
|
496 | 485 | return $error($boundEx);
|
|
515 | 504 | };
|
516 | 505 | var addEvents = function addEvents(ctx) {
|
517 | 506 | var events = ctx.events;
|
518 |
| - ctx.trigger; |
519 |
| - var timer = ctx.debounce, |
520 |
| - resultsList = ctx.resultsList; |
521 | 507 | var run = debounce(function () {
|
522 | 508 | return start(ctx);
|
523 |
| - }, timer); |
| 509 | + }, ctx.debounce); |
524 | 510 | var publicEvents = ctx.events = _objectSpread2({
|
525 | 511 | input: _objectSpread2({}, events && events.input)
|
526 |
| - }, resultsList && { |
| 512 | + }, ctx.resultsList && { |
527 | 513 | list: events ? _objectSpread2({}, events.list) : {}
|
528 | 514 | });
|
529 | 515 | var privateEvents = {
|
|
548 | 534 | }
|
549 | 535 | };
|
550 | 536 | eventsManager(privateEvents, function (element, event) {
|
551 |
| - if (!resultsList && event !== "input") return; |
| 537 | + if (!ctx.resultsList && event !== "input") return; |
552 | 538 | if (publicEvents[element][event]) return;
|
553 | 539 | publicEvents[element][event] = privateEvents[element][event];
|
554 | 540 | });
|
|
565 | 551 | function init (ctx) {
|
566 | 552 | var _this = this;
|
567 | 553 | return new Promise(function ($return, $error) {
|
568 |
| - var name, input, placeHolder, resultsList, data, parentAttrs; |
569 |
| - name = ctx.name; |
570 |
| - input = ctx.input; |
| 554 | + var placeHolder, resultsList, parentAttrs; |
571 | 555 | placeHolder = ctx.placeHolder;
|
572 | 556 | resultsList = ctx.resultsList;
|
573 |
| - data = ctx.data; |
574 | 557 | parentAttrs = {
|
575 | 558 | role: "combobox",
|
576 | 559 | "aria-owns": resultsList.id,
|
577 | 560 | "aria-haspopup": true,
|
578 | 561 | "aria-expanded": false
|
579 | 562 | };
|
580 |
| - create(input, _objectSpread2(_objectSpread2({ |
| 563 | + create(ctx.input, _objectSpread2(_objectSpread2({ |
581 | 564 | "aria-controls": resultsList.id,
|
582 | 565 | "aria-autocomplete": "both"
|
583 | 566 | }, placeHolder && {
|
584 | 567 | placeholder: placeHolder
|
585 | 568 | }), !ctx.wrapper && _objectSpread2({}, parentAttrs)));
|
586 | 569 | if (ctx.wrapper) ctx.wrapper = create("div", _objectSpread2({
|
587 |
| - around: input, |
588 |
| - "class": name + "_wrapper" |
| 570 | + around: ctx.input, |
| 571 | + "class": ctx.name + "_wrapper" |
589 | 572 | }, parentAttrs));
|
590 | 573 | if (resultsList) ctx.list = create(resultsList.tag, _objectSpread2({
|
591 |
| - dest: [typeof resultsList.destination === "string" ? document.querySelector(resultsList.destination) : resultsList.destination(), resultsList.position], |
| 574 | + dest: [resultsList.destination, resultsList.position], |
592 | 575 | id: resultsList.id,
|
593 | 576 | role: "listbox",
|
594 | 577 | hidden: "hidden"
|
595 | 578 | }, resultsList["class"] && {
|
596 | 579 | "class": resultsList["class"]
|
597 | 580 | }));
|
598 |
| - if (data.cache) { |
| 581 | + if (ctx.data.cache) { |
599 | 582 | return getData(ctx).then(function ($await_2) {
|
600 | 583 | try {
|
601 | 584 | return $If_1.call(_this);
|
|
648 | 631 | select(this, null, index);
|
649 | 632 | };
|
650 | 633 | autoComplete.search = prototype.search = function (query, record, options) {
|
651 |
| - search(query, record, options); |
| 634 | + return search(query, record, options); |
652 | 635 | };
|
653 | 636 | }
|
654 | 637 |
|
|
0 commit comments