2015-07-15 12:30:11 +08:00
|
|
|
import Component from 'flarum/Component';
|
2015-04-25 20:58:39 +08:00
|
|
|
import icon from 'flarum/helpers/icon';
|
2015-07-15 12:30:11 +08:00
|
|
|
import extract from 'flarum/utils/extract';
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* The `Badge` component represents a user/discussion badge, indicating some
|
|
|
|
* status (e.g. a discussion is stickied, a user is an admin).
|
|
|
|
*
|
|
|
|
* A badge may have the following special props:
|
|
|
|
*
|
|
|
|
* - `type` The type of badge this is. This will be used to give the badge a
|
2015-07-17 13:17:49 +08:00
|
|
|
* class name of `Badge--{type}`.
|
2015-07-15 12:30:11 +08:00
|
|
|
* - `icon` The name of an icon to show inside the badge.
|
2015-09-22 16:18:21 +08:00
|
|
|
* - `label`
|
2015-07-15 12:30:11 +08:00
|
|
|
*
|
|
|
|
* All other props will be assigned as attributes on the badge element.
|
|
|
|
*/
|
2015-04-25 20:58:39 +08:00
|
|
|
export default class Badge extends Component {
|
2015-07-15 12:30:11 +08:00
|
|
|
view() {
|
|
|
|
const attrs = Object.assign({}, this.props);
|
|
|
|
const type = extract(attrs, 'type');
|
|
|
|
const iconName = extract(attrs, 'icon');
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-31 18:46:47 +08:00
|
|
|
attrs.className = 'Badge ' + (type ? 'Badge--' + type : '') + ' ' + (attrs.className || '');
|
|
|
|
attrs.title = extract(attrs, 'label') || '';
|
2015-07-15 12:30:11 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<span {...attrs}>
|
2015-07-31 18:46:47 +08:00
|
|
|
{iconName ? icon(iconName, {className: 'Badge-icon'}) : m.trust(' ')}
|
2015-07-15 12:30:11 +08:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
config(isInitialized) {
|
|
|
|
if (isInitialized) return;
|
|
|
|
|
2015-08-13 11:31:13 +08:00
|
|
|
if (this.props.label) this.$().tooltip({container: 'body'});
|
2015-04-25 20:58:39 +08:00
|
|
|
}
|
|
|
|
}
|