Skip to content

Commit

Permalink
Route theme body class (#1672)
Browse files Browse the repository at this point in the history
* added body class for current route based on theme and routeName

* optimised method by setting/getting session

* fixed import order
  • Loading branch information
lcampanis authored and mikemurray committed Jan 4, 2017
1 parent c08cd12 commit 194767f
Showing 1 changed file with 68 additions and 2 deletions.
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

0 comments on commit 194767f

Please sign in to comment.