Skip to content

Commit

Permalink
πŸ“¦ v3.3.0
Browse files Browse the repository at this point in the history
__Major Changes:__

- πŸ‘¨β€πŸ’» Refactor: codebase
- πŸ‘¨β€πŸ”¬ Refactor: tests
- 🀝 Compatibility with `[email protected]` and `[email protected]`
- 🀝 Compatibility with `ostrio:[email protected]`
  • Loading branch information
dr-dimitru committed Feb 11, 2024
1 parent 8c8961b commit 751f8d1
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 139 deletions.
75 changes: 38 additions & 37 deletions .versions
Original file line number Diff line number Diff line change
@@ -1,53 +1,54 @@
[email protected]
babel-compiler@7.9.0
babel-compiler@7.10.5
[email protected]
[email protected]
[email protected]
[email protected].1
callback-hook@1.4.0
[email protected].1
[email protected].0
ddp-client@2.5.0
[email protected].2
callback-hook@1.5.1
[email protected].2
[email protected].1
ddp-client@2.6.1
[email protected]
ddp-server@2.5.0
[email protected].1
[email protected].2
[email protected].2
[email protected].0
ddp-server@2.7.0
[email protected].2
[email protected].3
[email protected].8
[email protected].1
[email protected]
[email protected]
[email protected].2
[email protected].1
[email protected].10
[email protected].3
[email protected].4
[email protected].11
[email protected]
[email protected]
local-test:ostrio:flow-router-title@3.2.2
[email protected].1
meteor@1.10.0
minimongo@1.8.0
[email protected].8
modules@0.18.0
[email protected].0
mongo@1.15.0
local-test:ostrio:flow-router-title@3.3.0
[email protected].3
meteor@1.11.4
minimongo@1.9.3
[email protected].10
modules@0.20.0
[email protected].1
mongo@1.16.8
[email protected]
[email protected]
[email protected]
npm-mongo@4.3.1
npm-mongo@4.17.2
[email protected]
ostrio:flow-router-extra@3.8.0
ostrio:flow-router-title@3.2.2
[email protected].0
[email protected].0
[email protected].3
[email protected].0
[email protected].11
ostrio:flow-router-extra@3.10.0
ostrio:flow-router-title@3.3.0
[email protected].2
[email protected].1
[email protected].8
[email protected].1
[email protected].12
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
154 changes: 78 additions & 76 deletions flow-router-title.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Tracker } from 'meteor/tracker';
import { Tracker } from 'meteor/tracker';
import { ReactiveVar } from 'meteor/reactive-var';

const helpers = {
isObject(obj) {
if (this.isArray(obj) || this.isFunction(obj)) {
if (this.isArray(obj) || this.isFunction(obj) || obj === null) {
return false;
}
return obj === Object(obj);
Expand Down Expand Up @@ -48,21 +48,78 @@ const helpers = {

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] + ']';
helpers[`is${_helpers[i]}`] = function (obj) {
return Object.prototype.toString.call(obj) === `[object ${_helpers[i]}]`;
};
}

const applyRouteTitle = (title, context, args) => {
if (helpers.isFunction(title)) {
return title.apply(context, args);
} else if (helpers.isString(title)) {
return title;
}
return '';
};

const getParentPrefix = (group, context, args, _i = 0) => {
let prefix = '';
const i = _i + 1;
if (group) {
if (group.options && group.options.titlePrefix) {
if ((context.route?.options?.title && i === 1) || i !== 1) {
let _gt = group.options.titlePrefix;
if (helpers.isFunction(_gt)) {
_gt = _gt.apply(context, args);
}
prefix += _gt;
}
}

if (group.parent) {
prefix = getParentPrefix(group.parent, context, args, i) + prefix;
}
}
return prefix;
};

const applyGroupTitle = function (groupTitle, context, args) {
let title = '';
const routeTitle = applyRouteTitle(context.route?.options?.title, context, args);
const groupTitlePrefix = getParentPrefix(((this.router._current?.route?.group) ? this.router._current.route.group : void 0), context, args);

if (!routeTitle) {
title = groupTitlePrefix + applyRouteTitle(groupTitle || this.defaultTitle || this.hardCodedTitle, context, args);
} else if (routeTitle && groupTitlePrefix) {
title = groupTitlePrefix + routeTitle;
} else {
title = groupTitlePrefix + applyRouteTitle(routeTitle || this.defaultTitle || this.hardCodedTitle, context, args);
}
return title;
};

export class FlowRouterTitle {
constructor(router) {
const self = this;
const computations = [];
this.router = router;
let hardCodedTitle = document.title || null;
this.title = new ReactiveVar(hardCodedTitle);
this.hardCodedTitle = document.title || null;
this.defaultTitle = null;

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')) {
this.defaultTitle = this.router.globals[j].title;
break;
}
}
}

this.title = new ReactiveVar(this.hardCodedTitle);
this.title.set = function(newValue) {
if (this.curValue !== newValue) {
if (!hardCodedTitle) {
hardCodedTitle = document.title;
if (!self.hardCodedTitle) {
self.hardCodedTitle = document.title;
}
setTimeout(() => {
document.title = newValue;
Expand All @@ -71,17 +128,16 @@ export class FlowRouterTitle {
}
};

const computations = [];
this._reactivate = (titleFunc, _context, context, _arguments, cb) => {
this._reactivate = (titleFunc, context, _arguments, cb) => {
const compute = () => {
let result = titleFunc.apply(_context, _arguments);
let result = titleFunc.apply(context, _arguments);
if (cb) {
result = cb(result);
result = cb.call(this, result, context, _arguments);
}

if (helpers.isString(result)) {
self.title.set(result);
if (context && context.context && helpers.isObject(context.context)) {
if (context?.context) {
context.context.title = result;
}
}
Expand All @@ -100,66 +156,33 @@ export class FlowRouterTitle {

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 (context.route?.group?.options) {
const _routeTitle = (context.route.options?.title) ? context.route.options.title : void 0;
const _groupTitle = (context.route.group.options?.title) ? context.route.group.options.title : void 0;

if (helpers.isFunction(_routeTitle)) {
this._reactivate(_routeTitle, _context, context, _arguments, applyGroupTitle);
this._reactivate(_routeTitle, _context, _arguments, applyGroupTitle.bind(this));
return;
}

if (helpers.isFunction(_groupTitle)) {
this._reactivate(_groupTitle, _context, context, _arguments, applyGroupTitle);
this._reactivate(_groupTitle, _context, _arguments, applyGroupTitle.bind(this));
return;
}
_title = applyGroupTitle(_groupTitle);
_title = applyGroupTitle.call(this, _groupTitle, _context, _arguments);
} else {
_title = (context.route.options && context.route.options.title) ? context.route.options.title : (defaultTitle || hardCodedTitle);
_title = (context.route.options?.title) ? context.route.options.title : (this.defaultTitle || this.hardCodedTitle);
if (helpers.isFunction(_title)) {
this._reactivate(_title, _context, context, _arguments);
this._reactivate(_title, _context, _arguments);
}
}

if (helpers.isString(_title)) {
self.title.set(_title);
if (context && context.context && helpers.isObject(context.context)) {
if (context?.context && helpers.isObject(context.context)) {
context.context.title = _title;
}
}
Expand Down Expand Up @@ -192,25 +215,4 @@ export class FlowRouterTitle {
}
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;
}
}
12 changes: 4 additions & 8 deletions package.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
Package.describe({
name: 'ostrio:flow-router-title',
version: '3.2.2',
version: '3.3.0',
summary: 'Change document.title (page title) on the fly via flow-router definition',
git: 'https://github.com/veliovgroup/Meteor-flow-router-title',
documentation: 'README.md'
});

Package.onUse((api) => {
api.versionsFrom('1.4');
api.versionsFrom(['1.4', '3.0-beta.0']);
api.use(['ecmascript', 'reactive-var', 'tracker'], 'client');
api.mainModule('flow-router-title.js', 'client');
});

Package.onTest((api) => {
api.use(['tinytest', 'ecmascript', 'random', 'session', 'reactive-var', 'tracker', 'ostrio:[email protected]'], 'client');
api.addFiles('tests/init.js', 'client');
api.addFiles('tests/common.js', 'client');
api.addFiles('tests/group.js', 'client');
api.addFiles('tests/reactive.js', 'client');
api.addFiles('tests/group-reactive.js', 'client');
api.use(['tinytest', 'ecmascript', 'random', 'session', 'reactive-var', 'tracker', 'ostrio:[email protected]'], 'client');
api.addFiles('tests/index.js', 'client');
});
21 changes: 8 additions & 13 deletions tests/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Meteor } from 'meteor/meteor';
import { Random } from 'meteor/random';
import { FlowRouter } from 'meteor/ostrio:flow-router-extra';
import { ReactiveVar } from 'meteor/reactive-var';
import { titleHandler } from './init.js';

if (Meteor.isServer) {
return;
Expand All @@ -23,7 +22,7 @@ FlowRouter.route('/', {
action() {
Meteor.setTimeout(() => {
defaultReactiveTitle.set(defaultNewTitleStr);
}, 2048);
}, 50);
}
});

Expand All @@ -43,12 +42,10 @@ FlowRouter.route('/thirdPage/:something', {

Tinytest.addAsync('COMMON - Global Defaults', function (test, next) {
FlowRouter.go('/');
test.equal(document.title, defaultTitleStr);
setTimeout(() => {
test.equal(document.title, defaultTitleStr);
setTimeout(() => {
test.equal(document.title, defaultNewTitleStr);
next();
}, 3000);
test.equal(document.title, defaultNewTitleStr);
next();
}, 100);
});

Expand Down Expand Up @@ -79,16 +76,14 @@ Tinytest.addAsync('COMMON - 404 via FlowRouter.notFound', function (test, next)

Tinytest.addAsync('COMMON - .set() Method', function (test, next) {
const _title = 'Title set via .set() method';
titleHandler.set(_title);
Meteor.__test.titleHandler.set(_title);
setTimeout(() => {
test.equal(document.title, _title);
next();
}, 25);
});

Meteor.startup(() => {
FlowRouter.route('*', {
action() {},
title: '404: Page not found'
});
FlowRouter.route('*', {
action() {},
title: '404: Page not found'
});
Loading

0 comments on commit 751f8d1

Please sign in to comment.