2020-05-14 04:23:41 +08:00
|
|
|
import I18n from "I18n";
|
2019-11-01 01:37:24 +08:00
|
|
|
import { isEmpty } from "@ember/utils";
|
2019-10-31 04:28:29 +08:00
|
|
|
import { empty } from "@ember/object/computed";
|
2019-10-30 21:48:24 +08:00
|
|
|
import { scheduleOnce } from "@ember/runloop";
|
2019-10-24 00:30:52 +08:00
|
|
|
import Component from "@ember/component";
|
2015-07-11 00:28:56 +08:00
|
|
|
import UserField from "admin/models/user-field";
|
2015-08-08 03:08:27 +08:00
|
|
|
import { bufferedProperty } from "discourse/mixins/buffered-content";
|
2015-07-11 00:28:56 +08:00
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2015-08-08 03:08:27 +08:00
|
|
|
import { propertyEqual } from "discourse/lib/computed";
|
2019-02-19 16:30:38 +08:00
|
|
|
import { i18n } from "discourse/lib/computed";
|
2020-01-17 01:56:53 +08:00
|
|
|
import discourseComputed, {
|
2019-02-19 16:30:38 +08:00
|
|
|
observes,
|
|
|
|
on,
|
2019-11-08 05:38:28 +08:00
|
|
|
} from "discourse-common/utils/decorators";
|
2015-07-11 00:28:56 +08:00
|
|
|
|
2019-10-24 00:30:52 +08:00
|
|
|
export default Component.extend(bufferedProperty("userField"), {
|
2019-10-31 04:28:29 +08:00
|
|
|
editing: empty("userField.id"),
|
2015-07-11 00:28:56 +08:00
|
|
|
classNameBindings: [":user-field"],
|
|
|
|
|
2015-08-08 03:08:27 +08:00
|
|
|
cantMoveUp: propertyEqual("userField", "firstField"),
|
|
|
|
cantMoveDown: propertyEqual("userField", "lastField"),
|
2015-07-31 02:52:53 +08:00
|
|
|
|
2019-02-19 16:30:38 +08:00
|
|
|
userFieldsDescription: i18n("admin.user_fields.description"),
|
2015-07-28 02:22:12 +08:00
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("buffered.field_type")
|
2019-02-19 16:30:38 +08:00
|
|
|
bufferedFieldType(fieldType) {
|
|
|
|
return UserField.fieldTypeById(fieldType);
|
|
|
|
},
|
2015-07-29 00:29:40 +08:00
|
|
|
|
2019-02-19 16:30:38 +08:00
|
|
|
@on("didInsertElement")
|
|
|
|
@observes("editing")
|
|
|
|
_focusOnEdit() {
|
2019-05-27 16:15:39 +08:00
|
|
|
if (this.editing) {
|
2019-10-30 21:48:24 +08:00
|
|
|
scheduleOnce("afterRender", this, "_focusName");
|
2015-07-11 00:28:56 +08:00
|
|
|
}
|
2019-02-19 16:30:38 +08:00
|
|
|
},
|
2015-07-11 00:28:56 +08:00
|
|
|
|
2019-02-19 16:30:38 +08:00
|
|
|
_focusName() {
|
2015-07-11 00:28:56 +08:00
|
|
|
$(".user-field-name").select();
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("userField.field_type")
|
2019-02-19 16:30:38 +08:00
|
|
|
fieldName(fieldType) {
|
|
|
|
return UserField.fieldTypeById(fieldType).get("name");
|
|
|
|
},
|
2015-07-11 00:28:56 +08:00
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed(
|
2019-02-19 16:30:38 +08:00
|
|
|
"userField.editable",
|
|
|
|
"userField.required",
|
|
|
|
"userField.show_on_profile",
|
|
|
|
"userField.show_on_user_card"
|
|
|
|
)
|
|
|
|
flags(editable, required, showOnProfile, showOnUserCard) {
|
2015-07-11 00:28:56 +08:00
|
|
|
const ret = [];
|
2019-02-19 16:30:38 +08:00
|
|
|
if (editable) {
|
2015-07-11 00:28:56 +08:00
|
|
|
ret.push(I18n.t("admin.user_fields.editable.enabled"));
|
|
|
|
}
|
2019-02-19 16:30:38 +08:00
|
|
|
if (required) {
|
2015-07-11 00:28:56 +08:00
|
|
|
ret.push(I18n.t("admin.user_fields.required.enabled"));
|
|
|
|
}
|
2019-02-19 16:30:38 +08:00
|
|
|
if (showOnProfile) {
|
2015-07-11 00:28:56 +08:00
|
|
|
ret.push(I18n.t("admin.user_fields.show_on_profile.enabled"));
|
|
|
|
}
|
2019-02-19 16:30:38 +08:00
|
|
|
if (showOnUserCard) {
|
2016-04-08 20:35:41 +08:00
|
|
|
ret.push(I18n.t("admin.user_fields.show_on_user_card.enabled"));
|
|
|
|
}
|
2015-07-11 00:28:56 +08:00
|
|
|
|
|
|
|
return ret.join(", ");
|
2019-02-19 16:30:38 +08:00
|
|
|
},
|
2015-07-11 00:28:56 +08:00
|
|
|
|
|
|
|
actions: {
|
2015-07-28 02:22:12 +08:00
|
|
|
save() {
|
2019-05-27 16:15:39 +08:00
|
|
|
const buffered = this.buffered;
|
2015-07-29 00:29:40 +08:00
|
|
|
const attrs = buffered.getProperties(
|
|
|
|
"name",
|
|
|
|
"description",
|
|
|
|
"field_type",
|
|
|
|
"editable",
|
|
|
|
"required",
|
|
|
|
"show_on_profile",
|
2016-04-08 20:35:41 +08:00
|
|
|
"show_on_user_card",
|
2015-07-29 00:29:40 +08:00
|
|
|
"options"
|
|
|
|
);
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
this.userField
|
2015-07-31 02:52:53 +08:00
|
|
|
.save(attrs)
|
2019-02-19 16:30:38 +08:00
|
|
|
.then(() => {
|
|
|
|
this.set("editing", false);
|
|
|
|
this.commitBuffer();
|
2015-07-11 00:28:56 +08:00
|
|
|
})
|
|
|
|
.catch(popupAjaxError);
|
|
|
|
},
|
|
|
|
|
2015-07-28 02:22:12 +08:00
|
|
|
edit() {
|
2015-07-11 00:28:56 +08:00
|
|
|
this.set("editing", true);
|
|
|
|
},
|
|
|
|
|
2015-07-28 02:22:12 +08:00
|
|
|
cancel() {
|
2015-07-11 00:28:56 +08:00
|
|
|
const id = this.get("userField.id");
|
2019-11-01 01:37:24 +08:00
|
|
|
if (isEmpty(id)) {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.destroyAction(this.userField);
|
2015-07-11 00:28:56 +08:00
|
|
|
} else {
|
|
|
|
this.rollbackBuffer();
|
|
|
|
this.set("editing", false);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|