avoid page refresh when I change the 'global' variable

86 views
Skip to first unread message

Shareda

unread,
Oct 10, 2020, 12:39:43 PM10/10/20
to TiddlyWiki
How i can avoid page refresh when I change the 'global' variable? I need redraw, but i don't want to loose page's scroll position. Now this variable is declared inside of $:/core/ui/PageTemplate and linked to some config tiddler:

<$set name="hb-highlight-intensity-kw" value={{$:/hb/HighlightIntensityKW}}>

Is there any better place for my 'global' variable?  

TW Tones

unread,
Oct 10, 2020, 8:27:20 PM10/10/20
to TiddlyWiki
Hi,

Could you give us a minimum example we can install of tiddlywiki.com?

Can you defer changing the value until later and use a variable in the mean time, so that the change need not be propagated until you want it to?

Regards
Tones

Eric Shulman

unread,
Oct 10, 2020, 9:46:31 PM10/10/20
to TiddlyWiki
If your "global variable" is only needed when *viewing* tiddlers in the Story River, or when *previewing* while editing a tiddler, but *not* in the Sidebar, SiteTitle, etc., you can do this:

1) In $:/hb/HighlightIntensityKW, instead of just containing the value (e.g., "12345"), use \define, like this:
\define hb-highlight-intensity-kw() 12345

2) Tag $:/hb/HighlightIntensityKW with "$:/tags/Macro/View"

Then, you can use <<hb-highlight-intensity-kw>> in tiddler content or as a widget parameter value and it will be replaced by 12345 as before, but without causing the entire PageTemplate to be re-rendered. Thus, the Story River's current scroll position will not be lost.

enjoy,
-e

Shareda

unread,
Oct 11, 2020, 2:51:14 PM10/11/20
to TiddlyWiki
TW Tones, yes, maybe I can do so, but.. I made this variable to get the desired value from inside of my custumized link.js widget, which redraws all internal links. The $:/hb/HighlightIntensityKW value can be changed only by RangeWidget from UI, that's what I need. It works as i need except of loosing  current scroll position. 

Eric, can I get value of such  <<hb-highlight-intensity-kw>> macro inside of my customized link.js by some JavaScript  expression? 
If so, wouldn't that make it slower (compared to using a variable)? Now i have 10 109  linked tiddlers, and soon there will be ~ 25 000. 

Now I get value by this JS line:
var highlightGain = Number(this.getVariable("hb-highlight-intensity-kw"));




 

воскресенье, 11 октября 2020 г. в 07:46:31 UTC+6, Eric Shulman:

Eric Shulman

unread,
Oct 11, 2020, 5:47:55 PM10/11/20
to TiddlyWiki
On Sunday, October 11, 2020 at 11:51:14 AM UTC-7, Shareda wrote:
TW Tones, yes, maybe I can do so, but.. I made this variable to get the desired value from inside of my custumized link.js widget, which redraws all internal links. The $:/hb/HighlightIntensityKW value can be changed only by RangeWidget from UI, that's what I need. It works as i need except of loosing  current scroll position. 

Eric, can I get value of such  <<hb-highlight-intensity-kw>> macro inside of my customized link.js by some JavaScript  expression? 
If so, wouldn't that make it slower (compared to using a variable)? Now i have 10 109  linked tiddlers, and soon there will be ~ 25 000.  
Now I get value by this JS line: 
var highlightGain = Number(this.getVariable("hb-highlight-intensity-kw"));

Macros *are* variables.  Internally, in the TWCore, they are one and the same.

The only difference is that the macro declaration syntax allows you to pass in
optional parameters to be substituted into the macro value before returning.

See https://tiddlywiki.com/#Macros, in which Jeremy writes:
Most macros are in fact just parameterised variables.
and
The dumpvariables macro lists all variables (including macros) that are available at that position in the widget tree.

You should be able to get the value of the macro using Javascript in your customized
link.js widget, as long as that widget is being rendered in a tiddler view, since the
macro/variable defininition is being loaded via $:/tags/Macro/View.

However, I can't test this myself since I don't have your customized code.

I suggest placing the <<dumpvariables>> macro somewhere just before an occurrence of a link that uses your customized link.js code
to see if it lists your hb-highlight-intensity-kw variable and value.

You could also add
console.log("highlight gain is:" + hightlightGain);
somewhere in your link.js code, and then check the output in the browser's developer console window.

-e

Shareda

unread,
Oct 12, 2020, 4:31:23 AM10/12/20
to TiddlyWiki
Eric, thank you for showing what I've missed about macros, that is important for me! I will use that with any variable/macros used only in the Story River. 

But in this case and on the present time links are to be refreshed everywhere including the Sidebar. 

Here is my custom piece of link.js. Later i'll optimize it, as it is my first touch of JavaScript, styles, etc..
if(!this.isMissing && !this.isShadow) {
          var toTiddler = self.wiki.getTiddler(self.to),                
              toFreq = Number(toTiddler.getFieldString("freq")),
              toWeight = Number(toTiddler.getFieldString("weight")),
              highlightGain = Number(this.getVariable("hb-highlight-intensity-kw"));
            if (toWeight || toFreq) {
            domNode.setAttribute("style","font-weight:"+(highlightGain*4.99*(toFreq)+400-200*highlightGain)+"; background:rgba("+(150+toWeight)+","+(250-toWeight)+",235,"+ (highlightGain*(toFreq/(150)+0.5)) + "); text-decoration: none; color: inherit;border-color: rgba("+(155+toWeight)+","+(255-toWeight)+",255,"+ (highlightGain*(toFreq/(150)+0.5)) + "); border-radius: 4px; border-bottom-width: 0px; border-left-width: 2px; border-right-width: 2px; border-top-width: 0px; padding-inline-end: 1px; padding-inline-start: 1px;border-style: solid");
           domNode.setAttribute("data-toWeight",toWeight);
        }  }


понедельник, 12 октября 2020 г. в 03:47:55 UTC+6, Eric Shulman:
Reply all
Reply to author
Forward
0 new messages