2019-10-24 00:30:52 +08:00
|
|
|
import Component from "@ember/component";
|
2019-01-22 22:09:21 +08:00
|
|
|
import I18n from "I18n";
|
2017-07-06 04:47:01 +08:00
|
|
|
import Permalink from "admin/models/permalink";
|
2020-08-27 00:57:13 +08:00
|
|
|
import bootbox from "bootbox";
|
2020-01-17 01:56:53 +08:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2019-01-22 22:09:21 +08:00
|
|
|
import { fmt } from "discourse/lib/computed";
|
2019-10-30 21:48:24 +08:00
|
|
|
import { schedule } from "@ember/runloop";
|
2017-07-06 04:47:01 +08:00
|
|
|
|
2019-10-24 00:30:52 +08:00
|
|
|
export default Component.extend({
|
2015-07-15 20:54:28 +08:00
|
|
|
classNames: ["permalink-form"],
|
|
|
|
formSubmitted: false,
|
|
|
|
permalinkType: "topic_id",
|
2019-01-22 22:09:21 +08:00
|
|
|
permalinkTypePlaceholder: fmt("permalinkType", "admin.permalink.%@"),
|
2015-07-15 20:54:28 +08:00
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed
|
2019-01-22 22:09:21 +08:00
|
|
|
permalinkTypes() {
|
2015-07-15 20:54:28 +08:00
|
|
|
return [
|
|
|
|
{ id: "topic_id", name: I18n.t("admin.permalink.topic_id") },
|
|
|
|
{ id: "post_id", name: I18n.t("admin.permalink.post_id") },
|
|
|
|
{ id: "category_id", name: I18n.t("admin.permalink.category_id") },
|
2020-05-25 17:48:54 +08:00
|
|
|
{ id: "tag_name", name: I18n.t("admin.permalink.tag_name") },
|
2015-07-15 20:54:28 +08:00
|
|
|
{ id: "external_url", name: I18n.t("admin.permalink.external_url") },
|
|
|
|
];
|
2019-01-22 22:09:21 +08:00
|
|
|
},
|
2015-07-15 20:54:28 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
schedule("afterRender", () => {
|
|
|
|
$(this.element.querySelector(".external-url")).keydown((e) => {
|
|
|
|
// enter key
|
|
|
|
if (e.keyCode === 13) {
|
|
|
|
this.send("submit");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-01-22 22:09:21 +08:00
|
|
|
focusPermalink() {
|
2019-10-30 21:48:24 +08:00
|
|
|
schedule("afterRender", () =>
|
2019-07-16 18:45:15 +08:00
|
|
|
this.element.querySelector(".permalink-url").focus()
|
|
|
|
);
|
2019-01-22 22:09:21 +08:00
|
|
|
},
|
2015-07-15 20:54:28 +08:00
|
|
|
|
|
|
|
actions: {
|
2019-01-22 22:09:21 +08:00
|
|
|
submit() {
|
2019-05-27 16:15:39 +08:00
|
|
|
if (!this.formSubmitted) {
|
2019-01-22 22:09:21 +08:00
|
|
|
this.set("formSubmitted", true);
|
|
|
|
|
|
|
|
Permalink.create({
|
2019-05-27 16:15:39 +08:00
|
|
|
url: this.url,
|
|
|
|
permalink_type: this.permalinkType,
|
|
|
|
permalink_type_value: this.permalink_type_value,
|
2019-01-22 22:09:21 +08:00
|
|
|
})
|
|
|
|
.save()
|
|
|
|
.then(
|
|
|
|
(result) => {
|
|
|
|
this.setProperties({
|
|
|
|
url: "",
|
|
|
|
permalink_type_value: "",
|
|
|
|
formSubmitted: false,
|
2018-06-15 23:03:24 +08:00
|
|
|
});
|
2019-01-22 22:09:21 +08:00
|
|
|
|
|
|
|
this.action(Permalink.create(result.permalink));
|
|
|
|
|
|
|
|
this.focusPermalink();
|
|
|
|
},
|
|
|
|
(e) => {
|
|
|
|
this.set("formSubmitted", false);
|
|
|
|
|
|
|
|
let error;
|
2021-03-30 02:36:59 +08:00
|
|
|
if (
|
|
|
|
e.jqXHR &&
|
|
|
|
e.jqXHR.responseJSON &&
|
|
|
|
e.jqXHR.responseJSON.errors
|
|
|
|
) {
|
2019-01-22 22:09:21 +08:00
|
|
|
error = I18n.t("generic_error_with_reason", {
|
2021-03-30 02:36:59 +08:00
|
|
|
error: e.jqXHR.responseJSON.errors.join(". "),
|
2019-01-22 22:09:21 +08:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
error = I18n.t("generic_error");
|
|
|
|
}
|
|
|
|
bootbox.alert(error, () => this.focusPermalink());
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
2019-01-22 22:09:21 +08:00
|
|
|
);
|
2015-07-15 20:54:28 +08:00
|
|
|
}
|
2020-02-03 21:22:14 +08:00
|
|
|
},
|
2019-01-22 22:09:21 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
onChangePermalinkType(type) {
|
|
|
|
this.set("permalinkType", type);
|
|
|
|
},
|
2015-07-15 20:54:28 +08:00
|
|
|
},
|
|
|
|
});
|