Sadmachine has asked for the wisdom of the Perl Monks concerning the following question:

Can anyone tell me how I'd go about implementing a collasable tree of hypertext links in Perl? Or if possible direct me to a working example that I can adapt? I do have a javascript prog that does this but I don't know how to integrate javascript into a perl script.

Replies are listed 'Best First'.
Re: collapsable tree
by suaveant (Parson) on May 17, 2001 at 16:36 UTC
    Probably your best bet would be to assign each of your items in the tree a unique identifier, and then in your html keep the info of which branches are expanded. Like a hidden field that looks something like "00,05,0A,2B" something like that, or else maintain a state in a session on the system. Then you link the items to change state with something like an expand or collapse command containing its identifier. Or you could put it in all the links, thought that would make your html much longer...
    ?expanded=00,05,0A,2B&collapse=2B
    of course, I'm sure there are many other ways to do it.
                    - Ant
      The 'session' state could be implemented in a CGI-way. Give all branches an ID, and give them to states: expanded (0) or collapsed (1). This way, you can address 32 branches in a long integer. So for every 32 branches you assign one number. You can pack/unpack these numbers to a CGI-compatible string with CGIpack. This allows the branches to be coded in a compact url.

      In the script, you unpack the url to a series of numbers, which are translated with unpack to a series of bits. With substr you pick the bit for the particular branch, to expand or collapse.

      Jeroen
      "We are not alone"(FZ)

Re: collapsable tree
by TGI (Parson) on May 17, 2001 at 20:12 UTC

    Although I haven't tried it yet, I've been meaning to give WDDX.pm a spin.

    WDDX stands for Web Distributed Data Exchange. It allows you to pass complex data between Perl and JavaScript (and other systems too). WDDX.pm can act as the javascript equivalent of Data::Dumper. It can print out JavaScript code that creates and initializes your variables when it is executed. You should be able to find loads of info on their website. There is also a nice little article at webmonkey.

    Your project sound like what I've been meaning to get it together and use WDDX for, almost exactly. Unfortunately other projects have a higher priority. I'm very curious about your results. Good luck.


    TGI says moo

Re: collapsable tree
by ckohl1 (Hermit) on May 17, 2001 at 17:53 UTC
    You might want to output all of your javascript through the print statement and the 'here to' block:
    print <<endhtml; <script language="javascript" text="javascript"> var this = 'that'; </script> endhtml;

    This will allow you to cut and paste all of your javascript into the document.

    Or you could include your Javascript in the Web document with the script tag in this fashion:
    print "<script src='your_javascript_file.js'>";

    ---
    Chris
    'You can't get there from here.'