/
MorphTransition.js
399 lines (340 loc) · 13.6 KB
/
MorphTransition.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _Transition = _interopRequireDefault(require("react-transition-group/Transition"));
var _PropTypes = require("react-transition-group/utils/PropTypes");
var _irrelonMorph = _interopRequireWildcard(require("./irrelon-morph"));
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function areChildrenDifferent(oldChildren, newChildren) {
if (oldChildren === newChildren) {
return false;
}
return !(_react.default.isValidElement(oldChildren) && _react.default.isValidElement(newChildren) && oldChildren.key != null && oldChildren.key === newChildren.key);
}
function buildClassName(className, state) {
switch (state) {
case 'enter':
return "".concat(className, " enter");
case 'entering':
return "".concat(className, " enter ").concat(className, " enter active");
case 'entered':
return "".concat(className, " enter.done");
case 'exit':
return "".concat(className, " exit");
case 'exiting':
return "".concat(className, " exit ").concat(className, " exit active");
case 'exited':
return "".concat(className, " exit done");
default:
return '';
}
}
function shouldDelayEnter(children) {
return _react.default.isValidElement(children) && children.type.pageTransitionDelayEnter;
}
var MorphTransition =
/*#__PURE__*/
function (_React$Component) {
_inherits(MorphTransition, _React$Component);
function MorphTransition(props) {
var _this;
_classCallCheck(this, MorphTransition);
_this = _possibleConstructorReturn(this, (MorphTransition.__proto__ || Object.getPrototypeOf(MorphTransition)).call(this, props));
var children = props.children;
_this.state = {
state: 'enter',
isIn: !shouldDelayEnter(children),
currentChildren: children,
nextChildren: null,
renderedChildren: children,
showLoading: false
};
return _this;
}
_createClass(MorphTransition, [{
key: "componentDidMount",
value: function componentDidMount() {
if (shouldDelayEnter(this.props.children)) {
this.setState({
timeoutId: this.startEnterTimer()
});
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps, prevState) {
var _state = this.state,
currentChildren = _state.currentChildren,
renderedChildren = _state.renderedChildren,
nextChildren = _state.nextChildren,
isIn = _state.isIn,
state = _state.state;
var children = this.props.children;
var hasNewChildren = areChildrenDifferent(currentChildren, children);
var needsTransition = areChildrenDifferent(renderedChildren, children);
if (hasNewChildren) {
// We got a new set of children while we were transitioning some in
// Immediately start transitioning out this component and update the next
// component
this.setState({
isIn: false,
nextChildren: children,
currentChildren: children
});
if (this.state.timeoutId) {
clearTimeout(this.state.timeoutId);
} //console.log('New DOM changes');
} else if (needsTransition && !isIn && state === 'exited') {
if (shouldDelayEnter(nextChildren)) {
// Wait for the ready callback to actually transition in, but still
// mount the component to allow it to start loading things
this.setState({
renderedChildren: nextChildren,
nextChildren: null
});
} else {
// No need to wait, mount immediately
this.setState({
isIn: true,
renderedChildren: nextChildren,
nextChildren: null,
timeoutId: this.startEnterTimer()
});
}
} else if (prevState.showLoading && !this.state.showLoading) {
// We hid the loading indicator; now that that change has been flushed to
// the DOM, we can now bring in the next component!
this.setState({
isIn: true
});
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (this.state.timeoutId) {
clearTimeout(this.state.timeoutId);
}
}
}, {
key: "onEnter",
value: function onEnter(pageElem) {
var _this2 = this;
//console.log('onEnter');
// It's safe to reenable scrolling now
this.disableScrolling = false;
this.setState({
state: 'enter',
showLoading: false
});
if (!this._sourceMorphElements) {
return;
} // Find the source and target pairs
var promiseArr = [];
this._sourceMorphElements.forEach(function (sourceItem) {
var sourceNode = sourceItem.node;
var customTargetSelector = sourceNode.getAttribute("data-morph-target");
var targetSelector = customTargetSelector || "#" + sourceItem.node.id;
promiseArr.push(new Promise(function (resolve) {
var target = pageElem.querySelector(targetSelector);
if (target) {
(0, _irrelonMorph.default)(sourceItem, target, parseInt(sourceNode.getAttribute('data-morph-ms'), 10) || 600).then(function (morphData) {
(0, _irrelonMorph.endMorph)(morphData);
resolve();
});
} else {
resolve();
}
}));
});
Promise.all(promiseArr).then(function () {
_this2.onMorphComplete(pageElem);
});
}
}, {
key: "onEntering",
value: function onEntering() {
//console.log('onEntering');
this.setState({
state: 'entering'
});
}
}, {
key: "scanDomForMorphElements",
value: function scanDomForMorphElements(pageElem) {
var morphElems = pageElem.querySelectorAll('[data-morph-ms]');
var sourceSnapshot = [];
if (morphElems && morphElems.length) {
for (var i = 0; i < morphElems.length; i++) {
sourceSnapshot.push(new _irrelonMorph.ScannedNode(morphElems[i]));
}
}
return sourceSnapshot;
}
}, {
key: "onEntered",
value: function onEntered(pageElem) {
var _this3 = this;
//console.log('onEntered');
this.setState({
state: 'entered'
});
setTimeout(function () {
_this3._sourceMorphElements = _this3.scanDomForMorphElements(pageElem); //console.log('On entered detected ' + this._sourceMorphElements.length + ' new source elements');
}, 1);
}
}, {
key: "onMorphComplete",
value: function onMorphComplete(pageElem) {
this._sourceMorphElements = this.scanDomForMorphElements(pageElem); //console.log('On entered detected ' + this._sourceMorphElements.length + ' new source elements');
}
}, {
key: "onExit",
value: function onExit() {
//console.log('onExit');
// Disable scrolling until this component has unmounted
this.disableScrolling = true;
this.setState({
state: 'exit'
});
(0, _irrelonMorph.cancelAllMorphs)();
}
}, {
key: "onExiting",
value: function onExiting() {
//console.log('onExiting');
this.setState({
state: 'exiting'
});
}
}, {
key: "onExited",
value: function onExited() {
//console.log('onExited');
this.setState({
state: 'exited',
renderedChildren: null
});
}
}, {
key: "onChildLoaded",
value: function onChildLoaded() {
//console.log('Child loaded');
if (this.state.timeoutId) {
clearTimeout(this.state.timeoutId);
}
this.setState({
isIn: true
});
}
}, {
key: "startEnterTimer",
value: function startEnterTimer() {
var _this4 = this;
return setTimeout(function () {
_this4.setState({
showLoading: true
});
}, this.props.loadingDelay);
}
}, {
key: "render",
value: function render() {
var _this5 = this;
var _props = this.props,
timeout = _props.timeout,
loadingCallbackName = _props.loadingCallbackName;
var _state2 = this.state,
children = _state2.renderedChildren,
state = _state2.state;
if (['entering', 'exiting', 'exited'].indexOf(state) !== -1) {
// Need to reflow!
// eslint-disable-next-line no-unused-expressions
if (document.body) {
document.body.scrollTop;
}
}
var containerClassName = buildClassName(this.props.classNames, state);
return _react.default.createElement(_Transition.default, {
timeout: timeout,
"in": this.state.isIn,
appear: true,
onEnter: function onEnter() {
return _this5.onEnter.apply(_this5, arguments);
},
onEntering: function onEntering() {
return _this5.onEntering.apply(_this5, arguments);
},
onEntered: function onEntered() {
return _this5.onEntered.apply(_this5, arguments);
},
onExit: function onExit() {
return _this5.onExit.apply(_this5, arguments);
},
onExiting: function onExiting() {
return _this5.onExiting.apply(_this5, arguments);
},
onExited: function onExited() {
return _this5.onExited.apply(_this5, arguments);
}
}, _react.default.createElement("div", {
className: containerClassName
}, children && _react.default.cloneElement(children, _defineProperty({}, loadingCallbackName, function () {
return _this5.onChildLoaded();
}))));
}
}]);
return MorphTransition;
}(_react.default.Component);
MorphTransition.propTypes = {
children: _propTypes.default.node.isRequired,
classNames: _propTypes.default.string.isRequired,
timeout: _propTypes.default.number.isRequired,
loadingComponent: _propTypes.default.element,
loadingDelay: _propTypes.default.number,
loadingCallbackName: _propTypes.default.string,
/* eslint-disable react/require-default-props */
loadingTimeout: function loadingTimeout(props) {
var pt = _PropTypes.timeoutsShape;
if (props.loadingComponent) {
pt = pt.isRequired;
}
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
return pt.apply(void 0, [props].concat(args));
},
loadingClassNames: function loadingClassNames(props) {
var pt = _propTypes.default.string;
if (props.loadingTimeout) {
pt = pt.isRequired;
}
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
args[_key2 - 1] = arguments[_key2];
}
return pt.apply(void 0, [props].concat(args));
},
/* eslint-enable react/require-default-props */
monkeyPatchScrolling: _propTypes.default.bool
};
MorphTransition.defaultProps = {
loadingComponent: null,
loadingCallbackName: 'pageTransitionReadyToEnter',
loadingDelay: 500,
monkeyPatchScrolling: false
};
var _default = MorphTransition;
exports.default = _default;