mirror of
https://github.com/discourse/discourse.git
synced 2025-02-22 01:12:11 +08:00
Adds a new plugin API for adding classes to be shown on user avatars
This commit is contained in:
parent
a4aeb74aba
commit
08db10491e
@ -1,5 +1,6 @@
|
|||||||
import { htmlHelper } from 'discourse-common/lib/helpers';
|
import { htmlHelper } from 'discourse-common/lib/helpers';
|
||||||
import { avatarImg } from 'discourse/lib/utilities';
|
import { avatarImg } from 'discourse/lib/utilities';
|
||||||
|
import { classesForUser } from 'discourse/helpers/user-avatar';
|
||||||
|
|
||||||
export default htmlHelper((user, size) => {
|
export default htmlHelper((user, size) => {
|
||||||
if (Ember.isEmpty(user)) {
|
if (Ember.isEmpty(user)) {
|
||||||
@ -7,5 +8,10 @@ export default htmlHelper((user, size) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const avatarTemplate = Em.get(user, 'avatar_template');
|
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);
|
||||||
});
|
});
|
||||||
|
@ -1,6 +1,23 @@
|
|||||||
import { registerUnbound } from 'discourse-common/lib/helpers';
|
import { registerUnbound } from 'discourse-common/lib/helpers';
|
||||||
import { avatarImg, formatUsername } from 'discourse/lib/utilities';
|
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) {
|
function renderAvatar(user, options) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
import PreloadStore from 'preload-store';
|
import PreloadStore from 'preload-store';
|
||||||
|
import { withPluginApi } from 'discourse/lib/plugin-api';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "banner",
|
name: "banner",
|
||||||
after: "message-bus",
|
after: "message-bus",
|
||||||
|
|
||||||
initialize(container) {
|
initialize(container) {
|
||||||
|
|
||||||
const banner = Em.Object.create(PreloadStore.get("banner")),
|
const banner = Em.Object.create(PreloadStore.get("banner")),
|
||||||
site = container.lookup('site:main');
|
site = container.lookup('site:main');
|
||||||
|
|
||||||
|
@ -23,9 +23,10 @@ import { addNavItem } from 'discourse/models/nav-item';
|
|||||||
import { replaceFormatter } from 'discourse/lib/utilities';
|
import { replaceFormatter } from 'discourse/lib/utilities';
|
||||||
import { modifySelectKit } from "select-kit/mixins/plugin-api";
|
import { modifySelectKit } from "select-kit/mixins/plugin-api";
|
||||||
import { addGTMPageChangedCallback } from 'discourse/lib/page-tracker';
|
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
|
// 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 {
|
class PluginApi {
|
||||||
constructor(version, container) {
|
constructor(version, container) {
|
||||||
@ -368,6 +369,23 @@ class PluginApi {
|
|||||||
appEvents.on(name, fn);
|
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
|
* Changes a setting associated with a widget. For example, if
|
||||||
|
Loading…
x
Reference in New Issue
Block a user