rejetto forum

When to use Javascript/JQuery vs. HFS Macros? Can I combine them?

skb · 6 · 3521

0 Members and 1 Guest are viewing this topic.

Offline skb

  • Occasional poster
  • *
    • Posts: 50
    • View Profile
Kind of a general question, more about "why" to code things a particular way, but also a specific "how". What sorts of things do you do with the built-in macros rather than Javascript? I'm trying to gain understanding of each, but don't really get how they fit together.

Are macros only for HFS events, whereas Javascript is necessary for button onclick handling? Is it possible to mix them? Are there pluses and minuses to either approach? Can I trigger an HFS event from a Javascript button click handler?

I'm currently reading through the docs and  the standard template code, and exploring how to modify it in some simple ways. As an example, I want to make a custom version of rename, to process a list of specific file names in a particular way; we will change only a few ID character positions of each file name, so we can process a batch in one go.

When I read the scripting commands page, I found the rename macro:
   {.rename | A | B.}
which renames the file A to B. I assumed I'd be using this macro for each file in my list, as well as .cut and .set  and all to build the new file names.

I'd also like to change each file's timestamp to the current time when it is renamed, which I was thinking to do via {.exec| touch <filename>.}, but it is not clear how to blend macro functions in with Javascript actions.

However, the built-in "Rename" button handler does it in pure Javascript instead, e.g.:

[...some setup code ommitted...]
ezprompt(
    this.innerHTML,
    {"type":"text", "default":getItemName(a[0])},
    function(s){ajax("rename", {from:getItemName(a[0]), to:s});}
);

Is this just because it is the event handler for the button, or are there other reasons to use Javascript rather than macros? Not clear to me how or if it is possible to mix macros and Javascript functions; when do macros "execute" with respect to the Java script functions on the page?

Is there any way to get at the {.exec .} macro from within a click handler for rename? Or, conversely, is there any way to have Javascript run commands on files on the host system? (I've only used Javascript in client side code, which is restricted from such access by the browser.)

Thanks for any general light you can shed!

Steve


Offline Mars

  • Operator
  • Tireless poster
  • *****
    • Posts: 2059
    • View Profile
in hfs, macros are the equivalent of the PHP language, the server prepares the web page dealing macros before sending it to the user, all parties using javascript are managed by the browser


Offline skb

  • Occasional poster
  • *
    • Posts: 50
    • View Profile
Thanks for that, very helpful.

And, re-reading the default template source, I see what is happening now with that ajax("rename", ...) call. I'd thought before that it was jumping off into the bowels of secret server code to do some magic thing. But now I see that it is just building a section name (e.g. "ajax.rename") and POSTing that section! Way clever! This POST triggers execution of the macro code.

So the code I need to modify to get the time stamps on rename is indeed standard macro stuff, in the [ajax.rename] section.

Thanks for the pointers!


Offline skb

  • Occasional poster
  • *
    • Posts: 50
    • View Profile
Much simpler than I feared: Events are really handy! Using "on macro rename", and the touch.exe util from http://sourceforge.net/projects/unxutils/ it is a one liner to update a file's timestamp after it is renamed:

[+on macro rename]
{.exec|C:\hfs\bin\touch.exe -c {.vfs to disk|%new-name%.}.}

Thanks for a great tool!

Steve


Offline rejetto

  • Administrator
  • Tireless poster
  • *****
    • Posts: 13516
    • View Profile
i'm glad you worked it out Steve :)
you have to keep in mind that it's a mixed (complex) system, some work is done by the server, some by the browser. The two languages are executed in different places, and different times, but there is some possible communication between the two.


Offline skb

  • Occasional poster
  • *
    • Posts: 50
    • View Profile
Yes. I was a bit confused by the concept that even though the Javascript source is in my hfs.tpl file that is located on the server, it is being executed in the client's browser. Thus, to do anything affecting the files on the server (rename, delete, etc) there has to be some GET or POST call to the server.

In general, the HFS macros provide the way to do this "server-side" interaction. Also, it seems that your "Events" interface is the best way for working with these server processes.

That is, at first I was thinking I had to mess with the {.rename .} macro to have it adjust timestamps when it renamed a file. In fact, the solution was to apply the code at the  [+on macro rename] event rather than in the hfs template.

Thanks again!