DEV: Client side category routing changes

This commit corresponds to d84c34ad which applies the same changes to
the server-side. This changes the category routes, except for the routes
that contain tags.
This commit is contained in:
Daniel Waterworth 2019-11-26 10:06:50 +00:00
parent 2dd47df6d1
commit 8876db874f
4 changed files with 63 additions and 23 deletions

View File

@ -20,6 +20,7 @@ export default {
app.DiscoveryCategoryNoneRoute = buildCategoryRoute("default", {
no_subcategories: true
});
app.DiscoveryCategoryWithIDRoute = buildCategoryRoute("default");
const site = Site.current();
site.get("filters").forEach(filter => {

View File

@ -22,44 +22,55 @@ export default function() {
this.route("topicBySlugOrId", { path: "/t/:slugOrId", resetNamespace: true });
this.route("discovery", { path: "/", resetNamespace: true }, function() {
// legacy route
this.route("topParentCategory", { path: "/c/:slug/l/top" });
// top
this.route("top");
this.route("topParentCategory", { path: "/c/:slug/l/top" });
this.route("topCategoryNone", { path: "/c/:slug/none/l/top" });
this.route("topCategory", { path: "/c/:parentSlug/:slug/l/top" });
this.route("topCategoryNone", {
path: "/c/*categorySlugPathWithID/none/l/top"
});
this.route("topCategory", { path: "/c/*categorySlugPathWithID/l/top" });
// top by periods
Site.currentProp("periods").forEach(period => {
const top = "top" + period.capitalize();
this.route(top, { path: "/top/" + period });
// legacy route
this.route(top + "ParentCategory", { path: "/c/:slug/l/top/" + period });
this.route(top, { path: "/top/" + period });
this.route(top + "CategoryNone", {
path: "/c/:slug/none/l/top/" + period
path: "/c/*categorySlugPathWithID/none/l/top/" + period
});
this.route(top + "Category", {
path: "/c/:parentSlug/:slug/l/top/" + period
path: "/c/*categorySlugPathWithID/l/top/" + period
});
});
// filters
Site.currentProp("filters").forEach(filter => {
this.route(filter, { path: "/" + filter });
// legacy route
this.route(filter + "ParentCategory", { path: "/c/:slug/l/" + filter });
this.route(filter, { path: "/" + filter });
this.route(filter + "CategoryNone", {
path: "/c/:slug/none/l/" + filter
path: "/c/*categorySlugPathWithID/none/l/" + filter
});
this.route(filter + "Category", {
path: "/c/:parentSlug/:slug/l/" + filter
path: "/c/*categorySlugPathWithID/l/" + filter
});
});
this.route("categories");
// default filter for a category
// legacy routes
this.route("parentCategory", { path: "/c/:slug" });
this.route("categoryNone", { path: "/c/:slug/none" });
this.route("category", { path: "/c/:parentSlug/:slug" });
this.route("categoryWithID", { path: "/c/:parentSlug/:slug/:id" });
// default filter for a category
this.route("categoryNone", { path: "/c/*categorySlugPathWithID/none" });
this.route("category", { path: "/c/*categorySlugPathWithID" });
});
this.route("groups", { resetNamespace: true, path: "/g" }, function() {

View File

@ -16,11 +16,46 @@ export default (filterArg, params) => {
return DiscourseRoute.extend({
queryParams,
model(modelParams) {
const category = Category.findBySlug(
serialize(modelParams) {
if (!modelParams.categorySlugPathWithID) {
modelParams.categorySlugPathWithID = [
modelParams.parentSlug,
modelParams.slug,
modelParams.parentSlug
);
modelParams.id
]
.filter(x => x)
.join("/");
}
return modelParams;
},
model(modelParams) {
modelParams = this.serialize(modelParams);
const parts = modelParams.categorySlugPathWithID.split("/");
let category = null;
if (parts.length > 0 && parts[parts.length - 1].match(/^\d+$/)) {
const id = parseInt(parts.pop(), 10);
category = Category.findByID(id);
} else {
const [slug, parentSlug] = [...parts].reverse();
category = Category.findBySlug(slug, parentSlug);
if (
!category &&
parts.length > 0 &&
parts[parts.length - 1].match(/^\d+-/)
) {
const id = parseInt(parts.pop(), 10);
category = Category.findByID(id);
}
}
if (category) {
return { category };
}

View File

@ -1,7 +0,0 @@
import Category from "discourse/models/category";
export default Discourse.DiscoveryCategoryRoute.extend({
model(params) {
return { category: Category.findById(params.id) };
}
});