routeNotMatchedChain = routeNotMatchedChain.then(() => { if (_.isNumber(_.get(this.history, 'location.state.scrollY'))) { window.scrollTo(0, _.get(this.history, 'location.state.scrollY')); } else { window.scrollTo(0, 0); } if (!rEvent.isStopped()) { // If URL starts with loaded app prefix, go to default route if (this.baseUrl !== '/' && url.startsWith(this.baseUrl)) { const defaultRoute = Webiny.Router.getDefaultRoute(); if (defaultRoute) { url = defaultRoute.getHref(); } else { url = Webiny.Router.getBaseUrl(); } this.history.replace(url); return true; } // Else reload the page, it is a URL within our domain - but not handled by the current app window.location.reload(); } if (rEvent.goTo !== null) { return Webiny.Router.goToRoute(rEvent.goTo, rEvent.goToParams); } return Promise.resolve(true); });
async getRouteContent(route) { const components = await route.getComponents(); let defComponents = []; if (!route.skipDefaultComponents()) { defComponents = Webiny.Router.getDefaultComponents(); } return _.merge({Layout: Webiny.Router.getLayout(route.layout)}, defComponents, components); }
__processSubmitResponse(model, apiResponse) { this.enableSubmit(); Webiny.Growl.remove(this.growlId); this.hideLoading(); if (apiResponse.isError()) { this.handleApiError(apiResponse); return apiResponse; } const responseData = apiResponse.getData(); const newModel = _.has(responseData, 'entity') ? responseData.entity : responseData; this.setState({model: newModel, initialModel: _.cloneDeep(newModel), error: null, showError: false}); if (_.isFunction(this.props.onSuccessMessage)) { Webiny.Growl.success(this.props.onSuccessMessage({model, apiResponse, form: this})); } const onSubmitSuccess = this.props.onSubmitSuccess; if (_.isFunction(onSubmitSuccess)) { return onSubmitSuccess({model, apiResponse, form: this}); } if (_.isString(onSubmitSuccess)) { return Webiny.Router.goToRoute(onSubmitSuccess); } return apiResponse; }
cancel() { if (_.isString(this.props.onCancel)) { Webiny.Router.goToRoute(this.props.onCancel); } else if (_.isFunction(this.props.onCancel)) { this.props.onCancel({form: this}); } }
return routeWillChangeChain.then(() => { if (routerEvent.isStopped() && routerEvent.goTo !== null) { Webiny.Router.goToRoute(routerEvent.goTo, routerEvent.goToParams); } return routerEvent; });
/** * RENDER ROUTE * @param route */ async renderRoute(route) { const content = await this.getRouteContent(route); if (route.getTitle()) { Webiny.Router.setTitle(route.getTitle()); } await Webiny.ViewManager.render(content); return route; }
loadModel(id = null, model = null) { if (!id) { if (this.props.connectToRouter) { id = Webiny.Router.getParams('id'); } } if (id) { if (this.request) { return this.request; } this.showLoading(); this.request = this.api.execute(this.api.httpMethod, this.api.url + '/' + id).then(apiResponse => { this.request = null; if (apiResponse.isAborted()) { return; } if (apiResponse.isError()) { if (this.props.onFailure) { this.props.onFailure({apiResponse, form: this}); } return; } let newModel; if (_.isFunction(this.props.prepareLoadedData) && this.props.prepareLoadedData !== _.noop) { newModel = _.merge({}, this.props.defaultModel || {}, this.props.prepareLoadedData({data: apiResponse.getData('entity')})); } else { newModel = _.merge({}, this.props.defaultModel || {}, apiResponse.getData('entity')) } this.setState({model: newModel, initialModel: _.cloneDeep(newModel), loading: false}, () => { // Execute optional `onLoad` callback if (_.isFunction(this.props.onLoad)) { this.props.onLoad({model: this.getModel(), form: this}); } this.__processWatches(); }); }); return this.request; } if (model) { model = _.merge({}, this.props.defaultModel || {}, model); // Find watches to trigger - this is mostly necessary on static forms const changes = []; _.each(this.watches, (watches, name) => { if (!_.isEqual(_.get(model, name), _.get(this.state.model, name))) { changes.push(name); } }); this.setState({model, initialModel: _.cloneDeep(model)}, () => this.__processWatches(changes)); } }
function chainLink(callback, routerEvent) { if (!routerEvent.isStopped()) { return callback(routerEvent); } if (routerEvent.goTo !== null) { Webiny.Router.goToRoute(routerEvent.goTo, routerEvent.goToParams); } return routerEvent; }