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

Route theme body class #1672

Merged
merged 3 commits into from
Jan 4, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 68 additions & 2 deletions imports/plugins/core/layout/client/templates/theme/theme.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,73 @@
import { Reaction } from "/client/api";
import { Shops } from "/lib/collections";
import { Meteor } from "meteor/meteor";
import { Tracker } from "meteor/tracker";
import { Reaction, Router } from "/client/api";
import { Packages, 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 package = Packages.findOne({ "registry.name": context.route.name, "enabled": true });

if(package) {
const registryRoute = package.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.layout.find((x) => {
if (x.layout === registryRoute.layout && x.workflow === registryRoute.workflow && x.enabled === true) {
return true;
}
});

return currentLayout;
}
}

return null;
}

/**
* addBodyClasses
* Adds body classes to help themes distinguish pages and components based on the current route name and layout theme
* @param {Object} context - route context
*/
function addBodyClasses(context) {
let classes = [
// push clean route-name
"app-" + context.route.name.replace(/[\/_]/i, "-")
];

// 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");
}

classes = classes.join(" ");

$("body").removeClass(Session.get("BODY_CLASS")).addClass(classes);

// save for removal on next enter
Session.set("BODY_CLASS", classes);
}

Router.Hooks.onEnter(addBodyClasses);

Meteor.startup(() => {
Tracker.autorun(() => {
Expand Down