nk-routeutil
Version:
route handling library
227 lines (195 loc) • 7.04 kB
JavaScript
import { Virtual, VirtualCSSTransitionGroup } from 'nk-virtual';
var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
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;
};
}();
var inherits = function (subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
}
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;
};
var possibleConstructorReturn = function (self, call) {
if (!self) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return call && (typeof call === "object" || typeof call === "function") ? call : self;
};
var Virtual$1 = window.interfaces.Virtual;
var LazilyLoad = function (_Virtual$PureComponen) {
inherits(LazilyLoad, _Virtual$PureComponen);
function LazilyLoad() {
classCallCheck(this, LazilyLoad);
var _this = possibleConstructorReturn(this, (LazilyLoad.__proto__ || Object.getPrototypeOf(LazilyLoad)).apply(this, arguments));
_this.state = {
isLoaded: false,
modules: null
};
return _this;
}
createClass(LazilyLoad, [{
key: "componentDidMount",
value: function componentDidMount() {
this._isMounted = true;
this.load();
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(previous) {
var _this2 = this;
var shouldLoad = !!Object.keys(this.props.modules).filter(function (key) {
return _this2.props.modules[key] !== previous.modules[key];
}).length;
if (shouldLoad) {
this.load();
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this._isMounted = false;
}
}, {
key: "load",
value: function load() {
var _this3 = this;
this.setState({
isLoaded: false
});
var modules = this.props.modules;
var keys = Object.keys(modules);
Promise.all(keys.map(function (key) {
return modules[key]();
})).then(function (values) {
return keys.reduce(function (agg, key, index) {
agg[key] = values[index];
return agg;
}, {});
}).then(function (result) {
if (!_this3._isMounted) return null;
_this3.setState({ modules: result, isLoaded: true });
});
}
}, {
key: "render",
value: function render() {
if (!this.state.isLoaded) return null;
return Virtual$1.Children.only(this.props.children(this.state.modules));
}
}]);
return LazilyLoad;
}(Virtual$1.PureComponent);
LazilyLoad.propTypes = {
children: Virtual$1.PropTypes.func.isRequired,
modules: Virtual$1.PropTypes.object.isRequired
};
LazilyLoad.propTypes = {
children: Virtual$1.PropTypes.func.isRequired
};
var _class = function (_LazilyLoad) {
inherits(_class, _LazilyLoad);
function _class() {
classCallCheck(this, _class);
return possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
}
createClass(_class, [{
key: "render",
value: function render() {
var view = void 0;
if (!this.state.isLoaded) {
view = this.props.loader;
} else {
var isChild = this.props.children(this.state.modules);
view = Virtual.createElement(
"div",
{ key: this.props.transitionKey },
isChild && Virtual.Children.only(isChild)
);
}
if (this.props.transition) {
return Virtual.createElement(
VirtualCSSTransitionGroup,
{ style: this.props.style, className: "lazilyLoad " + this.props.className, transitionName: this.props.transitionName, transitionEnterTimeout: this.props.transitionEnterTimeout, transitionLeaveTimeout: this.props.transitionLeaveTimeout },
view
);
}
return Virtual.createElement(
"span",
{ className: "lazilyLoad " + this.props.className },
view
);
}
}]);
return _class;
}(LazilyLoad);
_class.propTypes = {
children: Virtual.PropTypes.func.isRequired,
modules: Virtual.PropTypes.object.isRequired,
loader: Virtual.PropTypes.node.isRequired,
style: Virtual.PropTypes.object,
transition: Virtual.PropTypes.bool,
className: Virtual.PropTypes.string,
transitionKey: Virtual.PropTypes.string,
transitionName: Virtual.PropTypes.string,
transitionEnterTimeout: Virtual.PropTypes.number,
transitionLeaveTimeout: Virtual.PropTypes.number
};
_class.defaultProps = {
loader: null,
transition: true,
style: null,
className: "",
transitionKey: "",
transitionName: "fade",
transitionEnterTimeout: 500,
transitionLeaveTimeout: 300
};
var requireErrorCallback = function requireErrorCallback(err) {
//Undefine failed module id
err.requireModules.map(function (failedId) {
requirejs.undef(failedId);
});
};
var modifyReponse = function modifyReponse(url, response) {
/*if(isCssUrl(url) && loadedUrls[url]){
return "";
}*/
return response;
};
var loadModulePromise = (function (url) {
return new Promise(function (resolve, reject) {
//requirejs is considerd a global dependecy
requirejs([url], function (response) {
resolve(modifyReponse(url, response));
}, function (err) {
requireErrorCallback(err);
reject(err);
});
});
});
export { _class as LazilyLoadWithLoader, loadModulePromise };