Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router Performance fix to include theme in route's context #2971

Merged
merged 8 commits into from
Sep 29, 2017
45 changes: 3 additions & 42 deletions imports/plugins/core/layout/client/templates/theme/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,8 @@ import { Tracker } from "meteor/tracker";
import { Session } from "meteor/session";
import { $ } from "meteor/jquery";
import { Reaction, Router } from "/client/api";
import { Packages, Shops } from "/lib/collections";
import { Shops } from "/lib/collections";

/**
* getRouteLayout
* Gets layout combo based on current route context
* @param {Object} context - route context
* @returns {Object|null} The layout hash
*/
function getRouteLayout(context) {
const pkg = Packages.findOne({ "registry.name": context.route.name, "enabled": true });

if (pkg) {
const registryRoute = pkg.registry.find((x) => {
return x.name === context.route.name;
});

if (registryRoute) {
// set a default layout if none is given
if (!registryRoute.layout) {
registryRoute.layout = Session.get("DEFAULT_LAYOUT") || "coreLayout";
}

const shop = Shops.findOne(Reaction.getShopId());
const currentLayout = shop && shop.layout.find((x) => {
if (x.layout === registryRoute.layout && x.workflow === registryRoute.workflow && x.enabled === true) {
return true;
}
});

return currentLayout;
}
}

return null;
}

/**
* addBodyClasses
Expand All @@ -59,15 +26,9 @@ function addBodyClasses(context) {
];
}

// find the layout combo for this route
const currentLayout = getRouteLayout(context);

// add theme class for route layout or default
if (currentLayout && currentLayout.theme) {
classes.push(currentLayout.theme);
} else {
classes.push("default");
}
// add theme class for route layout
classes.push(context.route.options.theme);

classes = classes.join(" ");

Expand Down
20 changes: 16 additions & 4 deletions imports/plugins/core/router/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,15 +466,22 @@ export function ReactionLayout(options = {}) {
adminControlsFooter: ""
};

let layoutTheme = "default";

// Find a registered layout using the layoutName and workflowName
if (shop) {
const sortedLayout = shop.layout.sort((prev, next) => prev.priority - next.priority);
const foundLayout = sortedLayout.find((x) => selectLayout(x, layoutName, workflowName));

if (foundLayout && foundLayout.structure) {
layoutStructure = {
...foundLayout.structure
};
if (foundLayout) {
if (foundLayout.structure) {
layoutStructure = {
...foundLayout.structure
};
}
if (foundLayout.theme) {
layoutTheme = foundLayout.theme;
}
}
}

Expand Down Expand Up @@ -511,6 +518,7 @@ export function ReactionLayout(options = {}) {

// Render the layout
return {
theme: layoutTheme,
structure: layoutStructure,
component: (props) => { // eslint-disable-line react/no-multi-comp, react/display-name
const route = Router.current().route;
Expand Down Expand Up @@ -610,6 +618,7 @@ Router.initPackageRoutes = (options) => {
options: {
name: "index",
...options.indexRoute,
theme: indexLayout.theme,
component: indexLayout.component,
structure: indexLayout.structure
}
Expand All @@ -622,6 +631,7 @@ Router.initPackageRoutes = (options) => {
name: "index",
type: "shop-prefix",
...options.indexRoute,
theme: indexLayout.theme,
component: indexLayout.component,
structure: indexLayout.structure
}
Expand All @@ -634,6 +644,7 @@ Router.initPackageRoutes = (options) => {
options: {
name: "not-found",
...notFoundLayout.indexRoute,
theme: notFoundLayout.theme,
component: notFoundLayout.component,
structure: notFoundLayout.structure
}
Expand Down Expand Up @@ -674,6 +685,7 @@ Router.initPackageRoutes = (options) => {
triggersEnter: Router.Hooks.get("onEnter", name),
triggersExit: Router.Hooks.get("onExit", name),
component: reactionLayout.component,
theme: reactionLayout.theme,
structure: reactionLayout.structure
}
};
Expand Down