forked from lookback/meteor-bodyclass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
body-class.js
103 lines (82 loc) · 2.43 KB
/
body-class.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const FlowRouter = Package['ostrio:flow-router-extra'] ? Package['ostrio:flow-router-extra'].FlowRouter : false;
const routeClasses = () => {
let classes = [];
if(Package['iron:router']){
let ctrl = Router.current();
classes = [ctrl._layout._template, ctrl.lookupTemplate()];
}
if(Package['kadira:flow-router']) {
// Gets a route's layout OR its group's layout OR its group's parent's layout
// OR default layout in that order
function getRouteLayout(route) {
return route.options.layout
|| route.group && route.group.options.layout
|| route.group && route.group.parent && route.group.parent.options.layout
|| FlowRouter.defaultLayout;
}
let route = FlowRouter.current().route;
classes = [route.name, getRouteLayout(route)];
}
return _.chain(classes)
.compact()
.invoke('toLowerCase')
.value();
};
BodyClass = {
settings: {
element: 'body',
classes: []
},
config(opts) {
_.extend(this.settings, opts);
return this;
},
add(fn) {
if(!Match.test(fn, Match.OneOf(Array, String, Function))) {
return console.warn('BodyClass: Argument to "add" must be an array, string or function!');
}
if(Match.test(fn, Array)) {
return fn.forEach(_.bind(BodyClass.add, BodyClass));
}
if(!Match.test(fn, Function)) {
return Meteor.startup(() => $(this.settings.element).addClass(fn));
}
if(Match.test(fn, Function)) {
Meteor.startup(() => {
Tracker.autorun(() => {
$(this.settings.element)
.removeClass(fn._prev)
.addClass(fn._prev = fn());
});
});
}
},
run(opts = {}) {
let classes = this.settings.classes.concat(routeClasses());
if(opts.classes) {
classes = classes.concat(opts.classes);
}
$(this.settings.element).addClass(classes.join(' '));
},
remove(className) {
check(className, String);
$(this.settings.element).removeClass(className);
},
cleanup() {
BodyClass.remove(this.settings.classes.concat(routeClasses()).join(' '));
}
};
if(Package['iron:router']) {
Iron.Router.plugins.bodyClasses = (router, options) => {
router.onAfterAction(() => {
BodyClass.run(options);
}, options);
router.onStop(() => {
BodyClass.cleanup();
}, options);
};
}
if(FlowRouter) {
FlowRouter.triggers.enter([() => BodyClass.run()]);
FlowRouter.triggers.exit([() => BodyClass.cleanup()]);
}