discourse/app/assets/javascripts/admin/components/admin-user-field-item.js.es6

101 lines
2.6 KiB
Plaintext
Raw Normal View History

2018-06-15 23:03:24 +08: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";
2018-06-15 23:03:24 +08:00
export default Ember.Component.extend(bufferedProperty("userField"), {
editing: Ember.computed.empty("userField.id"),
classNameBindings: [":user-field"],
2018-06-15 23:03:24 +08:00
cantMoveUp: propertyEqual("userField", "firstField"),
cantMoveDown: propertyEqual("userField", "lastField"),
2015-07-31 02:52:53 +08:00
userFieldsDescription: function() {
2018-06-15 23:03:24 +08:00
return I18n.t("admin.user_fields.description");
}.property(),
bufferedFieldType: function() {
2018-06-15 23:03:24 +08:00
return UserField.fieldTypeById(this.get("buffered.field_type"));
}.property("buffered.field_type"),
_focusOnEdit: function() {
2018-06-15 23:03:24 +08:00
if (this.get("editing")) {
Ember.run.scheduleOnce("afterRender", this, "_focusName");
}
2018-06-15 23:03:24 +08:00
}
.observes("editing")
.on("didInsertElement"),
_focusName: function() {
2018-06-15 23:03:24 +08:00
$(".user-field-name").select();
},
fieldName: function() {
2018-06-15 23:03:24 +08:00
return UserField.fieldTypeById(this.get("userField.field_type")).get(
"name"
);
}.property("userField.field_type"),
flags: function() {
const ret = [];
2018-06-15 23:03:24 +08:00
if (this.get("userField.editable")) {
ret.push(I18n.t("admin.user_fields.editable.enabled"));
}
2018-06-15 23:03:24 +08:00
if (this.get("userField.required")) {
ret.push(I18n.t("admin.user_fields.required.enabled"));
}
2018-06-15 23:03:24 +08:00
if (this.get("userField.show_on_profile")) {
ret.push(I18n.t("admin.user_fields.show_on_profile.enabled"));
}
2018-06-15 23:03:24 +08:00
if (this.get("userField.show_on_user_card")) {
ret.push(I18n.t("admin.user_fields.show_on_user_card.enabled"));
}
2018-06-15 23:03:24 +08:00
return ret.join(", ");
}.property(
"userField.editable",
"userField.required",
"userField.show_on_profile",
"userField.show_on_user_card"
),
actions: {
save() {
const self = this;
2018-06-15 23:03:24 +08:00
const buffered = this.get("buffered");
const attrs = buffered.getProperties(
"name",
"description",
"field_type",
"editable",
"required",
"show_on_profile",
"show_on_user_card",
"options"
);
this.get("userField")
.save(attrs)
.then(function() {
self.set("editing", false);
self.commitBuffer();
})
.catch(popupAjaxError);
},
edit() {
2018-06-15 23:03:24 +08:00
this.set("editing", true);
},
cancel() {
2018-06-15 23:03:24 +08:00
const id = this.get("userField.id");
if (Ember.isEmpty(id)) {
this.destroyAction(this.get("userField"));
} else {
this.rollbackBuffer();
2018-06-15 23:03:24 +08:00
this.set("editing", false);
}
}
}
});