REFACTOR: tags-show (#7079)

This commit is contained in:
Joffrey JAFFEUX 2019-02-27 12:59:39 +01:00 committed by GitHub
parent 7d2ea2d4dd
commit 8ff3fc20a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 73 deletions

View File

@ -1,4 +1,7 @@
import { default as computed } from "ember-addons/ember-computed-decorators"; import {
default as computed,
observes
} from "ember-addons/ember-computed-decorators";
import BulkTopicSelection from "discourse/mixins/bulk-topic-selection"; import BulkTopicSelection from "discourse/mixins/bulk-topic-selection";
import { import {
default as NavItem, default as NavItem,
@ -19,14 +22,14 @@ if (extraNavItemProperties) {
if (customNavItemHref) { if (customNavItemHref) {
customNavItemHref(function(navItem) { customNavItemHref(function(navItem) {
if (navItem.get("tagId")) { if (navItem.get("tagId")) {
var name = navItem.get("name"); const name = navItem.get("name");
if (!Discourse.Site.currentProp("filters").includes(name)) { if (!Discourse.Site.currentProp("filters").includes(name)) {
return null; return null;
} }
var path = "/tags/", let path = "/tags/";
category = navItem.get("category"); const category = navItem.get("category");
if (category) { if (category) {
path += "c/"; path += "c/";
@ -37,8 +40,8 @@ if (customNavItemHref) {
path += "/"; path += "/";
} }
path += navItem.get("tagId") + "/l/"; path += `${navItem.get("tagId")}/l/`;
return path + name.replace(" ", "-"); return `${path}${name.replace(" ", "-")}`;
} else { } else {
return null; return null;
} }
@ -66,9 +69,10 @@ export default Ember.Controller.extend(BulkTopicSelection, {
categories: Ember.computed.alias("site.categoriesList"), categories: Ember.computed.alias("site.categoriesList"),
createTopicLabel: function() { @computed("list", "list.draft")
return this.get("list.draft") ? "topic.open_draft" : "topic.create"; createTopicLabel(list, listDraft) {
}.property("list", "list.draft"), return listDraft ? "topic.open_draft" : "topic.create";
},
@computed("canCreateTopic", "category", "canCreateTopicOnCategory") @computed("canCreateTopic", "category", "canCreateTopicOnCategory")
createTopicDisabled(canCreateTopic, category, canCreateTopicOnCategory) { createTopicDisabled(canCreateTopic, category, canCreateTopicOnCategory) {
@ -85,92 +89,87 @@ export default Ember.Controller.extend(BulkTopicSelection, {
"q" "q"
], ],
navItems: function() { @computed("category", "tag.id", "filterMode")
return NavItem.buildList(this.get("category"), { navItems(category, tagId, filterMode) {
tagId: this.get("tag.id"), return NavItem.buildList(category, {
filterMode: this.get("filterMode") tagId,
filterMode
}); });
}.property("category", "tag.id", "filterMode"), },
showTagFilter: function() { @computed("category")
showTagFilter() {
return Discourse.SiteSettings.show_filter_by_tag; return Discourse.SiteSettings.show_filter_by_tag;
}.property("category"), },
showAdminControls: function() { @computed("additionalTags", "canAdminTag", "category")
return ( showAdminControls(additionalTags, canAdminTag, category) {
!this.get("additionalTags") && return !additionalTags && canAdminTag && !category;
this.get("canAdminTag") && },
!this.get("category")
);
}.property("additionalTags", "canAdminTag", "category"),
loadMoreTopics() { loadMoreTopics() {
return this.get("list").loadMore(); return this.get("list").loadMore();
}, },
_showFooter: function() { @observes("list.canLoadMore")
_showFooter() {
this.set("application.showFooter", !this.get("list.canLoadMore")); this.set("application.showFooter", !this.get("list.canLoadMore"));
}.observes("list.canLoadMore"), },
footerMessage: function() { @computed("navMode", "list.topics.length", "loading")
if (this.get("loading") || this.get("list.topics.length") !== 0) { footerMessage(navMode, listTopicsLength, loading) {
if (loading || listTopicsLength !== 0) {
return; return;
} }
if (this.get("list.topics.length") === 0) { if (listTopicsLength === 0) {
return I18n.t("tagging.topics.none." + this.get("navMode"), { return I18n.t(`tagging.topics.none.${navMode}`, {
tag: this.get("tag.id") tag: this.get("tag.id")
}); });
} else { } else {
return I18n.t("tagging.topics.bottom." + this.get("navMode"), { return I18n.t(`tagging.topics.bottom.${navMode}`, {
tag: this.get("tag.id") tag: this.get("tag.id")
}); });
} }
}.property("navMode", "list.topics.length", "loading"), },
actions: { actions: {
changeSort(sortBy) { changeSort(order) {
if (sortBy === this.get("order")) { if (order === this.get("order")) {
this.toggleProperty("ascending"); this.toggleProperty("ascending");
} else { } else {
this.setProperties({ order: sortBy, ascending: false }); this.setProperties({ order, ascending: false });
} }
this.send("invalidateModel"); this.send("invalidateModel");
}, },
refresh() { refresh() {
const self = this;
// TODO: this probably doesn't work anymore // TODO: this probably doesn't work anymore
return this.store return this.store
.findFiltered("topicList", { filter: "tags/" + this.get("tag.id") }) .findFiltered("topicList", { filter: "tags/" + this.get("tag.id") })
.then(function(list) { .then(list => {
self.set("list", list); this.set("list", list);
self.resetSelected(); this.resetSelected();
}); });
}, },
deleteTag() { deleteTag() {
const self = this;
const numTopics = const numTopics =
this.get("list.topic_list.tags.firstObject.topic_count") || 0; this.get("list.topic_list.tags.firstObject.topic_count") || 0;
const confirmText = const confirmText =
numTopics === 0 numTopics === 0
? I18n.t("tagging.delete_confirm_no_topics") ? I18n.t("tagging.delete_confirm_no_topics")
: I18n.t("tagging.delete_confirm", { count: numTopics }); : I18n.t("tagging.delete_confirm", { count: numTopics });
bootbox.confirm(confirmText, function(result) {
if (!result) {
return;
}
self bootbox.confirm(confirmText, result => {
.get("tag") if (!result) return;
this.get("tag")
.destroyRecord() .destroyRecord()
.then(function() { .then(() => this.transitionToRoute("tags.index"))
self.transitionToRoute("tags.index"); .catch(() => bootbox.alert(I18n.t("generic_error")));
})
.catch(function() {
bootbox.alert(I18n.t("generic_error"));
});
}); });
}, },

View File

@ -17,10 +17,10 @@ export default Discourse.Route.extend({
}, },
model(params) { model(params) {
var tag = this.store.createRecord("tag", { const tag = this.store.createRecord("tag", {
id: Handlebars.Utils.escapeExpression(params.tag_id) id: Handlebars.Utils.escapeExpression(params.tag_id)
}), });
f = ""; let f = "";
if (params.additional_tags) { if (params.additional_tags) {
this.set( this.set(
@ -38,9 +38,9 @@ export default Discourse.Route.extend({
if (params.category) { if (params.category) {
f = "c/"; f = "c/";
if (params.parent_category) { if (params.parent_category) {
f += params.parent_category + "/"; f += `${params.parent_category}/`;
} }
f += params.category + "/l/"; f += `${params.category}/l/`;
} }
f += this.get("navMode"); f += this.get("navMode");
this.set("filterMode", f); this.set("filterMode", f);
@ -76,29 +76,29 @@ export default Discourse.Route.extend({
const categorySlug = this.get("categorySlug"); const categorySlug = this.get("categorySlug");
const parentCategorySlug = this.get("parentCategorySlug"); const parentCategorySlug = this.get("parentCategorySlug");
const filter = this.get("navMode"); const filter = this.get("navMode");
const tag_id = tag ? tag.id.toLowerCase() : "none"; const tagId = tag ? tag.id.toLowerCase() : "none";
if (categorySlug) { if (categorySlug) {
var category = Discourse.Category.findBySlug( const category = Discourse.Category.findBySlug(
categorySlug, categorySlug,
parentCategorySlug parentCategorySlug
); );
if (parentCategorySlug) { if (parentCategorySlug) {
params.filter = `tags/c/${parentCategorySlug}/${categorySlug}/${tag_id}/l/${filter}`; params.filter = `tags/c/${parentCategorySlug}/${categorySlug}/${tagId}/l/${filter}`;
} else { } else {
params.filter = `tags/c/${categorySlug}/${tag_id}/l/${filter}`; params.filter = `tags/c/${categorySlug}/${tagId}/l/${filter}`;
} }
if (category) { if (category) {
category.setupGroupsAndPermissions(); category.setupGroupsAndPermissions();
this.set("category", category); this.set("category", category);
} }
} else if (this.get("additionalTags")) { } else if (this.get("additionalTags")) {
params.filter = `tags/intersection/${tag_id}/${this.get( params.filter = `tags/intersection/${tagId}/${this.get(
"additionalTags" "additionalTags"
).join("/")}`; ).join("/")}`;
this.set("category", null); this.set("category", null);
} else { } else {
params.filter = `tags/${tag_id}/l/${filter}`; params.filter = `tags/${tagId}/l/${filter}`;
this.set("category", null); this.set("category", null);
} }
@ -110,10 +110,11 @@ export default Discourse.Route.extend({
{} {}
).then(list => { ).then(list => {
if (list.topic_list.tags && list.topic_list.tags.length === 1) { if (list.topic_list.tags && list.topic_list.tags.length === 1) {
tag.set("id", list.topic_list.tags[0].name); // Update name of tag (case might be different) // Update name of tag (case might be different)
tag.set("id", list.topic_list.tags[0].name);
} }
controller.setProperties({ controller.setProperties({
list: list, list,
canCreateTopic: list.get("can_create_topic"), canCreateTopic: list.get("can_create_topic"),
loading: false, loading: false,
canCreateTopicOnCategory: canCreateTopicOnCategory:
@ -124,9 +125,9 @@ export default Discourse.Route.extend({
titleToken() { titleToken() {
const filterText = I18n.t( const filterText = I18n.t(
"filters." + this.get("navMode").replace("/", ".") + ".title" `filters.${this.get("navMode").replace("/", ".")}.title`
), );
controller = this.controllerFor("tags.show"); const controller = this.controllerFor("tags.show");
if (controller.get("model.id")) { if (controller.get("model.id")) {
if (this.get("category")) { if (this.get("category")) {
@ -177,8 +178,7 @@ export default Discourse.Route.extend({
}, },
createTopic() { createTopic() {
var controller = this.controllerFor("tags.show"), const controller = this.controllerFor("tags.show");
self = this;
if (controller.get("list.draft")) { if (controller.get("list.draft")) {
this.openTopicDraft(controller.get("list")); this.openTopicDraft(controller.get("list"));
@ -190,11 +190,12 @@ export default Discourse.Route.extend({
draftKey: controller.get("list.draft_key"), draftKey: controller.get("list.draft_key"),
draftSequence: controller.get("list.draft_sequence") draftSequence: controller.get("list.draft_sequence")
}) })
.then(function() { .then(() => {
// Pre-fill the tags input field // Pre-fill the tags input field
if (controller.get("model.id")) { if (controller.get("model.id")) {
var c = self.controllerFor("composer").get("model"); const composerModel = this.controllerFor("composer").get("model");
c.set(
composerModel.set(
"tags", "tags",
_.compact( _.compact(
_.flatten([ _.flatten([