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

108 lines
2.6 KiB
Plaintext
Raw Normal View History

import { empty } from "@ember/object/computed";
import { scheduleOnce } from "@ember/runloop";
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";
import { i18n } from "discourse/lib/computed";
import {
default as computed,
observes,
on
} from "ember-addons/ember-computed-decorators";
export default Component.extend(bufferedProperty("userField"), {
editing: empty("userField.id"),
2018-06-15 11:03:24 -04:00
classNameBindings: [":user-field"],
2018-06-15 11:03:24 -04:00
cantMoveUp: propertyEqual("userField", "firstField"),
cantMoveDown: propertyEqual("userField", "lastField"),
2015-07-30 14:52:53 -04:00
userFieldsDescription: i18n("admin.user_fields.description"),
@computed("buffered.field_type")
bufferedFieldType(fieldType) {
return UserField.fieldTypeById(fieldType);
},
@on("didInsertElement")
@observes("editing")
_focusOnEdit() {
if (this.editing) {
scheduleOnce("afterRender", this, "_focusName");
}
},
_focusName() {
2018-06-15 11:03:24 -04:00
$(".user-field-name").select();
},
@computed("userField.field_type")
fieldName(fieldType) {
return UserField.fieldTypeById(fieldType).get("name");
},
@computed(
"userField.editable",
"userField.required",
"userField.show_on_profile",
"userField.show_on_user_card"
)
flags(editable, required, showOnProfile, showOnUserCard) {
const ret = [];
if (editable) {
2018-06-15 11:03:24 -04:00
ret.push(I18n.t("admin.user_fields.editable.enabled"));
}
if (required) {
2018-06-15 11:03:24 -04:00
ret.push(I18n.t("admin.user_fields.required.enabled"));
}
if (showOnProfile) {
2018-06-15 11:03:24 -04:00
ret.push(I18n.t("admin.user_fields.show_on_profile.enabled"));
}
if (showOnUserCard) {
2018-06-15 11:03:24 -04:00
ret.push(I18n.t("admin.user_fields.show_on_user_card.enabled"));
}
2018-06-15 11:03:24 -04:00
return ret.join(", ");
},
actions: {
save() {
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"
);
this.userField
2018-06-15 11:03:24 -04:00
.save(attrs)
.then(() => {
this.set("editing", false);
this.commitBuffer();
2018-06-15 11:03:24 -04:00
})
.catch(popupAjaxError);
},
edit() {
2018-06-15 11:03:24 -04:00
this.set("editing", true);
},
cancel() {
2018-06-15 11:03:24 -04:00
const id = this.get("userField.id");
if (Ember.isEmpty(id)) {
this.destroyAction(this.userField);
} else {
this.rollbackBuffer();
2018-06-15 11:03:24 -04:00
this.set("editing", false);
}
}
}
});