swup 2.0.19 → 3.0.0-rc.2

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.
Files changed (117) hide show
  1. package/dist/Swup.cjs +2 -0
  2. package/dist/Swup.cjs.map +1 -0
  3. package/dist/Swup.modern.js +2 -0
  4. package/dist/Swup.modern.js.map +1 -0
  5. package/dist/Swup.module.js +2 -0
  6. package/dist/Swup.module.js.map +1 -0
  7. package/dist/Swup.umd.js +2 -0
  8. package/dist/Swup.umd.js.map +1 -0
  9. package/dist/helpers.cjs +2 -0
  10. package/dist/helpers.cjs.map +1 -0
  11. package/dist/helpers.modern.js +2 -0
  12. package/dist/helpers.modern.js.map +1 -0
  13. package/dist/helpers.module.js +2 -0
  14. package/dist/helpers.module.js.map +1 -0
  15. package/dist/types/Swup.d.ts +102 -0
  16. package/dist/types/config/version.d.ts +5 -0
  17. package/dist/types/helpers/Location.d.ts +31 -0
  18. package/dist/types/helpers/classify.d.ts +2 -0
  19. package/dist/types/helpers/cleanupAnimationClasses.d.ts +2 -0
  20. package/dist/types/helpers/createHistoryRecord.d.ts +2 -0
  21. package/dist/types/helpers/delegateEvent.d.ts +8 -0
  22. package/dist/types/helpers/fetch.d.ts +6 -0
  23. package/dist/types/helpers/getCurrentUrl.d.ts +4 -0
  24. package/dist/types/helpers/getDataFromHtml.d.ts +8 -0
  25. package/dist/types/helpers/index.d.ts +11 -0
  26. package/dist/types/helpers/markSwupElements.d.ts +2 -0
  27. package/dist/types/helpers/updateHistoryRecord.d.ts +2 -0
  28. package/dist/types/helpers/versionSatisfies.d.ts +12 -0
  29. package/dist/types/helpers.d.ts +1 -0
  30. package/dist/types/index.d.ts +8 -0
  31. package/dist/types/modules/Cache.d.ts +20 -0
  32. package/dist/types/modules/enterPage.d.ts +6 -0
  33. package/dist/types/modules/fetchPage.d.ts +4 -0
  34. package/dist/types/modules/getAnchorElement.d.ts +2 -0
  35. package/dist/types/modules/getAnimationPromises.d.ts +7 -0
  36. package/dist/types/modules/getPageData.d.ts +7 -0
  37. package/dist/types/modules/leavePage.d.ts +7 -0
  38. package/dist/types/modules/loadPage.d.ts +7 -0
  39. package/dist/types/modules/off.d.ts +4 -0
  40. package/dist/types/modules/on.d.ts +6 -0
  41. package/dist/types/modules/plugins.d.ts +14 -0
  42. package/dist/types/modules/renderPage.d.ts +7 -0
  43. package/dist/types/modules/replaceContent.d.ts +17 -0
  44. package/dist/types/modules/triggerEvent.d.ts +4 -0
  45. package/dist/types/modules/updateTransition.d.ts +3 -0
  46. package/dist/types/utils/index.d.ts +5 -0
  47. package/dist/types/utils.d.ts +1 -0
  48. package/dist/utils.cjs +2 -0
  49. package/dist/utils.cjs.map +1 -0
  50. package/dist/utils.modern.js +2 -0
  51. package/dist/utils.modern.js.map +1 -0
  52. package/dist/utils.module.js +2 -0
  53. package/dist/utils.module.js.map +1 -0
  54. package/package.json +30 -23
  55. package/readme.md +52 -36
  56. package/src/Swup.ts +369 -0
  57. package/src/config/version.ts +13 -0
  58. package/src/helpers/Location.ts +44 -0
  59. package/src/helpers/classify.ts +13 -0
  60. package/src/helpers/cleanupAnimationClasses.ts +10 -0
  61. package/src/helpers/createHistoryRecord.ts +14 -0
  62. package/src/helpers/delegateEvent.ts +23 -0
  63. package/src/helpers/fetch.ts +35 -0
  64. package/src/helpers/getCurrentUrl.ts +5 -0
  65. package/src/helpers/getDataFromHtml.ts +41 -0
  66. package/src/helpers/index.ts +11 -0
  67. package/src/helpers/markSwupElements.ts +18 -0
  68. package/src/helpers/updateHistoryRecord.ts +18 -0
  69. package/src/helpers/versionSatisfies.ts +46 -0
  70. package/src/helpers.ts +4 -0
  71. package/src/index.ts +8 -0
  72. package/src/modules/Cache.ts +57 -0
  73. package/src/modules/enterPage.ts +28 -0
  74. package/src/modules/fetchPage.ts +35 -0
  75. package/src/modules/getAnchorElement.ts +19 -0
  76. package/src/modules/getAnimationPromises.ts +179 -0
  77. package/src/modules/getPageData.ts +26 -0
  78. package/src/modules/leavePage.ts +33 -0
  79. package/src/modules/loadPage.ts +55 -0
  80. package/src/modules/off.ts +23 -0
  81. package/src/modules/on.ts +35 -0
  82. package/src/modules/plugins.ts +58 -0
  83. package/src/modules/renderPage.ts +52 -0
  84. package/src/modules/replaceContent.ts +28 -0
  85. package/src/modules/triggerEvent.ts +23 -0
  86. package/src/modules/updateTransition.ts +7 -0
  87. package/src/utils/index.ts +32 -0
  88. package/src/utils.ts +4 -0
  89. package/.editorconfig +0 -19
  90. package/cypress.config.js +0 -14
  91. package/dist/swup.js +0 -1524
  92. package/dist/swup.min.js +0 -1
  93. package/lib/helpers/Link.js +0 -56
  94. package/lib/helpers/classify.js +0 -18
  95. package/lib/helpers/cleanupAnimationClasses.js +0 -18
  96. package/lib/helpers/createHistoryRecord.js +0 -14
  97. package/lib/helpers/fetch.js +0 -41
  98. package/lib/helpers/getCurrentUrl.js +0 -10
  99. package/lib/helpers/getDataFromHtml.js +0 -43
  100. package/lib/helpers/index.js +0 -64
  101. package/lib/helpers/markSwupElements.js +0 -24
  102. package/lib/helpers/normalizeUrl.js +0 -17
  103. package/lib/helpers/transitionEnd.js +0 -14
  104. package/lib/helpers/transitionProperty.js +0 -14
  105. package/lib/index.js +0 -305
  106. package/lib/modules/Cache.js +0 -66
  107. package/lib/modules/getAnchorElement.js +0 -25
  108. package/lib/modules/getAnimationPromises.js +0 -43
  109. package/lib/modules/getPageData.js +0 -26
  110. package/lib/modules/loadPage.js +0 -123
  111. package/lib/modules/off.js +0 -34
  112. package/lib/modules/on.js +0 -14
  113. package/lib/modules/plugins.js +0 -54
  114. package/lib/modules/renderPage.js +0 -76
  115. package/lib/modules/triggerEvent.js +0 -21
  116. package/lib/modules/updateTransition.js +0 -15
  117. package/lib/utils/index.js +0 -32
package/lib/index.js DELETED
@@ -1,305 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
-
7
- var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
8
-
9
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
10
-
11
- // modules
12
-
13
-
14
- var _delegateIt = require('delegate-it');
15
-
16
- var _delegateIt2 = _interopRequireDefault(_delegateIt);
17
-
18
- var _Cache = require('./modules/Cache');
19
-
20
- var _Cache2 = _interopRequireDefault(_Cache);
21
-
22
- var _loadPage = require('./modules/loadPage');
23
-
24
- var _loadPage2 = _interopRequireDefault(_loadPage);
25
-
26
- var _renderPage = require('./modules/renderPage');
27
-
28
- var _renderPage2 = _interopRequireDefault(_renderPage);
29
-
30
- var _triggerEvent = require('./modules/triggerEvent');
31
-
32
- var _triggerEvent2 = _interopRequireDefault(_triggerEvent);
33
-
34
- var _on = require('./modules/on');
35
-
36
- var _on2 = _interopRequireDefault(_on);
37
-
38
- var _off = require('./modules/off');
39
-
40
- var _off2 = _interopRequireDefault(_off);
41
-
42
- var _updateTransition = require('./modules/updateTransition');
43
-
44
- var _updateTransition2 = _interopRequireDefault(_updateTransition);
45
-
46
- var _getAnchorElement = require('./modules/getAnchorElement');
47
-
48
- var _getAnchorElement2 = _interopRequireDefault(_getAnchorElement);
49
-
50
- var _getAnimationPromises = require('./modules/getAnimationPromises');
51
-
52
- var _getAnimationPromises2 = _interopRequireDefault(_getAnimationPromises);
53
-
54
- var _getPageData = require('./modules/getPageData');
55
-
56
- var _getPageData2 = _interopRequireDefault(_getPageData);
57
-
58
- var _plugins = require('./modules/plugins');
59
-
60
- var _utils = require('./utils');
61
-
62
- var _helpers = require('./helpers');
63
-
64
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
65
-
66
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
67
-
68
- var Swup = function () {
69
- function Swup(setOptions) {
70
- _classCallCheck(this, Swup);
71
-
72
- // default options
73
- var defaults = {
74
- animateHistoryBrowsing: false,
75
- animationSelector: '[class*="transition-"]',
76
- linkSelector: 'a[href^="' + window.location.origin + '"]:not([data-no-swup]), a[href^="/"]:not([data-no-swup]), a[href^="#"]:not([data-no-swup])',
77
- cache: true,
78
- containers: ['#swup'],
79
- requestHeaders: {
80
- 'X-Requested-With': 'swup',
81
- Accept: 'text/html, application/xhtml+xml'
82
- },
83
- plugins: [],
84
- skipPopStateHandling: function skipPopStateHandling(event) {
85
- return !(event.state && event.state.source === 'swup');
86
- }
87
- };
88
-
89
- // merge options
90
- var options = _extends({}, defaults, setOptions);
91
-
92
- // handler arrays
93
- this._handlers = {
94
- animationInDone: [],
95
- animationInStart: [],
96
- animationOutDone: [],
97
- animationOutStart: [],
98
- animationSkipped: [],
99
- clickLink: [],
100
- contentReplaced: [],
101
- disabled: [],
102
- enabled: [],
103
- openPageInNewTab: [],
104
- pageLoaded: [],
105
- pageRetrievedFromCache: [],
106
- pageView: [],
107
- popState: [],
108
- samePage: [],
109
- samePageWithHash: [],
110
- serverError: [],
111
- transitionStart: [],
112
- transitionEnd: [],
113
- willReplaceContent: []
114
- };
115
-
116
- // variable for anchor to scroll to after render
117
- this.scrollToElement = null;
118
- // variable for promise used for preload, so no new loading of the same page starts while page is loading
119
- this.preloadPromise = null;
120
- // variable for save options
121
- this.options = options;
122
- // variable for plugins array
123
- this.plugins = [];
124
- // variable for current transition object
125
- this.transition = {};
126
- // variable for keeping event listeners from "delegate"
127
- this.delegatedListeners = {};
128
- // so we are able to remove the listener
129
- this.boundPopStateHandler = this.popStateHandler.bind(this);
130
-
131
- // make modules accessible in instance
132
- this.cache = new _Cache2.default();
133
- this.cache.swup = this;
134
- this.loadPage = _loadPage2.default;
135
- this.renderPage = _renderPage2.default;
136
- this.triggerEvent = _triggerEvent2.default;
137
- this.on = _on2.default;
138
- this.off = _off2.default;
139
- this.updateTransition = _updateTransition2.default;
140
- this.getAnimationPromises = _getAnimationPromises2.default;
141
- this.getPageData = _getPageData2.default;
142
- this.getAnchorElement = _getAnchorElement2.default;
143
- this.log = function () {}; // here so it can be used by plugins
144
- this.use = _plugins.use;
145
- this.unuse = _plugins.unuse;
146
- this.findPlugin = _plugins.findPlugin;
147
- this.getCurrentUrl = _helpers.getCurrentUrl;
148
- this.cleanupAnimationClasses = _helpers.cleanupAnimationClasses;
149
-
150
- // enable swup
151
- this.enable();
152
- }
153
-
154
- _createClass(Swup, [{
155
- key: 'enable',
156
- value: function enable() {
157
- var _this = this;
158
-
159
- // check for Promise support
160
- if (typeof Promise === 'undefined') {
161
- console.warn('Promise is not supported');
162
- return;
163
- }
164
-
165
- // add event listeners
166
- this.delegatedListeners.click = (0, _delegateIt2.default)(document, this.options.linkSelector, 'click', this.linkClickHandler.bind(this));
167
- window.addEventListener('popstate', this.boundPopStateHandler);
168
-
169
- // initial save to cache
170
- if (this.options.cache) {}
171
- // disabled to avoid caching modified dom state
172
- // https://github.com/swup/swup/issues/475
173
- // logic moved to preload plugin
174
-
175
-
176
- // mark swup blocks in html
177
- (0, _helpers.markSwupElements)(document.documentElement, this.options.containers);
178
-
179
- // mount plugins
180
- this.options.plugins.forEach(function (plugin) {
181
- _this.use(plugin);
182
- });
183
-
184
- // modify initial history record
185
- window.history.replaceState(Object.assign({}, window.history.state, {
186
- url: window.location.href,
187
- random: Math.random(),
188
- source: 'swup'
189
- }), document.title, window.location.href);
190
-
191
- // trigger enabled event
192
- this.triggerEvent('enabled');
193
-
194
- // add swup-enabled class to html tag
195
- document.documentElement.classList.add('swup-enabled');
196
-
197
- // trigger page view event
198
- this.triggerEvent('pageView');
199
- }
200
- }, {
201
- key: 'destroy',
202
- value: function destroy() {
203
- var _this2 = this;
204
-
205
- // remove delegated listeners
206
- this.delegatedListeners.click.destroy();
207
-
208
- // remove popstate listener
209
- window.removeEventListener('popstate', this.boundPopStateHandler);
210
-
211
- // empty cache
212
- this.cache.empty();
213
-
214
- // unmount plugins
215
- this.options.plugins.forEach(function (plugin) {
216
- _this2.unuse(plugin);
217
- });
218
-
219
- // remove swup data atributes from blocks
220
- (0, _utils.queryAll)('[data-swup]').forEach(function (element) {
221
- element.removeAttribute('data-swup');
222
- });
223
-
224
- // remove handlers
225
- this.off();
226
-
227
- // trigger disable event
228
- this.triggerEvent('disabled');
229
-
230
- // remove swup-enabled class from html tag
231
- document.documentElement.classList.remove('swup-enabled');
232
- }
233
- }, {
234
- key: 'linkClickHandler',
235
- value: function linkClickHandler(event) {
236
- // no control key pressed
237
- if (!event.metaKey && !event.ctrlKey && !event.shiftKey && !event.altKey) {
238
- // index of pressed button needs to be checked because Firefox triggers click on all mouse buttons
239
- if (event.button === 0) {
240
- this.triggerEvent('clickLink', event);
241
- event.preventDefault();
242
- var link = new _helpers.Link(event.delegateTarget);
243
- if (link.getAddress() == (0, _helpers.getCurrentUrl)() || link.getAddress() == '') {
244
- // link to the same URL
245
- if (link.getHash() != '') {
246
- // link to the same URL with hash
247
- this.triggerEvent('samePageWithHash', event);
248
- var element = (0, _getAnchorElement2.default)(link.getHash());
249
- if (element != null) {
250
- history.replaceState({
251
- url: link.getAddress() + link.getHash(),
252
- random: Math.random(),
253
- source: 'swup'
254
- }, document.title, link.getAddress() + link.getHash());
255
- } else {
256
- // referenced element not found
257
- console.warn('Element for offset not found (' + link.getHash() + ')');
258
- }
259
- } else {
260
- // link to the same URL without hash
261
- this.triggerEvent('samePage', event);
262
- }
263
- } else {
264
- // link to different url
265
- if (link.getHash() != '') {
266
- this.scrollToElement = link.getHash();
267
- }
268
-
269
- // get custom transition from data
270
- var customTransition = event.delegateTarget.getAttribute('data-swup-transition');
271
-
272
- // load page
273
- this.loadPage({ url: link.getAddress(), customTransition: customTransition }, false);
274
- }
275
- }
276
- } else {
277
- // open in new tab (do nothing)
278
- this.triggerEvent('openPageInNewTab', event);
279
- }
280
- }
281
- }, {
282
- key: 'popStateHandler',
283
- value: function popStateHandler(event) {
284
- if (this.options.skipPopStateHandling(event)) return;
285
- var link = new _helpers.Link(event.state ? event.state.url : window.location.pathname);
286
- if (link.getHash() !== '') {
287
- this.scrollToElement = link.getHash();
288
- } else {
289
- event.preventDefault();
290
- }
291
- this.triggerEvent('popState', event);
292
-
293
- if (!this.options.animateHistoryBrowsing) {
294
- document.documentElement.classList.remove('is-animating');
295
- (0, _helpers.cleanupAnimationClasses)();
296
- }
297
-
298
- this.loadPage({ url: link.getAddress() }, event);
299
- }
300
- }]);
301
-
302
- return Swup;
303
- }();
304
-
305
- exports.default = Swup;
@@ -1,66 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.Cache = undefined;
7
-
8
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
9
-
10
- var _helpers = require('../helpers');
11
-
12
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
13
-
14
- var Cache = exports.Cache = function () {
15
- function Cache() {
16
- _classCallCheck(this, Cache);
17
-
18
- this.pages = {};
19
- this.last = null;
20
- }
21
-
22
- _createClass(Cache, [{
23
- key: 'cacheUrl',
24
- value: function cacheUrl(page) {
25
- page.url = (0, _helpers.normalizeUrl)(page.url);
26
- if (page.url in this.pages === false) {
27
- this.pages[page.url] = page;
28
- }
29
- this.last = this.pages[page.url];
30
- this.swup.log('Cache (' + Object.keys(this.pages).length + ')', this.pages);
31
- }
32
- }, {
33
- key: 'getPage',
34
- value: function getPage(url) {
35
- url = (0, _helpers.normalizeUrl)(url);
36
- return this.pages[url];
37
- }
38
- }, {
39
- key: 'getCurrentPage',
40
- value: function getCurrentPage() {
41
- return this.getPage((0, _helpers.getCurrentUrl)());
42
- }
43
- }, {
44
- key: 'exists',
45
- value: function exists(url) {
46
- url = (0, _helpers.normalizeUrl)(url);
47
- return url in this.pages;
48
- }
49
- }, {
50
- key: 'empty',
51
- value: function empty() {
52
- this.pages = {};
53
- this.last = null;
54
- this.swup.log('Cache cleared');
55
- }
56
- }, {
57
- key: 'remove',
58
- value: function remove(url) {
59
- delete this.pages[url];
60
- }
61
- }]);
62
-
63
- return Cache;
64
- }();
65
-
66
- exports.default = Cache;
@@ -1,25 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
-
7
- var _utils = require('../utils');
8
-
9
- var getAnchorElement = function getAnchorElement(hash) {
10
- if (!hash) {
11
- return null;
12
- }
13
-
14
- if (hash.charAt(0) === '#') {
15
- hash = hash.substring(1);
16
- }
17
-
18
- hash = decodeURIComponent(hash);
19
- hash = (0, _utils.escapeCssIdentifier)(hash);
20
-
21
- // https://html.spec.whatwg.org/#find-a-potential-indicated-element
22
- return (0, _utils.query)('#' + hash) || (0, _utils.query)('a[name=\'' + hash + '\']');
23
- };
24
-
25
- exports.default = getAnchorElement;
@@ -1,43 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
-
7
- var _utils = require('../utils');
8
-
9
- var _helpers = require('../helpers');
10
-
11
- var getAnimationPromises = function getAnimationPromises() {
12
- var selector = this.options.animationSelector;
13
- var durationProperty = (0, _helpers.transitionProperty)() + 'Duration';
14
- var promises = [];
15
- var animatedElements = (0, _utils.queryAll)(selector, document.body);
16
-
17
- if (!animatedElements.length) {
18
- console.warn('[swup] No animated elements found by selector ' + selector);
19
- return [Promise.resolve()];
20
- }
21
-
22
- animatedElements.forEach(function (element) {
23
- var transitionDuration = window.getComputedStyle(element)[durationProperty];
24
- // Resolve immediately if no transition defined
25
- if (!transitionDuration || transitionDuration == '0s') {
26
- console.warn('[swup] No CSS transition duration defined for element of selector ' + selector);
27
- promises.push(Promise.resolve());
28
- return;
29
- }
30
- var promise = new Promise(function (resolve) {
31
- element.addEventListener((0, _helpers.transitionEnd)(), function (event) {
32
- if (element == event.target) {
33
- resolve();
34
- }
35
- });
36
- });
37
- promises.push(promise);
38
- });
39
-
40
- return promises;
41
- };
42
-
43
- exports.default = getAnimationPromises;
@@ -1,26 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
-
7
- var _helpers = require('../helpers');
8
-
9
- var getPageData = function getPageData(request) {
10
- // this method can be replaced in case other content than html is expected to be received from server
11
- // this function should always return {title, pageClass, originalContent, blocks, responseURL}
12
- // in case page has invalid structure - return null
13
- var html = request.responseText;
14
- var pageObject = (0, _helpers.getDataFromHtml)(html, this.options.containers);
15
-
16
- if (pageObject) {
17
- pageObject.responseURL = request.responseURL ? request.responseURL : window.location.href;
18
- } else {
19
- console.warn('[swup] Received page is invalid.');
20
- return null;
21
- }
22
-
23
- return pageObject;
24
- };
25
-
26
- exports.default = getPageData;
@@ -1,123 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
-
7
- var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
8
-
9
- var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
10
-
11
- var _helpers = require('../helpers');
12
-
13
- var loadPage = function loadPage(data, popstate) {
14
- var _this = this;
15
-
16
- // create array for storing animation promises
17
- var animationPromises = [],
18
- xhrPromise = void 0;
19
- var animateOut = function animateOut() {
20
- _this.triggerEvent('animationOutStart');
21
-
22
- // handle classes
23
- document.documentElement.classList.add('is-changing');
24
- document.documentElement.classList.add('is-leaving');
25
- document.documentElement.classList.add('is-animating');
26
- if (popstate) {
27
- document.documentElement.classList.add('is-popstate');
28
- }
29
- document.documentElement.classList.add('to-' + (0, _helpers.classify)(data.url));
30
-
31
- // animation promise stuff
32
- animationPromises = _this.getAnimationPromises('out');
33
- Promise.all(animationPromises).then(function () {
34
- _this.triggerEvent('animationOutDone');
35
- });
36
-
37
- // create history record if this is not a popstate call
38
- if (!popstate) {
39
- // create pop element with or without anchor
40
- var state = void 0;
41
- if (_this.scrollToElement != null) {
42
- state = data.url + _this.scrollToElement;
43
- } else {
44
- state = data.url;
45
- }
46
-
47
- (0, _helpers.createHistoryRecord)(state);
48
- }
49
- };
50
-
51
- this.triggerEvent('transitionStart', popstate);
52
-
53
- // set transition object
54
- if (data.customTransition != null) {
55
- this.updateTransition(window.location.pathname, data.url, data.customTransition);
56
- document.documentElement.classList.add('to-' + (0, _helpers.classify)(data.customTransition));
57
- } else {
58
- this.updateTransition(window.location.pathname, data.url);
59
- }
60
-
61
- // start/skip animation
62
- if (!popstate || this.options.animateHistoryBrowsing) {
63
- animateOut();
64
- } else {
65
- this.triggerEvent('animationSkipped');
66
- }
67
-
68
- // start/skip loading of page
69
- if (this.cache.exists(data.url)) {
70
- xhrPromise = new Promise(function (resolve) {
71
- resolve(_this.cache.getPage(data.url));
72
- });
73
- this.triggerEvent('pageRetrievedFromCache');
74
- } else {
75
- if (!this.preloadPromise || this.preloadPromise.route != data.url) {
76
- xhrPromise = new Promise(function (resolve, reject) {
77
- (0, _helpers.fetch)(_extends({}, data, { headers: _this.options.requestHeaders }), function (response) {
78
- if (response.status === 500) {
79
- _this.triggerEvent('serverError');
80
- reject(data.url);
81
- return;
82
- } else {
83
- // get json data
84
- var page = _this.getPageData(response);
85
- if (page != null && page.blocks.length > 0) {
86
- page.url = data.url;
87
- } else {
88
- reject(data.url);
89
- return;
90
- }
91
- // render page
92
- _this.cache.cacheUrl(page);
93
- _this.triggerEvent('pageLoaded');
94
- resolve(page);
95
- }
96
- });
97
- });
98
- } else {
99
- xhrPromise = this.preloadPromise;
100
- }
101
- }
102
-
103
- // when everything is ready, handle the outcome
104
- Promise.all([xhrPromise].concat(animationPromises)).then(function (_ref) {
105
- var _ref2 = _slicedToArray(_ref, 1),
106
- pageData = _ref2[0];
107
-
108
- // render page
109
- _this.renderPage(pageData, popstate);
110
- _this.preloadPromise = null;
111
- }).catch(function (errorUrl) {
112
- // rewrite the skipPopStateHandling function to redirect manually when the history.go is processed
113
- _this.options.skipPopStateHandling = function () {
114
- window.location = errorUrl;
115
- return true;
116
- };
117
-
118
- // go back to the actual page were still at
119
- window.history.go(-1);
120
- });
121
- };
122
-
123
- exports.default = loadPage;
@@ -1,34 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- var off = function off(event, handler) {
7
- var _this = this;
8
-
9
- if (event != null) {
10
- if (handler != null) {
11
- if (this._handlers[event] && this._handlers[event].filter(function (savedHandler) {
12
- return savedHandler === handler;
13
- }).length) {
14
- var toRemove = this._handlers[event].filter(function (savedHandler) {
15
- return savedHandler === handler;
16
- })[0];
17
- var index = this._handlers[event].indexOf(toRemove);
18
- if (index > -1) {
19
- this._handlers[event].splice(index, 1);
20
- }
21
- } else {
22
- console.warn("Handler for event '" + event + "' no found.");
23
- }
24
- } else {
25
- this._handlers[event] = [];
26
- }
27
- } else {
28
- Object.keys(this._handlers).forEach(function (keys) {
29
- _this._handlers[keys] = [];
30
- });
31
- }
32
- };
33
-
34
- exports.default = off;
package/lib/modules/on.js DELETED
@@ -1,14 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- var on = function on(event, handler) {
7
- if (this._handlers[event]) {
8
- this._handlers[event].push(handler);
9
- } else {
10
- console.warn("Unsupported event " + event + ".");
11
- }
12
- };
13
-
14
- exports.default = on;
@@ -1,54 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- var use = exports.use = function use(plugin) {
7
- if (!plugin.isSwupPlugin) {
8
- console.warn('Not swup plugin instance ' + plugin + '.');
9
- return;
10
- }
11
-
12
- this.plugins.push(plugin);
13
- plugin.swup = this;
14
- if (typeof plugin._beforeMount === 'function') {
15
- plugin._beforeMount();
16
- }
17
- plugin.mount();
18
-
19
- return this.plugins;
20
- };
21
-
22
- var unuse = exports.unuse = function unuse(plugin) {
23
- var pluginReference = void 0;
24
-
25
- if (typeof plugin === 'string') {
26
- pluginReference = this.plugins.find(function (p) {
27
- return plugin === p.name;
28
- });
29
- } else {
30
- pluginReference = plugin;
31
- }
32
-
33
- if (!pluginReference) {
34
- console.warn('No such plugin.');
35
- return;
36
- }
37
-
38
- pluginReference.unmount();
39
-
40
- if (typeof pluginReference._afterUnmount === 'function') {
41
- pluginReference._afterUnmount();
42
- }
43
-
44
- var index = this.plugins.indexOf(pluginReference);
45
- this.plugins.splice(index, 1);
46
-
47
- return this.plugins;
48
- };
49
-
50
- var findPlugin = exports.findPlugin = function findPlugin(pluginName) {
51
- return this.plugins.find(function (p) {
52
- return pluginName === p.name;
53
- });
54
- };