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
Add actions parameter to radio-widget #5026
Conversation
Apologies @pmario I've now looked at the code, and of course you're already doing exactly what I suggested. This is actually a good solution to the challenge of using the same action string with a bunch of radio buttons. I'd strongly suggest adding the prefix |
@Jermolene I also think that at the most, we need to only set variables that arise from attributes the widget already supports, or attributes with a given prefix. And then only if we can consistently do so for other input widgets as well. The catch-all approach of assigning any parameter passed as a variable will lead to problems down the line. If we need to extend a widget in the future with an extra parameter, we are looking at a potential clash where users may have used that parameter name just to set up some variables. Let's consider the |
I was suggesting that the I think in this case it might make sense to just pass all the widget attributes because any of them (including user defined ones) might be useful in the actions handler. |
Latest test version: radio-actions.zip
|
Docs will follow if merged. |
Maybe I am missing something, but wouldn't it be useful to pass the selected value as a variable to the actions as well? Droppable and Draggable widgets use the term actionTiddler, perhaps we could use that name, or call it actionValue? Regardless of the exact name it would be good for it be consistent across input widgets. |
It's there. It should be named "value" ... The 4th parameter in the notification. |
The radioWidget has 2 prameters: tiddler and field ... Both of them are available as I think it would be confusing, if we would duplicate them. If you need other parameters, you can add them. Every additional param will be prefixed with |
@pmario I am very happy with the prefixing of the variables created with "attr-". It addresses one of my key concerns, namely variables declared by the user elsewhere getting overwritten and potential backwards compatibility concerns if the same pattern was introduced in other widgets. I have a few other concerns and I would appreciate it if we could discuss them. I believe our goals are aligned, to make the widgets flexible and consistent. I'd also like for us to keep in mind the usability aspect of this, considering that a lot of TiddlyWiki users do not have a technical background. Consistency, and clear and non-overlapping terminology, is key to aiding user understanding. Firstly, let me clarify what I meant by using a consistent name for a variable holding the However, this isn't the case with widgets such as the Hence my question: for the sake of consistency and usability, would it make sense to use a consistent and unique variable name for the value associated with the event that fires actions on input widgets? Another thought here is if it might be worth considering to refactor the code that creates the variables from widget parameters into a separate function on the That took longer than expected, so I'll come back with the one other concern later. |
I'm with you ... about all of them. ... I'll need to think about it. |
About:
For the docs, we can group them under eg: actionXYZ Tiddler I think this would be consistently starting with an |
@pmario that summary is very helpful, thank you. If we were to group those widgets by what they are used for, we would have:
Exceptions to rules is usually where users get confused and usability can suffer greatly. I think we would do well here to prioritize the user perspective over the developer one, as this a user facing feature rather than an internal JavaScript variable. I understand why you feel Not to mention that semantically it is not wrong to use Sorry for the late reply. I'm expecting to be needed volunteering at the hospital from Monday, so this week is a bit hectic getting things organized in terms of other commitments. |
@saqimtiaz .. see: 89ad523 |
Excellent @pmario |
… label wrapper will _not_ be updated.
core/modules/widgets/radio.js
Outdated
@@ -123,17 +123,15 @@ Selectively refreshes the widget if needed. Returns true if the widget or any of | |||
*/ | |||
RadioWidget.prototype.refresh = function(changedTiddlers) { | |||
var changedAttributes = this.computeAttributes(); | |||
if(changedAttributes.tiddler || changedAttributes.field || changedAttributes.index || changedAttributes.value || | |||
changedAttributes["class"] || changedAttributes.disabled || changedAttributes.actions ) { | |||
if($tw.utils.count(changedAttributes) > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps:
if($tw.utils.count(changedAttributes) > 0 || changedTiddlers[this.radioTitle]) {
this.refreshSelf();
return true;
}
// Trigger actions. Use variables = {key:value, key:value ...} | ||
if(this.radioActions) { | ||
$tw.utils.each(this.attributes,function(val,key) { | ||
if(key.charAt(0) !== "$") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Jermolene @pmario So is the intention that all attributes starting with $
are a reserved name space for future attributes that we might want to add to the widget? By allowing users to use arbitrary attribute names, in effect every attribute name becomes a reserved one, as it could clash with widget attributes that we introduce in the future.
At the moment it is just action-widgets
that have attributes starting with $
. This is confusing enough as it is but at least users can remember the rule that non-action widget attribute names do not start with $
.
It would be quite confusing if suddenly any widget could have attribute names that may or not start with $
. Especially if the widget previously has many attributes with no $ prefix and then suddenly has one new one introduced with a $
prefix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would you suggest?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could imagine, that "user_attributes" start with an underscore. eg: _user-attribute
and reserved-for-core
and $reserved-for-core
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or: __attribute
with 2 underscores
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pmario I don't have an obvious solution that I particularly love.
However I think it is important that:
a) we try to avoid the potential confusion with attribute names if we can.
b) if we are going to proceed with this change, it needs to be a conscious decision where we consider the benefit of declaring variables in this manner vs the cost of creating usability issues and decide if this change is worthwhile.
In terms of other implementation approaches I considered requiring all user declared attributes to have a specific prefix, like var-
. The problem is that while that makes it easier to identify user attributes, it makes it trickier to loop through the attributes and identify which is a widget "native" attribute since they have no specific prefix.
Every widget could have a list of attributes that get turned into variables, plus any user ones with a given prefix.
So in pseudo-code:
var widgetAttributes = ["value","name",label"];
$tw.utils.each(this.attributes,function(val,key) {
if(widgetAttributes.indexOf(key) !== -1 || key.substring(0,4) === "var-"){
// create a variable for this attribute.
}
}
The other option we could consider is not to enforce a prefix for user attributes in the code, but rather make it a part of documentation and best practice. Explaining that using user attributes without a reserved prefix like "var-" could cause upgrade problems in the future. The problem with this approach is that I suspect most users will ignore it, and we will still have support issues when attribute names do clash in the future.
Does any of this perhaps inspire any ideas for you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pmario I didn't get to see your subsequent comments while typing. But yes, a prefix for user declared attributes would solve the problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sometimes using a similar approach in my plugins, to make attribute code "loopable", if there are many of them, or if I'm not sure about the names. So it's easy to change the names, because I only need to change the innitial array.
So IMO @Jermolene should have a closer look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Jermolene to try and summarize for you:
-
allowing any arbitrary name for attributes assigned by users to create variables, means that every attribute name is potentially a reserved name when/if we want to add more widget attributes in the future
-
The code in this PR reserves the
$
prefix namespace for attributes. So presumably future widget attributes would have to start with$
. This is a usability concern, as outside of action widgets users can reliably expect widget attributes not to start with$
. Imagine if theButton
widget which has been around for a long time and has many attributes, suddenly has a new attribute that starts with$
. -
One suggestion to mitigate this is, to require user assigned widget attributes to have a specific prefix, such as
_
or evenattr-
(in which case the name of the custom attribute and the variable arising from it could be the same).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The behaviour here is so novel compared to other widgets that I favour a big obvious prefix on the user defined attributes that are to be passed through to the action string. I think var-xxx
makes some sense for both the attribute names and variable names because it makes it clearer that this is a mini version of the <$vars>
widget...
@Jermolene ... We can not use the same prefix for attributes and user-variables ... because this would also cause a "name clash" later, if we add new attributes. ... It would be OK to use So instead of the user code looking like this: (which imo is unreadable)
It could look like this:
Which imo is a 100 times more intuitive |
@Jermolene ... Since we probably can't reach a consensus about the variable naming convention, I'll close this PR and create a new one, which only passes the Instead of adding hardcoded variables I'll call a So we are able to play with the functionality, until we are sure about the hardcoded names. CC @saqimtiaz new PR for radio-widget follows soon. |
@pmario I think that is a good call and I was considering to suggest the same. There are quite a few things to consider with the variables and it would be good to be able to make a more considered decision and have the chance to test it out in practice before making it a part of an official release. |
new PR see "Add radio actions, th-radio-variables hook and fix label refresh problem" #5154 |
For a new plugin of mine I need a functionality like this, because I can't use checkboxes.
The checkbox-widget has 3 possibilities to activate custom actions. .... The radio-widget has none.
This PR allows us to do add an
actions=<<xxx>>
parameter to the radio button. It also builds a hashmap of all parameters, that don't start with a $ ... Similar to the actions-xx widgets. At the moment those params are available as "local" variables in the macro. See example.Latest test-version see: #5026 (comment))
@saqimtiaz @BurningTreeC @flibbles @Jermolene What do you think.