mirror of
https://github.com/flarum/core.git
synced 2025-08-12 03:14:33 +02:00
Check for selectable tags before opening tag selection modal (#112)
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
import { extend, override } from 'flarum/extend';
|
import { extend, override } from 'flarum/extend';
|
||||||
import IndexPage from 'flarum/components/IndexPage';
|
import IndexPage from 'flarum/components/IndexPage';
|
||||||
import DiscussionComposer from 'flarum/components/DiscussionComposer';
|
import DiscussionComposer from 'flarum/components/DiscussionComposer';
|
||||||
|
import classList from 'flarum/utils/classList';
|
||||||
|
|
||||||
import TagDiscussionModal from './components/TagDiscussionModal';
|
import TagDiscussionModal from './components/TagDiscussionModal';
|
||||||
import tagsLabel from '../common/helpers/tagsLabel';
|
import tagsLabel from '../common/helpers/tagsLabel';
|
||||||
|
import getSelectableTags from './utils/getSelectableTags';
|
||||||
|
|
||||||
export default function () {
|
export default function () {
|
||||||
extend(IndexPage.prototype, 'newDiscussionAction', function (promise) {
|
extend(IndexPage.prototype, 'newDiscussionAction', function (promise) {
|
||||||
@@ -20,6 +22,10 @@ export default function () {
|
|||||||
|
|
||||||
// Add tag-selection abilities to the discussion composer.
|
// Add tag-selection abilities to the discussion composer.
|
||||||
DiscussionComposer.prototype.chooseTags = function () {
|
DiscussionComposer.prototype.chooseTags = function () {
|
||||||
|
const selectableTags = getSelectableTags();
|
||||||
|
|
||||||
|
if (!selectableTags.length) return;
|
||||||
|
|
||||||
app.modal.show(TagDiscussionModal, {
|
app.modal.show(TagDiscussionModal, {
|
||||||
selectedTags: (this.composer.fields.tags || []).slice(0),
|
selectedTags: (this.composer.fields.tags || []).slice(0),
|
||||||
onsubmit: tags => {
|
onsubmit: tags => {
|
||||||
@@ -33,9 +39,10 @@ export default function () {
|
|||||||
// title.
|
// title.
|
||||||
extend(DiscussionComposer.prototype, 'headerItems', function (items) {
|
extend(DiscussionComposer.prototype, 'headerItems', function (items) {
|
||||||
const tags = this.composer.fields.tags || [];
|
const tags = this.composer.fields.tags || [];
|
||||||
|
const selectableTags = getSelectableTags();
|
||||||
|
|
||||||
items.add('tags', (
|
items.add('tags', (
|
||||||
<a className="DiscussionComposer-changeTags" onclick={this.chooseTags.bind(this)}>
|
<a className={classList(['DiscussionComposer-changeTags', !selectableTags.length && 'disabled'])} onclick={this.chooseTags.bind(this)}>
|
||||||
{tags.length
|
{tags.length
|
||||||
? tagsLabel(tags)
|
? tagsLabel(tags)
|
||||||
: <span className="TagLabel untagged">{app.translator.trans('flarum-tags.forum.composer_discussion.choose_tags_link')}</span>}
|
: <span className="TagLabel untagged">{app.translator.trans('flarum-tags.forum.composer_discussion.choose_tags_link')}</span>}
|
||||||
@@ -47,9 +54,12 @@ export default function () {
|
|||||||
const chosenTags = this.composer.fields.tags || [];
|
const chosenTags = this.composer.fields.tags || [];
|
||||||
const chosenPrimaryTags = chosenTags.filter(tag => tag.position() !== null && !tag.isChild());
|
const chosenPrimaryTags = chosenTags.filter(tag => tag.position() !== null && !tag.isChild());
|
||||||
const chosenSecondaryTags = chosenTags.filter(tag => tag.position() === null);
|
const chosenSecondaryTags = chosenTags.filter(tag => tag.position() === null);
|
||||||
if (!chosenTags.length
|
const selectableTags = getSelectableTags();
|
||||||
|| (chosenPrimaryTags.length < app.forum.attribute('minPrimaryTags'))
|
|
||||||
|| (chosenSecondaryTags.length < app.forum.attribute('minSecondaryTags'))) {
|
if ((!chosenTags.length
|
||||||
|
|| (chosenPrimaryTags.length < app.forum.attribute('minPrimaryTags'))
|
||||||
|
|| (chosenSecondaryTags.length < app.forum.attribute('minSecondaryTags'))
|
||||||
|
) && selectableTags.length) {
|
||||||
app.modal.show(TagDiscussionModal, {
|
app.modal.show(TagDiscussionModal, {
|
||||||
selectedTags: chosenTags,
|
selectedTags: chosenTags,
|
||||||
onsubmit: tags => {
|
onsubmit: tags => {
|
||||||
|
@@ -10,18 +10,13 @@ import Stream from 'flarum/utils/Stream';
|
|||||||
import tagLabel from '../../common/helpers/tagLabel';
|
import tagLabel from '../../common/helpers/tagLabel';
|
||||||
import tagIcon from '../../common/helpers/tagIcon';
|
import tagIcon from '../../common/helpers/tagIcon';
|
||||||
import sortTags from '../../common/utils/sortTags';
|
import sortTags from '../../common/utils/sortTags';
|
||||||
|
import getSelectableTags from '../utils/getSelectableTags';
|
||||||
|
|
||||||
export default class TagDiscussionModal extends Modal {
|
export default class TagDiscussionModal extends Modal {
|
||||||
oninit(vnode) {
|
oninit(vnode) {
|
||||||
super.oninit(vnode);
|
super.oninit(vnode);
|
||||||
|
|
||||||
this.tags = app.store.all('tags');
|
this.tags = getSelectableTags(this.attrs.discussion);
|
||||||
|
|
||||||
if (this.attrs.discussion) {
|
|
||||||
this.tags = this.tags.filter(tag => tag.canAddToDiscussion() || this.attrs.discussion.tags().indexOf(tag) !== -1);
|
|
||||||
} else {
|
|
||||||
this.tags = this.tags.filter(tag => tag.canStartDiscussion());
|
|
||||||
}
|
|
||||||
|
|
||||||
this.tags = sortTags(this.tags);
|
this.tags = sortTags(this.tags);
|
||||||
|
|
||||||
|
11
extensions/tags/js/src/forum/utils/getSelectableTags.js
Normal file
11
extensions/tags/js/src/forum/utils/getSelectableTags.js
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
export default function getSelectableTags(discussion) {
|
||||||
|
let tags = app.store.all('tags');
|
||||||
|
|
||||||
|
if (discussion) {
|
||||||
|
tags = tags.filter(tag => tag.canAddToDiscussion() || discussion.tags().indexOf(tag) !== -1);
|
||||||
|
} else {
|
||||||
|
tags = tags.filter(tag => tag.canStartDiscussion());
|
||||||
|
}
|
||||||
|
|
||||||
|
return tags;
|
||||||
|
}
|
@@ -25,6 +25,11 @@
|
|||||||
.DiscussionComposer-changeTags {
|
.DiscussionComposer-changeTags {
|
||||||
margin-right: 15px;
|
margin-right: 15px;
|
||||||
vertical-align: 2px;
|
vertical-align: 2px;
|
||||||
|
|
||||||
|
&.disabled {
|
||||||
|
opacity: 0.5;
|
||||||
|
cursor: default;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.DiscussionListItem-info > .item-tags {
|
.DiscussionListItem-info > .item-tags {
|
||||||
margin-right: 4px;
|
margin-right: 4px;
|
||||||
|
Reference in New Issue
Block a user