From b1a786071e085cfb04da5cf5ce3a955f42618571 Mon Sep 17 00:00:00 2001 From: Bruno Alves de Faria Date: Sun, 4 Sep 2016 22:03:36 -0300 Subject: [PATCH] AWV-3: Creating mixin for form button components --- .../ohif-core/client/components/base/index.js | 1 + .../ohif-core/client/components/base/mixin.js | 5 + .../client/components/base/mixins/button.js | 38 ++++++ .../client/components/base/mixins/formItem.js | 26 ++++ .../components/bootstrap/form/button.html | 2 +- .../viewer/cineDialog/cineDialog.html | 25 ++-- .../viewer/cineDialog/cineDialog.js | 123 +++++++++++------- 7 files changed, 157 insertions(+), 63 deletions(-) create mode 100644 Packages/ohif-core/client/components/base/mixins/button.js diff --git a/Packages/ohif-core/client/components/base/index.js b/Packages/ohif-core/client/components/base/index.js index 86401060d..4e3539a0f 100644 --- a/Packages/ohif-core/client/components/base/index.js +++ b/Packages/ohif-core/client/components/base/index.js @@ -8,6 +8,7 @@ import './section/section.html'; import './section/section.js'; // Mixins +import './mixins/button.js'; import './mixins/component.js'; import './mixins/form.js'; import './mixins/formItem.js'; diff --git a/Packages/ohif-core/client/components/base/mixin.js b/Packages/ohif-core/client/components/base/mixin.js index eee820828..355ec1b6b 100644 --- a/Packages/ohif-core/client/components/base/mixin.js +++ b/Packages/ohif-core/client/components/base/mixin.js @@ -28,6 +28,11 @@ class Mixin { // Get the dependent mixin to be initizalized const mixin = Mixin.getMixin(dependency); + // Throw an error if a cyclic dependency was found on this mixin + if (mixin === this) { + throw new Error(`Mixin ${dependency} has a cyclic dependency.`); + } + // Initizalize the mixin dependencies recursively mixin.init(template, data, applied, behaviors); }); diff --git a/Packages/ohif-core/client/components/base/mixins/button.js b/Packages/ohif-core/client/components/base/mixins/button.js new file mode 100644 index 000000000..2c04eceb0 --- /dev/null +++ b/Packages/ohif-core/client/components/base/mixins/button.js @@ -0,0 +1,38 @@ +import { OHIF } from 'meteor/ohif:core'; +import { Template } from 'meteor/templating'; + +/* + * input: controls a button + */ +OHIF.mixins.button = new OHIF.Mixin({ + dependencies: 'formItem', + composition: { + onRendered() { + const instance = Template.instance(); + const component = instance.component; + + // Set the element to be controlled + component.$element = instance.$('button:first'); + }, + + events: { + 'click button'(event, instance) { + const component = instance.component; + + // Extract the action and the params + const { action, params } = instance.data; + + // Get the current component's API + const api = component.getApi(); + + // Stop here if no API or action was defined + if (!api || !action || typeof api[action] !== 'function') { + return; + } + + // Call the defined action function + api[action].call(this, params); + } + } + } +}); diff --git a/Packages/ohif-core/client/components/base/mixins/formItem.js b/Packages/ohif-core/client/components/base/mixins/formItem.js index 88b7c4ae8..5bf2ab9f3 100644 --- a/Packages/ohif-core/client/components/base/mixins/formItem.js +++ b/Packages/ohif-core/client/components/base/mixins/formItem.js @@ -140,6 +140,32 @@ OHIF.mixins.formItem = new OHIF.Mixin({ } }; + // Get the current component API + component.getApi = () => { + const api = instance.data.api; + + // Check if the API was not given + if (!api) { + // Stop here if the component is form and API was not given + if (component.isForm) { + return; + } + + // Get the current component's form + const form = component.getForm(); + + // Stop here if the component has no form + if (!form) { + return; + } + + return form.getApi(); + } + + // Return the given API + return api; + }; + // Check if the component value is valid in its form's schema component.validate = () => { // Get the component's form diff --git a/Packages/ohif-core/client/components/bootstrap/form/button.html b/Packages/ohif-core/client/components/bootstrap/form/button.html index f48add25d..5a33a716f 100644 --- a/Packages/ohif-core/client/components/bootstrap/form/button.html +++ b/Packages/ohif-core/client/components/bootstrap/form/button.html @@ -2,7 +2,7 @@ {{#baseComponent (extend this base='baseButton' class=(concat 'btn ' this.class) - mixins=(concat 'formItem ' this.mixins) + mixins=(concat 'button ' this.mixins) )}} {{>UI.contentBlock}} {{/baseComponent}} diff --git a/Packages/viewerbase/client/components/viewer/cineDialog/cineDialog.html b/Packages/viewerbase/client/components/viewer/cineDialog/cineDialog.html index 9a6cbf490..060102702 100644 --- a/Packages/viewerbase/client/components/viewer/cineDialog/cineDialog.html +++ b/Packages/viewerbase/client/components/viewer/cineDialog/cineDialog.html @@ -1,51 +1,52 @@