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

Toolbars: allow passing additional html attributes to button <a> tags. #8079

Merged
merged 4 commits into from May 17, 2016

Conversation

martinpovolny
Copy link

No description provided.

@martinpovolny
Copy link
Author

ping @dkorn

@zeari
Copy link

zeari commented Apr 19, 2016

I couldnt really figure out how to use this.
Can I just set any attributes on buttons in a _center.rb file?

@martinpovolny
Copy link
Author

Sorry ;-)

diff --git a/app/helpers/application_helper/toolbar/ems_container_center.rb b/app/helpers/application_helper/toolbar/ems_container_center.rb
index 6ff9900..299270d 100644
--- a/app/helpers/application_helper/toolbar/ems_container_center.rb
+++ b/app/helpers/application_helper/toolbar/ems_container_center.rb
@@ -16,6 +16,7 @@ class ApplicationHelper::Toolbar::EmsContainerCenter < ApplicationHelper::Toolba
           'fa fa-refresh fa-lg',
           N_('Refresh items and relationships related to this #{ui_lookup(:table=>"ems_container")}'),
           N_('Refresh items and relationships'),
+          :html_attributes => {'ng-click' => 'calSomeJavascriptStuff();'},
           :confirm => N_("Refresh items and relationships related to this \#{ui_lookup(:table=>\"ems_container\")}?")),
         separator,
         button(

@himdel
Copy link
Contributor

himdel commented Apr 21, 2016

I'm a bit afraid that allowing to add any html_attributes may be an overkill - sooner or later, somebody will try adding onclick or something similar, which would hopelessly break the code that's supposed to handle the click...

Probably better to allow just custom data-* attributes, and define, say, data-function to mean "when I click, that function will be called (by miqToolbarOnClick) and will get passed all the custom data (or the element itself, so that function can get the data out)".

@martinpovolny
Copy link
Author

@himdel : I agree with the overkill. We need to figure out the usecases. Ping @pilhuhn.

I think that we need to be able to execute arbitrary javascript. And we need to be able to submit to the URL that the button would submit to + pass in additional data.

@martinpovolny
Copy link
Author

Also related is the custom toolbar content #6320. There will be a need to add code to support the custom toolbar content. Such as collecting values from input fields. Etc.

@martinpovolny
Copy link
Author

zeari

@martinpovolny
Copy link
Author

martinpovolny commented Apr 21, 2016

@zeari : the example shows how the value of from the html_attributes is passed to the a tag on the button.

I think we shall go in a bit different direction as @himdel wrote, but I want to make sure I understand what you need before we change the toolbars.

@pilhuhn
Copy link
Contributor

pilhuhn commented Apr 21, 2016

Yes, display of an input form with one or more fields that are then passed to the backend ruby code are a use case for me. And I would like to get this done by passing a definition of my form and not by coding it in html/js/css.
A definition could look like this on the button definition.
:form => [ :field => {:name => "bla", :label => "Timeout", :type=> numeric}]
When the button is hit, MiQ would see the :form tag, render it (like it does for the confirmation) and then after the user has clicked ok, would pass a map of name+value to the Controller#button as it is done with ids and so on.

@zeari
Copy link

zeari commented Apr 21, 2016

@zeari : the example shows how the value of from the html_attributes is passed to the a tag on the button.

Thanks! I'll try that out.

@zeari
Copy link

zeari commented Apr 26, 2016

@abonas

@miq-bot
Copy link
Member

miq-bot commented Apr 26, 2016

<pr_mergeability_checker />This pull request is not mergeable. Please rebase and repush.

@pilhuhn
Copy link
Contributor

pilhuhn commented Apr 27, 2016

@martinpovolny c8d922d illustrates my idea via code.
If this is cool, I can submit it as individual PR.

@himdel
Copy link
Contributor

himdel commented Apr 27, 2016

@pilhuhn that would need some more refactoring on miqToolbarOnClick so that it doesn't call miqJqueryRequest in such case and just pass the generated url fragment to the miqShowForm method - your example only works because prompt is magic and blocks everything - we probably want a patternfly-like modal with an actual html form instead..

@pilhuhn
Copy link
Contributor

pilhuhn commented Apr 27, 2016

@himdel This is certainly not a/the final version, but for illustration purposes. One would probably also want to support more than 1 field.

@himdel
Copy link
Contributor

himdel commented Apr 27, 2016

Understood :) but.. to tie it back into the previous discussion, is the form case really that significant compared to the other possible customizations? What I'm trying to see is whether it really makes sense to add another special case to that code that works just for forms, or if it'd be better to go the more generic function name + data way, and handling that form case by simply setting function name to "miqShowForm".

@pilhuhn
Copy link
Contributor

pilhuhn commented Apr 27, 2016

The form case allows to declaratively put this without the knowledge of the underlying javascript, which can/could then be exchanged transparently.

@himdel
Copy link
Contributor

himdel commented Apr 27, 2016

Yeah, but if we do have a predefined function to do that, it's the same..

@martinpovolny
Copy link
Author

To utilize data-function with angular you need:

function callAngular(data) {
   ManageIQ.angular.scope.$apply(function() {
    ManageIQ.angular.scope[data.name].apply(ManageIQ.angular.scope, data.args);
  });
}

@@ -1403,6 +1403,11 @@ function miqToolbarOnClick(e) {
}
}
}
} else if (button.data('function') {
// hack to support data-function and data-function-data
var fn = new Function(button.data('function')); // eval
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually broken because new Function("foobar") returns a function that evaluates foobar and ..nothing, doesn't call it, whereas new Function("foobar()") would call it but can't pass in arguments...

Simple fix:

var fn = new Function("return " + button.data('function')); // eval - returns a function returning the right function
fn().call(button, button.data('functionData'));

And then any function name (without parentheses) will work in data-function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Thx!

@dkorn
Copy link
Contributor

dkorn commented May 8, 2016

To utilize data-function with angular you need:

function callAngular(data) {
ManageIQ.angular.scope.$apply(function() {
ManageIQ.angular.scope[data.name].apply(ManageIQ.angular.scope, data.args);
});
}

@martinpovolny do I need to place this function in my controller/somewhere else?

@martinpovolny
Copy link
Author

@dkorn : anywhere. I thought you just try to use is. It uses the scope saved in ManageIQ.angular.scope to call function data.name with args data.args

This is to call angular stuff from non-angular context (toolbar button click).

Adding it in a separate commit. Wait a sec...

@martinpovolny martinpovolny changed the title [WIP] Toolbars: allow passing of arbitrary html args to button <a> tags. [WIP] Toolbars: allow passing additional html attributes to button <a> tags. May 12, 2016
@martinpovolny martinpovolny removed the wip label May 13, 2016
@martinpovolny martinpovolny changed the title [WIP] Toolbars: allow passing additional html attributes to button <a> tags. Toolbars: allow passing additional html attributes to button <a> tags. May 13, 2016
@martinpovolny
Copy link
Author

martinpovolny commented May 13, 2016

@dkorn: can you, please, update #7620 to work with this branch (after the last commit)?

If everything is ok, I'd like to merge this.

@dkorn
Copy link
Contributor

dkorn commented May 16, 2016

@martinpovolny you tagged the wrong guy :) (its dkorn)
your last commit required a small change from my side but now its working for me 👍

@dkorn
Copy link
Contributor

dkorn commented May 16, 2016

@martinpovolny please also update the example in the commit message

Example:

button(
    :ems_container_deployment,
    'pficon pficon-add-circle-o fa-lg',
    t = N_('Create #{ui_lookup(:table=>"ems_container")}'),
    t,
    :data => {
     'function' => 'miqCallAngular',
     'function-data' => '{ "name": "showListener", "args": [] }'}
  ),

@martinpovolny
Copy link
Author

@h-kataria, @dclarizio : merge, please?

@martinpovolny
Copy link
Author

@dkorn : updated the commit message

Martin Povolny added 4 commits May 17, 2016 18:57
To support support data-function= on buttons we need a way to call
Angular code (in a scope) from outside.

Example:

  button(
    :ems_container_deployment,
    'pficon pficon-add-circle-o fa-lg',
    t = N_('Create #{ui_lookup(:table=>"ems_container")}'),
    t,
    :data => {
     'function' => 'miqCallAngular',
     'function-data' => '{ "name": "showListener", "args": [] }'}
  ),
Custom button attributes can be passed in 'data' key and are added
to the <a> tag as 'data-*' attributes.
@miq-bot
Copy link
Member

miq-bot commented May 17, 2016

Checked commits martinpovolny/manageiq@d410d44~...754a318 with ruby 2.2.3, rubocop 0.37.2, and haml-lint 0.16.1
3 files checked, 0 offenses detected
Everything looks good. ⭐

@dclarizio dclarizio merged commit db30bc9 into ManageIQ:master May 17, 2016
@dclarizio dclarizio added this to the Sprint 41 Ending May 30, 2016 milestone May 17, 2016
@chessbyte
Copy link
Member

@dclarizio is there a BZ for this?

chessbyte pushed a commit that referenced this pull request Jul 6, 2016
Toolbars: allow passing additional html attributes to button <a> tags.
(cherry picked from commit db30bc9)
@JPrause
Copy link
Member

JPrause commented Sep 9, 2016

@martinpovolny martinpovolny deleted the toolbar_button_attr branch November 28, 2017 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants