forked from projectcaluma/ember-emeis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(emeisOptions): passing options as a function (projectcaluma#438)
- this enable to pass options for meta-fields of type choice as static list or as a function
- Loading branch information
1 parent
cf6395b
commit 953421c
Showing
15 changed files
with
370 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{{#if this.visible.value}} | ||
<EditForm::Element @label={{optional-translate @field.label}}> | ||
{{#if (eq @field.type "choice")}} | ||
{{#if this.options.isRunning}} | ||
<UkSpinner @ratio="1" /> | ||
{{else}} | ||
<PowerSelect | ||
@disabled={{this.readOnly.value}} | ||
@options={{this.options.value}} | ||
@selected={{find-by "value" (get @model.metainfo @field.slug) this.options.value}} | ||
@onChange={{fn this.updateMetaField @field @model}} | ||
@placeholder={{t @field.label}} | ||
@allowClear={{true}} | ||
@renderInPlace={{true}} | ||
as |option| | ||
> | ||
{{optional-translate option.label}} | ||
</PowerSelect> | ||
{{/if}} | ||
{{/if}} | ||
{{#if (eq @field.type "text")}} | ||
<input | ||
data-test-meta-field-text={{@field.slug}} | ||
class="uk-input" | ||
type="text" | ||
value={{get @model.metainfo @field.slug}} | ||
disabled={{this.readOnly.value}} | ||
{{on "input" (fn this.updateMetaField @field @model)}} | ||
> | ||
{{/if}} | ||
</EditForm::Element> | ||
{{/if}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { assert } from "@ember/debug"; | ||
import { action } from "@ember/object"; | ||
import { inject as service } from "@ember/service"; | ||
import Component from "@glimmer/component"; | ||
import { task } from "ember-concurrency"; | ||
import { useTask } from "ember-resources"; | ||
|
||
export default class MetaFieldComponent extends Component { | ||
@service intl; | ||
|
||
constructor(...args) { | ||
super(...args); | ||
|
||
assert("Must pass a valid field argument", this.args.field); | ||
assert("Must pass a valid model argument", this.args.model); | ||
} | ||
|
||
async evaluateToBoolean(expression) { | ||
if (typeof expression === "boolean") { | ||
return expression; | ||
} | ||
if (typeof expression === "function") { | ||
return await expression(this.args.model); | ||
} | ||
if (typeof expression === "string") { | ||
return expression === "true"; | ||
} | ||
return false; | ||
} | ||
|
||
visible = useTask(this, this.evalVisible, () => [this.args.field.visible]); | ||
readOnly = useTask(this, this.evalReadOnly, () => [this.args.field.readOnly]); | ||
options = useTask(this, this.evalOptions, () => [this.args.field.options]); | ||
|
||
@task | ||
*evalVisible(visible) { | ||
return yield this.evaluateToBoolean(visible); | ||
} | ||
|
||
@task | ||
*evalReadOnly(readOnly) { | ||
return yield this.evaluateToBoolean(readOnly); | ||
} | ||
|
||
@task | ||
*evalOptions(options) { | ||
// options may be a (async) function or a complex property | ||
if (typeof options !== "function") { | ||
return options; | ||
} | ||
return yield options(this.args.model); | ||
} | ||
|
||
@action | ||
updateMetaField(field, model, optionOrEvent) { | ||
const value = optionOrEvent?.target?.value ?? optionOrEvent?.value; | ||
model.metainfo = { ...model.metainfo, [field.slug]: value }; | ||
model.notifyPropertyChange("metainfo"); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "ember-emeis/components/meta-field"; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.