Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(emeisOptions): action button label overrides #515

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 37 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,68 +91,68 @@ export default class EmeisOptionsService extends Service {
// hide "username" field
emailAsUsername = false;

// show only a subset of the "additional" fields on the user model
additionalUserFields = {
"phone": "required",
"language": "required",
"address": "optional",
"city": "optional",
"zip": "optional"
];

// show only a subset of the main navigation entries
navigationEntries = ["users", "scopes"];

/*
On each model edit view (e.g. users) you can define a custom component. The component will be rendered at the bottom of the edit view, but above the primary form buttons. Each component can be designed freely and the model will be passed into the component as `@model` argument. For a working demo have a look at our "dummy-button" at "dummy/app/components/dummy-button".
*/
customComponents = {
users: DummyButton,
},
/*
Within the actions block you can define functions which evaluate the visibility of the "deactivate" and "delete" buttons in the model edit form. The visibilty must be defined for each model separately.

The model must support the "isActive" property for deactivation capabilities, which are currently only supported by user and scope.
*/
actions = {
user: {
// user view specific settings
user = {
/*
Within the actions block you can define functions which evaluate the visibility of the "deactivate" and "delete" buttons in the model edit form. The visibilty must be defined for each model separately. The model must support the "isActive" property for deactivation capabilities, which are currently only supported by user and scope.
*/
actions: {
deactivate: (model) => myUser.canChange(model),
delete: (model) => myUser.canDelete(model),
delete: {
label: "some.translation.key", // you can optionally override the label for the action button with translation key or static string
fn: (model) => myUser.canDelete(model), // in case of label overrides, you have to define th function override via the "fn" key
},
},
scope: {
// show only a subset of the "additional" fields on the user model
additionalFields: {
phone: "required",
language: "required",
address: "optional",
city: "optional",
zip: "optional",
},
/*
On each model edit view (e.g. users) you can define a custom component. The component will be rendered at the bottom of the edit view, but above the primary form buttons. Each component can be designed freely and the model will be passed into the component as `@model` argument. For a working demo have a look at our "dummy-button" at "dummy/app/components/dummy-button".
*/
customComponent: DummyButton,
};

scope = {
actions: {
deactivate: () => false, // statically deactivate the deactivate-button
// leaving out the "delete" key here will always display the delete button
}
}
// define custom fields for a given context (user, scope, role or permission)
metaFields = {
user: [],
scope: [
},
// define custom fields for a given context (user, scope, role or permission)
metaFields: [
{
slug: "test-input",
label: "My Input", // this could also be an ember-intl translation key
type: "text",
visible: true,
readOnly: false,
required: false, //marks this field as optional
placeholder: "some.translation.key" //ember-intl translation key or plain string
placeholder: "some.translation.key", //ember-intl translation key or plain string
},
{
slug: "test-input-2",
label: "some.translation.key",
options: [ // insert a static list of options (value, label), or a (async) function which resolves to a list of options
options: [
// insert a static list of options (value, label), or a (async) function which resolves to a list of options
{
value: "option-1",
label: "Option one"
}
label: "Option one",
},
],
type: "choice",
visible: () => true,
readOnly: false,
required: true, //marks this field as required
}
]
}
},
],
};
}
```

Expand Down
4 changes: 2 additions & 2 deletions addon/components/edit-form.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
data-test-toggle-active
@onClick={{perform this.toggleActiveState}}
>
{{t (concat "emeis.form." (if @model.isActive "deactivate" "activate"))}}
{{optional-translate this.deactivateLabelOverride}}
</UkButton>
{{/if}}

Expand All @@ -34,7 +34,7 @@
data-test-delete
@onClick={{perform this.delete}}
>
{{t "emeis.form.delete"}}
{{optional-translate this.deleteLabelOverride}}
</UkButton>
{{/if}}

Expand Down
53 changes: 39 additions & 14 deletions addon/components/edit-form.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { inject as service } from "@ember/service";
import Component from "@glimmer/component";
import { task } from "ember-concurrency";
import { singularize } from "ember-inflector";

import { confirmTask } from "../decorators/confirm-task";

Expand Down Expand Up @@ -42,33 +43,57 @@ export default class EditFormComponent extends Component {
}

get modelName() {
return this.relativeParentRouteName.split(".")[0];
return singularize(this.relativeParentRouteName.split(".")[0]);
}

get customComponent() {
return this.emeisOptions.customComponents?.[this.modelName];
return this.emeisOptions[this.modelName]?.customComponent;
}

get modelHasActiveState() {
return this.args.model?.isActive !== undefined;
}

get canChangeActiveState() {
return this.emeisOptions.actions?.[this.args.model._internalModel.modelName]
?.deactivate
? this.emeisOptions.actions[
this.args.model._internalModel.modelName
].deactivate(this.args.model)
: true;
const option =
this.emeisOptions[this.args.model?._internalModel?.modelName]?.actions
?.deactivate;
const func = option?.func || option;
return typeof func === "function" ? func(this.args.model) : true;
}

get canDeleteModel() {
return this.emeisOptions.actions?.[this.args.model._internalModel.modelName]
?.delete
? this.emeisOptions.actions[
this.args.model._internalModel.modelName
].delete(this.args.model)
: true;
const option =
this.emeisOptions[this.args.model?._internalModel?.modelName]?.actions
?.delete;
const func = option?.fn || option;
return typeof func === "function" ? func(this.args.model) : true;
}

get deactivateLabelOverride() {
const label =
this.emeisOptions[this.args.model?._internalModel?.modelName]?.actions
?.deactivate?.label;
if (typeof label === "function") {
return label(this.args.model);
} else if (label) {
return label;
}
return this.intl.t(
`emeis.form.${this.args.model.isActive ? "deactivate" : "activate"}`
);
}

get deleteLabelOverride() {
const label =
this.emeisOptions[this.args.model?._internalModel?.modelName]?.actions
?.delete?.label;
if (typeof label === "function") {
return label(this.args.model);
} else if (label) {
return label;
}
return this.intl.t("emeis.form.delete");
}

@task
Expand Down
2 changes: 1 addition & 1 deletion addon/controllers/scopes/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class ScopesEditIndexController extends PaginationController {
}

get metaFields() {
return this.emeisOptions.metaFields?.scope;
return this.emeisOptions.scope?.metaFields;
}

get allScopes() {
Expand Down
10 changes: 5 additions & 5 deletions addon/controllers/users/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ export default class UsersEditController extends PaginationController {
@tracked showAclWizzard = false;

get metaFields() {
return this.emeisOptions.metaFields?.user;
return this.emeisOptions.user?.metaFields;
}

get emailAsUsername() {
return this.emeisOptions.emailAsUsername;
}

get visibleFields() {
if (!this.emeisOptions.additionalUserFields) {
if (!this.emeisOptions.user.additionalFields) {
return ALL_ADDITIONAL_FIELDS;
}

return Object.keys(this.emeisOptions.additionalUserFields);
return Object.keys(this.emeisOptions.user.additionalFields);
}

get requiredFields() {
if (!this.emeisOptions.additionalUserFields) {
if (!this.emeisOptions.user.additionalFields) {
return ALL_ADDITIONAL_FIELDS;
}
return Object.entries(this.emeisOptions.additionalUserFields, {})
return Object.entries(this.emeisOptions.user.additionalFields, {})
.filter(([, value]) => value === "required")
.map(([key]) => key);
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"ember-composable-helpers": "^5.0.0",
"ember-concurrency": "^2.1.2",
"ember-data": "^3.28.3",
"ember-inflector": "^4.0.2",
"ember-intl": "^5.7.2",
"ember-modifier": "^3.2.7",
"ember-power-select": "^5.0.4",
Expand Down
8 changes: 5 additions & 3 deletions tests/acceptance/users-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import { module, test } from "qunit";
import setupRequestAssertions from "./../helpers/assert-request";

class EmeisOptionsStub extends Service {
additionalUserFields = {
phone: "optional",
language: "required",
user = {
additionalFields: {
phone: "optional",
language: "required",
},
};
}

Expand Down
40 changes: 23 additions & 17 deletions tests/dummy/app/services/emeis-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,19 @@ export default class EmeisOptionsService extends Service {
// forceLocale = {
// scope: "en",
// };
// additionalUserFields = {
// phone: "optional",
// language: "optional",
// };
// navigationEntries = ["users", "scopes"];
customComponents = {
users: TestButtonComponent,
};
actions = {
user: {

// user view specific settings
user = {
actions: {
delete: () => true,
},
scope: {
delete: (model) => model.id !== "special",
deactivate: (model) => model.id !== "special",
},
};
metaFields = {
user: [
// additionalFields: {
// phone: "optional",
// language: "optional",
// }
customComponent: TestButtonComponent,
metaFields: [
{
slug: "user-meta-example",
label: "emeis.options.meta.user.example", // ember-intl translation key
Expand Down Expand Up @@ -55,7 +49,19 @@ export default class EmeisOptionsService extends Service {
// readOnly: false,
// },
],
scope: [
};

// scope view specific settings
scope = {
actions: {
delete: (model) => model.id !== "special",
deactivate: {
label: (model) =>
model.isActive ? "my deactivate label" : "my reactivate label",
fn: (model) => model.id !== "special",
},
},
metaFields: [
{
slug: "meta-example",
label: "emeis.options.meta.scope.meta-example",
Expand Down
13 changes: 11 additions & 2 deletions tests/integration/components/edit-form-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { module, test } from "qunit";
import DummyButton from "../../../components/dummy-button/dummy-button";

class EmeisOptionsStub extends Service {
customComponents = {
users: DummyButton,
user = {
customComponent: DummyButton,
};
}

Expand Down Expand Up @@ -53,6 +53,9 @@ module("Integration | Component | edit-form", function (hooks) {

this.setProperties({
model: {
_internalModel: {
modelName: "user",
},
save() {
assert.step("save");
},
Expand Down Expand Up @@ -80,6 +83,9 @@ module("Integration | Component | edit-form", function (hooks) {
};

this.set("model", {
_internalModel: {
modelName: "user",
},
destroyRecord() {
assert.step("destroyRecord");
},
Expand All @@ -103,6 +109,9 @@ module("Integration | Component | edit-form", function (hooks) {

this.setProperties({
model: {
_internalModel: {
modelName: "user",
},
save() {
assert.step("save");
},
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6512,7 +6512,7 @@ ember-in-element-polyfill@^1.0.1:
ember-cli-htmlbars "^5.3.1"
ember-cli-version-checker "^5.1.2"

"ember-inflector@^2.0.0 || ^3.0.0 || ^4.0.2", ember-inflector@^4.0.1:
"ember-inflector@^2.0.0 || ^3.0.0 || ^4.0.2", ember-inflector@^4.0.1, ember-inflector@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/ember-inflector/-/ember-inflector-4.0.2.tgz#4494f1a5f61c1aca7702d59d54024cc92211d8ec"
integrity sha512-+oRstEa52mm0jAFzhr51/xtEWpCEykB3SEBr7vUg8YnXUZJ5hKNBppP938q8Zzr9XfJEbzrtDSGjhKwJCJv6FQ==
Expand Down