2019-10-24 00:30:52 +08:00
|
|
|
import Component from "@ember/component";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { action } from "@ember/object";
|
|
|
|
import { schedule } from "@ember/runloop";
|
2023-12-28 23:24:24 +08:00
|
|
|
import { inject as service } from "@ember/service";
|
2019-11-01 01:37:24 +08:00
|
|
|
import { isEmpty } from "@ember/utils";
|
2015-07-11 00:28:56 +08:00
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { i18n, propertyEqual } from "discourse/lib/computed";
|
|
|
|
import { bufferedProperty } from "discourse/mixins/buffered-content";
|
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2023-10-18 18:07:09 +08:00
|
|
|
import I18n from "discourse-i18n";
|
2023-10-11 02:38:59 +08:00
|
|
|
import UserField from "admin/models/user-field";
|
2015-07-11 00:28:56 +08:00
|
|
|
|
2019-10-24 00:30:52 +08:00
|
|
|
export default Component.extend(bufferedProperty("userField"), {
|
2023-12-28 23:24:24 +08:00
|
|
|
adminCustomUserFields: service(),
|
|
|
|
|
2022-04-19 19:31:43 +08:00
|
|
|
tagName: "",
|
|
|
|
isEditing: false,
|
2015-07-11 00:28:56 +08:00
|
|
|
|
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
|
|
|
|
2022-04-19 19:31:43 +08:00
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
this._focusName();
|
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() {
|
2022-04-19 19:31:43 +08:00
|
|
|
schedule("afterRender", () => {
|
|
|
|
document.querySelector(".user-field-name")?.focus();
|
|
|
|
});
|
2015-07-11 00:28:56 +08:00
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("userField.field_type")
|
2019-02-19 16:30:38 +08:00
|
|
|
fieldName(fieldType) {
|
2022-04-19 19:31:43 +08:00
|
|
|
return UserField.fieldTypeById(fieldType)?.name;
|
2019-02-19 16:30:38 +08:00
|
|
|
},
|
2015-07-11 00:28:56 +08:00
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed(
|
2021-04-27 13:52:45 +08:00
|
|
|
"userField.{editable,required,show_on_profile,show_on_user_card,searchable}"
|
2019-02-19 16:30:38 +08:00
|
|
|
)
|
2021-04-27 13:52:45 +08:00
|
|
|
flags(userField) {
|
2015-07-11 00:28:56 +08:00
|
|
|
const ret = [];
|
2021-04-27 13:52:45 +08:00
|
|
|
if (userField.editable) {
|
2015-07-11 00:28:56 +08:00
|
|
|
ret.push(I18n.t("admin.user_fields.editable.enabled"));
|
|
|
|
}
|
2021-04-27 13:52:45 +08:00
|
|
|
if (userField.required) {
|
2015-07-11 00:28:56 +08:00
|
|
|
ret.push(I18n.t("admin.user_fields.required.enabled"));
|
|
|
|
}
|
2023-08-16 04:59:04 +08:00
|
|
|
if (userField.show_on_profile) {
|
2015-07-11 00:28:56 +08:00
|
|
|
ret.push(I18n.t("admin.user_fields.show_on_profile.enabled"));
|
|
|
|
}
|
2023-08-16 04:59:04 +08:00
|
|
|
if (userField.show_on_user_card) {
|
2016-04-08 20:35:41 +08:00
|
|
|
ret.push(I18n.t("admin.user_fields.show_on_user_card.enabled"));
|
|
|
|
}
|
2021-04-27 13:52:45 +08:00
|
|
|
if (userField.searchable) {
|
|
|
|
ret.push(I18n.t("admin.user_fields.searchable.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
|
|
|
|
2022-04-19 19:31:43 +08:00
|
|
|
@action
|
|
|
|
save() {
|
|
|
|
const attrs = this.buffered.getProperties(
|
|
|
|
"name",
|
|
|
|
"description",
|
|
|
|
"field_type",
|
|
|
|
"editable",
|
|
|
|
"required",
|
|
|
|
"show_on_profile",
|
|
|
|
"show_on_user_card",
|
|
|
|
"searchable",
|
2023-12-28 23:24:24 +08:00
|
|
|
"options",
|
|
|
|
...this.adminCustomUserFields.additionalProperties
|
2022-04-19 19:31:43 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
return this.userField
|
|
|
|
.save(attrs)
|
|
|
|
.then(() => {
|
|
|
|
if (this.isDestroying || this.isDestroyed) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2022-04-19 19:31:43 +08:00
|
|
|
this.set("isEditing", false);
|
|
|
|
this.commitBuffer();
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError);
|
|
|
|
},
|
2015-07-11 00:28:56 +08:00
|
|
|
|
2022-04-19 19:31:43 +08:00
|
|
|
@action
|
|
|
|
edit() {
|
|
|
|
this.set("isEditing", true);
|
|
|
|
this._focusName();
|
|
|
|
},
|
2015-07-11 00:28:56 +08:00
|
|
|
|
2022-04-19 19:31:43 +08:00
|
|
|
@action
|
|
|
|
cancel() {
|
|
|
|
if (isEmpty(this.userField?.id)) {
|
|
|
|
this.destroyAction(this.userField);
|
|
|
|
} else {
|
|
|
|
this.rollbackBuffer();
|
|
|
|
this.set("isEditing", false);
|
|
|
|
}
|
2015-07-11 00:28:56 +08:00
|
|
|
},
|
|
|
|
});
|