Vulcan/packages/telescope-users/lib/routes.js
Maxime Quandalle 94c6121d91 Improve jsHint consistency
This commit touch a lot of lines of code with the goal to be more
rigorous about JavaScript code conventions defined in the `.jshintrc`.

Some modification:

* Add a list of used global symbols in the corresponding section of
  `.jshintrc`
* Use local variables instead of global in a lot of places where the
  keyword `var` was mistakenly forgotten
* Add missing semi-colons after instructions
* Add new lines at the end of files
* Remove trailing whitespaces
* Use newer name of some Meteor APIs, eg `addFiles` instead of
  `add_files`
* Add missing `break` statements in `switch` blocks
* Use `===` instead of `==` and `!==` instead of `!=`
* Remove unused variables

This commit should also fix a few bugs due to this lack of rigor. One
example of that was the test `typeof navElements === "array"` that was
never true because in JavaScript, `typeof [] === "object"`, we
replaced this test by the `_.isArray` method provided by underscore.
It might also fix some potential collision related to global
variables.

There is still plenty of work until Telescope code base passes jsHint
validation, but at least this commit is a step in the right direction.
2015-05-01 18:38:27 +02:00

130 lines
2.7 KiB
JavaScript

// Controller for user pages
Users.controllers = {};
Users.controllers.page = RouteController.extend({
waitOn: function() {
return [
coreSubscriptions.subscribe('singleUser', this.params._idOrSlug)
];
},
getUser: function () {
return Meteor.users.findOne({"telescope.slug": this.params._idOrSlug});
},
data: function() {
var findById = Meteor.users.findOne(this.params._idOrSlug);
var findBySlug = Meteor.users.findOne({"telescope.slug": this.params._idOrSlug});
if (typeof findById !== 'undefined') {
// redirect to slug-based URL
Router.go(Users.getProfileUrl(findById), {replaceState: true});
} else {
return {
user: findById || findBySlug
};
}
},
getTitle: function () {
return Users.getDisplayName(this.getUser());
},
getDescription: function () {
return i18n.t('the_profile_of') + ' ' + Users.getDisplayName(this.getUser());
},
fastRender: true
});
// Controller for user account editing
Users.controllers.edit = RouteController.extend({
waitOn: function() {
return [
coreSubscriptions.subscribe('singleUser', this.params.slug)
];
},
data: function() {
// if there is no slug, default to current user
var user = !!this.params.slug ? Meteor.users.findOne({"telescope.slug": this.params.slug}) : Meteor.user();
return {
user: user
};
},
fastRender: true
});
Meteor.startup(function () {
// User Logout
Router.route('/sign-out', {
name: 'signOut',
template: 'sign_out',
onBeforeAction: function() {
Meteor.logout(function() {
});
this.next();
}
});
// User Profile
Router.route('/users/:_idOrSlug', {
name: 'user_profile',
template: 'user_profile',
controller: Users.controllers.page
});
// User Edit
Router.route('/users/:slug/edit', {
name: 'user_edit',
template: 'user_edit',
controller: Users.controllers.edit,
onBeforeAction: function () {
// Only allow users with permissions to see the user edit page.
if (Meteor.user() && (
Users.is.admin(Meteor.user()) ||
this.params.slug === Meteor.user().telescope.slug
)) {
this.next();
} else {
this.render('no_rights');
}
}
});
Router.route('/account', {
name: 'userAccountShortcut',
template: 'user_edit',
controller: Users.controllers.edit
});
// All Users
Router.route('/users-dashboard', {
controller: Telescope.controllers.admin,
name: 'usersDashboard'
// template: 'users'
});
// Unsubscribe (from notifications)
Router.route('/unsubscribe/:hash', {
name: 'unsubscribe',
template: 'unsubscribe',
data: function() {
return {
hash: this.params.hash
};
}
});
});