2022-01-03 17:09:16 +03:00
|
|
|
import Component from '@glimmer/component';
|
2022-01-21 22:25:47 +03:00
|
|
|
import {action, get} from '@ember/object';
|
2022-01-03 17:09:16 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2016-08-17 18:01:46 +03:00
|
|
|
|
2022-01-03 17:09:16 +03:00
|
|
|
export default class GhThemeTableComponent extends Component {
|
|
|
|
@service ghostPaths;
|
|
|
|
@service modals;
|
|
|
|
@service themeManagement;
|
|
|
|
@service utils;
|
2016-08-17 18:01:46 +03:00
|
|
|
|
2022-01-03 17:09:16 +03:00
|
|
|
activateTaskInstance = null;
|
|
|
|
confirmDeleteModal = null;
|
2016-08-17 18:01:46 +03:00
|
|
|
|
2022-01-03 17:09:16 +03:00
|
|
|
willDestroy() {
|
|
|
|
super.willDestroy(...arguments);
|
|
|
|
this.confirmDeleteModal?.close?.();
|
|
|
|
this.activateTaskInstance?.cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
get sortedThemes() {
|
|
|
|
let themes = this.args.themes.map((t) => {
|
2016-08-17 18:01:46 +03:00
|
|
|
let theme = {};
|
2017-02-21 21:28:44 +03:00
|
|
|
let themePackage = get(t, 'package');
|
2016-08-17 18:01:46 +03:00
|
|
|
|
2017-03-03 18:31:42 +03:00
|
|
|
theme.model = t;
|
2017-02-21 21:28:44 +03:00
|
|
|
theme.name = get(t, 'name');
|
2017-02-27 09:31:01 +03:00
|
|
|
theme.label = themePackage ? `${themePackage.name}` : theme.name;
|
|
|
|
theme.version = themePackage ? `${themePackage.version}` : '1.0';
|
2017-02-21 21:28:44 +03:00
|
|
|
theme.package = themePackage;
|
2017-03-03 18:31:42 +03:00
|
|
|
theme.active = get(t, 'active');
|
2016-08-23 18:55:23 +03:00
|
|
|
theme.isDeletable = !theme.active;
|
2016-08-17 18:01:46 +03:00
|
|
|
|
|
|
|
return theme;
|
2016-08-23 18:55:23 +03:00
|
|
|
});
|
|
|
|
let duplicateThemes = [];
|
|
|
|
|
|
|
|
themes.forEach((theme) => {
|
|
|
|
let duplicateLabels = themes.filterBy('label', theme.label);
|
|
|
|
|
|
|
|
if (duplicateLabels.length > 1) {
|
|
|
|
duplicateThemes.pushObject(theme);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
duplicateThemes.forEach((theme) => {
|
|
|
|
if (theme.name !== 'casper') {
|
|
|
|
theme.label = `${theme.label} (${theme.name})`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// "(default)" needs to be added to casper manually as it's always
|
|
|
|
// displayed and would mess up the duplicate checking if added earlier
|
|
|
|
let casper = themes.findBy('name', 'casper');
|
|
|
|
if (casper) {
|
|
|
|
casper.label = `${casper.label} (default)`;
|
|
|
|
casper.isDefault = true;
|
|
|
|
casper.isDeletable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sorting manually because .sortBy('label') has a different sorting
|
|
|
|
// algorithm to [...strings].sort()
|
|
|
|
return themes.sort((themeA, themeB) => {
|
|
|
|
let a = themeA.label.toLowerCase();
|
|
|
|
let b = themeB.label.toLowerCase();
|
|
|
|
|
|
|
|
if (a < b) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a > b) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
2022-01-03 17:09:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
downloadTheme(themeName, dropdown) {
|
|
|
|
dropdown?.actions.close();
|
|
|
|
this.utils.downloadFile(`${this.ghostPaths.apiRoot}/themes/${themeName}/download/`);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
activateTheme(theme, dropdown) {
|
|
|
|
dropdown?.actions.close();
|
|
|
|
this.activateTaskInstance = this.themeManagement.activateTask.perform(theme);
|
|
|
|
}
|
2016-08-17 18:01:46 +03:00
|
|
|
|
2022-01-03 17:09:16 +03:00
|
|
|
@action
|
|
|
|
deleteTheme(theme, dropdown) {
|
|
|
|
dropdown?.actions.close();
|
|
|
|
|
|
|
|
this.confirmDeleteModal = this.modals.open('modals/design/confirm-delete-theme', {
|
|
|
|
theme
|
|
|
|
}).finally(() => {
|
|
|
|
this.confirmDeleteModal = null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|