ElkArte Community

Elk Development => Feature Discussion => Exterminated Features => Topic started by: scripple on March 06, 2014, 01:01:52 am

Title: Adding Buttons - supply an onclick function?
Post by: scripple on March 06, 2014, 01:01:52 am
Is there any way short of writing custom code to add a new button to the button array that has an onclick script instead of a link?  Or since the buttons with sub_buttons are very much select like, is there a way to make one actually be a select?  Meaning you select the option from the drop down and it changes the button name and passes a value in on submit?
Title: Re: Adding Buttons - supply on onclick function?
Post by: Antechinus on March 06, 2014, 01:13:33 am
It'd be possible, but with the proviso that you couldn't make it part of the array as such, AFAIK. The thing is the array is set up to deal with anchors, not onclick. What I think you would have to do is add a button outside the array, like the header upshrink. That uses onclick and is styled to look like the other buttons, but is not part of the array in Sources.

Same applies for using selects.
Title: Re: Adding Buttons - supply on onclick function?
Post by: scripple on March 06, 2014, 01:19:31 am
Perhaps the array could have an id field, then a script could be attached via an external script assigning an onclick function to a button with that id.  This would be a future change though as I don't think the array holds IDs.  Or is selecting on an href doable and reasonable?
Title: Re: Adding Buttons - supply on onclick function?
Post by: Antechinus on March 06, 2014, 01:25:20 am
Buttons already have id's that depend on the actions they perform. So yes, you could do it that way I suppose. I hadn't thought it through.

For instance, the "Messages" button is #button_pm. As long as an action is defined for the button, it'll get a suitable id.
Title: Re: Adding Buttons - supply on onclick function?
Post by: scripple on March 06, 2014, 01:40:17 am
It seems sub-buttons don't have id's though.  Can they if the right field is set in the array?
Title: Re: Adding Buttons - supply on onclick function?
Post by: Antechinus on March 06, 2014, 01:44:31 am
Dunno. Never tried it. I suppose the back end code could be tweaked so they did, if it's not allowed already. I'd suggest adding an id on a test site and seeing what happens.
Title: Re: Adding Buttons - supply on onclick function?
Post by: scripple on March 06, 2014, 01:47:48 am
Yeah I'll have to look at the array and see what field needs to be added.

Edit:  Didn't see an obvious tag in the array that creates the id on main buttons vs sub-buttons.
Title: Re: Adding Buttons - supply on onclick function?
Post by: emanuele on March 06, 2014, 02:40:56 am
Maybe use the key of the array?
Code: [Select]
foreach ($button['sub_buttons'] as $child_act => $childbutton)
Code: [Select]
foreach ($childbutton['sub_buttons'] as $grandc_act => $grandchildbutton)
Can't check right now, if you confirm it works I can add it later. ;)
Title: Re: Adding Buttons - supply on onclick function?
Post by: scripple on March 06, 2014, 10:06:03 am
Works to add ids to childbuttons, not sure what buttons have grandchild buttons and have a meeting to rush off to this morning so can't do anything more but thanks for this.
Title: Re: Adding Buttons - supply an onclick function?
Post by: scripple on March 06, 2014, 11:30:46 pm
Ok, I gave it a try and noticed the admin menu has 3rd level buttons.  I decided to make the ids be button_$act_$child_act, and button_$act_$child_act_$grandc_act as the odds of a name collision at that point are lower than just using one of the subvariables.

I then ran the main page through an html5 validator and it was fine.  Here's the modified code secion.

Code: [Select]
		// Any 2nd level menus?
if (!empty($button['sub_buttons']))
{
echo '
<ul class="menulevel2">';

foreach ($button['sub_buttons'] as $child_act => $childbutton)
{
echo '
<li id="button_', $act , '_', $child_act, '" class="listlevel2', !empty($childbutton['sub_buttons']) ? ' subsections" aria-haspopup="true"' : '"', '>
<a class="linklevel2" href="', $childbutton['href'], '" ', isset($childbutton['target']) ? 'target="' . $childbutton['target'] . '"' : '', '>', $childbutton['title'], '</a>';

// 3rd level menus :)
if (!empty($childbutton['sub_buttons']))
{
echo '
<ul class="menulevel3">';

foreach ($childbutton['sub_buttons'] as $grandc_act => $grandchildbutton)
echo '
<li id="button_', $act, '_', $child_act, '_', $grandc_act, '" class="listlevel3">
<a class="linklevel3" href="', $grandchildbutton['href'], '" ', isset($grandchildbutton['target']) ? 'target="' . $grandchildbutton['target'] . '"' : '', '>', $grandchildbutton['title'], '</a>
</li>';

echo '
</ul>';
}

Haven't attached a script to any of them yet but I don't see why it would be a problem.
Title: Re: Adding Buttons - supply an onclick function?
Post by: emanuele on March 07, 2014, 04:28:48 am
In theory there shouldn't be id collision (any identical key name should be avoided because it could be a problem in future possible menu editors O:-)).
The problem with a hierarchical structure is that the moment the position of the menu is changed (for whatever reason) you have to change the the javascript too. If that is acceptable, okay we can use that. I would prefer the "simple" one (also because the IDs generated are looooooong).
Let me know. ;D
Title: Re: Adding Buttons - supply an onclick function?
Post by: scripple on March 07, 2014, 09:28:22 am
I prefer the short form too, I just wasn't sure since you hadn't been setting id's on the sub_buttons if there was any confidence they would be unique.  Since you're happier with the short form I say definitely go that way.  Especially with your point about what happens if a button moves.
Title: Re: Adding Buttons - supply an onclick function?
Post by: emanuele on March 07, 2014, 09:50:11 am
And if you notice any duplicate id, feel free to report it as bug! ;D
Title: Re: Adding Buttons - supply an onclick function?
Post by: scripple on March 29, 2014, 03:46:28 pm
So it seems this never made it into the baseline.  Should I assume it's been rejected?
Title: Re: Adding Buttons - supply an onclick function?
Post by: emanuele on March 29, 2014, 07:41:38 pm
More likely it slipped out of sight... O:-)
Title: Re: Adding Buttons - supply an onclick function?
Post by: scripple on March 30, 2014, 03:17:26 pm
Fair enough.  I suppose I should figure out that github pull request thing some day.
Title: Re: Adding Buttons - supply an onclick function?
Post by: Spuds on March 30, 2014, 03:36:44 pm
Quote from: scripple – Fair enough.  I suppose I should figure out that github pull request thing some day.
There is a bit of a learning curve with Git, but once you get used to it, and learn to use it, its pretty powerful.  

I have not even gotten pissed off at Git in many months which is awesome  :)

Several decent free gui interfaces if you don't want to use the command line (I use TortoiseGit, and only use the command line when I need to teach Git a lesson lol)
Title: Re: Adding Buttons - supply an onclick function?
Post by: scripple on March 30, 2014, 03:51:51 pm
Well I've sent git style patches to kde in the past, but that's about the limit of my use of git.
Title: Re: Adding Buttons - supply an onclick function?
Post by: scripple on April 05, 2014, 03:11:28 am
Just to follow up here,  I did submit a pull request on github to add these classes.  If you get a chance take a look and let me know if I got it right.
Title: Re: Adding Buttons - supply an onclick function?
Post by: emanuele on April 05, 2014, 07:30:44 am
Yep, I've seen it. Thanks! ;D
Tomorrow I should have some free time (this week has been more busy than expected, sorry).