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