2020-05-13 16:23:41 -04:00
|
|
|
import I18n from "I18n";
|
2019-10-31 13:37:24 -04:00
|
|
|
import { isEmpty } from "@ember/utils";
|
2019-10-30 16:28:29 -04:00
|
|
|
import { empty } from "@ember/object/computed";
|
2019-10-30 09:48:24 -04:00
|
|
|
import { scheduleOnce } from "@ember/runloop";
|
2019-10-23 12:30:52 -04:00
|
|
|
import Component from "@ember/component";
|
2018-06-15 11:03:24 -04:00
|
|
|
import UserField from "admin/models/user-field";
|
|
|
|
import { bufferedProperty } from "discourse/mixins/buffered-content";
|
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
|
|
|
import { propertyEqual } from "discourse/lib/computed";
|
2019-02-19 03:30:38 -05:00
|
|
|
import { i18n } from "discourse/lib/computed";
|
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-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"),
|
2018-06-15 11:03:24 -04:00
|
|
|
classNameBindings: [":user-field"],
|
2015-07-10 12:28:56 -04:00
|
|
|
|
2018-06-15 11:03:24 -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() {
|
2018-06-15 11:03:24 -04:00
|
|
|
$(".user-field-name").select();
|
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) {
|
|
|
|
return UserField.fieldTypeById(fieldType).get("name");
|
|
|
|
},
|
2015-07-10 12:28:56 -04:00
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed(
|
2019-02-19 03:30:38 -05:00
|
|
|
"userField.editable",
|
|
|
|
"userField.required",
|
|
|
|
"userField.show_on_profile",
|
|
|
|
"userField.show_on_user_card"
|
|
|
|
)
|
|
|
|
flags(editable, required, showOnProfile, showOnUserCard) {
|
2015-07-10 12:28:56 -04:00
|
|
|
const ret = [];
|
2019-02-19 03:30:38 -05:00
|
|
|
if (editable) {
|
2018-06-15 11:03:24 -04:00
|
|
|
ret.push(I18n.t("admin.user_fields.editable.enabled"));
|
2015-07-10 12:28:56 -04:00
|
|
|
}
|
2019-02-19 03:30:38 -05:00
|
|
|
if (required) {
|
2018-06-15 11:03:24 -04:00
|
|
|
ret.push(I18n.t("admin.user_fields.required.enabled"));
|
2015-07-10 12:28:56 -04:00
|
|
|
}
|
2019-02-19 03:30:38 -05:00
|
|
|
if (showOnProfile) {
|
2018-06-15 11:03:24 -04:00
|
|
|
ret.push(I18n.t("admin.user_fields.show_on_profile.enabled"));
|
2015-07-10 12:28:56 -04:00
|
|
|
}
|
2019-02-19 03:30:38 -05:00
|
|
|
if (showOnUserCard) {
|
2018-06-15 11:03:24 -04:00
|
|
|
ret.push(I18n.t("admin.user_fields.show_on_user_card.enabled"));
|
2016-04-08 08:35:41 -04:00
|
|
|
}
|
2015-07-10 12:28:56 -04:00
|
|
|
|
2018-06-15 11:03:24 -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;
|
2018-06-15 11:03:24 -04:00
|
|
|
const attrs = buffered.getProperties(
|
|
|
|
"name",
|
|
|
|
"description",
|
|
|
|
"field_type",
|
|
|
|
"editable",
|
|
|
|
"required",
|
|
|
|
"show_on_profile",
|
|
|
|
"show_on_user_card",
|
|
|
|
"options"
|
|
|
|
);
|
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
this.userField
|
2018-06-15 11:03:24 -04:00
|
|
|
.save(attrs)
|
2019-02-19 03:30:38 -05:00
|
|
|
.then(() => {
|
|
|
|
this.set("editing", false);
|
|
|
|
this.commitBuffer();
|
2018-06-15 11:03:24 -04:00
|
|
|
})
|
|
|
|
.catch(popupAjaxError);
|
2015-07-10 12:28:56 -04:00
|
|
|
},
|
|
|
|
|
2015-07-27 14:22:12 -04:00
|
|
|
edit() {
|
2018-06-15 11:03:24 -04:00
|
|
|
this.set("editing", true);
|
2015-07-10 12:28:56 -04:00
|
|
|
},
|
|
|
|
|
2015-07-27 14:22:12 -04:00
|
|
|
cancel() {
|
2018-06-15 11:03:24 -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();
|
2018-06-15 11:03:24 -04:00
|
|
|
this.set("editing", false);
|
2015-07-10 12:28:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|