@hotwired/turbo 7.0.0-rc.2 → 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Turbo 7.0.0-rc.2
2
+ Turbo 7.0.0
3
3
  Copyright © 2021 Basecamp, LLC
4
4
  */
5
5
  (function (global, factory) {
@@ -41,6 +41,8 @@ Copyright © 2021 Basecamp, LLC
41
41
  (function () {
42
42
  if ("SubmitEvent" in window)
43
43
  return;
44
+ if ("submitter" in Event.prototype)
45
+ return;
44
46
  addEventListener("click", clickCaptured, true);
45
47
  Object.defineProperty(Event.prototype, "submitter", {
46
48
  get() {
@@ -228,11 +230,11 @@ Copyright © 2021 Basecamp, LLC
228
230
  return this.header("Content-Type");
229
231
  }
230
232
  get responseText() {
231
- return this.response.text();
233
+ return this.response.clone().text();
232
234
  }
233
235
  get responseHTML() {
234
236
  if (this.isHTML) {
235
- return this.response.text();
237
+ return this.response.clone().text();
236
238
  }
237
239
  else {
238
240
  return Promise.resolve(undefined);
@@ -245,7 +247,12 @@ Copyright © 2021 Basecamp, LLC
245
247
 
246
248
  function dispatch(eventName, { target, cancelable, detail } = {}) {
247
249
  const event = new CustomEvent(eventName, { cancelable, bubbles: true, detail });
248
- void (target || document.documentElement).dispatchEvent(event);
250
+ if (target && target.isConnected) {
251
+ target.dispatchEvent(event);
252
+ }
253
+ else {
254
+ document.documentElement.dispatchEvent(event);
255
+ }
249
256
  return event;
250
257
  }
251
258
  function nextAnimationFrame() {
@@ -307,7 +314,7 @@ Copyright © 2021 Basecamp, LLC
307
314
  }
308
315
  }
309
316
  class FetchRequest {
310
- constructor(delegate, method, location, body = new URLSearchParams) {
317
+ constructor(delegate, method, location, body = new URLSearchParams, target = null) {
311
318
  this.abortController = new AbortController;
312
319
  this.resolveRequestPromise = (value) => { };
313
320
  this.delegate = delegate;
@@ -320,6 +327,7 @@ Copyright © 2021 Basecamp, LLC
320
327
  this.body = body;
321
328
  this.url = location;
322
329
  }
330
+ this.target = target;
323
331
  }
324
332
  get location() {
325
333
  return this.url;
@@ -355,7 +363,7 @@ Copyright © 2021 Basecamp, LLC
355
363
  }
356
364
  async receive(response) {
357
365
  const fetchResponse = new FetchResponse(response);
358
- const event = dispatch("turbo:before-fetch-response", { cancelable: true, detail: { fetchResponse } });
366
+ const event = dispatch("turbo:before-fetch-response", { cancelable: true, detail: { fetchResponse }, target: this.target });
359
367
  if (event.defaultPrevented) {
360
368
  this.delegate.requestPreventedHandlingResponse(this, fetchResponse);
361
369
  }
@@ -392,7 +400,15 @@ Copyright © 2021 Basecamp, LLC
392
400
  }
393
401
  async allowRequestToBeIntercepted(fetchOptions) {
394
402
  const requestInterception = new Promise(resolve => this.resolveRequestPromise = resolve);
395
- const event = dispatch("turbo:before-fetch-request", { cancelable: true, detail: { fetchOptions, url: this.url.href, resume: this.resolveRequestPromise } });
403
+ const event = dispatch("turbo:before-fetch-request", {
404
+ cancelable: true,
405
+ detail: {
406
+ fetchOptions,
407
+ url: this.url.href,
408
+ resume: this.resolveRequestPromise
409
+ },
410
+ target: this.target
411
+ });
396
412
  if (event.defaultPrevented)
397
413
  await requestInterception;
398
414
  }
@@ -505,7 +521,7 @@ Copyright © 2021 Basecamp, LLC
505
521
  this.formElement = formElement;
506
522
  this.submitter = submitter;
507
523
  this.formData = buildFormData(formElement, submitter);
508
- this.fetchRequest = new FetchRequest(this, this.method, this.location, this.body);
524
+ this.fetchRequest = new FetchRequest(this, this.method, this.location, this.body, this.formElement);
509
525
  this.mustRedirect = mustRedirect;
510
526
  }
511
527
  get method() {
@@ -1118,6 +1134,7 @@ Copyright © 2021 Basecamp, LLC
1118
1134
  super(...arguments);
1119
1135
  this.detailsByOuterHTML = this.children
1120
1136
  .filter((element) => !elementIsNoscript(element))
1137
+ .map((element) => elementWithoutNonce(element))
1121
1138
  .reduce((result, element) => {
1122
1139
  const { outerHTML } = element;
1123
1140
  const details = outerHTML in result
@@ -1202,6 +1219,12 @@ Copyright © 2021 Basecamp, LLC
1202
1219
  const tagName = element.tagName.toLowerCase();
1203
1220
  return tagName == "meta" && element.getAttribute("name") == name;
1204
1221
  }
1222
+ function elementWithoutNonce(element) {
1223
+ if (element.hasAttribute("nonce")) {
1224
+ element.setAttribute("nonce", "");
1225
+ }
1226
+ return element;
1227
+ }
1205
1228
 
1206
1229
  class PageSnapshot extends Snapshot {
1207
1230
  constructor(element, headSnapshot) {
@@ -1587,7 +1610,7 @@ Copyright © 2021 Basecamp, LLC
1587
1610
  visitRequestStarted(visit) {
1588
1611
  this.progressBar.setValue(0);
1589
1612
  if (visit.hasCachedSnapshot() || visit.action != "restore") {
1590
- this.showProgressBarAfterDelay();
1613
+ this.showVisitProgressBarAfterDelay();
1591
1614
  }
1592
1615
  else {
1593
1616
  this.showProgressBar();
@@ -1608,7 +1631,7 @@ Copyright © 2021 Basecamp, LLC
1608
1631
  }
1609
1632
  visitRequestFinished(visit) {
1610
1633
  this.progressBar.setValue(1);
1611
- this.hideProgressBar();
1634
+ this.hideVisitProgressBar();
1612
1635
  }
1613
1636
  visitCompleted(visit) {
1614
1637
  }
@@ -1621,20 +1644,32 @@ Copyright © 2021 Basecamp, LLC
1621
1644
  }
1622
1645
  formSubmissionStarted(formSubmission) {
1623
1646
  this.progressBar.setValue(0);
1624
- this.showProgressBarAfterDelay();
1647
+ this.showFormProgressBarAfterDelay();
1625
1648
  }
1626
1649
  formSubmissionFinished(formSubmission) {
1627
1650
  this.progressBar.setValue(1);
1628
- this.hideProgressBar();
1651
+ this.hideFormProgressBar();
1652
+ }
1653
+ showVisitProgressBarAfterDelay() {
1654
+ this.visitProgressBarTimeout = window.setTimeout(this.showProgressBar, this.session.progressBarDelay);
1655
+ }
1656
+ hideVisitProgressBar() {
1657
+ this.progressBar.hide();
1658
+ if (this.visitProgressBarTimeout != null) {
1659
+ window.clearTimeout(this.visitProgressBarTimeout);
1660
+ delete this.visitProgressBarTimeout;
1661
+ }
1629
1662
  }
1630
- showProgressBarAfterDelay() {
1631
- this.progressBarTimeout = window.setTimeout(this.showProgressBar, this.session.progressBarDelay);
1663
+ showFormProgressBarAfterDelay() {
1664
+ if (this.formProgressBarTimeout == null) {
1665
+ this.formProgressBarTimeout = window.setTimeout(this.showProgressBar, this.session.progressBarDelay);
1666
+ }
1632
1667
  }
1633
- hideProgressBar() {
1668
+ hideFormProgressBar() {
1634
1669
  this.progressBar.hide();
1635
- if (this.progressBarTimeout != null) {
1636
- window.clearTimeout(this.progressBarTimeout);
1637
- delete this.progressBarTimeout;
1670
+ if (this.formProgressBarTimeout != null) {
1671
+ window.clearTimeout(this.formProgressBarTimeout);
1672
+ delete this.formProgressBarTimeout;
1638
1673
  }
1639
1674
  }
1640
1675
  reload() {
@@ -1725,6 +1760,7 @@ Copyright © 2021 Basecamp, LLC
1725
1760
  linkClickIntercepted(element, url) {
1726
1761
  const frame = this.findFrameElement(element);
1727
1762
  if (frame) {
1763
+ frame.setAttribute("reloadable", "");
1728
1764
  frame.src = url;
1729
1765
  }
1730
1766
  }
@@ -1732,17 +1768,18 @@ Copyright © 2021 Basecamp, LLC
1732
1768
  return this.shouldRedirect(element, submitter);
1733
1769
  }
1734
1770
  formSubmissionIntercepted(element, submitter) {
1735
- const frame = this.findFrameElement(element);
1771
+ const frame = this.findFrameElement(element, submitter);
1736
1772
  if (frame) {
1773
+ frame.removeAttribute("reloadable");
1737
1774
  frame.delegate.formSubmissionIntercepted(element, submitter);
1738
1775
  }
1739
1776
  }
1740
1777
  shouldRedirect(element, submitter) {
1741
- const frame = this.findFrameElement(element);
1778
+ const frame = this.findFrameElement(element, submitter);
1742
1779
  return frame ? frame != element.closest("turbo-frame") : false;
1743
1780
  }
1744
- findFrameElement(element) {
1745
- const id = element.getAttribute("data-turbo-frame");
1781
+ findFrameElement(element, submitter) {
1782
+ const id = (submitter === null || submitter === void 0 ? void 0 : submitter.getAttribute("data-turbo-frame")) || element.getAttribute("data-turbo-frame");
1746
1783
  if (id && id != "_top") {
1747
1784
  const frame = this.element.querySelector(`#${id}:not([disabled])`);
1748
1785
  if (frame instanceof FrameElement) {
@@ -2454,12 +2491,14 @@ Copyright © 2021 Basecamp, LLC
2454
2491
  this.convertLinkWithMethodClickToFormSubmission(link) || this.visit(location.href, { action });
2455
2492
  }
2456
2493
  convertLinkWithMethodClickToFormSubmission(link) {
2494
+ var _a;
2457
2495
  const linkMethod = link.getAttribute("data-turbo-method");
2458
2496
  if (linkMethod) {
2459
2497
  const form = document.createElement("form");
2460
2498
  form.method = linkMethod;
2461
2499
  form.action = link.getAttribute("href") || "undefined";
2462
- document.body.appendChild(form);
2500
+ form.hidden = true;
2501
+ (_a = link.parentNode) === null || _a === void 0 ? void 0 : _a.insertBefore(form, link);
2463
2502
  return dispatch("submit", { cancelable: true, target: form });
2464
2503
  }
2465
2504
  else {
@@ -2768,7 +2807,7 @@ Copyright © 2021 Basecamp, LLC
2768
2807
  this.reloadable = false;
2769
2808
  this.formSubmission = new FormSubmission(this, element, submitter);
2770
2809
  if (this.formSubmission.fetchRequest.isIdempotent) {
2771
- this.navigateFrame(element, this.formSubmission.fetchRequest.url.href);
2810
+ this.navigateFrame(element, this.formSubmission.fetchRequest.url.href, submitter);
2772
2811
  }
2773
2812
  else {
2774
2813
  const { fetchRequest } = this.formSubmission;
@@ -2805,7 +2844,7 @@ Copyright © 2021 Basecamp, LLC
2805
2844
  frame.setAttribute("busy", "");
2806
2845
  }
2807
2846
  formSubmissionSucceededWithResponse(formSubmission, response) {
2808
- const frame = this.findFrameElement(formSubmission.formElement);
2847
+ const frame = this.findFrameElement(formSubmission.formElement, formSubmission.submitter);
2809
2848
  frame.delegate.loadResponse(response);
2810
2849
  }
2811
2850
  formSubmissionFailedWithResponse(formSubmission, fetchResponse) {
@@ -2826,7 +2865,7 @@ Copyright © 2021 Basecamp, LLC
2826
2865
  viewInvalidated() {
2827
2866
  }
2828
2867
  async visit(url) {
2829
- const request = new FetchRequest(this, FetchMethod.get, expandURL(url));
2868
+ const request = new FetchRequest(this, FetchMethod.get, expandURL(url), undefined, this.element);
2830
2869
  return new Promise(resolve => {
2831
2870
  this.resolveVisitPromise = () => {
2832
2871
  this.resolveVisitPromise = () => { };
@@ -2835,13 +2874,14 @@ Copyright © 2021 Basecamp, LLC
2835
2874
  request.perform();
2836
2875
  });
2837
2876
  }
2838
- navigateFrame(element, url) {
2839
- const frame = this.findFrameElement(element);
2877
+ navigateFrame(element, url, submitter) {
2878
+ const frame = this.findFrameElement(element, submitter);
2879
+ frame.setAttribute("reloadable", "");
2840
2880
  frame.src = url;
2841
2881
  }
2842
- findFrameElement(element) {
2882
+ findFrameElement(element, submitter) {
2843
2883
  var _a;
2844
- const id = element.getAttribute("data-turbo-frame") || this.element.getAttribute("target");
2884
+ const id = (submitter === null || submitter === void 0 ? void 0 : submitter.getAttribute("data-turbo-frame")) || element.getAttribute("data-turbo-frame") || this.element.getAttribute("target");
2845
2885
  return (_a = getFrameElementById(id)) !== null && _a !== void 0 ? _a : this.element;
2846
2886
  }
2847
2887
  async extractForeignFrameElement(container) {
@@ -2863,7 +2903,7 @@ Copyright © 2021 Basecamp, LLC
2863
2903
  return new FrameElement();
2864
2904
  }
2865
2905
  shouldInterceptNavigation(element, submitter) {
2866
- const id = element.getAttribute("data-turbo-frame") || this.element.getAttribute("target");
2906
+ const id = (submitter === null || submitter === void 0 ? void 0 : submitter.getAttribute("data-turbo-frame")) || element.getAttribute("data-turbo-frame") || this.element.getAttribute("target");
2867
2907
  if (!this.enabled || id == "_top") {
2868
2908
  return false;
2869
2909
  }
@@ -3126,4 +3166,3 @@ Copyright © 2021 Basecamp, LLC
3126
3166
  Object.defineProperty(exports, '__esModule', { value: true });
3127
3167
 
3128
3168
  })));
3129
- //# sourceMappingURL=turbo.es2017-umd.js.map