forked from veliovgroup/Meteor-flow-router-title
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflow-router-title.js
217 lines (197 loc) · 6.71 KB
/
flow-router-title.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { Tracker } from 'meteor/tracker';
import { ReactiveVar } from 'meteor/reactive-var';
const helpers = {
isObject(obj) {
if (this.isArray(obj) || this.isFunction(obj)) {
return false;
}
return obj === Object(obj);
},
isArray(obj) {
return Array.isArray(obj);
},
isFunction(obj) {
return typeof obj === 'function' || false;
},
isEmpty(obj) {
if (this.isDate(obj)) {
return false;
}
if (this.isObject(obj)) {
return !Object.keys(obj).length;
}
if (this.isArray(obj) || this.isString(obj)) {
return !obj.length;
}
return false;
},
has(_obj, path) {
let obj = _obj;
if (!this.isObject(obj)) {
return false;
}
if (!this.isArray(path)) {
return this.isObject(obj) && Object.prototype.hasOwnProperty.call(obj, path);
}
const length = path.length;
for (let i = 0; i < length; i++) {
if (!Object.prototype.hasOwnProperty.call(obj, path[i])) {
return false;
}
obj = obj[path[i]];
}
return !!length;
}
};
const _helpers = ['String', 'Date'];
for (let i = 0; i < _helpers.length; i++) {
helpers['is' + _helpers[i]] = function (obj) {
return Object.prototype.toString.call(obj) === '[object ' + _helpers[i] + ']';
};
}
export class FlowRouterTitle {
constructor(router) {
const self = this;
this.router = router;
let hardCodedTitle = document.title || null;
this.title = new ReactiveVar(hardCodedTitle);
this.title.set = function(newValue) {
if (this.curValue !== newValue) {
if (!hardCodedTitle) {
hardCodedTitle = document.title;
}
setTimeout(() => {
document.title = newValue;
}, 0);
this.curValue = newValue;
}
};
const computations = [];
this._reactivate = (titleFunc, _context, context, _arguments, cb) => {
const comp = Tracker.autorun(titleFunc.bind(_context, ..._arguments));
const compute = () => {
let result = titleFunc.apply(_context, _arguments);
if (cb) {
result = cb(result);
}
if (helpers.isString(result)) {
self.title.set(result);
if (context && context.context && helpers.isObject(context.context)) {
context.context.title = result;
}
}
};
comp.onInvalidate(compute);
compute();
computations.push(comp);
};
this.titleExitHandler = () => {
for (let i = computations.length - 1; i >= 0; i--) {
computations[i].stop();
computations.splice(i, 1);
}
};
this.titleHandler = (context, redirect, stop, data) => {
let _title;
let defaultTitle = null;
const _context = Object.assign({}, context, { query: context.queryParams });
const _arguments = [context.params, context.queryParams, data];
let _groupTitlePrefix = this._getParentPrefix(((this.router._current && this.router._current.route && this.router._current.route.group) ? this.router._current.route.group : void 0), _context, _arguments);
if (this.router.globals.length) {
for (let j = 0; j < this.router.globals.length; j++) {
if (helpers.isObject(this.router.globals[j]) && helpers.has(this.router.globals[j], 'title')) {
defaultTitle = this.router.globals[j].title;
break;
}
}
}
if (context.route && context.route.group && context.route.group.options) {
let _routeTitle = (context.route.options && context.route.options.title) ? context.route.options.title : void 0;
let _groupTitle = (context.route.group.options && context.route.group.options.title) ? context.route.group.options.title : void 0;
const applyRouteTitle = (__routeTitle) => {
if (helpers.isFunction(__routeTitle)) {
return __routeTitle.apply(_context, _arguments);
} else if (helpers.isString(__routeTitle)) {
return __routeTitle;
}
return '';
};
const applyGroupTitle = (__groupTitle) => {
let __title = '';
const __routeTitle = applyRouteTitle(_routeTitle);
if (!__routeTitle) {
__title = _groupTitlePrefix + applyRouteTitle(__groupTitle || defaultTitle || hardCodedTitle);
} else if (__routeTitle && _groupTitlePrefix) {
__title = _groupTitlePrefix + __routeTitle;
} else {
__title = _groupTitlePrefix + applyRouteTitle(__routeTitle || defaultTitle || hardCodedTitle);
}
return __title;
};
if (helpers.isFunction(_routeTitle)) {
this._reactivate(_routeTitle, _context, context, _arguments, applyGroupTitle);
return;
}
if (helpers.isFunction(_groupTitle)) {
this._reactivate(_groupTitle, _context, context, _arguments, applyGroupTitle);
return;
}
_title = applyGroupTitle(_groupTitle);
} else {
_title = (context.route.options && context.route.options.title) ? context.route.options.title : (defaultTitle || hardCodedTitle);
if (helpers.isFunction(_title)) {
this._reactivate(_title, _context, context, _arguments);
}
}
if (helpers.isString(_title)) {
self.title.set(_title);
if (context && context.context && helpers.isObject(context.context)) {
context.context.title = _title;
}
}
};
this.router.triggers.enter([this.titleHandler]);
this.router.triggers.exit([this.titleExitHandler]);
const _orig = this.router._notfoundRoute;
this.router._notfoundRoute = function() {
const _context = {
route: {
options: {}
}
};
_context.route.options.title = (self.router.notFound && self.router.notFound.title) ? self.router.notFound.title : void 0;
if (!helpers.isEmpty(self.router._current)) {
self.titleHandler(Object.assign({}, self.router._current, _context));
} else {
self.titleHandler(_context);
}
_orig.apply(this, arguments);
};
}
set(str) {
if (helpers.isString(str)) {
this.title.set(str);
return true;
}
return false;
}
_getParentPrefix(group, _context, _arguments, i = 0) {
let prefix = '';
i++;
if (group) {
if (group.options && group.options.titlePrefix) {
if ((_context.route.options && _context.route.options.title && i === 1) || i !== 1) {
let _gt = group.options.titlePrefix;
if (helpers.isFunction(_gt)) {
_gt = _gt.apply(_context, _arguments);
}
prefix += _gt;
}
}
if (group.parent) {
prefix = this._getParentPrefix(group.parent, _context, _arguments, i) + prefix;
}
}
return prefix;
}
}