Adds a new plugin API for adding classes to be shown on user avatars

This commit is contained in:
Robin Ward 2018-02-01 15:23:52 -05:00
parent a4aeb74aba
commit 08db10491e
4 changed files with 44 additions and 3 deletions

View File

@ -1,5 +1,6 @@
import { htmlHelper } from 'discourse-common/lib/helpers';
import { avatarImg } from 'discourse/lib/utilities';
import { classesForUser } from 'discourse/helpers/user-avatar';
export default htmlHelper((user, size) => {
if (Ember.isEmpty(user)) {
@ -7,5 +8,10 @@ export default htmlHelper((user, size) => {
}
const avatarTemplate = Em.get(user, 'avatar_template');
return avatarImg({ size, avatarTemplate });
let args = { size, avatarTemplate };
let extraClasses = classesForUser(user).join(' ');
if (extraClasses && extraClasses.length) {
args.extraClasses = extraClasses;
}
return avatarImg(args);
});

View File

@ -1,6 +1,23 @@
import { registerUnbound } from 'discourse-common/lib/helpers';
import { avatarImg, formatUsername } from 'discourse/lib/utilities';
let _customAvatarHelpers;
export function registerCustomAvatarHelper(fn) {
_customAvatarHelpers = _customAvatarHelpers || [];
_customAvatarHelpers.push(fn);
}
export function classesForUser(u) {
let result = [];
if (_customAvatarHelpers) {
for (let i=0; i<_customAvatarHelpers.length; i++) {
result = result.concat(_customAvatarHelpers[i](u));
}
}
return result;
}
function renderAvatar(user, options) {
options = options || {};

View File

@ -1,11 +1,11 @@
import PreloadStore from 'preload-store';
import { withPluginApi } from 'discourse/lib/plugin-api';
export default {
name: "banner",
after: "message-bus",
initialize(container) {
const banner = Em.Object.create(PreloadStore.get("banner")),
site = container.lookup('site:main');

View File

@ -23,9 +23,10 @@ import { addNavItem } from 'discourse/models/nav-item';
import { replaceFormatter } from 'discourse/lib/utilities';
import { modifySelectKit } from "select-kit/mixins/plugin-api";
import { addGTMPageChangedCallback } from 'discourse/lib/page-tracker';
import { registerCustomAvatarHelper } from 'discourse/helpers/user-avatar';
// If you add any methods to the API ensure you bump up this number
const PLUGIN_API_VERSION = '0.8.17';
const PLUGIN_API_VERSION = '0.8.18';
class PluginApi {
constructor(version, container) {
@ -368,6 +369,23 @@ class PluginApi {
appEvents.on(name, fn);
}
/**
Registers a function to generate custom avatar CSS classes
for a particular user.
Takes a function that will accept a user as a parameter
and return an array of CSS classes to apply.
```javascript
api.customUserAvatarClasses(user => {
if (Ember.get(user, 'primary_group_name') === 'managers') {
return ['managers'];
}
});
**/
customUserAvatarClasses(fn) {
registerCustomAvatarHelper(fn);
}
/**
* Changes a setting associated with a widget. For example, if