discourse/app/assets/javascripts/admin/components/permalink-form.js

Failed to ignore revisions in .git-blame-ignore-revs.

88 lines
2.3 KiB
JavaScript
Raw Normal View History

import I18n from "I18n";
import { schedule } from "@ember/runloop";
import Component from "@ember/component";
import discourseComputed from "discourse-common/utils/decorators";
import { fmt } from "discourse/lib/computed";
2018-06-15 11:03:24 -04:00
import Permalink from "admin/models/permalink";
2017-07-05 16:47:01 -04:00
export default Component.extend({
2018-06-15 11:03:24 -04:00
classNames: ["permalink-form"],
2015-07-15 08:54:28 -04:00
formSubmitted: false,
2018-06-15 11:03:24 -04:00
permalinkType: "topic_id",
permalinkTypePlaceholder: fmt("permalinkType", "admin.permalink.%@"),
2015-07-15 08:54:28 -04:00
@discourseComputed
permalinkTypes() {
2015-07-15 08:54:28 -04:00
return [
2018-06-15 11:03:24 -04:00
{ 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") },
{ id: "external_url", name: I18n.t("admin.permalink.external_url") }
2015-07-15 08:54:28 -04:00
];
},
2015-07-15 08:54:28 -04:00
didInsertElement() {
this._super(...arguments);
schedule("afterRender", () => {
$(this.element.querySelector(".external-url")).keydown(e => {
// enter key
if (e.keyCode === 13) {
this.send("submit");
}
});
});
},
focusPermalink() {
schedule("afterRender", () =>
this.element.querySelector(".permalink-url").focus()
);
},
2015-07-15 08:54:28 -04:00
actions: {
submit() {
if (!this.formSubmitted) {
this.set("formSubmitted", true);
Permalink.create({
url: this.url,
permalink_type: this.permalinkType,
permalink_type_value: this.permalink_type_value
})
.save()
.then(
result => {
this.setProperties({
url: "",
permalink_type_value: "",
formSubmitted: false
2018-06-15 11:03:24 -04:00
});
this.action(Permalink.create(result.permalink));
this.focusPermalink();
},
e => {
this.set("formSubmitted", false);
let error;
if (e.responseJSON && e.responseJSON.errors) {
error = I18n.t("generic_error_with_reason", {
error: e.responseJSON.errors.join(". ")
});
} else {
error = I18n.t("generic_error");
}
bootbox.alert(error, () => this.focusPermalink());
2018-06-15 11:03:24 -04:00
}
);
2015-07-15 08:54:28 -04:00
}
},
onChangePermalinkType(type) {
this.set("permalinkType", type);
}
2015-07-15 08:54:28 -04:00
}
});