2019-10-31 04:28:29 +08:00
|
|
|
import { gt, and } from "@ember/object/computed";
|
2019-10-30 21:48:24 +08:00
|
|
|
import { schedule } from "@ember/runloop";
|
2019-10-24 00:30:52 +08:00
|
|
|
import Component from "@ember/component";
|
2020-01-17 01:56:53 +08:00
|
|
|
import discourseComputed, { observes } from "discourse-common/utils/decorators";
|
2019-07-03 16:18:11 +08:00
|
|
|
import { iconHTML } from "discourse-common/lib/icon-library";
|
|
|
|
import { escape } from "pretty-text/sanitizer";
|
2020-06-02 04:33:43 +08:00
|
|
|
import { isTesting } from "discourse-common/config/environment";
|
2018-08-31 03:23:15 +08:00
|
|
|
|
|
|
|
const MAX_COMPONENTS = 4;
|
|
|
|
|
2019-10-24 00:30:52 +08:00
|
|
|
export default Component.extend({
|
2018-09-07 02:56:00 +08:00
|
|
|
childrenExpanded: false,
|
2018-08-31 03:23:15 +08:00
|
|
|
classNames: ["themes-list-item"],
|
2018-09-07 02:56:00 +08:00
|
|
|
classNameBindings: ["theme.selected:selected"],
|
2019-10-31 04:28:29 +08:00
|
|
|
hasComponents: gt("children.length", 0),
|
|
|
|
displayComponents: and("hasComponents", "theme.isActive"),
|
|
|
|
displayHasMore: gt("theme.childThemes.length", MAX_COMPONENTS),
|
2018-09-07 02:56:00 +08:00
|
|
|
|
|
|
|
click(e) {
|
|
|
|
if (!$(e.target).hasClass("others-count")) {
|
|
|
|
this.navigateToTheme();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.scheduleAnimation();
|
|
|
|
},
|
|
|
|
|
|
|
|
@observes("theme.selected")
|
|
|
|
triggerAnimation() {
|
|
|
|
this.animate();
|
|
|
|
},
|
|
|
|
|
|
|
|
scheduleAnimation() {
|
2019-10-30 21:48:24 +08:00
|
|
|
schedule("afterRender", () => {
|
2018-09-07 02:56:00 +08:00
|
|
|
this.animate(true);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
animate(isInitial) {
|
2019-07-16 18:45:15 +08:00
|
|
|
const $container = $(this.element);
|
|
|
|
const $list = $(this.element.querySelector(".components-list"));
|
2020-06-02 04:33:43 +08:00
|
|
|
if ($list.length === 0 || isTesting()) {
|
2018-09-07 02:56:00 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const duration = 300;
|
|
|
|
if (this.get("theme.selected")) {
|
|
|
|
this.collapseComponentsList($container, $list, duration);
|
|
|
|
} else if (!isInitial) {
|
|
|
|
this.expandComponentsList($container, $list, duration);
|
|
|
|
}
|
|
|
|
},
|
2018-08-31 03:23:15 +08:00
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed(
|
2018-08-31 03:23:15 +08:00
|
|
|
"theme.component",
|
|
|
|
"theme.childThemes.@each.name",
|
2018-09-07 02:56:00 +08:00
|
|
|
"theme.childThemes.length",
|
|
|
|
"childrenExpanded"
|
2018-08-31 03:23:15 +08:00
|
|
|
)
|
|
|
|
children() {
|
2019-05-27 16:15:39 +08:00
|
|
|
const theme = this.theme;
|
2018-09-07 02:56:00 +08:00
|
|
|
let children = theme.get("childThemes");
|
2018-08-31 03:23:15 +08:00
|
|
|
if (theme.get("component") || !children) {
|
|
|
|
return [];
|
|
|
|
}
|
2019-05-27 16:15:39 +08:00
|
|
|
children = this.childrenExpanded
|
2018-09-07 02:56:00 +08:00
|
|
|
? children
|
|
|
|
: children.slice(0, MAX_COMPONENTS);
|
2019-07-03 16:18:11 +08:00
|
|
|
return children.map(t => {
|
|
|
|
const name = escape(t.name);
|
|
|
|
return t.enabled ? name : `${iconHTML("ban")} ${name}`;
|
|
|
|
});
|
2018-08-31 03:23:15 +08:00
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("children")
|
2018-11-13 21:57:50 +08:00
|
|
|
childrenString(children) {
|
|
|
|
return children.join(", ");
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed(
|
2018-09-07 02:56:00 +08:00
|
|
|
"theme.childThemes.length",
|
|
|
|
"theme.component",
|
|
|
|
"childrenExpanded",
|
|
|
|
"children.length"
|
|
|
|
)
|
|
|
|
moreCount(childrenCount, component, expanded) {
|
|
|
|
if (component || !childrenCount || expanded) {
|
2018-08-31 03:23:15 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return childrenCount - MAX_COMPONENTS;
|
2018-09-07 02:56:00 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
expandComponentsList($container, $list, duration) {
|
|
|
|
$container.css("height", `${$container.height()}px`);
|
|
|
|
$list.css("display", "");
|
|
|
|
$container.animate(
|
|
|
|
{
|
|
|
|
height: `${$container.height() + $list.outerHeight(true)}px`
|
|
|
|
},
|
|
|
|
{
|
|
|
|
duration,
|
|
|
|
done: () => {
|
|
|
|
$list.css("display", "");
|
|
|
|
$container.css("height", "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$list.animate(
|
|
|
|
{
|
|
|
|
opacity: 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
duration
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
collapseComponentsList($container, $list, duration) {
|
|
|
|
$container.animate(
|
|
|
|
{
|
|
|
|
height: `${$container.height() - $list.outerHeight(true)}px`
|
|
|
|
},
|
|
|
|
{
|
|
|
|
duration,
|
|
|
|
done: () => {
|
|
|
|
$list.css("display", "none");
|
|
|
|
$container.css("height", "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$list.animate(
|
|
|
|
{
|
|
|
|
opacity: 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
duration
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
toggleChildrenExpanded() {
|
|
|
|
this.toggleProperty("childrenExpanded");
|
|
|
|
}
|
2018-08-31 03:23:15 +08:00
|
|
|
}
|
|
|
|
});
|