2020-01-16 12:56:53 -05:00
|
|
|
import discourseComputed, {
|
2019-02-19 03:30:38 -05:00
|
|
|
observes,
|
|
|
|
on,
|
2019-11-07 16:38:28 -05:00
|
|
|
} 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-30 16:28:29 -04:00
|
|
|
import { empty } from "@ember/object/computed";
|
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";
|
2019-10-30 09:48:24 -04:00
|
|
|
import { scheduleOnce } from "@ember/runloop";
|
2015-07-10 12:28:56 -04:00
|
|
|
|
2019-10-23 12:30:52 -04:00
|
|
|
export default Component.extend(bufferedProperty("userField"), {
|
2019-10-30 16:28:29 -04:00
|
|
|
editing: empty("userField.id"),
|
2015-07-10 12:28:56 -04:00
|
|
|
classNameBindings: [":user-field"],
|
|
|
|
|
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
|
|
|
|
2019-02-19 03:30:38 -05:00
|
|
|
@on("didInsertElement")
|
|
|
|
@observes("editing")
|
|
|
|
_focusOnEdit() {
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.editing) {
|
2019-10-30 09:48:24 -04:00
|
|
|
scheduleOnce("afterRender", this, "_focusName");
|
2015-07-10 12:28:56 -04:00
|
|
|
}
|
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() {
|
2015-07-10 12:28:56 -04:00
|
|
|
$(".user-field-name").select();
|
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("userField.field_type")
|
2019-02-19 03:30:38 -05:00
|
|
|
fieldName(fieldType) {
|
|
|
|
return UserField.fieldTypeById(fieldType).get("name");
|
|
|
|
},
|
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
|
|
|
|
|
|
|
actions: {
|
2015-07-27 14:22:12 -04:00
|
|
|
save() {
|
2019-05-27 04:15:39 -04:00
|
|
|
const buffered = this.buffered;
|
2015-07-28 12:29:40 -04:00
|
|
|
const attrs = buffered.getProperties(
|
|
|
|
"name",
|
|
|
|
"description",
|
|
|
|
"field_type",
|
|
|
|
"editable",
|
|
|
|
"required",
|
|
|
|
"show_on_profile",
|
2016-04-08 08:35:41 -04:00
|
|
|
"show_on_user_card",
|
2021-04-27 01:52:45 -04:00
|
|
|
"searchable",
|
2015-07-28 12:29:40 -04:00
|
|
|
"options"
|
|
|
|
);
|
2018-06-15 11:03:24 -04:00
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
this.userField
|
2015-07-30 14:52:53 -04:00
|
|
|
.save(attrs)
|
2019-02-19 03:30:38 -05:00
|
|
|
.then(() => {
|
|
|
|
this.set("editing", false);
|
|
|
|
this.commitBuffer();
|
2015-07-10 12:28:56 -04:00
|
|
|
})
|
|
|
|
.catch(popupAjaxError);
|
|
|
|
},
|
|
|
|
|
2015-07-27 14:22:12 -04:00
|
|
|
edit() {
|
2015-07-10 12:28:56 -04:00
|
|
|
this.set("editing", true);
|
|
|
|
},
|
|
|
|
|
2015-07-27 14:22:12 -04:00
|
|
|
cancel() {
|
2015-07-10 12:28:56 -04:00
|
|
|
const id = this.get("userField.id");
|
2019-10-31 13:37:24 -04:00
|
|
|
if (isEmpty(id)) {
|
2019-05-27 04:15:39 -04:00
|
|
|
this.destroyAction(this.userField);
|
2015-07-10 12:28:56 -04:00
|
|
|
} else {
|
|
|
|
this.rollbackBuffer();
|
|
|
|
this.set("editing", false);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|