Skip to content

Commit

Permalink
feat(emeisOptions): enable action button label overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabauke committed Jun 28, 2022
1 parent 7fbc027 commit beca9f3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ export default class EmeisOptionsService extends Service {
*/
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
},
},
// show only a subset of the "additional" fields on the user model
additionalFields: {
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"))}}
{{or (optional-translate this.deactivateLabelOverride) (t (concat "emeis.form." (if @model.isActive "deactivate" "activate")))}}
</UkButton>
{{/if}}

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

Expand Down
33 changes: 22 additions & 11 deletions addon/components/edit-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,32 @@ export default class EditFormComponent extends Component {
}

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

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

get deactivateLabelOverride() {
return this.emeisOptions[this.args.model?._internalModel?.modelName]
?.actions?.deactivate?.label;
}

get deleteLabelOverride() {
return this.emeisOptions[this.args.model?._internalModel?.modelName]
?.actions?.delete
? this.emeisOptions[
this.args.model._internalModel.modelName
]?.actions?.delete(this.args.model)
: true;
?.actions?.delete?.label;
}

@task
Expand Down
5 changes: 4 additions & 1 deletion tests/dummy/app/services/emeis-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ export default class EmeisOptionsService extends Service {
// scope view specific settings
scope = {
actions: {
delete: (model) => model.id !== "special",
delete: {
label: "my delete label",
fn: (model) => model.id !== "special",
},
deactivate: (model) => model.id !== "special",
},
metaFields: [
Expand Down

0 comments on commit beca9f3

Please sign in to comment.