2020-05-14 04:23:41 +08:00
|
|
|
import I18n from "I18n";
|
2018-02-01 23:42:56 +08:00
|
|
|
import DropdownSelectBoxComponent from "select-kit/components/dropdown-select-box";
|
2018-03-14 03:59:12 +08:00
|
|
|
import {
|
|
|
|
PRIVATE_MESSAGE,
|
|
|
|
CREATE_TOPIC,
|
|
|
|
CREATE_SHARED_DRAFT,
|
2020-05-14 13:56:45 +08:00
|
|
|
REPLY,
|
|
|
|
EDIT
|
2018-03-14 03:59:12 +08:00
|
|
|
} from "discourse/models/composer";
|
2020-02-04 23:59:56 +08:00
|
|
|
import Draft from "discourse/models/draft";
|
2020-02-03 21:22:14 +08:00
|
|
|
import { computed } from "@ember/object";
|
2020-05-14 13:56:45 +08:00
|
|
|
import { equal } from "@ember/object/computed";
|
2019-11-06 02:43:49 +08:00
|
|
|
import { camelize } from "@ember/string";
|
2020-02-06 00:14:42 +08:00
|
|
|
import { isEmpty } from "@ember/utils";
|
2018-02-08 18:46:55 +08:00
|
|
|
|
|
|
|
// Component can get destroyed and lose state
|
|
|
|
let _topicSnapshot = null;
|
|
|
|
let _postSnapshot = null;
|
2020-05-16 01:54:44 +08:00
|
|
|
let _actionSnapshot = null;
|
2018-02-08 18:46:55 +08:00
|
|
|
|
|
|
|
export function _clearSnapshots() {
|
|
|
|
_topicSnapshot = null;
|
|
|
|
_postSnapshot = null;
|
2020-05-16 01:54:44 +08:00
|
|
|
_actionSnapshot = null;
|
2018-02-08 18:46:55 +08:00
|
|
|
}
|
2018-02-01 23:42:56 +08:00
|
|
|
|
|
|
|
export default DropdownSelectBoxComponent.extend({
|
2020-04-02 07:21:57 +08:00
|
|
|
seq: 0,
|
2018-02-01 23:42:56 +08:00
|
|
|
pluginApiIdentifiers: ["composer-actions"],
|
2020-02-03 21:22:14 +08:00
|
|
|
classNames: ["composer-actions"],
|
2020-05-14 13:56:45 +08:00
|
|
|
isEditing: equal("action", EDIT),
|
2020-02-03 21:22:14 +08:00
|
|
|
|
|
|
|
selectKitOptions: {
|
2020-05-14 13:56:45 +08:00
|
|
|
icon: "iconForComposerAction",
|
2020-02-03 21:22:14 +08:00
|
|
|
filterable: false,
|
|
|
|
showFullTitle: false
|
|
|
|
},
|
2018-02-01 23:42:56 +08:00
|
|
|
|
2020-05-14 13:56:45 +08:00
|
|
|
iconForComposerAction: computed("action", function() {
|
2020-05-15 16:40:49 +08:00
|
|
|
if (this.isEditing) {
|
|
|
|
return "pencil-alt";
|
2020-05-15 20:23:17 +08:00
|
|
|
} else if (this.action === CREATE_TOPIC) {
|
2020-05-15 16:40:49 +08:00
|
|
|
return "plus";
|
|
|
|
} else {
|
|
|
|
return "share";
|
|
|
|
}
|
2020-05-14 13:56:45 +08:00
|
|
|
}),
|
|
|
|
|
2020-03-31 11:40:00 +08:00
|
|
|
contentChanged() {
|
2020-04-02 07:21:57 +08:00
|
|
|
this.set("seq", this.seq + 1);
|
2020-03-31 11:40:00 +08:00
|
|
|
},
|
|
|
|
|
2018-02-08 18:46:55 +08:00
|
|
|
didReceiveAttrs() {
|
2019-01-19 17:05:51 +08:00
|
|
|
this._super(...arguments);
|
2020-05-16 01:54:44 +08:00
|
|
|
let changeContent = false;
|
2018-02-08 18:46:55 +08:00
|
|
|
|
|
|
|
// if we change topic we want to change both snapshots
|
|
|
|
if (
|
2020-04-01 11:23:26 +08:00
|
|
|
this.topic &&
|
|
|
|
(!_topicSnapshot || this.topic.id !== _topicSnapshot.id)
|
2018-02-08 18:46:55 +08:00
|
|
|
) {
|
2020-04-01 11:23:26 +08:00
|
|
|
_topicSnapshot = this.topic;
|
|
|
|
_postSnapshot = this.post;
|
2020-05-16 01:54:44 +08:00
|
|
|
changeContent = true;
|
2018-02-08 18:46:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we hit reply on a different post we want to change postSnapshot
|
2020-04-01 11:23:26 +08:00
|
|
|
if (this.post && (!_postSnapshot || this.post.id !== _postSnapshot.id)) {
|
|
|
|
_postSnapshot = this.post;
|
2020-05-16 01:54:44 +08:00
|
|
|
changeContent = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.action !== _actionSnapshot) {
|
|
|
|
_actionSnapshot = this.action;
|
|
|
|
changeContent = true;
|
2018-02-08 18:46:55 +08:00
|
|
|
}
|
|
|
|
|
2020-05-16 01:54:44 +08:00
|
|
|
if (changeContent) {
|
|
|
|
this.contentChanged();
|
2018-02-01 23:42:56 +08:00
|
|
|
}
|
2020-05-16 01:54:44 +08:00
|
|
|
|
|
|
|
this.set("selectKit.isHidden", isEmpty(this.content));
|
2020-02-03 21:22:14 +08:00
|
|
|
},
|
2018-02-01 23:42:56 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
modifySelection() {
|
|
|
|
return {};
|
2018-02-01 23:42:56 +08:00
|
|
|
},
|
|
|
|
|
2020-04-02 07:21:57 +08:00
|
|
|
content: computed("seq", function() {
|
2018-02-08 18:46:55 +08:00
|
|
|
let items = [];
|
|
|
|
|
2020-07-07 23:30:48 +08:00
|
|
|
if (
|
|
|
|
this.action === REPLY &&
|
2020-07-15 09:38:28 +08:00
|
|
|
this.topic &&
|
2020-07-07 23:30:48 +08:00
|
|
|
this.topic.isPrivateMessage &&
|
2020-07-15 09:38:28 +08:00
|
|
|
this.topic.details &&
|
2020-07-07 23:30:48 +08:00
|
|
|
(this.topic.details.allowed_users.length > 1 ||
|
|
|
|
this.topic.details.allowed_groups.length > 0) &&
|
|
|
|
!this.isEditing &&
|
|
|
|
_topicSnapshot
|
|
|
|
) {
|
|
|
|
items.push({
|
|
|
|
name: I18n.t(
|
|
|
|
"composer.composer_actions.reply_as_new_group_message.label"
|
|
|
|
),
|
|
|
|
description: I18n.t(
|
|
|
|
"composer.composer_actions.reply_as_new_group_message.desc"
|
|
|
|
),
|
|
|
|
icon: "plus",
|
|
|
|
id: "reply_as_new_group_message"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-24 00:25:58 +08:00
|
|
|
if (
|
2020-02-03 21:22:14 +08:00
|
|
|
this.action !== CREATE_TOPIC &&
|
|
|
|
this.action !== CREATE_SHARED_DRAFT &&
|
2020-07-07 23:30:48 +08:00
|
|
|
!(this.action === REPLY && this.topic.isPrivateMessage) &&
|
2020-05-14 13:56:45 +08:00
|
|
|
!this.isEditing &&
|
2018-05-24 00:25:58 +08:00
|
|
|
_topicSnapshot
|
|
|
|
) {
|
2018-02-08 18:46:55 +08:00
|
|
|
items.push({
|
2018-02-01 23:42:56 +08:00
|
|
|
name: I18n.t("composer.composer_actions.reply_as_new_topic.label"),
|
|
|
|
description: I18n.t(
|
|
|
|
"composer.composer_actions.reply_as_new_topic.desc"
|
|
|
|
),
|
|
|
|
icon: "plus",
|
|
|
|
id: "reply_as_new_topic"
|
2018-02-08 18:46:55 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2020-02-03 21:22:14 +08:00
|
|
|
(this.action !== REPLY && _postSnapshot) ||
|
|
|
|
(this.action === REPLY &&
|
2018-02-08 18:46:55 +08:00
|
|
|
_postSnapshot &&
|
2020-02-03 21:22:14 +08:00
|
|
|
!(this.replyOptions.userAvatar && this.replyOptions.userLink))
|
2018-02-08 18:46:55 +08:00
|
|
|
) {
|
|
|
|
items.push({
|
|
|
|
name: I18n.t("composer.composer_actions.reply_to_post.label", {
|
2020-02-03 21:22:14 +08:00
|
|
|
postNumber: _postSnapshot.post_number,
|
|
|
|
postUsername: _postSnapshot.username
|
2018-02-08 18:46:55 +08:00
|
|
|
}),
|
|
|
|
description: I18n.t("composer.composer_actions.reply_to_post.desc"),
|
2019-01-23 03:42:00 +08:00
|
|
|
icon: "share",
|
2018-02-08 18:46:55 +08:00
|
|
|
id: "reply_to_post"
|
|
|
|
});
|
|
|
|
}
|
2018-02-01 23:42:56 +08:00
|
|
|
|
2018-02-10 05:58:04 +08:00
|
|
|
if (
|
|
|
|
this.siteSettings.enable_personal_messages &&
|
2020-05-14 13:56:45 +08:00
|
|
|
this.action !== PRIVATE_MESSAGE &&
|
|
|
|
!this.isEditing
|
2018-02-10 05:58:04 +08:00
|
|
|
) {
|
2018-02-01 23:42:56 +08:00
|
|
|
items.push({
|
|
|
|
name: I18n.t(
|
|
|
|
"composer.composer_actions.reply_as_private_message.label"
|
|
|
|
),
|
|
|
|
description: I18n.t(
|
|
|
|
"composer.composer_actions.reply_as_private_message.desc"
|
|
|
|
),
|
|
|
|
icon: "envelope",
|
|
|
|
id: "reply_as_private_message"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-08 18:46:55 +08:00
|
|
|
if (
|
2020-05-14 13:56:45 +08:00
|
|
|
!this.isEditing &&
|
|
|
|
((this.action !== REPLY && _topicSnapshot) ||
|
|
|
|
(this.action === REPLY &&
|
|
|
|
_topicSnapshot &&
|
|
|
|
this.replyOptions.userAvatar &&
|
|
|
|
this.replyOptions.userLink &&
|
|
|
|
this.replyOptions.topicLink))
|
2018-02-08 18:46:55 +08:00
|
|
|
) {
|
2018-02-01 23:42:56 +08:00
|
|
|
items.push({
|
|
|
|
name: I18n.t("composer.composer_actions.reply_to_topic.label"),
|
|
|
|
description: I18n.t("composer.composer_actions.reply_to_topic.desc"),
|
2019-01-23 03:42:00 +08:00
|
|
|
icon: "share",
|
2018-02-01 23:42:56 +08:00
|
|
|
id: "reply_to_topic"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-08 18:46:55 +08:00
|
|
|
// if answered post is a whisper, we can only answer with a whisper so no need for toggle
|
2018-02-08 21:18:53 +08:00
|
|
|
if (
|
2020-02-03 21:22:14 +08:00
|
|
|
this.canWhisper &&
|
2018-02-08 21:18:53 +08:00
|
|
|
(!_postSnapshot ||
|
2020-05-07 22:28:28 +08:00
|
|
|
_postSnapshot.post_type !== this.site.post_types.whisper)
|
2018-02-08 21:18:53 +08:00
|
|
|
) {
|
2018-02-02 06:07:37 +08:00
|
|
|
items.push({
|
|
|
|
name: I18n.t("composer.composer_actions.toggle_whisper.label"),
|
|
|
|
description: I18n.t("composer.composer_actions.toggle_whisper.desc"),
|
2019-01-22 19:02:02 +08:00
|
|
|
icon: "far-eye-slash",
|
2018-02-02 06:07:37 +08:00
|
|
|
id: "toggle_whisper"
|
|
|
|
});
|
2018-02-01 23:42:56 +08:00
|
|
|
}
|
|
|
|
|
2018-03-14 03:59:12 +08:00
|
|
|
let showCreateTopic = false;
|
2020-02-03 21:22:14 +08:00
|
|
|
if (this.action === CREATE_SHARED_DRAFT) {
|
2018-03-14 03:59:12 +08:00
|
|
|
showCreateTopic = true;
|
|
|
|
}
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
if (this.action === CREATE_TOPIC) {
|
2018-03-14 03:59:12 +08:00
|
|
|
if (this.site.shared_drafts_category_id) {
|
|
|
|
// Shared Drafts Choice
|
|
|
|
items.push({
|
|
|
|
name: I18n.t("composer.composer_actions.shared_draft.label"),
|
|
|
|
description: I18n.t("composer.composer_actions.shared_draft.desc"),
|
2019-07-16 23:13:44 +08:00
|
|
|
icon: "far-clipboard",
|
2018-03-14 03:59:12 +08:00
|
|
|
id: "shared_draft"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Edge case: If personal messages are disabled, it is possible to have
|
|
|
|
// no items which stil renders a button that pops up nothing. In this
|
|
|
|
// case, add an option for what you're currently doing.
|
|
|
|
if (items.length === 0) {
|
|
|
|
showCreateTopic = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (showCreateTopic) {
|
2018-02-10 05:58:04 +08:00
|
|
|
items.push({
|
|
|
|
name: I18n.t("composer.composer_actions.create_topic.label"),
|
|
|
|
description: I18n.t(
|
|
|
|
"composer.composer_actions.reply_as_new_topic.desc"
|
|
|
|
),
|
2019-01-23 03:42:00 +08:00
|
|
|
icon: "share",
|
2018-02-10 05:58:04 +08:00
|
|
|
id: "create_topic"
|
|
|
|
});
|
|
|
|
}
|
2018-03-14 03:59:12 +08:00
|
|
|
|
2019-01-03 03:15:12 +08:00
|
|
|
const showToggleTopicBump =
|
2019-07-26 17:20:11 +08:00
|
|
|
this.get("currentUser.staff") ||
|
|
|
|
this.get("currentUser.trust_level") === 4;
|
2018-08-10 08:48:30 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
if (this.action === REPLY && showToggleTopicBump) {
|
2018-08-10 08:48:30 +08:00
|
|
|
items.push({
|
|
|
|
name: I18n.t("composer.composer_actions.toggle_topic_bump.label"),
|
|
|
|
description: I18n.t("composer.composer_actions.toggle_topic_bump.desc"),
|
|
|
|
icon: "anchor",
|
|
|
|
id: "toggle_topic_bump"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-01 23:42:56 +08:00
|
|
|
return items;
|
2020-02-03 21:22:14 +08:00
|
|
|
}),
|
2018-02-01 23:42:56 +08:00
|
|
|
|
2018-02-08 18:46:55 +08:00
|
|
|
_replyFromExisting(options, post, topic) {
|
2019-01-10 18:06:01 +08:00
|
|
|
this.closeComposer();
|
|
|
|
this.openComposer(options, post, topic);
|
2018-02-01 23:42:56 +08:00
|
|
|
},
|
|
|
|
|
2018-03-14 03:59:12 +08:00
|
|
|
_openComposer(options) {
|
2019-01-10 18:06:01 +08:00
|
|
|
this.closeComposer();
|
|
|
|
this.openComposer(options);
|
2018-03-14 03:59:12 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
toggleWhisperSelected(options, model) {
|
|
|
|
model.toggleProperty("whisper");
|
|
|
|
},
|
|
|
|
|
2018-08-10 08:48:30 +08:00
|
|
|
toggleTopicBumpSelected(options, model) {
|
|
|
|
model.toggleProperty("noBump");
|
|
|
|
},
|
|
|
|
|
2020-07-07 23:30:48 +08:00
|
|
|
replyAsNewGroupMessageSelected(options) {
|
|
|
|
const recipients = [];
|
|
|
|
|
|
|
|
const details = this.topic.details;
|
|
|
|
details.allowed_users.forEach(u => recipients.push(u.username));
|
|
|
|
details.allowed_groups.forEach(g => recipients.push(g.name));
|
|
|
|
|
|
|
|
options.action = PRIVATE_MESSAGE;
|
|
|
|
options.recipients = recipients.join(",");
|
|
|
|
options.archetypeId = "private_message";
|
|
|
|
options.skipDraftCheck = true;
|
|
|
|
|
|
|
|
this._replyFromExisting(options, _postSnapshot, _topicSnapshot);
|
|
|
|
},
|
|
|
|
|
2018-03-14 03:59:12 +08:00
|
|
|
replyToTopicSelected(options) {
|
|
|
|
options.action = REPLY;
|
|
|
|
options.topic = _topicSnapshot;
|
2019-03-28 04:55:09 +08:00
|
|
|
options.skipDraftCheck = true;
|
2018-03-14 03:59:12 +08:00
|
|
|
this._openComposer(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
replyToPostSelected(options) {
|
|
|
|
options.action = REPLY;
|
|
|
|
options.post = _postSnapshot;
|
2019-03-28 04:55:09 +08:00
|
|
|
options.skipDraftCheck = true;
|
2018-03-14 03:59:12 +08:00
|
|
|
this._openComposer(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
replyAsNewTopicSelected(options) {
|
2020-02-04 23:59:56 +08:00
|
|
|
Draft.get("new_topic").then(response => {
|
|
|
|
if (response.draft) {
|
|
|
|
bootbox.confirm(
|
|
|
|
I18n.t("composer.composer_actions.reply_as_new_topic.confirm"),
|
|
|
|
result => {
|
|
|
|
if (result) this._replyAsNewTopicSelect(options);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this._replyAsNewTopicSelect(options);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_replyAsNewTopicSelect(options) {
|
2018-03-14 03:59:12 +08:00
|
|
|
options.action = CREATE_TOPIC;
|
|
|
|
options.categoryId = this.get("composerModel.topic.category.id");
|
2019-02-13 22:19:58 +08:00
|
|
|
options.disableScopedCategory = true;
|
2019-10-21 05:57:55 +08:00
|
|
|
options.skipDraftCheck = true;
|
2018-03-14 03:59:12 +08:00
|
|
|
this._replyFromExisting(options, _postSnapshot, _topicSnapshot);
|
|
|
|
},
|
|
|
|
|
|
|
|
replyAsPrivateMessageSelected(options) {
|
|
|
|
let usernames;
|
|
|
|
|
|
|
|
if (_postSnapshot && !_postSnapshot.get("yours")) {
|
|
|
|
const postUsername = _postSnapshot.get("username");
|
|
|
|
if (postUsername) {
|
|
|
|
usernames = postUsername;
|
|
|
|
}
|
2018-08-23 17:09:35 +08:00
|
|
|
} else if (this.get("composerModel.topic")) {
|
|
|
|
const stream = this.get("composerModel.topic.postStream");
|
|
|
|
|
|
|
|
if (stream.get("firstPostPresent")) {
|
|
|
|
const post = stream.get("posts.firstObject");
|
|
|
|
if (post && !post.get("yours") && post.get("username")) {
|
|
|
|
usernames = post.get("username");
|
|
|
|
}
|
|
|
|
}
|
2018-03-14 03:59:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
options.action = PRIVATE_MESSAGE;
|
2020-07-17 03:23:08 +08:00
|
|
|
options.recipients = usernames;
|
2018-03-14 03:59:12 +08:00
|
|
|
options.archetypeId = "private_message";
|
2019-10-21 05:57:55 +08:00
|
|
|
options.skipDraftCheck = true;
|
2018-03-14 03:59:12 +08:00
|
|
|
|
|
|
|
this._replyFromExisting(options, _postSnapshot, _topicSnapshot);
|
|
|
|
},
|
|
|
|
|
|
|
|
_switchCreate(options, action) {
|
|
|
|
options.action = action;
|
|
|
|
options.categoryId = this.get("composerModel.categoryId");
|
|
|
|
options.topicTitle = this.get("composerModel.title");
|
2019-01-23 00:26:52 +08:00
|
|
|
options.tags = this.get("composerModel.tags");
|
2018-12-19 17:25:33 +08:00
|
|
|
options.skipDraftCheck = true;
|
2018-03-14 03:59:12 +08:00
|
|
|
this._openComposer(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
createTopicSelected(options) {
|
|
|
|
this._switchCreate(options, CREATE_TOPIC);
|
|
|
|
},
|
|
|
|
|
|
|
|
sharedDraftSelected(options) {
|
|
|
|
this._switchCreate(options, CREATE_SHARED_DRAFT);
|
|
|
|
},
|
|
|
|
|
2018-02-01 23:42:56 +08:00
|
|
|
actions: {
|
2020-02-03 21:22:14 +08:00
|
|
|
onChange(value) {
|
|
|
|
const action = `${camelize(value)}Selected`;
|
2018-03-14 03:59:12 +08:00
|
|
|
if (this[action]) {
|
|
|
|
this[action](
|
2020-02-03 21:22:14 +08:00
|
|
|
this.composerModel.getProperties(
|
2019-02-13 22:19:58 +08:00
|
|
|
"draftKey",
|
|
|
|
"draftSequence",
|
2020-05-25 13:46:02 +08:00
|
|
|
"title",
|
2019-02-13 22:19:58 +08:00
|
|
|
"reply",
|
|
|
|
"disableScopedCategory"
|
|
|
|
),
|
2020-02-03 21:22:14 +08:00
|
|
|
this.composerModel
|
2018-03-14 03:59:12 +08:00
|
|
|
);
|
2020-03-31 11:40:00 +08:00
|
|
|
this.contentChanged();
|
2018-03-14 03:59:12 +08:00
|
|
|
} else {
|
2019-01-10 18:06:01 +08:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(`No method '${action}' found`);
|
2018-02-01 23:42:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|