2024-10-15 23:54:38 +08:00
|
|
|
import Component from "@glimmer/component";
|
2024-10-29 10:25:17 +08:00
|
|
|
import { tracked } from "@glimmer/tracking";
|
|
|
|
import { array } from "@ember/helper";
|
2024-10-17 00:13:36 +08:00
|
|
|
import { action } from "@ember/object";
|
2024-10-15 23:54:38 +08:00
|
|
|
import { service } from "@ember/service";
|
|
|
|
import DButton from "discourse/components/d-button";
|
2024-10-29 10:25:17 +08:00
|
|
|
import DropdownMenu from "discourse/components/dropdown-menu";
|
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2024-10-15 23:54:38 +08:00
|
|
|
import icon from "discourse-common/helpers/d-icon";
|
2024-11-20 04:45:18 +08:00
|
|
|
import { i18n } from "discourse-i18n";
|
2024-10-15 23:54:38 +08:00
|
|
|
import AdminConfigAreaCard from "admin/components/admin-config-area-card";
|
2024-10-29 10:25:17 +08:00
|
|
|
import DMenu from "float-kit/components/d-menu";
|
2024-10-15 23:54:38 +08:00
|
|
|
import ThemesGridPlaceholder from "./themes-grid-placeholder";
|
|
|
|
|
|
|
|
// NOTE (martin): We will need to revisit and improve this component
|
|
|
|
// over time.
|
|
|
|
//
|
|
|
|
// Much of the existing theme logic in /admin/customize/themes has old patterns
|
|
|
|
// and technical debt, so anything copied from there to here is subject
|
|
|
|
// to change as we improve this incrementally.
|
|
|
|
export default class ThemeCard extends Component {
|
|
|
|
@service siteSettings;
|
|
|
|
@service toasts;
|
|
|
|
|
2024-10-29 10:25:17 +08:00
|
|
|
@tracked isUpdating = false;
|
|
|
|
|
|
|
|
get themeCardClasses() {
|
|
|
|
return [
|
|
|
|
"theme-card",
|
|
|
|
this.args.theme.get("default") ? "-active" : "",
|
|
|
|
this.isUpdating ? "--updating" : "",
|
|
|
|
].join(" ");
|
2024-10-15 23:54:38 +08:00
|
|
|
}
|
|
|
|
|
2024-10-29 10:25:17 +08:00
|
|
|
get themeRouteModels() {
|
|
|
|
return ["themes", this.args.theme.id];
|
2024-10-15 23:54:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
get themePreviewUrl() {
|
|
|
|
return `/admin/themes/${this.args.theme.id}/preview`;
|
|
|
|
}
|
|
|
|
|
2024-10-29 10:25:17 +08:00
|
|
|
get footerActionIcon() {
|
2024-12-09 05:18:55 +08:00
|
|
|
return this.args.theme.isPendingUpdates ? "arrows-rotate" : "ellipsis";
|
2024-10-29 10:25:17 +08:00
|
|
|
}
|
|
|
|
|
2024-10-15 23:54:38 +08:00
|
|
|
// NOTE: inspired by -> https://github.com/discourse/discourse/blob/24caa36eef826bcdaed88aebfa7df154413fb349/app/assets/javascripts/admin/addon/controllers/admin-customize-themes-show.js#L366
|
|
|
|
//
|
|
|
|
// Will also need some cleanup when refactoring other theme code.
|
|
|
|
@action
|
|
|
|
async setDefault() {
|
2024-10-17 00:13:36 +08:00
|
|
|
let oldDefaultThemeId;
|
|
|
|
|
2024-10-15 23:54:38 +08:00
|
|
|
this.args.theme.set("default", true);
|
2024-10-17 00:13:36 +08:00
|
|
|
this.args.allThemes.forEach((theme) => {
|
|
|
|
if (theme.id !== this.args.theme.id) {
|
|
|
|
if (theme.get("default")) {
|
|
|
|
oldDefaultThemeId = theme.id;
|
2024-10-15 23:54:38 +08:00
|
|
|
}
|
2024-10-17 00:13:36 +08:00
|
|
|
|
|
|
|
theme.set("default", !this.args.theme.get("default"));
|
|
|
|
}
|
2024-10-15 23:54:38 +08:00
|
|
|
});
|
|
|
|
|
2024-10-17 00:13:36 +08:00
|
|
|
const changesSaved = await this.args.theme.saveChanges("default");
|
|
|
|
if (!changesSaved) {
|
|
|
|
this.args.allThemes
|
|
|
|
.find((theme) => theme.id === oldDefaultThemeId)
|
|
|
|
.set("default", true);
|
|
|
|
this.args.theme.set("default", false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.toasts.success({
|
|
|
|
data: {
|
2024-11-20 04:45:18 +08:00
|
|
|
message: i18n("admin.customize.theme.set_default_success", {
|
2024-10-17 00:13:36 +08:00
|
|
|
theme: this.args.theme.name,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
duration: 2000,
|
|
|
|
});
|
2024-10-15 23:54:38 +08:00
|
|
|
}
|
|
|
|
|
2024-10-29 10:25:17 +08:00
|
|
|
@action
|
|
|
|
updateTheme() {
|
|
|
|
if (this.isUpdating) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isUpdating = true;
|
|
|
|
this.args.theme
|
|
|
|
.updateToLatest()
|
|
|
|
.then(() => {
|
|
|
|
this.toasts.success({
|
|
|
|
data: {
|
2024-11-20 04:45:18 +08:00
|
|
|
message: i18n("admin.customize.theme.update_success", {
|
2024-10-29 10:25:17 +08:00
|
|
|
theme: this.args.theme.name,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
duration: 2000,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError)
|
|
|
|
.finally(() => {
|
|
|
|
this.isUpdating = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-10-15 23:54:38 +08:00
|
|
|
<template>
|
|
|
|
<AdminConfigAreaCard
|
2024-10-29 10:25:17 +08:00
|
|
|
class={{this.themeCardClasses}}
|
2024-10-17 00:13:36 +08:00
|
|
|
@translatedHeading={{@theme.name}}
|
2024-10-15 23:54:38 +08:00
|
|
|
>
|
|
|
|
<:content>
|
2024-10-29 10:25:17 +08:00
|
|
|
{{#if @theme.isPendingUpdates}}
|
|
|
|
<span
|
|
|
|
title={{i18n "admin.customize.theme.updates_available_tooltip"}}
|
|
|
|
class="theme-card__update-available"
|
2024-12-09 05:14:28 +08:00
|
|
|
>{{icon "circle-info"}}</span>
|
2024-10-29 10:25:17 +08:00
|
|
|
{{/if}}
|
2024-10-15 23:54:38 +08:00
|
|
|
<div class="theme-card__image-wrapper">
|
2024-10-28 08:10:20 +08:00
|
|
|
{{#if @theme.screenshot_url}}
|
2024-10-15 23:54:38 +08:00
|
|
|
<img
|
|
|
|
class="theme-card__image"
|
2024-10-28 08:10:20 +08:00
|
|
|
src={{@theme.screenshot_url}}
|
2024-10-17 00:13:36 +08:00
|
|
|
alt={{@theme.name}}
|
2024-10-15 23:54:38 +08:00
|
|
|
/>
|
|
|
|
{{else}}
|
|
|
|
<ThemesGridPlaceholder @theme={{@theme}} />
|
|
|
|
{{/if}}
|
|
|
|
</div>
|
|
|
|
<div class="theme-card__content">
|
2024-10-17 00:13:36 +08:00
|
|
|
{{#if @theme.description}}
|
|
|
|
<p class="theme-card__description">{{@theme.description}}</p>
|
|
|
|
{{/if}}
|
2024-10-15 23:54:38 +08:00
|
|
|
</div>
|
|
|
|
<div class="theme-card__footer">
|
|
|
|
<DButton
|
2024-10-29 10:25:17 +08:00
|
|
|
@translatedLabel={{i18n "admin.customize.theme.edit"}}
|
|
|
|
@route="adminCustomizeThemes.show"
|
|
|
|
@routeModels={{this.themeRouteModels}}
|
2024-11-20 03:10:02 +08:00
|
|
|
class="btn-primary theme-card__button"
|
2024-10-15 23:54:38 +08:00
|
|
|
@preventFocus={{true}}
|
|
|
|
/>
|
2024-10-29 10:25:17 +08:00
|
|
|
|
2024-10-17 00:13:36 +08:00
|
|
|
<div class="theme-card__footer-actions">
|
2024-10-29 10:25:17 +08:00
|
|
|
<DMenu
|
|
|
|
@identifier="theme-card__footer-menu"
|
|
|
|
@triggerClass="theme-card__footer-menu btn-flat"
|
|
|
|
@modalForMobile={{true}}
|
|
|
|
@icon={{this.footerActionIcon}}
|
|
|
|
@label={{if
|
|
|
|
this.isUpdating
|
|
|
|
(i18n "admin.customize.theme.updating")
|
|
|
|
""
|
|
|
|
}}
|
|
|
|
@triggers={{array "click"}}
|
|
|
|
>
|
|
|
|
<:content>
|
|
|
|
<DropdownMenu as |dropdown|>
|
|
|
|
{{#if @theme.isPendingUpdates}}
|
|
|
|
<dropdown.item>
|
|
|
|
<DButton
|
|
|
|
@action={{this.updateTheme}}
|
|
|
|
@icon="download"
|
2024-11-20 03:10:02 +08:00
|
|
|
class="theme-card__button -update"
|
2024-10-29 10:25:17 +08:00
|
|
|
@preventFocus={{true}}
|
|
|
|
@translatedLabel={{i18n
|
|
|
|
"admin.customize.theme.update_to_latest"
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</dropdown.item>
|
|
|
|
{{/if}}
|
|
|
|
{{! TODO: Jordan
|
|
|
|
solutions for broken, disabled states }}
|
|
|
|
<dropdown.item>
|
|
|
|
<DButton
|
|
|
|
@action={{this.setDefault}}
|
|
|
|
@preventFocus={{true}}
|
|
|
|
@icon={{if
|
|
|
|
@theme.default
|
2024-12-09 05:18:55 +08:00
|
|
|
"far-square-check"
|
2024-10-29 10:25:17 +08:00
|
|
|
"far-square"
|
|
|
|
}}
|
2024-11-20 03:10:02 +08:00
|
|
|
class="theme-card__button"
|
2024-10-29 10:25:17 +08:00
|
|
|
@translatedLabel={{i18n
|
|
|
|
(if
|
|
|
|
@theme.default
|
|
|
|
"admin.customize.theme.default_theme"
|
|
|
|
"admin.customize.theme.set_default_theme"
|
|
|
|
)
|
|
|
|
}}
|
|
|
|
@disabled={{@theme.default}}
|
|
|
|
/>
|
|
|
|
</dropdown.item>
|
|
|
|
<dropdown.item>
|
|
|
|
<a
|
|
|
|
href={{this.themePreviewUrl}}
|
|
|
|
title={{i18n "admin.customize.explain_preview"}}
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
target="_blank"
|
|
|
|
class="btn btn-transparent theme-card__button"
|
|
|
|
>{{icon "eye"}} {{i18n "admin.customize.theme.preview"}}</a>
|
|
|
|
</dropdown.item>
|
|
|
|
</DropdownMenu>
|
|
|
|
</:content>
|
|
|
|
</DMenu>
|
2024-10-15 23:54:38 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</:content>
|
|
|
|
</AdminConfigAreaCard>
|
|
|
|
</template>
|
|
|
|
}
|